millirec 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/README.md +133 -11
- package/dist/char-cnn/core.d.ts +13 -1
- package/dist/char-cnn/core.js +70 -2
- package/dist/char-cnn/index.js +70 -2
- package/dist/char-cnn/millirec-char-cnn-core.min.js +1 -1
- package/dist/char-cnn/millirec-char-cnn.min.js +1 -1
- package/dist/char-cnn/options/accurate.js +4 -4
- package/dist/char-cnn/options/accurate.json +4 -4
- package/dist/char-cnn/options/digits/digits-base.head.bin +0 -0
- package/dist/char-cnn/options/digits/digits-base.q4.bin +0 -0
- package/dist/char-cnn/options/digits/digits-tiny.bin +0 -0
- package/dist/char-cnn/options/digits/digits-tiny.head.bin +0 -0
- package/dist/char-cnn/options/digits.js +17 -0
- package/dist/char-cnn/options/digits.json +15 -0
- package/dist/char-cnn/options/fast/tiny.bin +0 -0
- package/dist/char-cnn/options/fast/tiny.head.bin +0 -0
- package/dist/char-cnn/options/fast.js +4 -4
- package/dist/char-cnn/options/fast.json +4 -4
- package/dist/char-cnn/options/lowercase/lowercase-base.head.bin +0 -0
- package/dist/char-cnn/options/lowercase/lowercase-base.q4.bin +0 -0
- package/dist/char-cnn/options/lowercase/lowercase-tiny.bin +0 -0
- package/dist/char-cnn/options/lowercase/lowercase-tiny.head.bin +0 -0
- package/dist/char-cnn/options/lowercase.js +17 -0
- package/dist/char-cnn/options/lowercase.json +15 -0
- package/dist/char-cnn/options/small/tiny.bin +0 -0
- package/dist/char-cnn/options/small/tiny.head.bin +0 -0
- package/dist/char-cnn/options/small.js +2 -2
- package/dist/char-cnn/options/small.json +2 -2
- package/dist/char-lite/index.js +42 -8
- package/dist/char-lite/millirec-char-lite.min.js +1 -1
- package/dist/char-lite/millirec.wasm +0 -0
- package/dist/char-lite/sets.json +25 -29
- package/dist/overlap/millirec.wasm +0 -0
- package/package.json +10 -2
- /package/dist/char-cnn/{base.head.bin → options/accurate/base.head.bin} +0 -0
- /package/dist/char-cnn/{base.q4.bin → options/accurate/base.q4.bin} +0 -0
- /package/dist/char-cnn/{tiny.bin → options/accurate/tiny.bin} +0 -0
- /package/dist/char-cnn/{tiny.head.bin → options/accurate/tiny.head.bin} +0 -0
- /package/dist/char-cnn/{base-p25.bin → options/fast/base-p25.bin} +0 -0
- /package/dist/char-cnn/{base-p25.head.bin → options/fast/base-p25.head.bin} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.2
|
|
4
|
+
|
|
5
|
+
- **char-cnn reads digits and lowercase letters**: two new options, `create('digits')` (99.6% of
|
|
6
|
+
strangers' digits) and `create('lowercase')` (93.7%), the same pair of networks as `accurate`,
|
|
7
|
+
trained on EMNIST plus this project's own writer.
|
|
8
|
+
- **`teach()`: symbols of your own with no training.** `rec.teach({ star: [d1, d2, d3], … })` keeps
|
|
9
|
+
each drawing as what the network makes of it and reads new drawings by the nearest example;
|
|
10
|
+
`forget()` undoes it, `create({ examples })` does it at creation. Three examples per class read
|
|
11
|
+
96% of one writer's digits with the capitals-only network.
|
|
12
|
+
- **`fast` and `small` now run their own first network.** 0.1.0 and 0.1.1 shipped one `tiny.bin`
|
|
13
|
+
for all three capital-letter options, so `fast` and `small` ran `accurate`'s first network instead
|
|
14
|
+
of the ones they were measured with. Each option's model files now live in
|
|
15
|
+
`dist/char-cnn/options/<name>/` (the option's JSON names them there; the shared wasm kernel stays
|
|
16
|
+
beside the runtime). If you copied `dist/char-cnn/` into your site, copy it again.
|
|
17
|
+
- **char-lite and overlap: a new assignment solver, about a quarter faster.** The matcher's
|
|
18
|
+
assignment step is now millirec's own (`cpp/assign.cpp` in the repository), written for this
|
|
19
|
+
problem: it stops as soon as a template provably cannot matter, and it starts from prices that
|
|
20
|
+
account for the points either side may drop. One recognition against 215 templates went from
|
|
21
|
+
0.66 ms to 0.49 ms; the wasm is 27.8 KB (was 29.9). Readings can differ from 0.1.1 in close
|
|
22
|
+
calls — on one writer's held-out drawings top-1 is unchanged for capitals and digits and two
|
|
23
|
+
better of 381 for lowercase. Input the matcher cannot use (thousands of points, NaN
|
|
24
|
+
coordinates) no longer traps.
|
|
25
|
+
- **A `recognize()` that timed out no longer poisons its strokes.** When char-lite's worker did
|
|
26
|
+
not answer within 4 s the call resolved `null`, but the request stayed marked as in flight, so
|
|
27
|
+
every later `recognize()` of the same strokes waited 4 s and resolved `null` again without the
|
|
28
|
+
worker being asked. The request is forgotten once nobody is waiting on it (`rawScores()` too).
|
|
29
|
+
- A worker that fails after it has booted releases every waiting call with `null` at once; a
|
|
30
|
+
worker that fails to boot is terminated and its blob URL revoked when `create()` rejects.
|
|
31
|
+
- README: a complete one-file page to start from (ES module from a CDN) and a complete script-tag
|
|
32
|
+
page; which CDNs work (jsDelivr, unpkg — not esm.sh); pin the full version; how to load the
|
|
33
|
+
overlap widget through a bundler; TypeScript notes; where to report bugs.
|
|
34
|
+
- package.json: `repository` (github.com/triplelog/millirec), `bugs` points at its issues.
|
|
35
|
+
- The source is public at github.com/triplelog/millirec, with the CNN trainer, one writer's
|
|
36
|
+
handwriting and a walk-through for training a recognizer for your own symbols (not part of the
|
|
37
|
+
npm download).
|
|
38
|
+
|
|
3
39
|
## 0.1.1
|
|
4
40
|
|
|
5
41
|
- **Bundlers**: the package found its own folder with `new URL('./', import.meta.url)`, which Vite
|
package/README.md
CHANGED
|
@@ -4,12 +4,44 @@ Small, fast handwriting recognition in the browser. Draw a character, get the
|
|
|
4
4
|
three likeliest readings back. Runs entirely on the device — no server, no
|
|
5
5
|
upload of anyone's ink.
|
|
6
6
|
|
|
7
|
+
**[millirec.com](https://millirec.com)** has a pad to try it on and the measurements ·
|
|
8
|
+
source and issues: **[github.com/triplelog/millirec](https://github.com/triplelog/millirec)**
|
|
9
|
+
|
|
10
|
+
## Try it: one file, no build
|
|
11
|
+
|
|
12
|
+
Save this as `index.html`, serve the folder (`npx serve`, `python3 -m http.server` — a page opened
|
|
13
|
+
straight from disk cannot start the worker) and draw a digit:
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<!doctype html>
|
|
17
|
+
<meta charset="utf-8">
|
|
18
|
+
<div id="pad" style="width: 240px; height: 240px; border: 1px solid #999"></div>
|
|
19
|
+
<p id="out">draw a digit</p>
|
|
20
|
+
<script type="module">
|
|
21
|
+
import { MillirecCharLite, loadSet } from 'https://cdn.jsdelivr.net/npm/millirec@0.1.2/dist/char-lite/index.js'
|
|
22
|
+
|
|
23
|
+
const rec = await MillirecCharLite.create({ training: await loadSet('digits') })
|
|
24
|
+
rec.attach(document.querySelector('#pad'))
|
|
25
|
+
document.querySelector('#pad').addEventListener('millirec:result', (e) => {
|
|
26
|
+
const r = e.detail.results || [] // best first; empty when the pad was cleared
|
|
27
|
+
document.querySelector('#out').textContent = r.length
|
|
28
|
+
? r.map((x) => `${x.char} ${Math.round(x.prob * 100)}%`).join(' ')
|
|
29
|
+
: 'draw a digit'
|
|
30
|
+
})
|
|
31
|
+
</script>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Write another digit after a two-second pause and the pad starts over; scribble over the ink to
|
|
35
|
+
clear it.
|
|
36
|
+
|
|
37
|
+
## What is in it
|
|
38
|
+
|
|
7
39
|
Three pieces, pick by what you need to read:
|
|
8
40
|
|
|
9
41
|
| | reads | size (gzip) | trainable on your own strokes |
|
|
10
42
|
|---|---|---|---|
|
|
11
43
|
| `millirec/char-lite` | anything you have samples for: digits, letters, punctuation, emoji, made-up symbols | 26 KB + the training set (digits 4 KB, all 27 KB) | **yes** — add samples, add whole new classes |
|
|
12
|
-
| `millirec/char-cnn` | capital letters
|
|
44
|
+
| `millirec/char-cnn` | capital letters, lowercase letters or digits — and symbols of your own from a few drawings each (`teach`) | 15 KB (`small`) to 47 KB (`fast`); `accurate`, `digits`, `lowercase` about 41 KB each | `teach` new symbols with no training; `personalize` toward one writer; or train a model |
|
|
13
45
|
| overlap (script tag) | whole words written letter over letter in one box | 34 KB (script 20 + wasm 14) + training (capitals 19 KB, all 42 KB) + the default word list (38 KB, skipped when you pass `words`) | — |
|
|
14
46
|
|
|
15
47
|
## Install
|
|
@@ -27,6 +59,9 @@ is safe during server-side rendering; call `create()` on the client. In Node, `c
|
|
|
27
59
|
message saying so, and the training sets can be imported as JSON
|
|
28
60
|
(`import digits from 'millirec/char-lite/sets/digits.json' with { type: 'json' }`) for tooling.
|
|
29
61
|
|
|
62
|
+
**TypeScript**: declarations are included. Importing a set or option as JSON needs
|
|
63
|
+
`resolveJsonModule` (and `esModuleInterop` under `moduleResolution: node10`).
|
|
64
|
+
|
|
30
65
|
## char-lite
|
|
31
66
|
|
|
32
67
|
```js
|
|
@@ -104,19 +139,77 @@ training set can also be built offline and loaded with `fetch`.
|
|
|
104
139
|
```js
|
|
105
140
|
import { MillirecCharCnn } from 'millirec/char-cnn'
|
|
106
141
|
|
|
107
|
-
const rec = await MillirecCharCnn.create() // the 'accurate' option
|
|
108
|
-
// or: await MillirecCharCnn.create({ option: '
|
|
109
|
-
// (a bare name works too: create('
|
|
142
|
+
const rec = await MillirecCharCnn.create() // the 'accurate' option: capital letters
|
|
143
|
+
// or: await MillirecCharCnn.create({ option: 'digits' }) // see the table below
|
|
144
|
+
// (a bare name works too: create('digits'))
|
|
110
145
|
rec.attach(document.querySelector('#pad'))
|
|
111
146
|
await rec.recognize(strokes, { only: 'AEIOU' }) // restrict the answer to some letters
|
|
112
147
|
await rec.personalize({ A: [a1, a2, a3], R: [r1, r2] }) // lean toward this writer's hand
|
|
113
148
|
rec.destroy() // let go of the models
|
|
114
149
|
```
|
|
115
150
|
|
|
151
|
+
| option | reads | strangers' handwriting read right | gzip, with the pad |
|
|
152
|
+
|---|---|---|---|
|
|
153
|
+
| `accurate` (default) | capitals A–Z | 98.5% | 41 KB |
|
|
154
|
+
| `fast` | capitals A–Z | 97.8% | 49 KB |
|
|
155
|
+
| `small` | capitals A–Z | 96.4% | 10 KB (no pad, no wasm) |
|
|
156
|
+
| `digits` | 0–9 | 99.6% | 40 KB |
|
|
157
|
+
| `lowercase` | a–z | 93.7% | 42 KB |
|
|
158
|
+
|
|
159
|
+
Measured on EMNIST test images, which none of the models were trained on (7,800 capitals, 3,000
|
|
160
|
+
digits, 7,800 lowercase letters, from hundreds of writers). `digits` and `lowercase` are the same
|
|
161
|
+
pair of networks as `accurate` — a small one answers, a larger one takes the close calls. Most of
|
|
162
|
+
what `lowercase` gets wrong is `g`/`q` and `i`/`l`/`j`, which people draw alike. One recognizer
|
|
163
|
+
reads one alphabet; to read two, create two and take the more confident answer, or `teach` your
|
|
164
|
+
own set.
|
|
165
|
+
|
|
116
166
|
`millirec/char-cnn/core` is the same recognizer without the drawing pad; it also runs inside a
|
|
117
167
|
Web Worker (it draws on an `OffscreenCanvas` there). After `destroy()`, `recognize()` and
|
|
118
168
|
`personalize()` return `null` and `embedding()` throws.
|
|
119
169
|
|
|
170
|
+
### Your own symbols
|
|
171
|
+
|
|
172
|
+
**No training: `teach()`.** Give the recognizer a few drawings of each symbol and it reads them —
|
|
173
|
+
letters of another alphabet, a rating scale, proof-reading marks, emoji:
|
|
174
|
+
|
|
175
|
+
```js
|
|
176
|
+
const rec = await MillirecCharCnn.create() // 'accurate' (or 'fast')
|
|
177
|
+
await rec.teach({
|
|
178
|
+
star: [star1, star2, star3], // each a drawing: [[{x, y}, …], …]
|
|
179
|
+
heart: [heart1, heart2, heart3],
|
|
180
|
+
'✓': [tick1, tick2, tick3],
|
|
181
|
+
})
|
|
182
|
+
await rec.recognize(strokes) // [{ char: 'star', prob: 0.93 }, …]
|
|
183
|
+
await rec.teach({ arrow: [a1, a2, a3] }, { add: true }) // one more, keeping the others
|
|
184
|
+
rec.forget() // back to the letters
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Each example is kept as what the network makes of it, and a new drawing is given to the name of
|
|
188
|
+
its nearest example; after `teach()` the recognizer answers among the taught names only. Nothing is
|
|
189
|
+
trained and nothing is stored: keep the example strokes yourself (`pad.getStrokes()`) and call
|
|
190
|
+
`teach()` again when the page loads, or pass them as `create({ examples })`.
|
|
191
|
+
|
|
192
|
+
How well it works, measured with the shipped `accurate` network — which was trained on capital
|
|
193
|
+
letters only — on one writer's drawings of things it has never seen, tested on that writer's other
|
|
194
|
+
drawings:
|
|
195
|
+
|
|
196
|
+
| examples of each | digits (10) | lowercase (26) | six punctuation marks |
|
|
197
|
+
|---|---|---|---|
|
|
198
|
+
| 1 | 92% | 86% | 84% |
|
|
199
|
+
| 3 | 96% | 93% | 88% |
|
|
200
|
+
| 5 | 99% | 96% | 89% |
|
|
201
|
+
|
|
202
|
+
Three is a good start and five is better; draw the variants people really use. Symbols that
|
|
203
|
+
differ only in size or position (`.` and `,`, `o` and `O`) cannot be told apart: every drawing is
|
|
204
|
+
scaled to fill the picture before it is read.
|
|
205
|
+
|
|
206
|
+
**With training.** `create()` also takes any model in the same format:
|
|
207
|
+
`MillirecCharCnn.create({ classes, stages, assetBase })`, where `classes` is a string of single
|
|
208
|
+
characters or a list of names. Training one from your own drawings — a dozen per symbol, a few
|
|
209
|
+
seconds — is a four-command walk-through in the repository:
|
|
210
|
+
[train your own recognizer](https://github.com/triplelog/millirec/blob/main/packages/millirec/train/README.md).
|
|
211
|
+
That is the route to the last few percent, the smallest download, and a recognizer for many writers.
|
|
212
|
+
|
|
120
213
|
## overlap: words written in one box
|
|
121
214
|
|
|
122
215
|
Letters written one on top of another in a single box, read against a word list. Script tag only
|
|
@@ -133,7 +226,11 @@ for now:
|
|
|
133
226
|
Copy `dist/overlap/` to your site (or load it from a CDN); the script finds its wasm, training and
|
|
134
227
|
default word list beside itself. `set` is `capitals`, `lowercase`, `digits`, `punctuation` or `all`.
|
|
135
228
|
TypeScript declarations for the `MillirecOverlap` global and its `millirec-overlap:*` events come
|
|
136
|
-
with `import type {} from 'millirec/overlap'
|
|
229
|
+
with `import type {} from 'millirec/overlap'`. That path is **types only**: the widget itself is
|
|
230
|
+
the script tag, and a plain `import 'millirec/overlap'` fails with "path not exported". To pull the
|
|
231
|
+
script through a bundler instead, import the file —
|
|
232
|
+
`import 'millirec/overlap/millirec-overlap.min.js'` — and pass `assetBase` to `create()`, since a
|
|
233
|
+
bundled script can no longer find the files beside it.
|
|
137
234
|
|
|
138
235
|
## Serving the files
|
|
139
236
|
|
|
@@ -201,20 +298,38 @@ If your site sends a CSP, the recognizers need:
|
|
|
201
298
|
|
|
202
299
|
## Script tag / CDN
|
|
203
300
|
|
|
204
|
-
No build step
|
|
301
|
+
No build step and no modules — a complete page:
|
|
205
302
|
|
|
206
303
|
```html
|
|
207
|
-
|
|
208
|
-
<
|
|
304
|
+
<!doctype html>
|
|
305
|
+
<meta charset="utf-8">
|
|
306
|
+
<div id="pad" style="width: 240px; height: 240px; border: 1px solid #999"></div>
|
|
307
|
+
<p id="out">draw a digit</p>
|
|
308
|
+
<script src="https://cdn.jsdelivr.net/npm/millirec@0.1.2/dist/char-lite/millirec-char-lite.min.js"></script>
|
|
309
|
+
<script src="https://cdn.jsdelivr.net/npm/millirec@0.1.2/dist/char-lite/sets/digits.js"></script>
|
|
209
310
|
<script>
|
|
210
|
-
MillirecCharLite.create().then((rec) =>
|
|
311
|
+
MillirecCharLite.create().then((rec) => {
|
|
312
|
+
rec.attach(document.querySelector('#pad'))
|
|
313
|
+
document.querySelector('#pad').addEventListener('millirec:result', (e) => {
|
|
314
|
+
var r = e.detail.results || []
|
|
315
|
+
document.querySelector('#out').textContent = r.length ? r.map((x) => x.char).join(' ') : 'draw a digit'
|
|
316
|
+
})
|
|
317
|
+
})
|
|
211
318
|
</script>
|
|
212
319
|
```
|
|
213
320
|
|
|
214
321
|
`sets/<name>.js` sets `window.MILLIREC_TRAINING`; for char-cnn load
|
|
215
322
|
`dist/char-cnn/options/accurate.js` and then `millirec-char-cnn.min.js`. The
|
|
216
323
|
script finds its worker and wasm next to itself, on any origin that sends CORS
|
|
217
|
-
headers
|
|
324
|
+
headers.
|
|
325
|
+
|
|
326
|
+
**Which CDNs work** (tried in a browser from another origin): **jsDelivr** and **unpkg**, for the
|
|
327
|
+
script tags and for the ES modules (`…/dist/char-lite/index.js`, and jsDelivr's `/+esm`). **esm.sh
|
|
328
|
+
does not**: it serves a rewritten copy of the JavaScript without the worker and wasm files beside
|
|
329
|
+
it, so `create()` reports that they are not served.
|
|
330
|
+
|
|
331
|
+
**Pin the full version** (`@0.1.2`, not `@0.1`). The script, its worker and its wasm are separate
|
|
332
|
+
files that must come from the same release, and a CDN resolves a version range file by file.
|
|
218
333
|
|
|
219
334
|
## Stable paths
|
|
220
335
|
|
|
@@ -238,7 +353,14 @@ Recognition runs entirely in the browser: nothing a user draws is sent anywhere
|
|
|
238
353
|
|
|
239
354
|
## Versions
|
|
240
355
|
|
|
241
|
-
0.x: the API may still change between minor versions. 1.0 will mark it stable.
|
|
356
|
+
0.x: the API may still change between minor versions. 1.0 will mark it stable. Changes are in
|
|
357
|
+
[CHANGELOG.md](https://github.com/triplelog/millirec/blob/main/packages/millirec/CHANGELOG.md).
|
|
358
|
+
|
|
359
|
+
## Questions and bugs
|
|
360
|
+
|
|
361
|
+
[Open an issue](https://github.com/triplelog/millirec/issues). A drawing that reads wrong is most
|
|
362
|
+
useful with its strokes (`pad.getStrokes()`, or `e.detail.strokes` from the result event) and the
|
|
363
|
+
set or option in use.
|
|
242
364
|
|
|
243
365
|
## License
|
|
244
366
|
|
package/dist/char-cnn/core.d.ts
CHANGED
|
@@ -10,9 +10,12 @@ export interface CreateOptions extends Partial<CnnConfig> {
|
|
|
10
10
|
* or page-relative; the trailing slash is optional. It (and `kernel`) decide what code is loaded:
|
|
11
11
|
* never fill them from a URL parameter or user input. */
|
|
12
12
|
assetBase?: string
|
|
13
|
+
/** Symbols of your own, a few drawings each: the same as calling `teach(examples)` after `create()`. */
|
|
14
|
+
examples?: Record<string, Array<Strokes | Stroke>>
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
/** `accurate`, `fast` and `small` read capital letters A–Z; `digits` reads 0–9 and `lowercase` a–z. */
|
|
18
|
+
export declare const OPTIONS: readonly ['accurate', 'fast', 'small', 'digits', 'lowercase']
|
|
16
19
|
export type OptionName = (typeof OPTIONS)[number]
|
|
17
20
|
export declare function loadOption(name: OptionName, opts?: { assetBase?: string }): Promise<CnnConfig>
|
|
18
21
|
|
|
@@ -29,6 +32,15 @@ export declare class MillirecCharCnnCore {
|
|
|
29
32
|
personalize(samples: Record<string, Array<Strokes | Stroke>>, opts?: { steps?: number; lr?: number; prior?: number }): Promise<{ n: number; classes: string[] } | null>
|
|
30
33
|
/** Back to the shipped head. */
|
|
31
34
|
depersonalize(): void
|
|
35
|
+
/**
|
|
36
|
+
* Read symbols of your own from a few drawings of each, with no training: every drawing is kept as what the
|
|
37
|
+
* last stage's network makes of it, and from then on `recognize()` answers among the taught names only, by the
|
|
38
|
+
* nearest example. Names are any strings. `{ add: true }` keeps what was taught before. Resolves `null` when
|
|
39
|
+
* nothing usable was given. Best on an option whose last stage is the larger network (`accurate`, `fast`).
|
|
40
|
+
*/
|
|
41
|
+
teach(examples: Record<string, Array<Strokes | Stroke>>, opts?: { add?: boolean }): Promise<{ classes: string[]; n: number } | null>
|
|
42
|
+
/** Drop what `teach()` learned: `recognize()` reads the option's own classes again. */
|
|
43
|
+
forget(): void
|
|
32
44
|
/** The last stage's unit embedding of a drawing — what the head reads. Throws after destroy(). */
|
|
33
45
|
embedding(strokes: Strokes | Stroke): Promise<Float32Array>
|
|
34
46
|
/** Let go of the models; recognize() and personalize() return null afterwards. */
|
package/dist/char-cnn/core.js
CHANGED
|
@@ -336,6 +336,15 @@ var ASSET_BASE = scriptBase();
|
|
|
336
336
|
var fetchBuf = (url2, base) => fetchAsset(url2, { as: "buf", what: "a char-cnn model file", base, recognizer: "char-cnn" });
|
|
337
337
|
var DESTROYED = "millirec: recognizer destroyed";
|
|
338
338
|
var makeCanvas = (n) => typeof document !== "undefined" && document.createElement ? Object.assign(document.createElement("canvas"), { width: n, height: n }) : typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(n, n) : null;
|
|
339
|
+
var TEACH_SHARPNESS = 18;
|
|
340
|
+
var unitOf = (e) => {
|
|
341
|
+
let n = 0;
|
|
342
|
+
for (let i = 0; i < e.length; i++) n += e[i] * e[i];
|
|
343
|
+
n = Math.sqrt(n) || 1;
|
|
344
|
+
const o = new Float32Array(e.length);
|
|
345
|
+
for (let i = 0; i < e.length; i++) o[i] = e[i] / n;
|
|
346
|
+
return o;
|
|
347
|
+
};
|
|
339
348
|
var MillirecCharCnnCore = class {
|
|
340
349
|
static async create(opts = {}) {
|
|
341
350
|
const g = typeof window !== "undefined" ? window : globalThis;
|
|
@@ -346,6 +355,7 @@ var MillirecCharCnnCore = class {
|
|
|
346
355
|
}
|
|
347
356
|
const inst = new this(config);
|
|
348
357
|
await inst.ready;
|
|
358
|
+
if (config.examples) await inst.teach(config.examples);
|
|
349
359
|
return inst;
|
|
350
360
|
}
|
|
351
361
|
constructor(config) {
|
|
@@ -436,6 +446,7 @@ var MillirecCharCnnCore = class {
|
|
|
436
446
|
if (this.destroyed || !this.stages.length) return null;
|
|
437
447
|
this._planeFor = null;
|
|
438
448
|
const keep = only ? new Set(only) : null;
|
|
449
|
+
if (this.taught) return this.nearest(strokes, keep);
|
|
439
450
|
let z = null, used = 0;
|
|
440
451
|
for (let s = 0; s < this.stages.length; s++) {
|
|
441
452
|
const st = this.stages[s];
|
|
@@ -467,6 +478,62 @@ var MillirecCharCnnCore = class {
|
|
|
467
478
|
out.stage = used;
|
|
468
479
|
return out;
|
|
469
480
|
}
|
|
481
|
+
/** recognize() once teach() has run: the drawing against every taught example, in the last stage's feature space. */
|
|
482
|
+
nearest(strokes, keep) {
|
|
483
|
+
const last = this.stages.length - 1, st = this.stages[last];
|
|
484
|
+
const q = unitOf(st.embed(this.raster(strokes, st.size)));
|
|
485
|
+
const names = [...this.taught.keys()].filter((c) => !keep || keep.has(c));
|
|
486
|
+
const out = [];
|
|
487
|
+
if (names.length) {
|
|
488
|
+
const sim = names.map((c) => {
|
|
489
|
+
let b = -2;
|
|
490
|
+
for (const e of this.taught.get(c)) {
|
|
491
|
+
let d = 0;
|
|
492
|
+
for (let i = 0; i < q.length; i++) d += q[i] * e[i];
|
|
493
|
+
if (d > b) b = d;
|
|
494
|
+
}
|
|
495
|
+
return b;
|
|
496
|
+
});
|
|
497
|
+
const p = softmax(sim.map((v) => v * TEACH_SHARPNESS));
|
|
498
|
+
for (const i of [...p.keys()].sort((a, b) => p[b] - p[a]).slice(0, 3)) out.push({ char: names[i], prob: p[i] });
|
|
499
|
+
}
|
|
500
|
+
out.stage = last;
|
|
501
|
+
return out;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* TEACH: read symbols of your own from a few drawings of each, with no training. `examples` is
|
|
505
|
+
* `{ name: [drawing, …] }` — names are any strings (a letter, 'star', an emoji), a drawing is strokes as for
|
|
506
|
+
* recognize(). Each drawing is kept as what the last stage's network makes of it; from then on recognize() answers
|
|
507
|
+
* AMONG THE TAUGHT NAMES ONLY, by the nearest example. Three drawings of each is a good start, five is better;
|
|
508
|
+
* draw the variants people really use. `{ add: true }` keeps what was taught before (new drawings of a known name
|
|
509
|
+
* are added to it). Returns { classes, n } or null (nothing usable). forget() goes back to the shipped classes.
|
|
510
|
+
* Works best on an option whose last stage is the larger network (`accurate`, the default, or `fast`).
|
|
511
|
+
*/
|
|
512
|
+
async teach(examples, { add = false } = {}) {
|
|
513
|
+
await this.ready;
|
|
514
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
515
|
+
const taught = add && this.taught ? new Map([...this.taught].map(([c, list]) => [c, list.slice()])) : /* @__PURE__ */ new Map();
|
|
516
|
+
for (const [name, list] of Object.entries(examples || {})) {
|
|
517
|
+
for (const drawing of list || []) {
|
|
518
|
+
const strokes = asStrokes(drawing);
|
|
519
|
+
if (!strokes || !strokes.length) continue;
|
|
520
|
+
const e = unitOf(await this.embedding(strokes));
|
|
521
|
+
if (this.destroyed) return null;
|
|
522
|
+
if (!taught.has(name)) taught.set(name, []);
|
|
523
|
+
taught.get(name).push(e);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (!taught.size) {
|
|
527
|
+
if (!add) this.taught = null;
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
this.taught = taught;
|
|
531
|
+
return { classes: [...taught.keys()], n: [...taught.values()].reduce((a, l) => a + l.length, 0) };
|
|
532
|
+
}
|
|
533
|
+
/** Drop everything teach() learned: recognize() reads the shipped classes again. */
|
|
534
|
+
forget() {
|
|
535
|
+
this.taught = null;
|
|
536
|
+
}
|
|
470
537
|
/** The last stage's (unit) embedding of drawn strokes — what the head reads. */
|
|
471
538
|
async embedding(strokes) {
|
|
472
539
|
strokes = asStrokes(strokes);
|
|
@@ -479,7 +546,7 @@ var MillirecCharCnnCore = class {
|
|
|
479
546
|
/**
|
|
480
547
|
* PERSONALIZE: refit the last stage's head (a softmax over the frozen embedding) on the player's own samples
|
|
481
548
|
* (`{ 'A': [strokes…], … }`), anchored to the shipped head — the anchor's pull is prior/n, so a few samples barely move
|
|
482
|
-
* a letter and many take it over
|
|
549
|
+
* a letter and many take it over. Plain
|
|
483
550
|
* gradient descent on the mean cross-entropy: a few hundred steps over a few hundred samples is ~100 ms. While a
|
|
484
551
|
* personal head is in place the cascade always runs to the last stage (the tiny head is the shipped one).
|
|
485
552
|
* Returns { n, classes } or null (nothing usable). `depersonalize()` restores the shipped head.
|
|
@@ -537,6 +604,7 @@ var MillirecCharCnnCore = class {
|
|
|
537
604
|
this.destroyed = true;
|
|
538
605
|
this.stages = [];
|
|
539
606
|
this.personal = null;
|
|
607
|
+
this.taught = null;
|
|
540
608
|
this._plane = null;
|
|
541
609
|
this._planeFor = null;
|
|
542
610
|
}
|
|
@@ -560,7 +628,7 @@ function nodeFileUrl(url2, spec) {
|
|
|
560
628
|
}
|
|
561
629
|
|
|
562
630
|
// src/options.js
|
|
563
|
-
var OPTIONS = ["accurate", "fast", "small"];
|
|
631
|
+
var OPTIONS = ["accurate", "fast", "small", "digits", "lowercase"];
|
|
564
632
|
async function loadOption(name, { assetBase = HERE } = {}) {
|
|
565
633
|
if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
|
|
566
634
|
const base = assetBaseOf(assetBase);
|
package/dist/char-cnn/index.js
CHANGED
|
@@ -336,6 +336,15 @@ var ASSET_BASE = scriptBase();
|
|
|
336
336
|
var fetchBuf = (url2, base) => fetchAsset(url2, { as: "buf", what: "a char-cnn model file", base, recognizer: "char-cnn" });
|
|
337
337
|
var DESTROYED = "millirec: recognizer destroyed";
|
|
338
338
|
var makeCanvas = (n) => typeof document !== "undefined" && document.createElement ? Object.assign(document.createElement("canvas"), { width: n, height: n }) : typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(n, n) : null;
|
|
339
|
+
var TEACH_SHARPNESS = 18;
|
|
340
|
+
var unitOf = (e) => {
|
|
341
|
+
let n = 0;
|
|
342
|
+
for (let i = 0; i < e.length; i++) n += e[i] * e[i];
|
|
343
|
+
n = Math.sqrt(n) || 1;
|
|
344
|
+
const o = new Float32Array(e.length);
|
|
345
|
+
for (let i = 0; i < e.length; i++) o[i] = e[i] / n;
|
|
346
|
+
return o;
|
|
347
|
+
};
|
|
339
348
|
var MillirecCharCnnCore = class {
|
|
340
349
|
static async create(opts = {}) {
|
|
341
350
|
const g = typeof window !== "undefined" ? window : globalThis;
|
|
@@ -346,6 +355,7 @@ var MillirecCharCnnCore = class {
|
|
|
346
355
|
}
|
|
347
356
|
const inst = new this(config);
|
|
348
357
|
await inst.ready;
|
|
358
|
+
if (config.examples) await inst.teach(config.examples);
|
|
349
359
|
return inst;
|
|
350
360
|
}
|
|
351
361
|
constructor(config) {
|
|
@@ -436,6 +446,7 @@ var MillirecCharCnnCore = class {
|
|
|
436
446
|
if (this.destroyed || !this.stages.length) return null;
|
|
437
447
|
this._planeFor = null;
|
|
438
448
|
const keep = only ? new Set(only) : null;
|
|
449
|
+
if (this.taught) return this.nearest(strokes, keep);
|
|
439
450
|
let z = null, used = 0;
|
|
440
451
|
for (let s = 0; s < this.stages.length; s++) {
|
|
441
452
|
const st = this.stages[s];
|
|
@@ -467,6 +478,62 @@ var MillirecCharCnnCore = class {
|
|
|
467
478
|
out.stage = used;
|
|
468
479
|
return out;
|
|
469
480
|
}
|
|
481
|
+
/** recognize() once teach() has run: the drawing against every taught example, in the last stage's feature space. */
|
|
482
|
+
nearest(strokes, keep) {
|
|
483
|
+
const last = this.stages.length - 1, st = this.stages[last];
|
|
484
|
+
const q = unitOf(st.embed(this.raster(strokes, st.size)));
|
|
485
|
+
const names = [...this.taught.keys()].filter((c) => !keep || keep.has(c));
|
|
486
|
+
const out = [];
|
|
487
|
+
if (names.length) {
|
|
488
|
+
const sim = names.map((c) => {
|
|
489
|
+
let b = -2;
|
|
490
|
+
for (const e of this.taught.get(c)) {
|
|
491
|
+
let d = 0;
|
|
492
|
+
for (let i = 0; i < q.length; i++) d += q[i] * e[i];
|
|
493
|
+
if (d > b) b = d;
|
|
494
|
+
}
|
|
495
|
+
return b;
|
|
496
|
+
});
|
|
497
|
+
const p = softmax(sim.map((v) => v * TEACH_SHARPNESS));
|
|
498
|
+
for (const i of [...p.keys()].sort((a, b) => p[b] - p[a]).slice(0, 3)) out.push({ char: names[i], prob: p[i] });
|
|
499
|
+
}
|
|
500
|
+
out.stage = last;
|
|
501
|
+
return out;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* TEACH: read symbols of your own from a few drawings of each, with no training. `examples` is
|
|
505
|
+
* `{ name: [drawing, …] }` — names are any strings (a letter, 'star', an emoji), a drawing is strokes as for
|
|
506
|
+
* recognize(). Each drawing is kept as what the last stage's network makes of it; from then on recognize() answers
|
|
507
|
+
* AMONG THE TAUGHT NAMES ONLY, by the nearest example. Three drawings of each is a good start, five is better;
|
|
508
|
+
* draw the variants people really use. `{ add: true }` keeps what was taught before (new drawings of a known name
|
|
509
|
+
* are added to it). Returns { classes, n } or null (nothing usable). forget() goes back to the shipped classes.
|
|
510
|
+
* Works best on an option whose last stage is the larger network (`accurate`, the default, or `fast`).
|
|
511
|
+
*/
|
|
512
|
+
async teach(examples, { add = false } = {}) {
|
|
513
|
+
await this.ready;
|
|
514
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
515
|
+
const taught = add && this.taught ? new Map([...this.taught].map(([c, list]) => [c, list.slice()])) : /* @__PURE__ */ new Map();
|
|
516
|
+
for (const [name, list] of Object.entries(examples || {})) {
|
|
517
|
+
for (const drawing of list || []) {
|
|
518
|
+
const strokes = asStrokes(drawing);
|
|
519
|
+
if (!strokes || !strokes.length) continue;
|
|
520
|
+
const e = unitOf(await this.embedding(strokes));
|
|
521
|
+
if (this.destroyed) return null;
|
|
522
|
+
if (!taught.has(name)) taught.set(name, []);
|
|
523
|
+
taught.get(name).push(e);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (!taught.size) {
|
|
527
|
+
if (!add) this.taught = null;
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
this.taught = taught;
|
|
531
|
+
return { classes: [...taught.keys()], n: [...taught.values()].reduce((a, l) => a + l.length, 0) };
|
|
532
|
+
}
|
|
533
|
+
/** Drop everything teach() learned: recognize() reads the shipped classes again. */
|
|
534
|
+
forget() {
|
|
535
|
+
this.taught = null;
|
|
536
|
+
}
|
|
470
537
|
/** The last stage's (unit) embedding of drawn strokes — what the head reads. */
|
|
471
538
|
async embedding(strokes) {
|
|
472
539
|
strokes = asStrokes(strokes);
|
|
@@ -479,7 +546,7 @@ var MillirecCharCnnCore = class {
|
|
|
479
546
|
/**
|
|
480
547
|
* PERSONALIZE: refit the last stage's head (a softmax over the frozen embedding) on the player's own samples
|
|
481
548
|
* (`{ 'A': [strokes…], … }`), anchored to the shipped head — the anchor's pull is prior/n, so a few samples barely move
|
|
482
|
-
* a letter and many take it over
|
|
549
|
+
* a letter and many take it over. Plain
|
|
483
550
|
* gradient descent on the mean cross-entropy: a few hundred steps over a few hundred samples is ~100 ms. While a
|
|
484
551
|
* personal head is in place the cascade always runs to the last stage (the tiny head is the shipped one).
|
|
485
552
|
* Returns { n, classes } or null (nothing usable). `depersonalize()` restores the shipped head.
|
|
@@ -537,6 +604,7 @@ var MillirecCharCnnCore = class {
|
|
|
537
604
|
this.destroyed = true;
|
|
538
605
|
this.stages = [];
|
|
539
606
|
this.personal = null;
|
|
607
|
+
this.taught = null;
|
|
540
608
|
this._plane = null;
|
|
541
609
|
this._planeFor = null;
|
|
542
610
|
}
|
|
@@ -1033,7 +1101,7 @@ function nodeFileUrl(url2, spec) {
|
|
|
1033
1101
|
}
|
|
1034
1102
|
|
|
1035
1103
|
// src/options.js
|
|
1036
|
-
var OPTIONS = ["accurate", "fast", "small"];
|
|
1104
|
+
var OPTIONS = ["accurate", "fast", "small", "digits", "lowercase"];
|
|
1037
1105
|
async function loadOption(name, { assetBase = HERE } = {}) {
|
|
1038
1106
|
if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
|
|
1039
1107
|
const base = assetBaseOf(assetBase);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";(()=>{var H={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function U(o){let e=new DataView(o.buffer?o.buffer:o,o.byteOffset||0,o.byteLength),c=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),r=0;return{u32:()=>{let t=e.getUint32(r,!0);return r+=4,t},f32s:t=>{let s=new Float32Array(t);for(let a=0;a<t;a++)s[a]=e.getFloat32(r,!0),r+=4;return s},i8s:t=>{let s=new Int8Array(t);for(let a=0;a<t;a++)s[a]=c[r+a]<<24>>24;return r+=t,s},ibits:(t,s)=>{if(s===8){let i=new Int8Array(t);for(let n=0;n<t;n++)i[n]=c[r+n]<<24>>24;return r+=t,i}let a=new Int8Array(t),y=(1<<s)-1,h=1<<s-1;for(let i=0;i<t;i++){let n=i*s,f=r+(n>>3),l=n&7,w=(c[f]|c[f+1]<<8)>>l&y;a[i]=w&h?w-(1<<s):w}return r+=t*s+7>>3,a},str:t=>{let s=String.fromCharCode(...c.subarray(r,r+t));return r+=t,s},done:()=>r>=e.byteLength}}function W(o,e=8){let c=o.u32(),r=o.u32(),t=o.f32s(c),s=o.f32s(c),a=o.ibits(c*r,e),y=new Float32Array(c*r);for(let h=0;h<c;h++)for(let i=0;i<r;i++)y[h*r+i]=a[h*r+i]*t[h];return{out:c,in:r,w:y,bias:s}}function Y(o){let e=U(o),c=e.str(6),r=H[c];if(!r)throw new Error("not an MRCNN1/3/4/5/6 model");let t=e.u32();if(t!==1)throw new Error("unknown model version "+t);let s=e.u32(),a=e.u32(),y=e.u32(),h=[];for(let n=0;n<y;n++){let f=e.u32(),l=e.u32(),w=e.u32(),x=e.u32(),d=e.f32s(f),p=e.f32s(f),A=e.ibits(f*l*w*w,r),I=new Float32Array(A.length),M=l*w*w;for(let m=0;m<f;m++)for(let b=0;b<M;b++)I[m*M+b]=A[m*M+b]*d[m];h.push({cout:f,cin:l,k:w,pool:x,w:I,bias:p})}let i=W(e,r);return{size:s,dim:a,convs:h,lin:i,bits:r}}function $(o){let e=new Uint8Array(o.buffer?o.buffer:o,o.byteOffset||0,o.byteLength),c=String.fromCharCode(...e.subarray(0,6)),r=H[c];if(!r)throw new Error("not an MRCNN model");if(r===8)return e;let t=U(o);t.str(6);let s=t.u32(),a=t.u32(),y=t.u32(),h=t.u32(),i=[],n=m=>{let b=new Uint8Array(4);return new DataView(b.buffer).setUint32(0,m,!0),b},f=m=>new Uint8Array(m.buffer,m.byteOffset,m.byteLength);i.push(new TextEncoder().encode("MRCNN1"),n(s),n(a),n(y),n(h));for(let m=0;m<h;m++){let b=t.u32(),u=t.u32(),g=t.u32(),k=t.u32(),N=t.f32s(b),S=t.f32s(b),F=t.ibits(b*u*g*g,r);i.push(n(b),n(u),n(g),n(k),f(N),f(S),new Uint8Array(F.buffer,F.byteOffset,F.length))}let l=t.u32(),w=t.u32(),x=t.f32s(l),d=t.f32s(l),p=t.ibits(l*w,r);i.push(n(l),n(w),f(x),f(d),new Uint8Array(p.buffer,p.byteOffset,p.length));let A=i.reduce((m,b)=>m+b.length,0),I=new Uint8Array(A),M=0;for(let m of i)I.set(m,M),M+=m.length;return I}function X(o){return W(U(o))}function ot(o,e,c,r){let{cout:t,cin:s,k:a,w:y,bias:h}=o,i=c*c,n=a-1>>1,f=s*a*a;for(let l=0;l<t;l++){let w=l*i,x=h[l],d=l*f;for(let p=0;p<c;p++)for(let A=0;A<c;A++){let I=x;for(let M=0;M<s;M++){let m=M*i,b=d+M*a*a;for(let u=0;u<a;u++){let g=p+u-n;if(g<0||g>=c)continue;let k=m+g*c,N=b+u*a;for(let S=0;S<a;S++){let F=A+S-n;F<0||F>=c||(I+=y[N+S]*e[k+F])}}}r[w+p*c+A]=I>0?I:0}}}function rt(o,e,c,r){let t=e>>1;for(let s=0;s<c;s++){let a=s*e*e,y=s*t*t;for(let h=0;h<t;h++)for(let i=0;i<t;i++){let n=a+2*h*e+2*i,f=o[n],l=o[n+1],w=o[n+e],x=o[n+e+1];r[y+h*t+i]=Math.max(f,l,w,x)}}}function q(o,e){let c=o.size,r=Float32Array.from(e),t=c,s=1;for(let n of o.convs){let f=new Float32Array(n.cout*t*t);if(ot(n,r,t,f),r=f,s=n.cout,n.pool){let l=new Float32Array(s*(t>>1)*(t>>1));rt(r,t,s,l),r=l,t>>=1}}let a=t*t,y=new Float32Array(s);for(let n=0;n<s;n++){let f=0;for(let l=0;l<a;l++)f+=r[n*a+l];y[n]=f/a}let{lin:h}=o,i=new Float32Array(h.out);for(let n=0;n<h.out;n++){let f=h.bias[n];for(let l=0;l<h.in;l++)f+=h.w[n*h.in+l]*y[l];i[n]=f}return i}function K(o){let e=0;for(let r=0;r<o.length;r++)e+=o[r]*o[r];e=1/(Math.sqrt(e)+1e-9);let c=new Float32Array(o.length);for(let r=0;r<o.length;r++)c[r]=o[r]*e;return c}function E(o,e){let c=new Float32Array(o.out);for(let r=0;r<o.out;r++){let t=o.bias[r];for(let s=0;s<o.in;s++){let a=e[s];a>0&&(t+=o.w[r*o.in+s]*a)}c[r]=t}return c}function B(o){let e=-1/0;for(let t of o)t>e&&(e=t);let c=new Float32Array(o.length),r=0;for(let t=0;t<o.length;t++)c[t]=Math.exp(o[t]-e),r+=c[t];for(let t=0;t<o.length;t++)c[t]/=r;return c}function Z(o,e,c=120,r=7,t=10){let s=1/0,a=1/0,y=-1/0,h=-1/0;for(let f of e)for(let l of f)l.x<s&&(s=l.x),l.x>y&&(y=l.x),l.y<a&&(a=l.y),l.y>h&&(h=l.y);let i=c/Math.max(y-s,h-a,1),n=c+2*t;o.fillStyle="#fff",o.fillRect(0,0,n,n),o.strokeStyle="#000",o.lineWidth=r,o.lineCap="round",o.lineJoin="round";for(let f of e)if(!(f.length<2)){o.beginPath(),o.moveTo((f[0].x-s)*i+t,(f[0].y-a)*i+t);for(let l=1;l<f.length;l++)o.lineTo((f[l].x-s)*i+t,(f[l].y-a)*i+t);o.stroke()}return n}function V(o,e,c){let r=new Float32Array(e*c);for(let t=0,s=0;t<r.length;t++,s+=4)r[t]=1-(o[s]*.299+o[s+1]*.587+o[s+2]*.114)/255;return r}function J(o,e,c,r=32,t=24){let s=1/0,a=-1,y=1/0,h=-1;for(let u=0;u<c;u++)for(let g=0;g<e;g++)o[u*e+g]>.15&&(g<s&&(s=g),g>a&&(a=g),u<y&&(y=u),u>h&&(h=u));let i=new Float32Array(r*r);if(a<0)return i;let n=a-s+1,f=h-y+1,l=new Float32Array(n*f);for(let u=0;u<f;u++)for(let g=0;g<n;g++)l[u*n+g]=o[(u+y)*e+(g+s)];let w=0;for(let u=0;u<l.length;u++)l[u]>w&&(w=l[u]);if(w>.05&&w<.999)for(let u=0;u<l.length;u++)l[u]=Math.min(1,l[u]/w);let x=t/Math.max(f,n),d=Math.max(1,Math.round(f*x)),p=Math.max(1,Math.round(n*x)),A=new Float32Array(p*d),I=n/p,M=f/d;for(let u=0;u<d;u++){let g=u*M,k=(u+1)*M,N=Math.floor(g),S=Math.min(f,Math.ceil(k));for(let F=0;F<p;F++){let z=F*I,P=(F+1)*I,et=Math.floor(z),nt=Math.min(n,Math.ceil(P)),D=0,L=0;for(let v=N;v<S;v++){let O=Math.min(k,v+1)-Math.max(g,v);if(!(O<=0))for(let C=et;C<nt;C++){let R=Math.min(P,C+1)-Math.max(z,C);R<=0||(D+=l[v*n+C]*R*O,L+=R*O)}}A[u*p+F]=L>0?D/L:0}}let m=r-d>>1,b=r-p>>1;for(let u=0;u<d;u++)for(let g=0;g<p;g++)i[(u+m)*r+g+b]=A[u*p+g];return i}function G(o){let e=o?String(o):"";return e?(e.endsWith("/")||(e+="/"),typeof location<"u"?new URL(e,location.href).href:e):typeof location<"u"?new URL("./",location.href).href:""}var _=o=>Array.isArray(o)&&o.length&&!Array.isArray(o[0])?[o]:o,T=(o,e,c,r)=>new Error(`millirec: ${o} is not ${e} \u2014 the package files are not served at ${c}; copy node_modules/millirec/dist/${r}/ and pass assetBase (see README "Serving the files")`);async function Q(o,{as:e="json",what:c,base:r,recognizer:t}){let s=n=>{let f=T(o,c,r,t);return n&&(f.message+=` (${n})`),f},a;try{a=await fetch(o)}catch(n){throw s(n&&n.message)}let y=a.headers&&a.headers.get("content-type")||"";if(!a.ok)throw s(`HTTP ${a.status}`);if(e==="json")try{return await a.json()}catch(n){throw s(/html/i.test(y)?"an HTML page":n&&n.message)}let h=await a.arrayBuffer(),i=String.fromCharCode(...new Uint8Array(h,0,Math.min(h.byteLength,16))).trimStart().toLowerCase();if(i.startsWith("<!doctype")||i.startsWith("<html"))throw s("an HTML page");return h}function st(){let o=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return o?o.slice(0,o.lastIndexOf("/")+1):""}var it=st(),tt=(o,e)=>Q(o,{as:"buf",what:"a char-cnn model file",base:e,recognizer:"char-cnn"}),at="millirec: recognizer destroyed",ct=o=>typeof document<"u"&&document.createElement?Object.assign(document.createElement("canvas"),{width:o,height:o}):typeof OffscreenCanvas<"u"?new OffscreenCanvas(o,o):null,j=class{static async create(e={}){let r={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...e};if(!r.stages||!r.stages.length)throw new Error("MillirecCharCnn: no stages configured");if(typeof fetch>"u"||typeof document>"u"&&typeof OffscreenCanvas>"u")throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");let t=new this(r);return await t.ready,t}constructor(e){this.config=e,this.base=G(e.assetBase||it),this.classes=typeof e.classes=="string"?e.classes.split(""):e.classes,this.stages=[],this.off=ct(140),this.octx=this.off?this.off.getContext("2d",{willReadFrequently:!0}):null,this.ready=this.load()}async load(){let{base:e,config:c}=this,r=null;if(c.kernel){let t=e+c.kernel;try{r=(await import(t)).default}catch(s){let a=T(t,"the char-cnn kernel",e,"char-cnn");throw a.message+=` (${s&&s.message})`,a}}for(let t of c.stages){let[s,a]=await Promise.all([tt(e+t.model,e),tt(e+t.head,e)]);if(this.destroyed)return;let y=X(a),h={theta:t.theta==null?null:t.theta,head:y,headNorm:t.headNorm!==!1,size:0};if(r){let i=await r();if(this.destroyed)return;let n=$(new Uint8Array(s)),f=i._malloc(n.byteLength);i.HEAPU8.set(n,f);let l=i._cnnLoad(f,n.byteLength);if(i._free(f),!l)throw new Error(`kernel rejected ${t.model} (channel widths must be multiples of four)`);let w=i._cnnSize(),x=i._cnnDim();h.size=w,h.embed=d=>{let p=i._cnnInputPtr(1)>>2;i.HEAPF32.set(d,p),i._cnnRun(1);let A=i._cnnEmbedPtr(1)>>2;return i.HEAPF32.slice(A,A+x)},h.logits=d=>E(h.head,h.embed(d))}else{let i=Y(s);h.size=i.size,h.embed=n=>{let f=q(i,n);return h.headNorm?K(f):f},h.logits=n=>E(h.head,h.embed(n))}this.stages.push(h)}}raster(e,c){if(!this.octx)throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");if(this._planeFor!==e){let s=Z(this.octx,e);this._plane={ink:V(this.octx.getImageData(0,0,s,s).data,s,s),W:s},this._planeFor=e}let{ink:r,W:t}=this._plane;return J(r,t,t,c,Math.round(c*.75))}async recognize(e,{only:c=null}={}){if(e=_(e),!e||!e.length||(await this.ready,this.destroyed||!this.stages.length))return null;this._planeFor=null;let r=c?new Set(c):null,t=null,s=0;for(let n=0;n<this.stages.length;n++){let f=this.stages[n];if((this.personal||r)&&n<this.stages.length-1)continue;if(t=f.logits(this.raster(e,f.size)),s=n,f.theta==null||n===this.stages.length-1)break;let l=-1/0,w=-1/0;for(let x of t)x>l?(w=l,l=x):x>w&&(w=x);if(l-w>=f.theta)break}let a=[...t.keys()];if(r&&(a=a.filter(n=>r.has(this.classes[n])),!a.length)){let n=[];return n.stage=s,n}let y=B(a.map(n=>t[n])),i=[...y.keys()].sort((n,f)=>y[f]-y[n]).slice(0,3).map(n=>({char:this.classes[a[n]],prob:y[n]}));return i.stage=s,i}async embedding(e){if(e=_(e),await this.ready,this.destroyed||!this.stages.length)throw new Error(at);this._planeFor=null;let c=this.stages[this.stages.length-1];return c.embed(this.raster(e,c.size))}async personalize(e,{steps:c=250,lr:r=.5,prior:t=1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let s=this.stages[this.stages.length-1],a=s.head0||(s.head0=s.head),y=[],h=[];for(let[w,x]of Object.entries(e||{})){let d=this.classes.indexOf(w);if(!(d<0))for(let p of x||[]){let A=_(p);!A||!A.length||(y.push(await this.embedding(A)),h.push(d))}}if(this.destroyed)return null;if(!y.length)return s.head=a,this.personal=null,null;let i={in:a.in,out:a.out,w:Float32Array.from(a.w),bias:Float32Array.from(a.bias)},n=y.length,f=new Float32Array(i.w.length),l=new Float32Array(i.out);for(let w=0;w<c;w++){f.fill(0),l.fill(0);for(let d=0;d<n;d++){let p=B(E(i,y[d])),A=y[d];for(let I=0;I<i.out;I++){let M=(p[I]-(I===h[d]?1:0))/n;if(!M)continue;l[I]+=M;let m=I*i.in;for(let b=0;b<i.in;b++){let u=A[b];u>0&&(f[m+b]+=M*u)}}}let x=t/n;for(let d=0;d<i.w.length;d++)i.w[d]-=r*(f[d]+x*(i.w[d]-a.w[d]));for(let d=0;d<i.out;d++)i.bias[d]-=r*(l[d]+x*(i.bias[d]-a.bias[d]))}return s.head=i,this.personal={n,classes:[...new Set(h)].map(w=>this.classes[w]).sort()},this.personal}destroy(){this.destroyed=!0,this.stages=[],this.personal=null,this._plane=null,this._planeFor=null}depersonalize(){let e=this.stages[this.stages.length-1];e&&e.head0&&(e.head=e.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=j);})();
|
|
1
|
+
"use strict";(()=>{var D={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function z(s){let n=new DataView(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=new Uint8Array(n.buffer,n.byteOffset,n.byteLength),e=0;return{u32:()=>{let t=n.getUint32(e,!0);return e+=4,t},f32s:t=>{let r=new Float32Array(t);for(let a=0;a<t;a++)r[a]=n.getFloat32(e,!0),e+=4;return r},i8s:t=>{let r=new Int8Array(t);for(let a=0;a<t;a++)r[a]=l[e+a]<<24>>24;return e+=t,r},ibits:(t,r)=>{if(r===8){let i=new Int8Array(t);for(let o=0;o<t;o++)i[o]=l[e+o]<<24>>24;return e+=t,i}let a=new Int8Array(t),u=(1<<r)-1,h=1<<r-1;for(let i=0;i<t;i++){let o=i*r,c=e+(o>>3),f=o&7,d=(l[c]|l[c+1]<<8)>>f&u;a[i]=d&h?d-(1<<r):d}return e+=t*r+7>>3,a},str:t=>{let r=String.fromCharCode(...l.subarray(e,e+t));return e+=t,r},done:()=>e>=n.byteLength}}function q(s,n=8){let l=s.u32(),e=s.u32(),t=s.f32s(l),r=s.f32s(l),a=s.ibits(l*e,n),u=new Float32Array(l*e);for(let h=0;h<l;h++)for(let i=0;i<e;i++)u[h*e+i]=a[h*e+i]*t[h];return{out:l,in:e,w:u,bias:r}}function W(s){let n=z(s),l=n.str(6),e=D[l];if(!e)throw new Error("not an MRCNN1/3/4/5/6 model");let t=n.u32();if(t!==1)throw new Error("unknown model version "+t);let r=n.u32(),a=n.u32(),u=n.u32(),h=[];for(let o=0;o<u;o++){let c=n.u32(),f=n.u32(),d=n.u32(),m=n.u32(),g=n.f32s(c),b=n.f32s(c),A=n.ibits(c*f*d*d,e),M=new Float32Array(A.length),I=f*d*d;for(let p=0;p<c;p++)for(let x=0;x<I;x++)M[p*I+x]=A[p*I+x]*g[p];h.push({cout:c,cin:f,k:d,pool:m,w:M,bias:b})}let i=q(n,e);return{size:r,dim:a,convs:h,lin:i,bits:e}}function Y(s){let n=new Uint8Array(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=String.fromCharCode(...n.subarray(0,6)),e=D[l];if(!e)throw new Error("not an MRCNN model");if(e===8)return n;let t=z(s);t.str(6);let r=t.u32(),a=t.u32(),u=t.u32(),h=t.u32(),i=[],o=p=>{let x=new Uint8Array(4);return new DataView(x.buffer).setUint32(0,p,!0),x},c=p=>new Uint8Array(p.buffer,p.byteOffset,p.byteLength);i.push(new TextEncoder().encode("MRCNN1"),o(r),o(a),o(u),o(h));for(let p=0;p<h;p++){let x=t.u32(),y=t.u32(),w=t.u32(),k=t.u32(),N=t.f32s(x),S=t.f32s(x),F=t.ibits(x*y*w*w,e);i.push(o(x),o(y),o(w),o(k),c(N),c(S),new Uint8Array(F.buffer,F.byteOffset,F.length))}let f=t.u32(),d=t.u32(),m=t.f32s(f),g=t.f32s(f),b=t.ibits(f*d,e);i.push(o(f),o(d),c(m),c(g),new Uint8Array(b.buffer,b.byteOffset,b.length));let A=i.reduce((p,x)=>p+x.length,0),M=new Uint8Array(A),I=0;for(let p of i)M.set(p,I),I+=p.length;return M}function $(s){return q(z(s))}function st(s,n,l,e){let{cout:t,cin:r,k:a,w:u,bias:h}=s,i=l*l,o=a-1>>1,c=r*a*a;for(let f=0;f<t;f++){let d=f*i,m=h[f],g=f*c;for(let b=0;b<l;b++)for(let A=0;A<l;A++){let M=m;for(let I=0;I<r;I++){let p=I*i,x=g+I*a*a;for(let y=0;y<a;y++){let w=b+y-o;if(w<0||w>=l)continue;let k=p+w*l,N=x+y*a;for(let S=0;S<a;S++){let F=A+S-o;F<0||F>=l||(M+=u[N+S]*n[k+F])}}}e[d+b*l+A]=M>0?M:0}}}function rt(s,n,l,e){let t=n>>1;for(let r=0;r<l;r++){let a=r*n*n,u=r*t*t;for(let h=0;h<t;h++)for(let i=0;i<t;i++){let o=a+2*h*n+2*i,c=s[o],f=s[o+1],d=s[o+n],m=s[o+n+1];e[u+h*t+i]=Math.max(c,f,d,m)}}}function X(s,n){let l=s.size,e=Float32Array.from(n),t=l,r=1;for(let o of s.convs){let c=new Float32Array(o.cout*t*t);if(st(o,e,t,c),e=c,r=o.cout,o.pool){let f=new Float32Array(r*(t>>1)*(t>>1));rt(e,t,r,f),e=f,t>>=1}}let a=t*t,u=new Float32Array(r);for(let o=0;o<r;o++){let c=0;for(let f=0;f<a;f++)c+=e[o*a+f];u[o]=c/a}let{lin:h}=s,i=new Float32Array(h.out);for(let o=0;o<h.out;o++){let c=h.bias[o];for(let f=0;f<h.in;f++)c+=h.w[o*h.in+f]*u[f];i[o]=c}return i}function K(s){let n=0;for(let e=0;e<s.length;e++)n+=s[e]*s[e];n=1/(Math.sqrt(n)+1e-9);let l=new Float32Array(s.length);for(let e=0;e<s.length;e++)l[e]=s[e]*n;return l}function _(s,n){let l=new Float32Array(s.out);for(let e=0;e<s.out;e++){let t=s.bias[e];for(let r=0;r<s.in;r++){let a=n[r];a>0&&(t+=s.w[e*s.in+r]*a)}l[e]=t}return l}function L(s){let n=-1/0;for(let t of s)t>n&&(n=t);let l=new Float32Array(s.length),e=0;for(let t=0;t<s.length;t++)l[t]=Math.exp(s[t]-n),e+=l[t];for(let t=0;t<s.length;t++)l[t]/=e;return l}function Z(s,n,l=120,e=7,t=10){let r=1/0,a=1/0,u=-1/0,h=-1/0;for(let c of n)for(let f of c)f.x<r&&(r=f.x),f.x>u&&(u=f.x),f.y<a&&(a=f.y),f.y>h&&(h=f.y);let i=l/Math.max(u-r,h-a,1),o=l+2*t;s.fillStyle="#fff",s.fillRect(0,0,o,o),s.strokeStyle="#000",s.lineWidth=e,s.lineCap="round",s.lineJoin="round";for(let c of n)if(!(c.length<2)){s.beginPath(),s.moveTo((c[0].x-r)*i+t,(c[0].y-a)*i+t);for(let f=1;f<c.length;f++)s.lineTo((c[f].x-r)*i+t,(c[f].y-a)*i+t);s.stroke()}return o}function V(s,n,l){let e=new Float32Array(n*l);for(let t=0,r=0;t<e.length;t++,r+=4)e[t]=1-(s[r]*.299+s[r+1]*.587+s[r+2]*.114)/255;return e}function J(s,n,l,e=32,t=24){let r=1/0,a=-1,u=1/0,h=-1;for(let y=0;y<l;y++)for(let w=0;w<n;w++)s[y*n+w]>.15&&(w<r&&(r=w),w>a&&(a=w),y<u&&(u=y),y>h&&(h=y));let i=new Float32Array(e*e);if(a<0)return i;let o=a-r+1,c=h-u+1,f=new Float32Array(o*c);for(let y=0;y<c;y++)for(let w=0;w<o;w++)f[y*o+w]=s[(y+u)*n+(w+r)];let d=0;for(let y=0;y<f.length;y++)f[y]>d&&(d=f[y]);if(d>.05&&d<.999)for(let y=0;y<f.length;y++)f[y]=Math.min(1,f[y]/d);let m=t/Math.max(c,o),g=Math.max(1,Math.round(c*m)),b=Math.max(1,Math.round(o*m)),A=new Float32Array(b*g),M=o/b,I=c/g;for(let y=0;y<g;y++){let w=y*I,k=(y+1)*I,N=Math.floor(w),S=Math.min(c,Math.ceil(k));for(let F=0;F<b;F++){let B=F*M,H=(F+1)*M,nt=Math.floor(B),ot=Math.min(o,Math.ceil(H)),P=0,O=0;for(let v=N;v<S;v++){let R=Math.min(k,v+1)-Math.max(w,v);if(!(R<=0))for(let E=nt;E<ot;E++){let U=Math.min(H,E+1)-Math.max(B,E);U<=0||(P+=f[v*o+E]*U*R,O+=U*R)}}A[y*b+F]=O>0?P/O:0}}let p=e-g>>1,x=e-b>>1;for(let y=0;y<g;y++)for(let w=0;w<b;w++)i[(y+p)*e+w+x]=A[y*b+w];return i}function G(s){let n=s?String(s):"";return n?(n.endsWith("/")||(n+="/"),typeof location<"u"?new URL(n,location.href).href:n):typeof location<"u"?new URL("./",location.href).href:""}var C=s=>Array.isArray(s)&&s.length&&!Array.isArray(s[0])?[s]:s,T=(s,n,l,e)=>new Error(`millirec: ${s} is not ${n} \u2014 the package files are not served at ${l}; copy node_modules/millirec/dist/${e}/ and pass assetBase (see README "Serving the files")`);async function Q(s,{as:n="json",what:l,base:e,recognizer:t}){let r=o=>{let c=T(s,l,e,t);return o&&(c.message+=` (${o})`),c},a;try{a=await fetch(s)}catch(o){throw r(o&&o.message)}let u=a.headers&&a.headers.get("content-type")||"";if(!a.ok)throw r(`HTTP ${a.status}`);if(n==="json")try{return await a.json()}catch(o){throw r(/html/i.test(u)?"an HTML page":o&&o.message)}let h=await a.arrayBuffer(),i=String.fromCharCode(...new Uint8Array(h,0,Math.min(h.byteLength,16))).trimStart().toLowerCase();if(i.startsWith("<!doctype")||i.startsWith("<html"))throw r("an HTML page");return h}function it(){let s=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return s?s.slice(0,s.lastIndexOf("/")+1):""}var at=it(),tt=(s,n)=>Q(s,{as:"buf",what:"a char-cnn model file",base:n,recognizer:"char-cnn"}),lt="millirec: recognizer destroyed",ct=s=>typeof document<"u"&&document.createElement?Object.assign(document.createElement("canvas"),{width:s,height:s}):typeof OffscreenCanvas<"u"?new OffscreenCanvas(s,s):null,ft=18,et=s=>{let n=0;for(let e=0;e<s.length;e++)n+=s[e]*s[e];n=Math.sqrt(n)||1;let l=new Float32Array(s.length);for(let e=0;e<s.length;e++)l[e]=s[e]/n;return l},j=class{static async create(n={}){let e={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...n};if(!e.stages||!e.stages.length)throw new Error("MillirecCharCnn: no stages configured");if(typeof fetch>"u"||typeof document>"u"&&typeof OffscreenCanvas>"u")throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");let t=new this(e);return await t.ready,e.examples&&await t.teach(e.examples),t}constructor(n){this.config=n,this.base=G(n.assetBase||at),this.classes=typeof n.classes=="string"?n.classes.split(""):n.classes,this.stages=[],this.off=ct(140),this.octx=this.off?this.off.getContext("2d",{willReadFrequently:!0}):null,this.ready=this.load()}async load(){let{base:n,config:l}=this,e=null;if(l.kernel){let t=n+l.kernel;try{e=(await import(t)).default}catch(r){let a=T(t,"the char-cnn kernel",n,"char-cnn");throw a.message+=` (${r&&r.message})`,a}}for(let t of l.stages){let[r,a]=await Promise.all([tt(n+t.model,n),tt(n+t.head,n)]);if(this.destroyed)return;let u=$(a),h={theta:t.theta==null?null:t.theta,head:u,headNorm:t.headNorm!==!1,size:0};if(e){let i=await e();if(this.destroyed)return;let o=Y(new Uint8Array(r)),c=i._malloc(o.byteLength);i.HEAPU8.set(o,c);let f=i._cnnLoad(c,o.byteLength);if(i._free(c),!f)throw new Error(`kernel rejected ${t.model} (channel widths must be multiples of four)`);let d=i._cnnSize(),m=i._cnnDim();h.size=d,h.embed=g=>{let b=i._cnnInputPtr(1)>>2;i.HEAPF32.set(g,b),i._cnnRun(1);let A=i._cnnEmbedPtr(1)>>2;return i.HEAPF32.slice(A,A+m)},h.logits=g=>_(h.head,h.embed(g))}else{let i=W(r);h.size=i.size,h.embed=o=>{let c=X(i,o);return h.headNorm?K(c):c},h.logits=o=>_(h.head,h.embed(o))}this.stages.push(h)}}raster(n,l){if(!this.octx)throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");if(this._planeFor!==n){let r=Z(this.octx,n);this._plane={ink:V(this.octx.getImageData(0,0,r,r).data,r,r),W:r},this._planeFor=n}let{ink:e,W:t}=this._plane;return J(e,t,t,l,Math.round(l*.75))}async recognize(n,{only:l=null}={}){if(n=C(n),!n||!n.length||(await this.ready,this.destroyed||!this.stages.length))return null;this._planeFor=null;let e=l?new Set(l):null;if(this.taught)return this.nearest(n,e);let t=null,r=0;for(let o=0;o<this.stages.length;o++){let c=this.stages[o];if((this.personal||e)&&o<this.stages.length-1)continue;if(t=c.logits(this.raster(n,c.size)),r=o,c.theta==null||o===this.stages.length-1)break;let f=-1/0,d=-1/0;for(let m of t)m>f?(d=f,f=m):m>d&&(d=m);if(f-d>=c.theta)break}let a=[...t.keys()];if(e&&(a=a.filter(o=>e.has(this.classes[o])),!a.length)){let o=[];return o.stage=r,o}let u=L(a.map(o=>t[o])),i=[...u.keys()].sort((o,c)=>u[c]-u[o]).slice(0,3).map(o=>({char:this.classes[a[o]],prob:u[o]}));return i.stage=r,i}nearest(n,l){let e=this.stages.length-1,t=this.stages[e],r=et(t.embed(this.raster(n,t.size))),a=[...this.taught.keys()].filter(h=>!l||l.has(h)),u=[];if(a.length){let h=a.map(o=>{let c=-2;for(let f of this.taught.get(o)){let d=0;for(let m=0;m<r.length;m++)d+=r[m]*f[m];d>c&&(c=d)}return c}),i=L(h.map(o=>o*ft));for(let o of[...i.keys()].sort((c,f)=>i[f]-i[c]).slice(0,3))u.push({char:a[o],prob:i[o]})}return u.stage=e,u}async teach(n,{add:l=!1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let e=l&&this.taught?new Map([...this.taught].map(([t,r])=>[t,r.slice()])):new Map;for(let[t,r]of Object.entries(n||{}))for(let a of r||[]){let u=C(a);if(!u||!u.length)continue;let h=et(await this.embedding(u));if(this.destroyed)return null;e.has(t)||e.set(t,[]),e.get(t).push(h)}return e.size?(this.taught=e,{classes:[...e.keys()],n:[...e.values()].reduce((t,r)=>t+r.length,0)}):(l||(this.taught=null),null)}forget(){this.taught=null}async embedding(n){if(n=C(n),await this.ready,this.destroyed||!this.stages.length)throw new Error(lt);this._planeFor=null;let l=this.stages[this.stages.length-1];return l.embed(this.raster(n,l.size))}async personalize(n,{steps:l=250,lr:e=.5,prior:t=1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let r=this.stages[this.stages.length-1],a=r.head0||(r.head0=r.head),u=[],h=[];for(let[d,m]of Object.entries(n||{})){let g=this.classes.indexOf(d);if(!(g<0))for(let b of m||[]){let A=C(b);!A||!A.length||(u.push(await this.embedding(A)),h.push(g))}}if(this.destroyed)return null;if(!u.length)return r.head=a,this.personal=null,null;let i={in:a.in,out:a.out,w:Float32Array.from(a.w),bias:Float32Array.from(a.bias)},o=u.length,c=new Float32Array(i.w.length),f=new Float32Array(i.out);for(let d=0;d<l;d++){c.fill(0),f.fill(0);for(let g=0;g<o;g++){let b=L(_(i,u[g])),A=u[g];for(let M=0;M<i.out;M++){let I=(b[M]-(M===h[g]?1:0))/o;if(!I)continue;f[M]+=I;let p=M*i.in;for(let x=0;x<i.in;x++){let y=A[x];y>0&&(c[p+x]+=I*y)}}}let m=t/o;for(let g=0;g<i.w.length;g++)i.w[g]-=e*(c[g]+m*(i.w[g]-a.w[g]));for(let g=0;g<i.out;g++)i.bias[g]-=e*(f[g]+m*(i.bias[g]-a.bias[g]))}return r.head=i,this.personal={n:o,classes:[...new Set(h)].map(d=>this.classes[d]).sort()},this.personal}destroy(){this.destroyed=!0,this.stages=[],this.personal=null,this.taught=null,this._plane=null,this._planeFor=null}depersonalize(){let n=this.stages[this.stages.length-1];n&&n.head0&&(n.head=n.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=j);})();
|