Files
BoomCast/scripts/verify_browser_yamnet.mjs
T
2026-10-04 17:09:22 +02:00

30 lines
1.5 KiB
JavaScript

import { readFileSync } from "node:fs";
import { join, dirname } from "node:path";
import * as tf from "@tensorflow/tfjs";
const directory = new URL("../fireworks_detect/static/browser-yamnet/", import.meta.url).pathname;
const manifest = JSON.parse(readFileSync(join(directory, "model.json"), "utf8"));
const mapping = JSON.parse(readFileSync(join(directory, "yamnet_class_map.json"), "utf8"));
if (manifest.format !== "graph-model" || mapping.length !== 521) throw new Error("Invalid browser YAMNet assets");
const specs = manifest.weightsManifest.flatMap((group) => group.weights);
const chunks = manifest.weightsManifest.flatMap((group) => group.paths.map((path) => readFileSync(join(directory, path))));
const combined = Buffer.concat(chunks);
const model = await tf.loadGraphModel({ load: async () => ({
modelTopology: manifest.modelTopology,
weightSpecs: specs,
weightData: combined.buffer.slice(combined.byteOffset, combined.byteOffset + combined.byteLength),
}) });
const input = tf.zeros([16000]);
let outputs;
try {
outputs = await model.executeAsync(input);
const tensors = Array.isArray(outputs) ? outputs : [outputs];
const scores = tensors.find((tensor) => tensor.shape.length === 2 && tensor.shape[1] === 521);
if (!scores || scores.shape[0] < 1) throw new Error("YAMNet did not produce scores");
console.log("Verified browser YAMNet score tensor:", scores.shape);
} finally {
input.dispose();
(Array.isArray(outputs) ? outputs : [outputs]).forEach((tensor) => tensor?.dispose());
model.dispose();
}