0

I followed this tutorial to learn how to use the tensorflow.js model mobilenet in node.js:link

Now I am trying to use my own tensorflow.js model trained in teachable machine using the @teachablemachine/image package: link

Here is my code:

const tf = require('@tensorflow/tfjs');
const tfnode = require('@tensorflow/tfjs-node');
const tmImage = require('@teachablemachine/image');
const fs = require('fs');
const path = require('path');
const FileAPI = require('file-api'), 
File = FileAPI.File;
global.FileReader = FileAPI.FileReader;
global.Response = require('response');

const uploadModel =  "model.json"
const uploadModelPath = path.join(process.cwd(), uploadModel);
const uploadModelFile = new File({ 
    name: "model.json",
    type: "application/json",
    path: uploadModelPath
});

const uploadWeights = "weights.bin"
const uploadWeightsPath = path.join(process.cwd(), uploadWeights);  
const uploadWeightsFile = new File({
    name: "weights.bin", 
    path: uploadWeightsPath
});

const uploadMetadata = "metadata.json"
const uploadMetadataPath = path.join(process.cwd(), uploadMetadata);
const uploadMetadataFile = new File({
    name: "metadata.json",
    type: "application/json",
    path: uploadMetadataPath
});

const readImage = path => {
    const imageBuffer = fs.readFileSync(path);
    const tfimage = tfnode.node.decodeImage(imageBuffer);
    return tfimage;
}

const imageClassification = async path => {
    const image = readImage(path);
    const model = await tmImage.loadFromFiles(uploadModelFile,uploadWeightsFile,uploadMetadataFile);
    //const model = await tmImage.load('https://teachablemachine.withgoogle.com/models/25uN0DSdd/model.json','https://teachablemachine.withgoogle.com/models/25uN0DSdd/metadata.json');
    const predictions = await model.predict(image);
    console.log('Classification Results:', predictions);
}

if (process.argv.length !== 3) throw new Error('Incorrect arguments: node classify.js <IMAGE_FILE>');

imageClassification(process.argv[2]);

When I run it I get error:

> (node:94924) UnhandledPromiseRejectionWarning: Error: Invalid Metadata provided
    at C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@teachablemachine\image\dist\custom-mobilenet.js:163:27

Which leads me to:

var processMetadata = function (metadata) { return __awaiter(void 0, void 0, void 0, function () {
    var metadataJSON, metadataResponse;
    return __generator(this, function (_a) {
        switch (_a.label) {
            case 0:
                if (!(typeof metadata === 'string')) return [3 /*break*/, 3];
                return [4 /*yield*/, fetch(metadata)];
            case 1:
                metadataResponse = _a.sent();
                return [4 /*yield*/, metadataResponse.json()];
            case 2:
                metadataJSON = _a.sent();
                return [3 /*break*/, 4];
            case 3:
                if (isMetadata(metadata)) {
                    metadataJSON = metadata;
                }
                else {
                    throw new Error('Invalid Metadata provided');
                }
                _a.label = 4;
            case 4: return [2 /*return*/, fillMetadata(metadataJSON)];
        }
    });
}); };

full file here: link

So I can see case 0-2 aren't being triggered and for case 3 the metadata file isn't passing the isMetadata function which is:

var isMetadata = function (c) {
    return !!c && Array.isArray(c.labels);
};

Which I think tests that the file is not undefined and has an array of labels.

Where to go from there I am not sure because I don't understand the rest of the code in that file. I am going to try an alternative approach but I thought I might post this encase someone with more experience can see the problem clearly and wants to help teach me something or point me in the right direction or just simply tell me that at my experience level this isn't the right use of my time.

Thanks for reading.

r3vdev
  • 315
  • 3
  • 10

0 Answers0