-
Notifications
You must be signed in to change notification settings - Fork 554
/
Copy pathindex.ts
445 lines (387 loc) · 13.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import * as tf from "@tensorflow/tfjs";
// Remove the trailing slash below if this Bun issue gets fixed: /~https://github.com/oven-sh/bun/issues/8683
import { Buffer } from "buffer/";
import { NSFW_CLASSES } from "./nsfw_classes";
declare global {
namespace NodeJS {
interface Global {
[x: string]: any;
}
}
interface Window {
[x: string]: any;
}
}
type IOHandler = tf.io.IOHandler;
type ModelJSON = tf.io.ModelJSON;
type ModelArtifacts = tf.io.ModelArtifacts;
type WeightDataBase64 = { [x: string]: string };
export type FrameResult = {
index: number;
totalFrames: number;
predictions: Array<PredictionType>;
image: HTMLCanvasElement | ImageData;
};
export type ClassifyConfig = {
topk?: number;
fps?: number;
onFrame?: (result: FrameResult) => any;
};
interface NSFWJSOptions {
size?: number;
type?: string;
}
export type PredictionType = {
className: (typeof NSFW_CLASSES)[keyof typeof NSFW_CLASSES];
probability: number;
};
export type ModelName = "MobileNetV2" | "MobileNetV2Mid" | "InceptionV3";
type ModelConfig = {
[key in ModelName]: {
numOfWeightBundles: number;
options?: NSFWJSOptions;
};
};
const availableModels: ModelConfig = {
MobileNetV2: { numOfWeightBundles: 1 },
MobileNetV2Mid: {
numOfWeightBundles: 2,
options: { type: "graph" },
},
InceptionV3: {
numOfWeightBundles: 6,
options: { size: 299 },
},
};
const DEFAULT_MODEL_NAME: ModelName = "MobileNetV2";
const IMAGE_SIZE = 224; // default to Mobilenet v2
const getGlobal = () => {
if (typeof globalThis !== "undefined")
return globalThis as typeof globalThis & NodeJS.Global & Window;
if (typeof global !== "undefined")
return global as typeof globalThis & NodeJS.Global & Window;
if (typeof window !== "undefined")
return window as typeof globalThis & NodeJS.Global & Window;
if (typeof self !== "undefined")
return self as typeof globalThis & NodeJS.Global & Window;
throw new Error("Unable to locate global object");
};
function isModelName(name?: string): name is ModelName {
return !!name && name in availableModels;
}
const getModelJson = async (modelName: ModelName) => {
const globalModel = getGlobal().model;
if (globalModel) {
// If the model is available globally (UMD via script tag), return it
return globalModel;
}
let modelJson;
if (modelName === "MobileNetV2")
({ modelJson } = await import("./model_imports/mobilenet_v2"));
else if (modelName === "MobileNetV2Mid")
({ modelJson } = await import("./model_imports/mobilenet_v2_mid"));
else if (modelName === "InceptionV3")
({ modelJson } = await import("./model_imports/inception_v3"));
return (await modelJson()).default;
};
const getWeightData = async (
modelName: ModelName
): Promise<WeightDataBase64> => {
const { numOfWeightBundles } = availableModels[modelName];
const bundles: WeightDataBase64[] = [];
for (let i = 0; i < numOfWeightBundles; i++) {
const bundleName = `group1-shard${i + 1}of${numOfWeightBundles}`;
const identifier = bundleName.replace(/-/g, "_");
const globalWeight = getGlobal()[identifier];
if (globalWeight) {
// If the weight data bundle is available globally (UMD via script tag), use it
bundles.push({ [bundleName]: globalWeight });
} else {
let weightBundles;
if (modelName === "MobileNetV2")
({ weightBundles } = await import("./model_imports/mobilenet_v2"));
else if (modelName === "MobileNetV2Mid")
({ weightBundles } = await import("./model_imports/mobilenet_v2_mid"));
else if (modelName === "InceptionV3")
({ weightBundles } = await import("./model_imports/inception_v3"));
const weight = (await weightBundles[i]()).default;
bundles.push({ [bundleName]: weight });
}
}
return Object.assign({}, ...bundles);
};
async function loadWeights(modelName: ModelName): Promise<WeightDataBase64> {
try {
const weightDataBundles = await getWeightData(modelName);
return weightDataBundles;
} catch {
throw new Error(
`Could not load the weight data. Make sure you are importing the correct shard files from the models directory. Ref: /~https://github.com/infinitered/nsfwjs?tab=readme-ov-file#browserify`
);
}
}
async function loadModel(modelName: ModelName | string) {
if (!isModelName(modelName)) return modelName; // Custom url for the model provided
try {
const modelJson = await getModelJson(modelName);
const weightData = await loadWeights(modelName);
const handler = new JSONHandler(modelJson, weightData);
return handler;
} catch {
throw new Error(
`Could not load the model. Make sure you are importing the model.min.js bundle. Ref: /~https://github.com/infinitered/nsfwjs?tab=readme-ov-file#browserify`
);
}
}
export async function load(modelOrUrl?: ModelName): Promise<NSFWJS>;
export async function load(
modelOrUrl?: string,
options?: NSFWJSOptions
): Promise<NSFWJS>;
export async function load(
modelOrUrl?: string,
options: NSFWJSOptions = { size: IMAGE_SIZE }
) {
if (tf == null) {
throw new Error(
`Cannot find TensorFlow.js. If you are using a <script> tag, please ` +
`also include @tensorflow/tfjs on the page before using this model.`
);
}
if (modelOrUrl === undefined) {
modelOrUrl = DEFAULT_MODEL_NAME;
console.info(
`%cBy not specifying 'modelOrUrl' parameter, you're using the default model: '${modelOrUrl}'. See NSFWJS docs for instructions on hosting your own model (/~https://github.com/infinitered/nsfwjs?tab=readme-ov-file#host-your-own-model).`,
"color: lightblue"
);
} else if (isModelName(modelOrUrl)) {
console.info(
`%cYou're using the model: '${modelOrUrl}'. See NSFWJS docs for instructions on hosting your own model (/~https://github.com/infinitered/nsfwjs?tab=readme-ov-file#host-your-own-model).`,
"color: lightblue"
);
options = availableModels[modelOrUrl].options ?? options;
}
// Default size is IMAGE_SIZE - needed if just type option is used
options.size = options?.size || IMAGE_SIZE;
const modelUrlOrHandler = await loadModel(modelOrUrl);
const nsfwnet = new NSFWJS(modelUrlOrHandler, options);
await nsfwnet.load();
return nsfwnet;
}
class JSONHandler implements IOHandler {
private modelJson: ModelJSON;
private weightDataBase64: WeightDataBase64;
constructor(modelJson: ModelJSON, weightDataBase64: WeightDataBase64) {
this.modelJson = modelJson;
this.weightDataBase64 = weightDataBase64;
}
arrayBufferFromBase64(base64: string) {
const binaryString = Buffer.from(base64, "base64").toString("binary");
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
async load() {
const modelArtifacts: ModelArtifacts = {
modelTopology: this.modelJson.modelTopology,
format: this.modelJson.format,
generatedBy: this.modelJson.generatedBy,
convertedBy: this.modelJson.convertedBy,
};
if (this.modelJson.weightsManifest != null) {
const weightSpecs: ModelArtifacts["weightSpecs"] = [];
const weightData: Uint8Array[] = [];
for (const group of this.modelJson.weightsManifest) {
for (const path of group.paths) {
const base64 = this.weightDataBase64[path];
if (!base64) {
throw new Error(
`Could not find the weight data. Make sure you are importing the correct weight bundle for the model: ${path}.min.js.`
);
}
const buffer = this.arrayBufferFromBase64(base64);
weightData.push(new Uint8Array(buffer));
}
weightSpecs.push(...group.weights);
}
modelArtifacts.weightSpecs = weightSpecs;
const weightDataConcat = new Uint8Array(
weightData.reduce((a, b) => a + b.length, 0)
);
let offset = 0;
for (let i = 0; i < weightData.length; i++) {
weightDataConcat.set(weightData[i], offset);
offset += weightData[i].byteLength;
}
modelArtifacts.weightData = weightDataConcat.buffer;
}
if (this.modelJson.trainingConfig != null) {
modelArtifacts.trainingConfig = this.modelJson.trainingConfig;
}
if (this.modelJson.userDefinedMetadata != null) {
modelArtifacts.userDefinedMetadata = this.modelJson.userDefinedMetadata;
}
return modelArtifacts;
}
}
export class NSFWJS {
public endpoints: string[];
public model: tf.LayersModel | tf.GraphModel;
private options: NSFWJSOptions;
private urlOrIOHandler: string | IOHandler;
private intermediateModels: { [layerName: string]: tf.LayersModel } = {};
private normalizationOffset: tf.Scalar;
constructor(modelUrlOrIOHandler: string | IOHandler, options: NSFWJSOptions) {
this.options = options;
this.normalizationOffset = tf.scalar(255);
this.urlOrIOHandler = modelUrlOrIOHandler;
if (
typeof modelUrlOrIOHandler === "string" &&
!modelUrlOrIOHandler.startsWith("indexeddb://") &&
!modelUrlOrIOHandler.startsWith("localstorage://") &&
!modelUrlOrIOHandler.endsWith("model.json")
) {
this.urlOrIOHandler = `${modelUrlOrIOHandler}model.json`;
} else {
this.urlOrIOHandler = modelUrlOrIOHandler;
}
}
async load() {
const { size, type } = this.options;
if (type === "graph") {
this.model = await tf.loadGraphModel(this.urlOrIOHandler);
} else {
// this is a Layers Model
this.model = await tf.loadLayersModel(this.urlOrIOHandler);
this.endpoints = this.model.layers.map((l) => l.name);
}
// Warmup the model.
const result = tf.tidy(() =>
this.model.predict(tf.zeros([1, size!, size!, 3]))
) as tf.Tensor;
await result.data();
result.dispose();
}
/**
* Infers through the model. Optionally takes an endpoint to return an
* intermediate activation.
*
* @param img The image to classify. Can be a tensor or a DOM element image,
* video, or canvas.
* @param endpoint The endpoint to infer through. If not defined, returns
* logits.
*/
infer(
img:
| tf.Tensor3D
| ImageData
| HTMLImageElement
| HTMLCanvasElement
| HTMLVideoElement,
endpoint?: string
): tf.Tensor {
if (endpoint != null && this.endpoints.indexOf(endpoint) === -1) {
throw new Error(
`Unknown endpoint ${endpoint}. Available endpoints: ${this.endpoints}.`
);
}
return tf.tidy(() => {
if (!(img instanceof tf.Tensor)) {
img = tf.browser.fromPixels(img);
}
// Normalize the image from [0, 255] to [0, 1].
const normalized = img
.toFloat()
.div(this.normalizationOffset) as tf.Tensor3D;
// Resize the image to
let resized = normalized;
const { size } = this.options;
// check width and height if resize needed
if (img.shape[0] !== size || img.shape[1] !== size) {
const alignCorners = true;
resized = tf.image.resizeBilinear(
normalized,
[size!, size!],
alignCorners
);
}
// Reshape to a single-element batch so we can pass it to predict.
const batched = resized.reshape([1, size!, size!, 3]);
let model: tf.LayersModel | tf.GraphModel;
if (endpoint == null) {
model = this.model;
} else {
if (
this.model.hasOwnProperty("layers") &&
this.intermediateModels[endpoint] == null
) {
// @ts-ignore
const layer = this.model.layers.find((l) => l.name === endpoint);
this.intermediateModels[endpoint] = tf.model({
// @ts-ignore
inputs: this.model.inputs,
outputs: layer.output,
});
}
model = this.intermediateModels[endpoint];
}
// return logits
return model.predict(batched) as tf.Tensor2D;
});
}
/**
* Classifies an image from the 5 classes returning a map of
* the most likely class names to their probability.
*
* @param img The image to classify. Can be a tensor or a DOM element image,
* video, or canvas.
* @param topk How many top values to use. Defaults to 5
*/
async classify(
img:
| tf.Tensor3D
| ImageData
| HTMLImageElement
| HTMLCanvasElement
| HTMLVideoElement,
topk = 5
): Promise<Array<PredictionType>> {
const logits = this.infer(img) as tf.Tensor2D;
const classes = await getTopKClasses(logits, topk);
logits.dispose();
return classes;
}
}
async function getTopKClasses(
logits: tf.Tensor2D,
topK: number
): Promise<Array<PredictionType>> {
const values = await logits.data();
const valuesAndIndices: {
value: (typeof values)[number];
index: number;
}[] = [];
for (let i = 0; i < values.length; i++) {
valuesAndIndices.push({ value: values[i], index: i });
}
valuesAndIndices.sort((a, b) => {
return b.value - a.value;
});
const topkValues = new Float32Array(topK);
const topkIndices = new Int32Array(topK);
for (let i = 0; i < topK; i++) {
topkValues[i] = valuesAndIndices[i].value;
topkIndices[i] = valuesAndIndices[i].index;
}
const topClassesAndProbs: PredictionType[] = [];
for (let i = 0; i < topkIndices.length; i++) {
topClassesAndProbs.push({
className: NSFW_CLASSES[topkIndices[i]],
probability: topkValues[i],
});
}
return topClassesAndProbs;
}