millirec 0.1.1 → 0.2.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/CHANGELOG.md +101 -0
- package/README.md +286 -51
- package/dist/char-cnn/cnn-web.wasm +0 -0
- package/dist/char-cnn/core.d.ts +15 -1
- package/dist/char-cnn/core.js +214 -100
- package/dist/char-cnn/index.d.ts +1 -1
- package/dist/char-cnn/index.js +267 -137
- 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/tiny.head.bin +0 -0
- package/dist/char-cnn/options/accurate.js +6 -6
- package/dist/char-cnn/options/accurate.json +5 -5
- package/dist/char-cnn/options/all/all-base.head.bin +0 -0
- package/dist/char-cnn/options/all/all-base.q4.bin +0 -0
- package/dist/char-cnn/options/all/all-tiny.bin +0 -0
- package/dist/char-cnn/options/all/all-tiny.head.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-base-p25.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-base-p25.head.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-tiny.bin +0 -0
- package/dist/char-cnn/options/all-fast/all-tiny.head.bin +0 -0
- package/dist/char-cnn/options/all-fast.js +17 -0
- package/dist/char-cnn/options/all-fast.json +15 -0
- package/dist/char-cnn/options/all-small/all-tiny-qat4.head.bin +0 -0
- package/dist/char-cnn/options/all-small/all-tiny-qat4.q4.bin +0 -0
- package/dist/char-cnn/options/all-small.js +11 -0
- package/dist/char-cnn/options/all-small.json +9 -0
- package/dist/char-cnn/options/all.js +17 -0
- package/dist/char-cnn/options/all.json +15 -0
- 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-fast/digits-base-p25.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-base-p25.head.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-tiny.bin +0 -0
- package/dist/char-cnn/options/digits-fast/digits-tiny.head.bin +0 -0
- package/dist/char-cnn/options/digits-fast.js +17 -0
- package/dist/char-cnn/options/digits-fast.json +15 -0
- package/dist/char-cnn/options/digits-small/digits-tiny-qat4.head.bin +0 -0
- package/dist/char-cnn/options/digits-small/digits-tiny-qat4.q4.bin +0 -0
- package/dist/char-cnn/options/digits-small.js +11 -0
- package/dist/char-cnn/options/digits-small.json +9 -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/base-p25.head.bin +0 -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 +6 -6
- package/dist/char-cnn/options/fast.json +5 -5
- 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-fast/lowercase-base-p25.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-fast.js +17 -0
- package/dist/char-cnn/options/lowercase-fast.json +15 -0
- package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.head.bin +0 -0
- package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.q4.bin +0 -0
- package/dist/char-cnn/options/lowercase-small.js +11 -0
- package/dist/char-cnn/options/lowercase-small.json +9 -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.head.bin +0 -0
- package/dist/char-cnn/options/small/tiny.q4.bin +0 -0
- package/dist/char-cnn/options/small.js +3 -3
- package/dist/char-cnn/options/small.json +2 -2
- package/dist/char-lite/core.d.ts +1 -0
- package/dist/char-lite/core.js +628 -0
- package/dist/char-lite/index.d.ts +28 -8
- package/dist/char-lite/index.js +459 -297
- 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/index.d.ts +35 -0
- package/dist/index.js +88 -0
- package/dist/overlap/millirec-overlap.min.js +7 -7
- package/dist/overlap/millirec.wasm +0 -0
- package/dist/pad/index.d.ts +8 -0
- package/dist/pad/index.js +482 -0
- package/package.json +30 -8
- package/dist/char-cnn/base-p25.head.bin +0 -0
- package/dist/char-cnn/tiny.head.bin +0 -0
- /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/{base-p25.bin → options/fast/base-p25.bin} +0 -0
package/dist/char-cnn/index.js
CHANGED
|
@@ -114,69 +114,102 @@ function parseHead(buf) {
|
|
|
114
114
|
return readLinear(reader(buf));
|
|
115
115
|
}
|
|
116
116
|
function conv3(c, inp, res, out) {
|
|
117
|
-
const { cout, cin,
|
|
118
|
-
const plane = res * res,
|
|
117
|
+
const { cout, cin, w, bias } = c;
|
|
118
|
+
const plane = res * res, rm1 = res - 1;
|
|
119
119
|
for (let co = 0; co < cout; co++) {
|
|
120
|
-
const ob = co * plane, b = bias[co], wb = co *
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
120
|
+
const ob = co * plane, b = bias[co], wb = co * cin * 9;
|
|
121
|
+
out.fill(b, ob, ob + plane);
|
|
122
|
+
for (let ci = 0; ci < cin; ci++) {
|
|
123
|
+
const ib = ci * plane, wc = wb + ci * 9;
|
|
124
|
+
const w0 = w[wc], w1 = w[wc + 1], w2 = w[wc + 2];
|
|
125
|
+
const w3 = w[wc + 3], w4 = w[wc + 4], w5 = w[wc + 5];
|
|
126
|
+
const w6 = w[wc + 6], w7 = w[wc + 7], w8 = w[wc + 8];
|
|
127
|
+
for (let y = 1; y < rm1; y++) {
|
|
128
|
+
const r0 = ib + (y - 1) * res, r1 = ib + y * res, r2 = ib + (y + 1) * res, orow = ob + y * res;
|
|
129
|
+
out[orow] += w1 * inp[r0] + w2 * inp[r0 + 1] + w4 * inp[r1] + w5 * inp[r1 + 1] + w7 * inp[r2] + w8 * inp[r2 + 1];
|
|
130
|
+
for (let x = 1; x < rm1; x++) {
|
|
131
|
+
out[orow + x] += w0 * inp[r0 + x - 1] + w1 * inp[r0 + x] + w2 * inp[r0 + x + 1] + w3 * inp[r1 + x - 1] + w4 * inp[r1 + x] + w5 * inp[r1 + x + 1] + w6 * inp[r2 + x - 1] + w7 * inp[r2 + x] + w8 * inp[r2 + x + 1];
|
|
132
|
+
}
|
|
133
|
+
out[orow + rm1] += w0 * inp[r0 + rm1 - 1] + w1 * inp[r0 + rm1] + w3 * inp[r1 + rm1 - 1] + w4 * inp[r1 + rm1] + w6 * inp[r2 + rm1 - 1] + w7 * inp[r2 + rm1];
|
|
134
|
+
}
|
|
135
|
+
{
|
|
136
|
+
const r1 = ib, r2 = ib + res, orow = ob;
|
|
137
|
+
out[orow] += w4 * inp[r1] + w5 * inp[r1 + 1] + w7 * inp[r2] + w8 * inp[r2 + 1];
|
|
138
|
+
for (let x = 1; x < rm1; x++) {
|
|
139
|
+
out[orow + x] += w3 * inp[r1 + x - 1] + w4 * inp[r1 + x] + w5 * inp[r1 + x + 1] + w6 * inp[r2 + x - 1] + w7 * inp[r2 + x] + w8 * inp[r2 + x + 1];
|
|
140
|
+
}
|
|
141
|
+
out[orow + rm1] += w3 * inp[r1 + rm1 - 1] + w4 * inp[r1 + rm1] + w6 * inp[r2 + rm1 - 1] + w7 * inp[r2 + rm1];
|
|
142
|
+
}
|
|
143
|
+
{
|
|
144
|
+
const r0 = ib + (rm1 - 1) * res, r1 = ib + rm1 * res, orow = ob + rm1 * res;
|
|
145
|
+
out[orow] += w1 * inp[r0] + w2 * inp[r0 + 1] + w4 * inp[r1] + w5 * inp[r1 + 1];
|
|
146
|
+
for (let x = 1; x < rm1; x++) {
|
|
147
|
+
out[orow + x] += w0 * inp[r0 + x - 1] + w1 * inp[r0 + x] + w2 * inp[r0 + x + 1] + w3 * inp[r1 + x - 1] + w4 * inp[r1 + x] + w5 * inp[r1 + x + 1];
|
|
136
148
|
}
|
|
137
|
-
out[
|
|
149
|
+
out[orow + rm1] += w0 * inp[r0 + rm1 - 1] + w1 * inp[r0 + rm1] + w3 * inp[r1 + rm1 - 1] + w4 * inp[r1 + rm1];
|
|
138
150
|
}
|
|
139
151
|
}
|
|
152
|
+
const end = ob + plane;
|
|
153
|
+
for (let i = ob; i < end; i++) if (out[i] < 0) out[i] = 0;
|
|
140
154
|
}
|
|
141
155
|
}
|
|
142
156
|
function pool2(inp, res, ch, out) {
|
|
143
157
|
const r2 = res >> 1;
|
|
144
158
|
for (let c = 0; c < ch; c++) {
|
|
145
159
|
const ib = c * res * res, ob = c * r2 * r2;
|
|
146
|
-
for (let y = 0; y < r2; y++)
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
160
|
+
for (let y = 0; y < r2; y++) {
|
|
161
|
+
const r0 = ib + (y << 1) * res, r1 = r0 + res, orow = ob + y * r2;
|
|
162
|
+
for (let x = 0; x < r2; x++) {
|
|
163
|
+
const i0 = r0 + (x << 1), i1 = r1 + (x << 1);
|
|
164
|
+
let m = inp[i0], b = inp[i0 + 1], d = inp[i1], e = inp[i1 + 1];
|
|
165
|
+
if (b > m) m = b;
|
|
166
|
+
if (d > m) m = d;
|
|
167
|
+
if (e > m) m = e;
|
|
168
|
+
out[orow + x] = m;
|
|
169
|
+
}
|
|
150
170
|
}
|
|
151
171
|
}
|
|
152
172
|
}
|
|
153
173
|
function embed(model, img) {
|
|
154
174
|
const S = model.size;
|
|
155
|
-
|
|
175
|
+
if (!model._bufA) {
|
|
176
|
+
let maxLen = S * S, r = S;
|
|
177
|
+
for (const c of model.convs) {
|
|
178
|
+
if (c.cout * r * r > maxLen) maxLen = c.cout * r * r;
|
|
179
|
+
if (c.pool) r >>= 1;
|
|
180
|
+
}
|
|
181
|
+
model._bufA = new Float32Array(maxLen);
|
|
182
|
+
model._bufB = new Float32Array(maxLen);
|
|
183
|
+
model._gap = new Float32Array(model.lin.in);
|
|
184
|
+
}
|
|
185
|
+
let cur = model._bufA, nxt = model._bufB, res = S, ch = 1;
|
|
186
|
+
cur.set(img);
|
|
156
187
|
for (const c of model.convs) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
cur =
|
|
188
|
+
conv3(c, cur, res, nxt);
|
|
189
|
+
let t = cur;
|
|
190
|
+
cur = nxt;
|
|
191
|
+
nxt = t;
|
|
160
192
|
ch = c.cout;
|
|
161
193
|
if (c.pool) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
cur =
|
|
194
|
+
pool2(cur, res, ch, nxt);
|
|
195
|
+
t = cur;
|
|
196
|
+
cur = nxt;
|
|
197
|
+
nxt = t;
|
|
165
198
|
res >>= 1;
|
|
166
199
|
}
|
|
167
200
|
}
|
|
168
|
-
const plane = res * res;
|
|
169
|
-
const gap =
|
|
201
|
+
const plane = res * res, invPlane = 1 / plane;
|
|
202
|
+
const gap = model._gap;
|
|
170
203
|
for (let c = 0; c < ch; c++) {
|
|
171
|
-
let s = 0;
|
|
172
|
-
for (let j = 0; j < plane; j++) s += cur[
|
|
173
|
-
gap[c] = s
|
|
204
|
+
let s = 0, off = c * plane;
|
|
205
|
+
for (let j = 0; j < plane; j++) s += cur[off + j];
|
|
206
|
+
gap[c] = s * invPlane;
|
|
174
207
|
}
|
|
175
|
-
const { lin } = model;
|
|
208
|
+
const { lin } = model, lw = lin.w, lb = lin.bias, linIn = lin.in;
|
|
176
209
|
const e = new Float32Array(lin.out);
|
|
177
210
|
for (let o = 0; o < lin.out; o++) {
|
|
178
|
-
let s =
|
|
179
|
-
for (let j = 0; j <
|
|
211
|
+
let s = lb[o], off = o * linIn;
|
|
212
|
+
for (let j = 0; j < linIn; j++) s += lw[off + j] * gap[j];
|
|
180
213
|
e[o] = s;
|
|
181
214
|
}
|
|
182
215
|
return e;
|
|
@@ -213,7 +246,8 @@ function softmax(z) {
|
|
|
213
246
|
for (let i = 0; i < z.length; i++) out[i] /= s;
|
|
214
247
|
return out;
|
|
215
248
|
}
|
|
216
|
-
|
|
249
|
+
var _planeBuf = null;
|
|
250
|
+
function strokePlane(strokes, box = 120, lw = 7, pad = 10) {
|
|
217
251
|
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
218
252
|
for (const s of strokes) for (const p of s) {
|
|
219
253
|
if (p.x < minX) minX = p.x;
|
|
@@ -221,73 +255,86 @@ function drawStrokes(ctx, strokes, box = 120, lw = 7, pad = 10) {
|
|
|
221
255
|
if (p.y < minY) minY = p.y;
|
|
222
256
|
if (p.y > maxY) maxY = p.y;
|
|
223
257
|
}
|
|
224
|
-
const sc = box / Math.max(maxX - minX, maxY - minY, 1);
|
|
225
258
|
const W = box + 2 * pad;
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
259
|
+
const ink = _planeBuf && _planeBuf.length === W * W ? _planeBuf : _planeBuf = new Float32Array(W * W);
|
|
260
|
+
ink.fill(0);
|
|
261
|
+
if (!(minX <= maxX)) return { ink, W, iMinX: 0, iMaxX: -1, iMinY: 0, iMaxY: -1 };
|
|
262
|
+
const sc = box / Math.max(maxX - minX, maxY - minY, 1);
|
|
263
|
+
const r = lw * 0.5, rOut = r + 0.5, rIn = r - 0.5, rOut2 = rOut * rOut, rIn2 = rIn * rIn;
|
|
264
|
+
let iMinX = W, iMaxX = -1, iMinY = W, iMaxY = -1;
|
|
265
|
+
const drawSeg = (ax, ay, bx, by) => {
|
|
266
|
+
const x0 = Math.max(0, Math.floor(Math.min(ax, bx) - rOut)), x1 = Math.min(W - 1, Math.ceil(Math.max(ax, bx) + rOut));
|
|
267
|
+
const y0 = Math.max(0, Math.floor(Math.min(ay, by) - rOut)), y1 = Math.min(W - 1, Math.ceil(Math.max(ay, by) + rOut));
|
|
268
|
+
const vx = bx - ax, vy = by - ay, l2 = vx * vx + vy * vy, invL2 = l2 > 1e-6 ? 1 / l2 : 0;
|
|
269
|
+
for (let y = y0; y <= y1; y++) {
|
|
270
|
+
const py = y + 0.5 - ay, row = y * W;
|
|
271
|
+
for (let x = x0; x <= x1; x++) {
|
|
272
|
+
const cur = ink[row + x];
|
|
273
|
+
if (cur >= 1) continue;
|
|
274
|
+
const px = x + 0.5 - ax;
|
|
275
|
+
let t = (px * vx + py * vy) * invL2;
|
|
276
|
+
if (t < 0) t = 0;
|
|
277
|
+
else if (t > 1) t = 1;
|
|
278
|
+
const dx = px - t * vx, dy = py - t * vy, d2 = dx * dx + dy * dy;
|
|
279
|
+
if (d2 >= rOut2) continue;
|
|
280
|
+
const v = d2 <= rIn2 ? 1 : rOut - Math.sqrt(d2);
|
|
281
|
+
if (v > cur) {
|
|
282
|
+
ink[row + x] = v;
|
|
283
|
+
if (v > 0.15) {
|
|
284
|
+
if (x < iMinX) iMinX = x;
|
|
285
|
+
if (x > iMaxX) iMaxX = x;
|
|
286
|
+
if (y < iMinY) iMinY = y;
|
|
287
|
+
if (y > iMaxY) iMaxY = y;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
};
|
|
232
293
|
for (const s of strokes) {
|
|
233
|
-
if (s.length
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
294
|
+
if (!s.length) continue;
|
|
295
|
+
let ax = (s[0].x - minX) * sc + pad, ay = (s[0].y - minY) * sc + pad;
|
|
296
|
+
if (s.length === 1) {
|
|
297
|
+
drawSeg(ax, ay, ax, ay);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
for (let i = 1; i < s.length; i++) {
|
|
301
|
+
const bx = (s[i].x - minX) * sc + pad, by = (s[i].y - minY) * sc + pad;
|
|
302
|
+
drawSeg(ax, ay, bx, by);
|
|
303
|
+
ax = bx;
|
|
304
|
+
ay = by;
|
|
305
|
+
}
|
|
238
306
|
}
|
|
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;
|
|
307
|
+
return { ink, W, iMinX, iMaxX, iMinY, iMaxY };
|
|
245
308
|
}
|
|
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
|
-
}
|
|
309
|
+
function normalizePlane({ ink, W, iMinX, iMaxX, iMinY, iMaxY }, size = 32, inkBox = Math.round(size * 0.75)) {
|
|
259
310
|
const out = new Float32Array(size * size);
|
|
260
|
-
if (
|
|
261
|
-
const cw =
|
|
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);
|
|
311
|
+
if (iMaxX < 0) return out;
|
|
312
|
+
const cw = iMaxX - iMinX + 1, ch = iMaxY - iMinY + 1;
|
|
267
313
|
const s = inkBox / Math.max(ch, cw);
|
|
268
314
|
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
315
|
const fx = cw / nw, fy = ch / nh;
|
|
316
|
+
const oy = size - nh >> 1, ox = size - nw >> 1;
|
|
271
317
|
for (let y = 0; y < nh; y++) {
|
|
272
318
|
const y0 = y * fy, y1 = (y + 1) * fy, iy0 = Math.floor(y0), iy1 = Math.min(ch, Math.ceil(y1));
|
|
319
|
+
const outRow = (y + oy) * size + ox;
|
|
273
320
|
for (let x = 0; x < nw; x++) {
|
|
274
321
|
const x0 = x * fx, x1 = (x + 1) * fx, ix0 = Math.floor(x0), ix1 = Math.min(cw, Math.ceil(x1));
|
|
275
322
|
let ss = 0, wsum = 0;
|
|
276
323
|
for (let yy = iy0; yy < iy1; yy++) {
|
|
277
324
|
const wy = Math.min(y1, yy + 1) - Math.max(y0, yy);
|
|
278
325
|
if (wy <= 0) continue;
|
|
326
|
+
const srcRow = (yy + iMinY) * W + iMinX;
|
|
279
327
|
for (let xx = ix0; xx < ix1; xx++) {
|
|
280
328
|
const wx = Math.min(x1, xx + 1) - Math.max(x0, xx);
|
|
281
329
|
if (wx <= 0) continue;
|
|
282
|
-
|
|
283
|
-
|
|
330
|
+
const wxy = wx * wy;
|
|
331
|
+
ss += ink[srcRow + xx] * wxy;
|
|
332
|
+
wsum += wxy;
|
|
284
333
|
}
|
|
285
334
|
}
|
|
286
|
-
|
|
335
|
+
out[outRow + x] = wsum > 0 ? ss / wsum : 0;
|
|
287
336
|
}
|
|
288
337
|
}
|
|
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
338
|
return out;
|
|
292
339
|
}
|
|
293
340
|
|
|
@@ -328,6 +375,7 @@ async function fetchAsset(url2, { as = "json", what, base, recognizer }) {
|
|
|
328
375
|
}
|
|
329
376
|
|
|
330
377
|
// ../../library/src/char-cnn-core.js
|
|
378
|
+
var finitePoints = (strokes) => strokes.every((st) => Array.isArray(st) && st.every((p) => p && Number.isFinite(p.x) && Number.isFinite(p.y)));
|
|
331
379
|
function scriptBase() {
|
|
332
380
|
const src = typeof document !== "undefined" && document.currentScript && document.currentScript.src || "";
|
|
333
381
|
return src ? src.slice(0, src.lastIndexOf("/") + 1) : "";
|
|
@@ -335,7 +383,15 @@ function scriptBase() {
|
|
|
335
383
|
var ASSET_BASE = scriptBase();
|
|
336
384
|
var fetchBuf = (url2, base) => fetchAsset(url2, { as: "buf", what: "a char-cnn model file", base, recognizer: "char-cnn" });
|
|
337
385
|
var DESTROYED = "millirec: recognizer destroyed";
|
|
338
|
-
var
|
|
386
|
+
var TEACH_SHARPNESS = 18;
|
|
387
|
+
var unitOf = (e) => {
|
|
388
|
+
let n = 0;
|
|
389
|
+
for (let i = 0; i < e.length; i++) n += e[i] * e[i];
|
|
390
|
+
n = Math.sqrt(n) || 1;
|
|
391
|
+
const o = new Float32Array(e.length);
|
|
392
|
+
for (let i = 0; i < e.length; i++) o[i] = e[i] / n;
|
|
393
|
+
return o;
|
|
394
|
+
};
|
|
339
395
|
var MillirecCharCnnCore = class {
|
|
340
396
|
static async create(opts = {}) {
|
|
341
397
|
const g = typeof window !== "undefined" ? window : globalThis;
|
|
@@ -346,6 +402,7 @@ var MillirecCharCnnCore = class {
|
|
|
346
402
|
}
|
|
347
403
|
const inst = new this(config);
|
|
348
404
|
await inst.ready;
|
|
405
|
+
if (config.examples) await inst.teach(config.examples);
|
|
349
406
|
return inst;
|
|
350
407
|
}
|
|
351
408
|
constructor(config) {
|
|
@@ -353,8 +410,6 @@ var MillirecCharCnnCore = class {
|
|
|
353
410
|
this.base = assetBaseOf(config.assetBase || ASSET_BASE);
|
|
354
411
|
this.classes = typeof config.classes === "string" ? config.classes.split("") : config.classes;
|
|
355
412
|
this.stages = [];
|
|
356
|
-
this.off = makeCanvas(140);
|
|
357
|
-
this.octx = this.off ? this.off.getContext("2d", { willReadFrequently: true }) : null;
|
|
358
413
|
this.ready = this.load();
|
|
359
414
|
}
|
|
360
415
|
async load() {
|
|
@@ -410,18 +465,19 @@ var MillirecCharCnnCore = class {
|
|
|
410
465
|
this.stages.push(stage);
|
|
411
466
|
}
|
|
412
467
|
}
|
|
468
|
+
/** Which engine this is (createRecognizer() picks one). */
|
|
469
|
+
get engine() {
|
|
470
|
+
return "char-cnn";
|
|
471
|
+
}
|
|
413
472
|
/** drawn strokes ([[{x,y},...],...]) -> the model input raster at `size`.
|
|
414
|
-
* The
|
|
415
|
-
*
|
|
473
|
+
* The 140x140 stroke plane is computed once per recognize() call and shared
|
|
474
|
+
* by every stage; only the resample differs per size. */
|
|
416
475
|
raster(strokes, size) {
|
|
417
|
-
if (!this.octx) throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");
|
|
418
476
|
if (this._planeFor !== strokes) {
|
|
419
|
-
|
|
420
|
-
this._plane = { ink: inkPlane(this.octx.getImageData(0, 0, W2, W2).data, W2, W2), W: W2 };
|
|
477
|
+
this._plane = strokePlane(strokes);
|
|
421
478
|
this._planeFor = strokes;
|
|
422
479
|
}
|
|
423
|
-
|
|
424
|
-
return normalizeInk(ink, W, W, size, Math.round(size * 0.75));
|
|
480
|
+
return normalizePlane(this._plane, size, Math.round(size * 0.75));
|
|
425
481
|
}
|
|
426
482
|
/**
|
|
427
483
|
* -> [{char, prob} x3] (best first) with `.stage` = index of the model that decided, or null.
|
|
@@ -431,11 +487,12 @@ var MillirecCharCnnCore = class {
|
|
|
431
487
|
*/
|
|
432
488
|
async recognize(strokes, { only = null } = {}) {
|
|
433
489
|
strokes = asStrokes(strokes);
|
|
434
|
-
if (!strokes || !strokes.length) return null;
|
|
490
|
+
if (!strokes || !strokes.length || !finitePoints(strokes)) return null;
|
|
435
491
|
await this.ready;
|
|
436
492
|
if (this.destroyed || !this.stages.length) return null;
|
|
437
493
|
this._planeFor = null;
|
|
438
494
|
const keep = only ? new Set(only) : null;
|
|
495
|
+
if (this.taught) return this.nearest(strokes, keep);
|
|
439
496
|
let z = null, used = 0;
|
|
440
497
|
for (let s = 0; s < this.stages.length; s++) {
|
|
441
498
|
const st = this.stages[s];
|
|
@@ -467,9 +524,67 @@ var MillirecCharCnnCore = class {
|
|
|
467
524
|
out.stage = used;
|
|
468
525
|
return out;
|
|
469
526
|
}
|
|
527
|
+
/** recognize() once teach() has run: the drawing against every taught example, in the last stage's feature space. */
|
|
528
|
+
nearest(strokes, keep) {
|
|
529
|
+
const last = this.stages.length - 1, st = this.stages[last];
|
|
530
|
+
const q = unitOf(st.embed(this.raster(strokes, st.size)));
|
|
531
|
+
const names = [...this.taught.keys()].filter((c) => !keep || keep.has(c));
|
|
532
|
+
const out = [];
|
|
533
|
+
if (names.length) {
|
|
534
|
+
const sim = names.map((c) => {
|
|
535
|
+
let b = -2;
|
|
536
|
+
for (const e of this.taught.get(c)) {
|
|
537
|
+
let d = 0;
|
|
538
|
+
for (let i = 0; i < q.length; i++) d += q[i] * e[i];
|
|
539
|
+
if (d > b) b = d;
|
|
540
|
+
}
|
|
541
|
+
return b;
|
|
542
|
+
});
|
|
543
|
+
const p = softmax(sim.map((v) => v * TEACH_SHARPNESS));
|
|
544
|
+
for (const i of [...p.keys()].sort((a, b) => p[b] - p[a]).slice(0, 3)) out.push({ char: names[i], prob: p[i] });
|
|
545
|
+
}
|
|
546
|
+
out.stage = last;
|
|
547
|
+
return out;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* TEACH: read symbols of your own from a few drawings of each, with no training. `examples` is
|
|
551
|
+
* `{ name: [drawing, …] }` — names are any strings (a letter, 'star', an emoji), a drawing is strokes as for
|
|
552
|
+
* recognize(). Each drawing is kept as what the last stage's network makes of it; from then on recognize() answers
|
|
553
|
+
* AMONG THE TAUGHT NAMES ONLY, by the nearest example. Three drawings of each is a good start, five is better;
|
|
554
|
+
* draw the variants people really use. `{ add: true }` keeps what was taught before (new drawings of a known name
|
|
555
|
+
* are added to it). Returns { classes, n } or null (nothing usable). forget() goes back to the shipped classes.
|
|
556
|
+
* Works best on an option whose last stage is the larger network (`accurate`, the default, or `fast`).
|
|
557
|
+
*/
|
|
558
|
+
async teach(examples, { add = false } = {}) {
|
|
559
|
+
await this.ready;
|
|
560
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
561
|
+
const taught = add && this.taught ? new Map([...this.taught].map(([c, list]) => [c, list.slice()])) : /* @__PURE__ */ new Map();
|
|
562
|
+
for (const [name, list] of Object.entries(examples || {})) {
|
|
563
|
+
for (const drawing of list || []) {
|
|
564
|
+
const strokes = asStrokes(drawing);
|
|
565
|
+
if (!strokes || !strokes.length) continue;
|
|
566
|
+
const e = unitOf(await this.embedding(strokes));
|
|
567
|
+
if (this.destroyed) return null;
|
|
568
|
+
if (!taught.has(name)) taught.set(name, []);
|
|
569
|
+
taught.get(name).push(e);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (!taught.size) {
|
|
573
|
+
if (!add) this.taught = null;
|
|
574
|
+
return null;
|
|
575
|
+
}
|
|
576
|
+
this.taught = taught;
|
|
577
|
+
return { classes: [...taught.keys()], n: [...taught.values()].reduce((a, l) => a + l.length, 0) };
|
|
578
|
+
}
|
|
579
|
+
/** Drop everything teach() learned: recognize() reads the shipped classes again. */
|
|
580
|
+
forget() {
|
|
581
|
+
this.taught = null;
|
|
582
|
+
}
|
|
470
583
|
/** The last stage's (unit) embedding of drawn strokes — what the head reads. */
|
|
471
584
|
async embedding(strokes) {
|
|
472
585
|
strokes = asStrokes(strokes);
|
|
586
|
+
if (!strokes || !strokes.length) throw new TypeError("MillirecCharCnn: embedding() needs at least one stroke");
|
|
587
|
+
if (!finitePoints(strokes)) throw new TypeError("MillirecCharCnn: embedding() got a point that is not a finite number");
|
|
473
588
|
await this.ready;
|
|
474
589
|
if (this.destroyed || !this.stages.length) throw new Error(DESTROYED);
|
|
475
590
|
this._planeFor = null;
|
|
@@ -479,7 +594,7 @@ var MillirecCharCnnCore = class {
|
|
|
479
594
|
/**
|
|
480
595
|
* PERSONALIZE: refit the last stage's head (a softmax over the frozen embedding) on the player's own samples
|
|
481
596
|
* (`{ '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
|
|
597
|
+
* a letter and many take it over. Plain
|
|
483
598
|
* gradient descent on the mean cross-entropy: a few hundred steps over a few hundred samples is ~100 ms. While a
|
|
484
599
|
* personal head is in place the cascade always runs to the last stage (the tiny head is the shipped one).
|
|
485
600
|
* Returns { n, classes } or null (nothing usable). `depersonalize()` restores the shipped head.
|
|
@@ -537,6 +652,7 @@ var MillirecCharCnnCore = class {
|
|
|
537
652
|
this.destroyed = true;
|
|
538
653
|
this.stages = [];
|
|
539
654
|
this.personal = null;
|
|
655
|
+
this.taught = null;
|
|
540
656
|
this._plane = null;
|
|
541
657
|
this._planeFor = null;
|
|
542
658
|
}
|
|
@@ -766,6 +882,18 @@ function createDrawCanvas({
|
|
|
766
882
|
for (let i = 1; i < st.length; i++) ctx.lineTo(st[i].x, st[i].y);
|
|
767
883
|
ctx.stroke();
|
|
768
884
|
}
|
|
885
|
+
function paintFrom(st, from) {
|
|
886
|
+
if (st.length < 2 || from >= st.length - 1) return;
|
|
887
|
+
ctx.lineWidth = 2.5;
|
|
888
|
+
ctx.lineCap = "round";
|
|
889
|
+
ctx.lineJoin = "round";
|
|
890
|
+
ctx.globalAlpha = 1;
|
|
891
|
+
ctx.strokeStyle = ink;
|
|
892
|
+
ctx.beginPath();
|
|
893
|
+
ctx.moveTo(st[Math.max(0, from)].x, st[Math.max(0, from)].y);
|
|
894
|
+
for (let i = Math.max(1, from + 1); i < st.length; i++) ctx.lineTo(st[i].x, st[i].y);
|
|
895
|
+
ctx.stroke();
|
|
896
|
+
}
|
|
769
897
|
function render() {
|
|
770
898
|
ctx.clearRect(0, 0, width, height);
|
|
771
899
|
ctx.lineWidth = 2.5;
|
|
@@ -831,8 +959,8 @@ function createDrawCanvas({
|
|
|
831
959
|
canvas.style.webkitTouchCallout = "none";
|
|
832
960
|
canvas.addEventListener("pointerdown", (e) => {
|
|
833
961
|
if (e.button > 0) return;
|
|
834
|
-
if (activeId !== null
|
|
835
|
-
if (activeType === "pen" && e.pointerType !== "pen") return;
|
|
962
|
+
if (activeId !== null) {
|
|
963
|
+
if (activeType === "pen" && e.pointerType !== "pen" && e.pointerId !== activeId) return;
|
|
836
964
|
finish({ pointerId: activeId });
|
|
837
965
|
}
|
|
838
966
|
e.preventDefault();
|
|
@@ -867,8 +995,9 @@ function createDrawCanvas({
|
|
|
867
995
|
}
|
|
868
996
|
return;
|
|
869
997
|
}
|
|
998
|
+
const from = current.length - 1;
|
|
870
999
|
for (const ev of events) current.push(getPos(ev));
|
|
871
|
-
|
|
1000
|
+
paintFrom(current, from);
|
|
872
1001
|
}, { passive: false });
|
|
873
1002
|
const finish = (e, cancelled = false) => {
|
|
874
1003
|
if (activeId !== e.pointerId) return;
|
|
@@ -926,8 +1055,14 @@ function createDrawCanvas({
|
|
|
926
1055
|
}
|
|
927
1056
|
render();
|
|
928
1057
|
};
|
|
929
|
-
canvas.addEventListener("pointerup", (e) =>
|
|
930
|
-
|
|
1058
|
+
canvas.addEventListener("pointerup", (e) => {
|
|
1059
|
+
if (e.pointerId === activeId) e.preventDefault();
|
|
1060
|
+
finish(e);
|
|
1061
|
+
}, { passive: false });
|
|
1062
|
+
canvas.addEventListener("pointercancel", (e) => finish(e, true), { passive: false });
|
|
1063
|
+
canvas.addEventListener("lostpointercapture", (e) => {
|
|
1064
|
+
if (!canvas.hasPointerCapture || !canvas.hasPointerCapture(e.pointerId)) finish(e, true);
|
|
1065
|
+
});
|
|
931
1066
|
return {
|
|
932
1067
|
el: canvas,
|
|
933
1068
|
getStrokes: () => strokes,
|
|
@@ -983,40 +1118,37 @@ function createDrawCanvas({
|
|
|
983
1118
|
};
|
|
984
1119
|
}
|
|
985
1120
|
|
|
1121
|
+
// ../../library/src/pad.js
|
|
1122
|
+
function attachPad(el, recognizer, { width = 240, height = 240, ink = "#1e293b", idleOverwrite = 2, scribbleClear = true, scribble = null, staleAlpha, onStale, recognizeOptions } = {}) {
|
|
1123
|
+
if (!recognizer || typeof recognizer.recognize !== "function") throw new TypeError("millirec: attachPad(el, recognizer) needs a recognizer with recognize(strokes)");
|
|
1124
|
+
const pad = createDrawCanvas({
|
|
1125
|
+
width,
|
|
1126
|
+
height,
|
|
1127
|
+
ink,
|
|
1128
|
+
idleOverwrite,
|
|
1129
|
+
scribbleClear,
|
|
1130
|
+
scribble,
|
|
1131
|
+
onStale,
|
|
1132
|
+
...staleAlpha === void 0 ? {} : { staleAlpha },
|
|
1133
|
+
onClear: (reason) => el.dispatchEvent(new CustomEvent("millirec:clear", { detail: { reason, undo: () => pad.undoClear() } })),
|
|
1134
|
+
onStrokeEnd: async (strokes) => {
|
|
1135
|
+
if (!strokes.length) {
|
|
1136
|
+
el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results: [], strokes: [] } }));
|
|
1137
|
+
return;
|
|
1138
|
+
}
|
|
1139
|
+
const results = await (recognizeOptions ? recognizer.recognize(strokes, recognizeOptions) : recognizer.recognize(strokes));
|
|
1140
|
+
el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results, strokes } }));
|
|
1141
|
+
}
|
|
1142
|
+
});
|
|
1143
|
+
el.appendChild(pad.el);
|
|
1144
|
+
return { clear: pad.clear, undoClear: pad.undoClear, isStale: pad.isStale, configure: pad.configure, setInk: pad.setInk, repaint: pad.repaint, getStrokes: pad.getStrokes, canvas: pad.el };
|
|
1145
|
+
}
|
|
1146
|
+
|
|
986
1147
|
// ../../library/src/char-cnn.js
|
|
987
1148
|
var MillirecCharCnn = class extends MillirecCharCnnCore {
|
|
988
|
-
/**
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
* drops its own ink.
|
|
992
|
-
*
|
|
993
|
-
* The pad reads ONE character, so by default writing in it after a pause
|
|
994
|
-
* starts a fresh one (`idleOverwrite`, seconds — 0 turns it off) and
|
|
995
|
-
* scribbling over it throws it away (`scribbleClear`). Both are measured:
|
|
996
|
-
* see widget/canvas.js and scripts/verify-scribble.mjs.
|
|
997
|
-
*/
|
|
998
|
-
attach(el, { width = 240, height = 240, ink = "#1e293b", idleOverwrite = 2, scribbleClear = true, scribble = null, staleAlpha, onStale } = {}) {
|
|
999
|
-
const pad = createDrawCanvas({
|
|
1000
|
-
width,
|
|
1001
|
-
height,
|
|
1002
|
-
ink,
|
|
1003
|
-
idleOverwrite,
|
|
1004
|
-
scribbleClear,
|
|
1005
|
-
scribble,
|
|
1006
|
-
onStale,
|
|
1007
|
-
...staleAlpha === void 0 ? {} : { staleAlpha },
|
|
1008
|
-
onClear: (reason) => el.dispatchEvent(new CustomEvent("millirec:clear", { detail: { reason, undo: () => pad.undoClear() } })),
|
|
1009
|
-
onStrokeEnd: async (strokes) => {
|
|
1010
|
-
if (!strokes.length) {
|
|
1011
|
-
el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results: [], strokes: [] } }));
|
|
1012
|
-
return;
|
|
1013
|
-
}
|
|
1014
|
-
const results = await this.recognize(strokes);
|
|
1015
|
-
el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results, strokes } }));
|
|
1016
|
-
}
|
|
1017
|
-
});
|
|
1018
|
-
el.appendChild(pad.el);
|
|
1019
|
-
return { clear: pad.clear, undoClear: pad.undoClear, isStale: pad.isStale, configure: pad.configure, setInk: pad.setInk, repaint: pad.repaint, getStrokes: pad.getStrokes, canvas: pad.el };
|
|
1149
|
+
/** Mount a drawing pad in `el` (library/src/pad.js): 'millirec:result' after each stroke, 'millirec:clear' when it drops its ink. */
|
|
1150
|
+
attach(el, opts) {
|
|
1151
|
+
return attachPad(el, this, opts);
|
|
1020
1152
|
}
|
|
1021
1153
|
};
|
|
1022
1154
|
if (typeof window !== "undefined" && false) window.MillirecCharCnn = MillirecCharCnn;
|
|
@@ -1033,7 +1165,7 @@ function nodeFileUrl(url2, spec) {
|
|
|
1033
1165
|
}
|
|
1034
1166
|
|
|
1035
1167
|
// src/options.js
|
|
1036
|
-
var OPTIONS = ["accurate", "fast", "small"];
|
|
1168
|
+
var OPTIONS = ["accurate", "fast", "small", "digits", "digits-fast", "digits-small", "lowercase", "lowercase-fast", "lowercase-small", "all", "all-fast", "all-small"];
|
|
1037
1169
|
async function loadOption(name, { assetBase = HERE } = {}) {
|
|
1038
1170
|
if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
|
|
1039
1171
|
const base = assetBaseOf(assetBase);
|
|
@@ -1044,9 +1176,7 @@ async function loadOption(name, { assetBase = HERE } = {}) {
|
|
|
1044
1176
|
async function withOption(opts) {
|
|
1045
1177
|
if (typeof opts === "string") opts = { option: opts };
|
|
1046
1178
|
opts = opts || {};
|
|
1047
|
-
if (typeof
|
|
1048
|
-
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");
|
|
1049
|
-
}
|
|
1179
|
+
if (typeof fetch === "undefined") throw new Error("millirec: char-cnn needs fetch to load its model files");
|
|
1050
1180
|
const assetBase = assetBaseOf(opts.assetBase || HERE);
|
|
1051
1181
|
const preset = opts.stages ? {} : await loadOption(opts.option || "accurate", { assetBase });
|
|
1052
1182
|
return { ...preset, ...opts, assetBase };
|
|
@@ -1054,7 +1184,7 @@ async function withOption(opts) {
|
|
|
1054
1184
|
|
|
1055
1185
|
// src/char-cnn.js
|
|
1056
1186
|
var MillirecCharCnn2 = class extends MillirecCharCnn {
|
|
1057
|
-
/** opts: option ('accurate'
|
|
1187
|
+
/** opts: option (one of OPTIONS: 'accurate', 'fast', 'small', 'digits', 'lowercase', 'all', each set also -fast and -small; default 'accurate') or your own
|
|
1058
1188
|
* { classes, stages, kernel }, plus assetBase (default: this module's folder).
|
|
1059
1189
|
* A string is the option's name: create('fast') = create({ option: 'fast' }). */
|
|
1060
1190
|
static async create(opts = {}) {
|