Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(async function() {
const handler = tf.io.fileSystem(process.env.modelFile); // see https://stackoverflow.com/a/53766926/5317732
model = await tf.loadLayersModel(handler);
// load model from remote file
//const path = 'https://www.adblockradio.com/models/' + canonical + '/model.json';
//model = await tf.loadModel(path);
log.info(process.env.canonical + ': ML model loaded');
send({ type: 'loading', err: null, loaded: true });
})();
async function saveModelAndRandomInputsAndOutputs(
model, exportPathprefix, inputIntegerMax) {
await model.save(tfjsNode.io.fileSystem(`${exportPathprefix}`));
const xs = [];
const xsData = [];
const xsShapes = [];
for (const inputTensor of model.inputs) {
const inputShape = inputTensor.shape;
inputShape[0] = 1;
if (inputShape.indexOf(null) !== -1) {
throw new Error(
`It is assumed that the only the first dimension of the tensor ` +
`is undetermined, but the assumption is not satisfied for ` +
`input shape ${JSON.stringify(inputTensor.shape)}`);
}
const xTensor = inputIntegerMax == null ?
tfc.randomNormal(inputShape) :
tfc.floor(tfc.randomUniform(inputShape, 0, inputIntegerMax));
async function saveModelAndRandomInputsAndOutputs(
model, exportPathprefix, inputIntegerMax) {
await model.save(tfjsNode.io.fileSystem(`${exportPathprefix}`));
const xs = [];
const xsData = [];
const xsShapes = [];
for (const inputTensor of model.inputs) {
const inputShape = inputTensor.shape;
inputShape[0] = 1;
if (inputShape.indexOf(null) !== -1) {
throw new Error(
`It is assumed that the only the first dimension of the tensor ` +
`is undetermined, but the assumption is not satisfied for ` +
`input shape ${JSON.stringify(inputTensor.shape)}`);
}
const xTensor = inputIntegerMax == null ?
tfc.randomNormal(inputShape) :
tfc.floor(tfc.randomUniform(inputShape, 0, inputIntegerMax));
loadingPromise = new Promise(async function(resolve, reject) {
if(isNodeEnvironment) {
tf = require('@tensorflow/tfjs')
console.log('Nodejs Environment detected ');
var tfnode = require('@tensorflow/tfjs-node');
var modelPath = require('path').resolve(__dirname, '../tf_model/model.json');
model = await tf.loadModel(tfnode.io.fileSystem(modelPath));
} else {
if(typeof (window as any).tf == "undefined") {
modelLoaded = false;
laodingModel = false;
console.log('Tensorflow js not imported, pattern detection may not work');
resolve();
return;
}
tf = (window as any).tf;
console.log('Browser Environment detected ', tf);
console.log('Loading model ....')
model = await tf.loadModel('/tf_model/model.json');
modelLoaded = true;
laodingModel = false;
setTimeout(resolve, 1000);
console.log('Loaded model');
return __awaiter(this, void 0, void 0, function* () {
if (isNodeEnvironment) {
tf = require('@tensorflow/tfjs');
console.log('Nodejs Environment detected ');
var tfnode = require('@tensorflow/tfjs-node');
var modelPath = require('path').resolve(__dirname, '../tf_model/model.json');
model = yield tf.loadModel(tfnode.io.fileSystem(modelPath));
}
else {
if (typeof window.tf == "undefined") {
modelLoaded = false;
laodingModel = false;
console.log('Tensorflow js not imported, pattern detection may not work');
resolve();
return;
}
tf = window.tf;
console.log('Browser Environment detected ', tf);
console.log('Loading model ....');
model = yield tf.loadModel('/tf_model/model.json');
modelLoaded = true;
laodingModel = false;
setTimeout(resolve, 1000);