首页 > 解决方案 > 故障跟踪错误“提供的元数据无效”

问题描述

我按照本教程学习了如何在 node.js 中使用 tensorflow.js 模型 mobilenet:链接

现在我正在尝试使用我自己的 tensorflow.js 模型,该模型使用 @teachablemachine/image 包在可教机器上训练:link

这是我的代码:

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]);

当我运行它时,我得到错误:

> (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

这导致我:

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)];
        }
    });
}); };

完整文件在这里:链接

所以我可以看到案例 0-2 没有被触发,对于案例 3,元数据文件没有传递 isMetadata 函数,它是:

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

我认为这可以测试文件是否未定义并且具有标签数组。

我不确定从那里去哪里,因为我不理解该文件中的其余代码。我将尝试另一种方法,但我想我可能会发布此包装,有更多经验的人可以清楚地看到问题并想帮助教我一些东西或为我指出正确的方向,或者只是简单地告诉我,以我的经验水平不是正确使用我的时间。

谢谢阅读。

标签: javascriptnode.jstensorflow.jsmobilenet

解决方案


推荐阅读