// Misma lógica que el Space (nucleo.js) con onnxruntime-web WASM en Node (1 hilo). import * as ort from 'onnxruntime-web'; import fs from 'fs'; import sharp from 'sharp'; import * as N from './nucleo.js'; ort.env.wasm.numThreads = 1; // Uso: npm i onnxruntime-web@1.31.1 sharp && node prueba_node.mjs [lado] const MD = (process.argv[2] || './onnx') + '/'; const lado = +(process.argv[3] || 504); let t = performance.now(); const ses = await ort.InferenceSession.create(fs.readFileSync(MD + 'model.onnx'), { executionProviders: ['wasm'], externalData: [{ path: 'model.onnx_data', data: fs.readFileSync(MD + 'model.onnx_data') }] }); console.log('carga ms', (performance.now() - t).toFixed(0)); const verdad = JSON.parse(fs.readFileSync('ejemplos/verdad.json')); async function rgba(f, w, h) { return new Uint8ClampedArray((await sharp(f).resize(w, h, { fit: 'cover' }).ensureAlpha().raw().toBuffer())); } const casos = verdad.ejemplos.map((e) => ({ id: e.id, files: e.vistas.map((v) => 'ejemplos/' + v.imagen), v: e })); // control negativo: ruido const ruido = []; for (let i = 0; i < 3; i++) { const b = Buffer.alloc(504 * 280 * 3); for (let k = 0; k < b.length; k++) b[k] = Math.random() * 255; const f = `/tmp/ruido_${i}.png`; await sharp(b, { raw: { width: 504, height: 280, channels: 3 } }).png().toFile(f); ruido.push(f); } casos.push({ id: 'ruido', files: ruido }); const res = {}; for (const c of casos) { const { w, h } = N.tamTrabajo(504, 280, lado), px = await Promise.all(c.files.map((f) => rgba(f, w, h))); const ten = N.aTensor(px, w, h); t = performance.now(); const s = await N.inferir(ort, ses, ten, px.length, w, h); const ti = performance.now() - t; const P = N.poses(s), est = N.estadisticas(s), nb = N.nube(s, px, P, { paso: 2 }), co = N.coherencia(s, P); const r = { w, h, infer_ms: Math.round(ti), dimsK: s.dimsK, puntos: nb.total, est, coherencia: co, giros: P.map((p) => p.giro), fx: P.map((p) => p.K[0]) }; if (c.v) r.verdad = N.compararVerdad(P, c.v, w, h); res[c.id] = r; console.log(c.id, JSON.stringify(r, (k, v) => (typeof v === 'number' ? +v.toFixed(4) : v))); } console.log('rss MB', (process.memoryUsage().rss / 1e6).toFixed(0)); fs.writeFileSync(`resultados_node_${lado}.json`, JSON.stringify(res, null, 1));