mobilenet.js
var loadFrozenModel = require('@tensorflow/tfjs-converter');
var NamedTensorMap = require('@tensorflow/tfjs-converter');
var tfc = require('@tensorflow/tfjs-core');
var IMAGENET_CLASSES = require('./imagenet_classes');
const GOOGLE_CLOUD_STORAGE_DIR = 'https://storage.googleapis.com/tfjs-models/savedmodel/';
const MODEL_FILE_URL = 'mobilenet_v1_1.0_224/optimized_model.pb';
const WEIGHT_MANIFEST_FILE_URL = 'mobilenet_v1_1.0_224/weights_manifest.json';
const INPUT_NODE_NAME = 'input';
const OUTPUT_NODE_NAME = 'MobilenetV1/Predictions/Reshape_1';
const PREPROCESS_DIVISOR = tfc.scalar(255 / 2);
class MobileNet {
constructor() {}
async load() {
this.model = await loadFrozenModel(
GOOGLE_CLOUD_STORAGE_DIR + MODEL_FILE_URL,
GOOGLE_CLOUD_STORAGE_DIR + WEIGHT_MANIFEST_FILE_URL);
}
dispose() {
if (this.model) {
this.model.dispose();
}
}
predict(input) {
const preprocessedInput = tfc.div(
tfc.sub(input.asType('float32'), PREPROCESS_DIVISOR),
PREPROCESS_DIVISOR);
const reshapedInput =
preprocessedInput.reshape([1, ...preprocessedInput.shape]);
const dict = {};
dict[INPUT_NODE_NAME] = reshapedInput;
return this.model.execute(dict, OUTPUT_NODE_NAME);
}
getTopKClasses(predictions, topK) {
const values = predictions.dataSync();
predictions.dispose();
let predictionList = [];
for (let i = 0; i < values.length; i++) {
predictionList.push({value: values[i], index: i});
}
predictionList = predictionList
.sort((a, b) => {
return b.value - a.value;
})
.slice(0, topK);
return predictionList.map(x => {
return {label: IMAGENET_CLASSES[x.index], value: x.value};
});
}
}
module.exports = MobileNet;
test.js
var tfc = require('@tensorflow/tfjs-core');
var MobileNet = require('./mobilenet');
var fs = require('fs');
var image = require('get-image-data')
var i = 0;
var meta;
image('./cat.jpg', function(err, getImageData){
if(err) throw err;
console.log('start to image data ');
console.log(i++);
console.log("meta : " + getImageData.data.length);
console.log("getImageData :"+getImageData);
const mobileNet = new MobileNet();
console.time('Loading of model');
// await mobileNet.load();
console.timeEnd('Loading of model');
console.log("maybee this is error on the data type");
const pixels = tfc.fromPixels(image);
console.time('First prediction');
let result = mobileNet.predict(pixels);
const topK = mobileNet.getTopKClasses(result, 5);
console.timeEnd('First prediction');
resultElement.innerText = '';
topK.forEach(x => {
resultElement.innerText += `${x.value.toFixed(3)}: ${x.label}\n`;
});
console.time('Subsequent predictions');
result = mobileNet.predict(pixels);
mobileNet.getTopKClasses(result, 5);
console.timeEnd('Subsequent predictions');
mobileNet.dispose();
});
我想使用tensorflow.js分析图像。 但它没有用。
ReferenceError:未定义ImageData 在MathBackendCPU.fromPixels(/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/@tensorflow/tfjs-core/dist/kernels/backend_cpu.js:75:31) at Engine.fromPixels(/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/@tensorflow/tfjs-core/dist/engine.js:292:29) 在ArrayOps.fromPixels(/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/@tensorflow/tfjs-core/dist/ops/array_ops.js:195:41) at /Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/@tensorflow/tfjs-core/dist/ops/operation.js:11:61 at Object.Tracking.tidy(/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/@tensorflow/tfjs-core/dist/tracking.js:36:22) at Object.descriptor.value [as fromPixels](/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/@tensorflow/tfjs-core/dist/ops/operation.js:11:26) at /Users/leeyongmin/Documents/tfjs-converter-master-2/demo/test.js:26:22 at /Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/get-image-data/index.js:18:7 在加载时(/Users/leeyongmin/Documents/tfjs-converter-master-2/demo/node_modules/get-image/server.js:18:5) 在FSReqWrap.readFileAfterClose [as oncomplete](fs.js:511:3)