【问题标题】:Error: cannot read as File when loading model错误:加载模型时无法读取为文件
【发布时间】:2020-04-26 04:32:24
【问题描述】:

我认为我犯了一个新手错误,但我无法弄清楚出了什么问题。

错误:

C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing>node classify rlc.jpg (node:38620) UnhandledPromiseRejectionWarning: Error: cannot read as File: "model.json" at readFile (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\filereader\FileReader.js:266:15) at FileReader.self.readAsText (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\filereader\FileReader.js:295:7) at C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:226:36 at new Promise (<anonymous>) at BrowserFiles.<anonymous> (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:159:39) at step (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:48:23) at Object.next (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:29:53) at C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:23:71 at new Promise (<anonymous>) at __awaiter (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\@tensorflow\tfjs-core\dist\io\browser_files.js:19:12) (node:38620) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:38620) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

代码:

const tf = require('@tensorflow/tfjs');
const tfnode = require('@tensorflow/tfjs-node');
const tmImage = require('@teachablemachine/image');
const fs = require('fs');
global.FileReader = require('filereader');
const uploadModel =  "model.json"
const uploadWeights = "weights.bin"
const uploadMetadata = "metadata.json"

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(uploadModel,uploadWeights,uploadMetadata);
    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]);

文件结构:

/Testing
  /node_modules
  classify.js
  metadata.json
  model.json
  package-lock.json
  rlc.jpg
  weights.bin

背景:试图利用我所学到的部署图像分类模型,该模型内置于带有本机 javascript 的可教机器中,并将其适应节点 js。我是一个新手,我正在考虑节点和浏览器之间的环境差异,我遵循的所有教程都基于这些差异。

我正在学习的教程:

【问题讨论】:

    标签: javascript node.js tensorflow tensorflow.js


    【解决方案1】:

    库需要 loadFromFiles 函数中的文件,documentation in github。 文件是browser API,默认情况下您不能在节点中使用。 所以你需要在节点环境中以某种方式填充它,查看这些库 node-fetch/fetch-blob node-file-api/file-api

    file-api 的用法示例:

    const fs = require('fs');
    const path = require('path');
    const FileAPI = require('file-api');
    
    const uploadModel =  "model.json"
    const uploadModelPath = path.join(process.cwd(), uploadModel);
    
    // polyfill
    Object.keys(FileApi).forEach(key => { 
        process[key] = FileApi[key];
    })
    
    const uploadModelFile = new File({ 
      buffer: fs.readFileSync(uploadModelPath)
    });
    

    这个库已经很老了,它可能无法工作,你可以尝试搜索其他 polyfill 库或编写你的。 您可以在tensor flow source code 中查看文件是如何读取的,或者您可以分叉并添加使用节点文件的可能性。

    【讨论】:

    • 使用 file-api 完成您的解决方案。收到此错误:C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\File\File.js:35 throw new Error("No name"); ^ 错误:新文件中没有名称 (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\node_modules\←[4mFile←[24m\File.js:35:13) 在 Object. (C:\Users\Awesome\Google Drive\Source\Programming\JS\Testing\classify.js:24:25)”。继续挖掘感谢您的建议。
    • 您需要为文件指定名称,例如name: "abc-song.txt"。你可以在node_moduels中看到库的源代码,我以前通过阅读库的源代码来理解问题。
    • 想通了,将 mime 回滚到以前的版本以解决另一个问题,现在似乎正在执行,但 @teachablemachine 模块中的函数“processMetaData”由于提供的无效元数据而引发错误。调查仍在继续,再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 2018-08-21
    • 2013-07-23
    • 1970-01-01
    • 2018-06-25
    相关资源
    最近更新 更多