millirec 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +156 -2
- package/dist/char-cnn/base-p25.bin +0 -0
- package/dist/char-cnn/base-p25.head.bin +0 -0
- package/dist/char-cnn/base.head.bin +0 -0
- package/dist/char-cnn/base.q4.bin +0 -0
- package/dist/char-cnn/cnn-web.js +8 -0
- package/dist/char-cnn/cnn-web.wasm +0 -0
- package/dist/char-cnn/core.d.ts +35 -0
- package/dist/char-cnn/core.js +536 -0
- package/dist/char-cnn/index.d.ts +9 -0
- package/dist/char-cnn/index.js +1009 -0
- package/dist/char-cnn/millirec-char-cnn-core.min.js +1 -0
- package/dist/char-cnn/millirec-char-cnn.min.js +1 -0
- package/dist/char-cnn/options/accurate.js +17 -0
- package/dist/char-cnn/options/accurate.json +15 -0
- package/dist/char-cnn/options/fast.js +17 -0
- package/dist/char-cnn/options/fast.json +15 -0
- package/dist/char-cnn/options/small.js +11 -0
- package/dist/char-cnn/options/small.json +9 -0
- package/dist/char-cnn/tiny.bin +0 -0
- package/dist/char-cnn/tiny.head.bin +0 -0
- package/dist/char-lite/index.d.ts +77 -0
- package/dist/char-lite/index.js +894 -0
- package/dist/char-lite/millirec-char-lite.min.js +1 -0
- package/dist/char-lite/millirec.wasm +0 -0
- package/dist/char-lite/sets/all.js +2 -0
- package/dist/char-lite/sets/all.json +1 -0
- package/dist/char-lite/sets/capitals.js +2 -0
- package/dist/char-lite/sets/capitals.json +1 -0
- package/dist/char-lite/sets/digits.js +2 -0
- package/dist/char-lite/sets/digits.json +1 -0
- package/dist/char-lite/sets/lowercase.js +2 -0
- package/dist/char-lite/sets/lowercase.json +1 -0
- package/dist/char-lite/sets/punctuation.js +2 -0
- package/dist/char-lite/sets/punctuation.json +1 -0
- package/dist/char-lite/sets.json +163 -0
- package/dist/char-lite/worker-stroke.js +11 -0
- package/dist/overlap/millirec-overlap.min.js +45 -0
- package/dist/overlap/millirec.wasm +0 -0
- package/dist/overlap/training.all.json +1 -0
- package/dist/overlap/training.capitals.json +1 -0
- package/dist/overlap/training.digits.json +1 -0
- package/dist/overlap/training.lowercase.json +1 -0
- package/dist/overlap/training.punctuation.json +1 -0
- package/dist/overlap/words.json +1 -0
- package/package.json +53 -4
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
// ../../server/public/wordgpu/cnnjs.mjs
|
|
2
|
+
var BITS_OF = { MRCNN1: 8, MRCNN3: 3, MRCNN4: 4, MRCNN5: 5, MRCNN6: 6 };
|
|
3
|
+
function reader(buf) {
|
|
4
|
+
const dv = new DataView(buf.buffer ? buf.buffer : buf, buf.byteOffset || 0, buf.byteLength);
|
|
5
|
+
const u8 = new Uint8Array(dv.buffer, dv.byteOffset, dv.byteLength);
|
|
6
|
+
let o = 0;
|
|
7
|
+
return {
|
|
8
|
+
u32: () => {
|
|
9
|
+
const v = dv.getUint32(o, true);
|
|
10
|
+
o += 4;
|
|
11
|
+
return v;
|
|
12
|
+
},
|
|
13
|
+
f32s: (n) => {
|
|
14
|
+
const a = new Float32Array(n);
|
|
15
|
+
for (let i = 0; i < n; i++) {
|
|
16
|
+
a[i] = dv.getFloat32(o, true);
|
|
17
|
+
o += 4;
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
},
|
|
21
|
+
i8s: (n) => {
|
|
22
|
+
const a = new Int8Array(n);
|
|
23
|
+
for (let i = 0; i < n; i++) a[i] = u8[o + i] << 24 >> 24;
|
|
24
|
+
o += n;
|
|
25
|
+
return a;
|
|
26
|
+
},
|
|
27
|
+
// n-bit two's-complement fields packed LSB-first, padded to a whole byte
|
|
28
|
+
ibits: (n, bits) => {
|
|
29
|
+
if (bits === 8) {
|
|
30
|
+
const a2 = new Int8Array(n);
|
|
31
|
+
for (let i = 0; i < n; i++) a2[i] = u8[o + i] << 24 >> 24;
|
|
32
|
+
o += n;
|
|
33
|
+
return a2;
|
|
34
|
+
}
|
|
35
|
+
const a = new Int8Array(n), mask = (1 << bits) - 1, sign = 1 << bits - 1;
|
|
36
|
+
for (let i = 0; i < n; i++) {
|
|
37
|
+
const bp = i * bits, byte = o + (bp >> 3), sh = bp & 7;
|
|
38
|
+
let v = (u8[byte] | u8[byte + 1] << 8) >> sh & mask;
|
|
39
|
+
a[i] = v & sign ? v - (1 << bits) : v;
|
|
40
|
+
}
|
|
41
|
+
o += n * bits + 7 >> 3;
|
|
42
|
+
return a;
|
|
43
|
+
},
|
|
44
|
+
str: (n) => {
|
|
45
|
+
const s = String.fromCharCode(...u8.subarray(o, o + n));
|
|
46
|
+
o += n;
|
|
47
|
+
return s;
|
|
48
|
+
},
|
|
49
|
+
done: () => o >= dv.byteLength
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function readLinear(r, bits = 8) {
|
|
53
|
+
const out = r.u32(), inn = r.u32();
|
|
54
|
+
const scale = r.f32s(out), bias = r.f32s(out), q = r.ibits(out * inn, bits);
|
|
55
|
+
const w = new Float32Array(out * inn);
|
|
56
|
+
for (let o = 0; o < out; o++) for (let i = 0; i < inn; i++) w[o * inn + i] = q[o * inn + i] * scale[o];
|
|
57
|
+
return { out, in: inn, w, bias };
|
|
58
|
+
}
|
|
59
|
+
function parseModel(buf) {
|
|
60
|
+
const r = reader(buf);
|
|
61
|
+
const magic = r.str(6);
|
|
62
|
+
const bits = BITS_OF[magic];
|
|
63
|
+
if (!bits) throw new Error("not an MRCNN1/3/4/5/6 model");
|
|
64
|
+
const version = r.u32();
|
|
65
|
+
if (version !== 1) throw new Error("unknown model version " + version);
|
|
66
|
+
const size = r.u32(), dim = r.u32(), nConv = r.u32();
|
|
67
|
+
const convs = [];
|
|
68
|
+
for (let L = 0; L < nConv; L++) {
|
|
69
|
+
const cout = r.u32(), cin = r.u32(), k = r.u32(), pool = r.u32();
|
|
70
|
+
const scale = r.f32s(cout), bias = r.f32s(cout), q = r.ibits(cout * cin * k * k, bits);
|
|
71
|
+
const w = new Float32Array(q.length);
|
|
72
|
+
const per = cin * k * k;
|
|
73
|
+
for (let o = 0; o < cout; o++) for (let i = 0; i < per; i++) w[o * per + i] = q[o * per + i] * scale[o];
|
|
74
|
+
convs.push({ cout, cin, k, pool, w, bias });
|
|
75
|
+
}
|
|
76
|
+
const lin = readLinear(r, bits);
|
|
77
|
+
return { size, dim, convs, lin, bits };
|
|
78
|
+
}
|
|
79
|
+
function expandToInt8(buf) {
|
|
80
|
+
const u8 = new Uint8Array(buf.buffer ? buf.buffer : buf, buf.byteOffset || 0, buf.byteLength);
|
|
81
|
+
const magic = String.fromCharCode(...u8.subarray(0, 6));
|
|
82
|
+
const bits = BITS_OF[magic];
|
|
83
|
+
if (!bits) throw new Error("not an MRCNN model");
|
|
84
|
+
if (bits === 8) return u8;
|
|
85
|
+
const r = reader(buf);
|
|
86
|
+
r.str(6);
|
|
87
|
+
const version = r.u32(), size = r.u32(), dim = r.u32(), nConv = r.u32();
|
|
88
|
+
const parts = [];
|
|
89
|
+
const u32 = (v) => {
|
|
90
|
+
const b = new Uint8Array(4);
|
|
91
|
+
new DataView(b.buffer).setUint32(0, v, true);
|
|
92
|
+
return b;
|
|
93
|
+
};
|
|
94
|
+
const f32s = (a) => new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
|
|
95
|
+
parts.push(new TextEncoder().encode("MRCNN1"), u32(version), u32(size), u32(dim), u32(nConv));
|
|
96
|
+
for (let L = 0; L < nConv; L++) {
|
|
97
|
+
const cout = r.u32(), cin = r.u32(), k = r.u32(), pool = r.u32();
|
|
98
|
+
const scale2 = r.f32s(cout), bias2 = r.f32s(cout), q2 = r.ibits(cout * cin * k * k, bits);
|
|
99
|
+
parts.push(u32(cout), u32(cin), u32(k), u32(pool), f32s(scale2), f32s(bias2), new Uint8Array(q2.buffer, q2.byteOffset, q2.length));
|
|
100
|
+
}
|
|
101
|
+
const out = r.u32(), inn = r.u32();
|
|
102
|
+
const scale = r.f32s(out), bias = r.f32s(out), q = r.ibits(out * inn, bits);
|
|
103
|
+
parts.push(u32(out), u32(inn), f32s(scale), f32s(bias), new Uint8Array(q.buffer, q.byteOffset, q.length));
|
|
104
|
+
const total = parts.reduce((a, p) => a + p.length, 0);
|
|
105
|
+
const res = new Uint8Array(total);
|
|
106
|
+
let o = 0;
|
|
107
|
+
for (const p of parts) {
|
|
108
|
+
res.set(p, o);
|
|
109
|
+
o += p.length;
|
|
110
|
+
}
|
|
111
|
+
return res;
|
|
112
|
+
}
|
|
113
|
+
function parseHead(buf) {
|
|
114
|
+
return readLinear(reader(buf));
|
|
115
|
+
}
|
|
116
|
+
function conv3(c, inp, res, out) {
|
|
117
|
+
const { cout, cin, k, w, bias } = c;
|
|
118
|
+
const plane = res * res, half = k - 1 >> 1, per = cin * k * k;
|
|
119
|
+
for (let co = 0; co < cout; co++) {
|
|
120
|
+
const ob = co * plane, b = bias[co], wb = co * per;
|
|
121
|
+
for (let y = 0; y < res; y++) {
|
|
122
|
+
for (let x = 0; x < res; x++) {
|
|
123
|
+
let s = b;
|
|
124
|
+
for (let ci = 0; ci < cin; ci++) {
|
|
125
|
+
const ib = ci * plane, wc = wb + ci * k * k;
|
|
126
|
+
for (let ky = 0; ky < k; ky++) {
|
|
127
|
+
const yy = y + ky - half;
|
|
128
|
+
if (yy < 0 || yy >= res) continue;
|
|
129
|
+
const row = ib + yy * res, wr = wc + ky * k;
|
|
130
|
+
for (let kx = 0; kx < k; kx++) {
|
|
131
|
+
const xx = x + kx - half;
|
|
132
|
+
if (xx < 0 || xx >= res) continue;
|
|
133
|
+
s += w[wr + kx] * inp[row + xx];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
out[ob + y * res + x] = s > 0 ? s : 0;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function pool2(inp, res, ch, out) {
|
|
143
|
+
const r2 = res >> 1;
|
|
144
|
+
for (let c = 0; c < ch; c++) {
|
|
145
|
+
const ib = c * res * res, ob = c * r2 * r2;
|
|
146
|
+
for (let y = 0; y < r2; y++) for (let x = 0; x < r2; x++) {
|
|
147
|
+
const i0 = ib + 2 * y * res + 2 * x;
|
|
148
|
+
const a = inp[i0], b = inp[i0 + 1], d = inp[i0 + res], e = inp[i0 + res + 1];
|
|
149
|
+
out[ob + y * r2 + x] = Math.max(a, b, d, e);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function embed(model, img) {
|
|
154
|
+
const S = model.size;
|
|
155
|
+
let cur = Float32Array.from(img), res = S, ch = 1;
|
|
156
|
+
for (const c of model.convs) {
|
|
157
|
+
const out = new Float32Array(c.cout * res * res);
|
|
158
|
+
conv3(c, cur, res, out);
|
|
159
|
+
cur = out;
|
|
160
|
+
ch = c.cout;
|
|
161
|
+
if (c.pool) {
|
|
162
|
+
const p = new Float32Array(ch * (res >> 1) * (res >> 1));
|
|
163
|
+
pool2(cur, res, ch, p);
|
|
164
|
+
cur = p;
|
|
165
|
+
res >>= 1;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const plane = res * res;
|
|
169
|
+
const gap = new Float32Array(ch);
|
|
170
|
+
for (let c = 0; c < ch; c++) {
|
|
171
|
+
let s = 0;
|
|
172
|
+
for (let j = 0; j < plane; j++) s += cur[c * plane + j];
|
|
173
|
+
gap[c] = s / plane;
|
|
174
|
+
}
|
|
175
|
+
const { lin } = model;
|
|
176
|
+
const e = new Float32Array(lin.out);
|
|
177
|
+
for (let o = 0; o < lin.out; o++) {
|
|
178
|
+
let s = lin.bias[o];
|
|
179
|
+
for (let j = 0; j < lin.in; j++) s += lin.w[o * lin.in + j] * gap[j];
|
|
180
|
+
e[o] = s;
|
|
181
|
+
}
|
|
182
|
+
return e;
|
|
183
|
+
}
|
|
184
|
+
function unit(e) {
|
|
185
|
+
let n = 0;
|
|
186
|
+
for (let i = 0; i < e.length; i++) n += e[i] * e[i];
|
|
187
|
+
n = 1 / (Math.sqrt(n) + 1e-9);
|
|
188
|
+
const out = new Float32Array(e.length);
|
|
189
|
+
for (let i = 0; i < e.length; i++) out[i] = e[i] * n;
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
function logits(head, e) {
|
|
193
|
+
const out = new Float32Array(head.out);
|
|
194
|
+
for (let o = 0; o < head.out; o++) {
|
|
195
|
+
let s = head.bias[o];
|
|
196
|
+
for (let j = 0; j < head.in; j++) {
|
|
197
|
+
const v = e[j];
|
|
198
|
+
if (v > 0) s += head.w[o * head.in + j] * v;
|
|
199
|
+
}
|
|
200
|
+
out[o] = s;
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
function softmax(z) {
|
|
205
|
+
let m = -Infinity;
|
|
206
|
+
for (const v of z) if (v > m) m = v;
|
|
207
|
+
const out = new Float32Array(z.length);
|
|
208
|
+
let s = 0;
|
|
209
|
+
for (let i = 0; i < z.length; i++) {
|
|
210
|
+
out[i] = Math.exp(z[i] - m);
|
|
211
|
+
s += out[i];
|
|
212
|
+
}
|
|
213
|
+
for (let i = 0; i < z.length; i++) out[i] /= s;
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
function drawStrokes(ctx, strokes, box = 120, lw = 7, pad = 10) {
|
|
217
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
218
|
+
for (const s of strokes) for (const p of s) {
|
|
219
|
+
if (p.x < minX) minX = p.x;
|
|
220
|
+
if (p.x > maxX) maxX = p.x;
|
|
221
|
+
if (p.y < minY) minY = p.y;
|
|
222
|
+
if (p.y > maxY) maxY = p.y;
|
|
223
|
+
}
|
|
224
|
+
const sc = box / Math.max(maxX - minX, maxY - minY, 1);
|
|
225
|
+
const W = box + 2 * pad;
|
|
226
|
+
ctx.fillStyle = "#fff";
|
|
227
|
+
ctx.fillRect(0, 0, W, W);
|
|
228
|
+
ctx.strokeStyle = "#000";
|
|
229
|
+
ctx.lineWidth = lw;
|
|
230
|
+
ctx.lineCap = "round";
|
|
231
|
+
ctx.lineJoin = "round";
|
|
232
|
+
for (const s of strokes) {
|
|
233
|
+
if (s.length < 2) continue;
|
|
234
|
+
ctx.beginPath();
|
|
235
|
+
ctx.moveTo((s[0].x - minX) * sc + pad, (s[0].y - minY) * sc + pad);
|
|
236
|
+
for (let i = 1; i < s.length; i++) ctx.lineTo((s[i].x - minX) * sc + pad, (s[i].y - minY) * sc + pad);
|
|
237
|
+
ctx.stroke();
|
|
238
|
+
}
|
|
239
|
+
return W;
|
|
240
|
+
}
|
|
241
|
+
function inkPlane(data, W, H) {
|
|
242
|
+
const ink = new Float32Array(W * H);
|
|
243
|
+
for (let i = 0, p = 0; i < ink.length; i++, p += 4) ink[i] = 1 - (data[p] * 0.299 + data[p + 1] * 0.587 + data[p + 2] * 0.114) / 255;
|
|
244
|
+
return ink;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ../../server/public/wordgpu/cnnprep.mjs
|
|
248
|
+
var SIZE = 32;
|
|
249
|
+
var INK = 24;
|
|
250
|
+
function normalizeInk(ink, w, h, size = SIZE, inkBox = INK) {
|
|
251
|
+
let minX = Infinity, maxX = -1, minY = Infinity, maxY = -1;
|
|
252
|
+
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++)
|
|
253
|
+
if (ink[y * w + x] > 0.15) {
|
|
254
|
+
if (x < minX) minX = x;
|
|
255
|
+
if (x > maxX) maxX = x;
|
|
256
|
+
if (y < minY) minY = y;
|
|
257
|
+
if (y > maxY) maxY = y;
|
|
258
|
+
}
|
|
259
|
+
const out = new Float32Array(size * size);
|
|
260
|
+
if (maxX < 0) return out;
|
|
261
|
+
const cw = maxX - minX + 1, ch = maxY - minY + 1;
|
|
262
|
+
const crop = new Float32Array(cw * ch);
|
|
263
|
+
for (let y = 0; y < ch; y++) for (let x = 0; x < cw; x++) crop[y * cw + x] = ink[(y + minY) * w + (x + minX)];
|
|
264
|
+
let peak = 0;
|
|
265
|
+
for (let i = 0; i < crop.length; i++) if (crop[i] > peak) peak = crop[i];
|
|
266
|
+
if (peak > 0.05 && peak < 0.999) for (let i = 0; i < crop.length; i++) crop[i] = Math.min(1, crop[i] / peak);
|
|
267
|
+
const s = inkBox / Math.max(ch, cw);
|
|
268
|
+
const nh = Math.max(1, Math.round(ch * s)), nw = Math.max(1, Math.round(cw * s));
|
|
269
|
+
const r = new Float32Array(nw * nh);
|
|
270
|
+
const fx = cw / nw, fy = ch / nh;
|
|
271
|
+
for (let y = 0; y < nh; y++) {
|
|
272
|
+
const y0 = y * fy, y1 = (y + 1) * fy, iy0 = Math.floor(y0), iy1 = Math.min(ch, Math.ceil(y1));
|
|
273
|
+
for (let x = 0; x < nw; x++) {
|
|
274
|
+
const x0 = x * fx, x1 = (x + 1) * fx, ix0 = Math.floor(x0), ix1 = Math.min(cw, Math.ceil(x1));
|
|
275
|
+
let ss = 0, wsum = 0;
|
|
276
|
+
for (let yy = iy0; yy < iy1; yy++) {
|
|
277
|
+
const wy = Math.min(y1, yy + 1) - Math.max(y0, yy);
|
|
278
|
+
if (wy <= 0) continue;
|
|
279
|
+
for (let xx = ix0; xx < ix1; xx++) {
|
|
280
|
+
const wx = Math.min(x1, xx + 1) - Math.max(x0, xx);
|
|
281
|
+
if (wx <= 0) continue;
|
|
282
|
+
ss += crop[yy * cw + xx] * wx * wy;
|
|
283
|
+
wsum += wx * wy;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
r[y * nw + x] = wsum > 0 ? ss / wsum : 0;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const oy = size - nh >> 1, ox = size - nw >> 1;
|
|
290
|
+
for (let y = 0; y < nh; y++) for (let x = 0; x < nw; x++) out[(y + oy) * size + x + ox] = r[y * nw + x];
|
|
291
|
+
return out;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// ../../library/src/assetbase.js
|
|
295
|
+
function assetBaseOf(base) {
|
|
296
|
+
let s = base ? String(base) : "";
|
|
297
|
+
if (!s) return typeof location !== "undefined" ? new URL("./", location.href).href : "";
|
|
298
|
+
if (!s.endsWith("/")) s += "/";
|
|
299
|
+
return typeof location !== "undefined" ? new URL(s, location.href).href : s;
|
|
300
|
+
}
|
|
301
|
+
var asStrokes = (s) => Array.isArray(s) && s.length && !Array.isArray(s[0]) ? [s] : s;
|
|
302
|
+
|
|
303
|
+
// ../../library/src/char-cnn-core.js
|
|
304
|
+
function scriptBase() {
|
|
305
|
+
const src = typeof document !== "undefined" && document.currentScript && document.currentScript.src || "";
|
|
306
|
+
return src ? src.slice(0, src.lastIndexOf("/") + 1) : "";
|
|
307
|
+
}
|
|
308
|
+
var ASSET_BASE = scriptBase();
|
|
309
|
+
var fetchBuf = (url) => fetch(url).then((r) => {
|
|
310
|
+
if (!r.ok) throw new Error(`${url}: ${r.status}`);
|
|
311
|
+
return r.arrayBuffer();
|
|
312
|
+
});
|
|
313
|
+
var MillirecCharCnnCore = class {
|
|
314
|
+
static async create(opts = {}) {
|
|
315
|
+
const g = typeof window !== "undefined" ? window : globalThis;
|
|
316
|
+
const config = { ...g.MILLIREC_CNN || {}, ...opts };
|
|
317
|
+
if (!config.stages || !config.stages.length) throw new Error("MillirecCharCnn: no stages configured");
|
|
318
|
+
const inst = new this(config);
|
|
319
|
+
await inst.ready;
|
|
320
|
+
return inst;
|
|
321
|
+
}
|
|
322
|
+
constructor(config) {
|
|
323
|
+
this.config = config;
|
|
324
|
+
this.base = assetBaseOf(config.assetBase || ASSET_BASE);
|
|
325
|
+
this.classes = typeof config.classes === "string" ? config.classes.split("") : config.classes;
|
|
326
|
+
this.stages = [];
|
|
327
|
+
this.off = typeof document !== "undefined" ? document.createElement("canvas") : null;
|
|
328
|
+
if (this.off) {
|
|
329
|
+
this.off.width = this.off.height = 140;
|
|
330
|
+
this.octx = this.off.getContext("2d", { willReadFrequently: true });
|
|
331
|
+
}
|
|
332
|
+
this.ready = this.load();
|
|
333
|
+
}
|
|
334
|
+
async load() {
|
|
335
|
+
const { base, config } = this;
|
|
336
|
+
let factory = null;
|
|
337
|
+
if (config.kernel) factory = (await import(
|
|
338
|
+
/* @vite-ignore */
|
|
339
|
+
base + config.kernel
|
|
340
|
+
)).default;
|
|
341
|
+
for (const st of config.stages) {
|
|
342
|
+
const [modelBuf, headBuf] = await Promise.all([fetchBuf(base + st.model), fetchBuf(base + st.head)]);
|
|
343
|
+
const head = parseHead(headBuf);
|
|
344
|
+
const stage = { theta: st.theta == null ? null : st.theta, head, headNorm: st.headNorm !== false, size: 0 };
|
|
345
|
+
if (factory) {
|
|
346
|
+
const M = await factory();
|
|
347
|
+
const bytes = expandToInt8(new Uint8Array(modelBuf));
|
|
348
|
+
const ptr = M._malloc(bytes.byteLength);
|
|
349
|
+
M.HEAPU8.set(bytes, ptr);
|
|
350
|
+
const loaded = M._cnnLoad(ptr, bytes.byteLength);
|
|
351
|
+
M._free(ptr);
|
|
352
|
+
if (!loaded) throw new Error(`kernel rejected ${st.model} (channel widths must be multiples of four)`);
|
|
353
|
+
const size = M._cnnSize(), dim = M._cnnDim();
|
|
354
|
+
stage.size = size;
|
|
355
|
+
stage.embed = (img) => {
|
|
356
|
+
const ip = M._cnnInputPtr(1) >> 2;
|
|
357
|
+
M.HEAPF32.set(img, ip);
|
|
358
|
+
M._cnnRun(1);
|
|
359
|
+
const o = M._cnnEmbedPtr(1) >> 2;
|
|
360
|
+
return M.HEAPF32.slice(o, o + dim);
|
|
361
|
+
};
|
|
362
|
+
stage.logits = (img) => logits(stage.head, stage.embed(img));
|
|
363
|
+
} else {
|
|
364
|
+
const model = parseModel(modelBuf);
|
|
365
|
+
stage.size = model.size;
|
|
366
|
+
stage.embed = (img) => {
|
|
367
|
+
const e = embed(model, img);
|
|
368
|
+
return stage.headNorm ? unit(e) : e;
|
|
369
|
+
};
|
|
370
|
+
stage.logits = (img) => logits(stage.head, stage.embed(img));
|
|
371
|
+
}
|
|
372
|
+
this.stages.push(stage);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/** drawn strokes ([[{x,y},...],...]) -> the model input raster at `size`.
|
|
376
|
+
* The canvas draw + readback is the dear part (~1 ms) and is shared by every
|
|
377
|
+
* stage of one recognize() call; only the resample differs per size. */
|
|
378
|
+
raster(strokes, size) {
|
|
379
|
+
if (this._planeFor !== strokes) {
|
|
380
|
+
const W2 = drawStrokes(this.octx, strokes);
|
|
381
|
+
this._plane = { ink: inkPlane(this.octx.getImageData(0, 0, W2, W2).data, W2, W2), W: W2 };
|
|
382
|
+
this._planeFor = strokes;
|
|
383
|
+
}
|
|
384
|
+
const { ink, W } = this._plane;
|
|
385
|
+
return normalizeInk(ink, W, W, size, Math.round(size * 0.75));
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* -> [{char, prob} x3] (best first) with `.stage` = index of the model that decided, or null.
|
|
389
|
+
* `only` (an array or Set of characters) restricts the answer to those: the softmax is taken over them alone, so a box
|
|
390
|
+
* that can hold one of two letters gets the likelier of the two however the rest of the alphabet scores — and the
|
|
391
|
+
* cascade runs to its last stage, since an early exit is judged on the whole alphabet's margin.
|
|
392
|
+
*/
|
|
393
|
+
async recognize(strokes, { only = null } = {}) {
|
|
394
|
+
strokes = asStrokes(strokes);
|
|
395
|
+
if (!strokes || !strokes.length) return null;
|
|
396
|
+
await this.ready;
|
|
397
|
+
if (!this.stages.length) return null;
|
|
398
|
+
this._planeFor = null;
|
|
399
|
+
const keep = only ? new Set(only) : null;
|
|
400
|
+
let z = null, used = 0;
|
|
401
|
+
for (let s = 0; s < this.stages.length; s++) {
|
|
402
|
+
const st = this.stages[s];
|
|
403
|
+
if ((this.personal || keep) && s < this.stages.length - 1) continue;
|
|
404
|
+
z = st.logits(this.raster(strokes, st.size));
|
|
405
|
+
used = s;
|
|
406
|
+
if (st.theta == null || s === this.stages.length - 1) break;
|
|
407
|
+
let b = -Infinity, b2 = -Infinity;
|
|
408
|
+
for (const v of z) {
|
|
409
|
+
if (v > b) {
|
|
410
|
+
b2 = b;
|
|
411
|
+
b = v;
|
|
412
|
+
} else if (v > b2) b2 = v;
|
|
413
|
+
}
|
|
414
|
+
if (b - b2 >= st.theta) break;
|
|
415
|
+
}
|
|
416
|
+
let idx = [...z.keys()];
|
|
417
|
+
if (keep) {
|
|
418
|
+
idx = idx.filter((c) => keep.has(this.classes[c]));
|
|
419
|
+
if (!idx.length) {
|
|
420
|
+
const out2 = [];
|
|
421
|
+
out2.stage = used;
|
|
422
|
+
return out2;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
const p = softmax(idx.map((c) => z[c]));
|
|
426
|
+
const order = [...p.keys()].sort((a, b) => p[b] - p[a]).slice(0, 3);
|
|
427
|
+
const out = order.map((i) => ({ char: this.classes[idx[i]], prob: p[i] }));
|
|
428
|
+
out.stage = used;
|
|
429
|
+
return out;
|
|
430
|
+
}
|
|
431
|
+
/** The last stage's (unit) embedding of drawn strokes — what the head reads. */
|
|
432
|
+
async embedding(strokes) {
|
|
433
|
+
strokes = asStrokes(strokes);
|
|
434
|
+
await this.ready;
|
|
435
|
+
this._planeFor = null;
|
|
436
|
+
const st = this.stages[this.stages.length - 1];
|
|
437
|
+
return st.embed(this.raster(strokes, st.size));
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* PERSONALIZE: refit the last stage's head (a softmax over the frozen embedding) on the player's own samples
|
|
441
|
+
* (`{ 'A': [strokes…], … }`), anchored to the shipped head — the anchor's pull is prior/n, so a few samples barely move
|
|
442
|
+
* a letter and many take it over (Rob, Sep 24 2026: "gradually add and then replace with the player's own"). Plain
|
|
443
|
+
* gradient descent on the mean cross-entropy: a few hundred steps over a few hundred samples is ~100 ms. While a
|
|
444
|
+
* personal head is in place the cascade always runs to the last stage (the tiny head is the shipped one).
|
|
445
|
+
* Returns { n, classes } or null (nothing usable). `depersonalize()` restores the shipped head.
|
|
446
|
+
*/
|
|
447
|
+
async personalize(samples, { steps = 250, lr = 0.5, prior = 1 } = {}) {
|
|
448
|
+
await this.ready;
|
|
449
|
+
const st = this.stages[this.stages.length - 1];
|
|
450
|
+
const H0 = st.head0 || (st.head0 = st.head);
|
|
451
|
+
const X = [], Y = [];
|
|
452
|
+
for (const [cls, list] of Object.entries(samples || {})) {
|
|
453
|
+
const c = this.classes.indexOf(cls);
|
|
454
|
+
if (c < 0) continue;
|
|
455
|
+
for (const drawing of list || []) {
|
|
456
|
+
const strokes = asStrokes(drawing);
|
|
457
|
+
if (!strokes || !strokes.length) continue;
|
|
458
|
+
X.push(await this.embedding(strokes));
|
|
459
|
+
Y.push(c);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (!X.length) {
|
|
463
|
+
st.head = H0;
|
|
464
|
+
this.personal = null;
|
|
465
|
+
return null;
|
|
466
|
+
}
|
|
467
|
+
const head = { in: H0.in, out: H0.out, w: Float32Array.from(H0.w), bias: Float32Array.from(H0.bias) };
|
|
468
|
+
const n = X.length, gW = new Float32Array(head.w.length), gB = new Float32Array(head.out);
|
|
469
|
+
for (let step = 0; step < steps; step++) {
|
|
470
|
+
gW.fill(0);
|
|
471
|
+
gB.fill(0);
|
|
472
|
+
for (let i = 0; i < n; i++) {
|
|
473
|
+
const p = softmax(logits(head, X[i])), x = X[i];
|
|
474
|
+
for (let o = 0; o < head.out; o++) {
|
|
475
|
+
const g = (p[o] - (o === Y[i] ? 1 : 0)) / n;
|
|
476
|
+
if (!g) continue;
|
|
477
|
+
gB[o] += g;
|
|
478
|
+
const wb = o * head.in;
|
|
479
|
+
for (let j = 0; j < head.in; j++) {
|
|
480
|
+
const v = x[j];
|
|
481
|
+
if (v > 0) gW[wb + j] += g * v;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
const a = prior / n;
|
|
486
|
+
for (let k = 0; k < head.w.length; k++) head.w[k] -= lr * (gW[k] + a * (head.w[k] - H0.w[k]));
|
|
487
|
+
for (let o = 0; o < head.out; o++) head.bias[o] -= lr * (gB[o] + a * (head.bias[o] - H0.bias[o]));
|
|
488
|
+
}
|
|
489
|
+
st.head = head;
|
|
490
|
+
this.personal = { n, classes: [...new Set(Y)].map((c) => this.classes[c]).sort() };
|
|
491
|
+
return this.personal;
|
|
492
|
+
}
|
|
493
|
+
/** Let go of the models and kernels; recognize() returns null afterwards. */
|
|
494
|
+
destroy() {
|
|
495
|
+
this.stages = [];
|
|
496
|
+
this.personal = null;
|
|
497
|
+
this._plane = null;
|
|
498
|
+
this._planeFor = null;
|
|
499
|
+
}
|
|
500
|
+
depersonalize() {
|
|
501
|
+
const st = this.stages[this.stages.length - 1];
|
|
502
|
+
if (st.head0) st.head = st.head0;
|
|
503
|
+
this.personal = null;
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
if (typeof window !== "undefined" && false) window.MillirecCharCnn = MillirecCharCnnCore;
|
|
507
|
+
|
|
508
|
+
// src/options.js
|
|
509
|
+
var HERE = new URL("./", import.meta.url).href;
|
|
510
|
+
var OPTIONS = ["accurate", "fast", "small"];
|
|
511
|
+
async function loadOption(name, { assetBase = HERE } = {}) {
|
|
512
|
+
if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
|
|
513
|
+
const url = new URL(`options/${name}.json`, assetBaseOf(assetBase));
|
|
514
|
+
const r = await fetch(url);
|
|
515
|
+
if (!r.ok) throw new Error(`millirec: ${url}: ${r.status}`);
|
|
516
|
+
return r.json();
|
|
517
|
+
}
|
|
518
|
+
async function withOption(opts) {
|
|
519
|
+
const assetBase = assetBaseOf(opts.assetBase || HERE);
|
|
520
|
+
const preset = opts.stages ? {} : await loadOption(opts.option || "accurate", { assetBase });
|
|
521
|
+
return { ...preset, ...opts, assetBase };
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// src/char-cnn-core.js
|
|
525
|
+
var MillirecCharCnnCore2 = class extends MillirecCharCnnCore {
|
|
526
|
+
/** opts: option ('accurate' | 'fast' | 'small', default 'accurate') or your own
|
|
527
|
+
* { classes, stages, kernel }, plus assetBase (default: this module's folder). */
|
|
528
|
+
static async create(opts = {}) {
|
|
529
|
+
return super.create(await withOption(opts));
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
export {
|
|
533
|
+
MillirecCharCnnCore2 as MillirecCharCnnCore,
|
|
534
|
+
OPTIONS,
|
|
535
|
+
loadOption
|
|
536
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AttachOptions, Pad } from '../char-lite/index.js'
|
|
2
|
+
import { MillirecCharCnnCore, type CreateOptions } from './core.js'
|
|
3
|
+
|
|
4
|
+
export { OPTIONS, loadOption, type OptionName, type CnnConfig, type Stage, type CreateOptions } from './core.js'
|
|
5
|
+
|
|
6
|
+
export declare class MillirecCharCnn extends MillirecCharCnnCore {
|
|
7
|
+
static create(opts?: CreateOptions): Promise<MillirecCharCnn>
|
|
8
|
+
attach(el: HTMLElement, opts?: AttachOptions): Pad
|
|
9
|
+
}
|