我正在浏览器中运行以下程序:
<script src="https://unpkg.com/@tensorflow/tfjs"></script>
<script src="https://unpkg.com/@tensorflow/tfjs-automl"></script>
<img
id="daisy"
crossorigin="anonymous"
src="https://storage.googleapis.com/tfjs-testing/tfjs-automl/img_classification/daisy.jpg"
/>
<script>
async function run() {
const model = await tf.automl.loadImageClassification("model.json");
const image = document.getElementById("daisy");
const predictions = await model.classify(image);
const pre = document.createElement("pre");
pre.textContent = JSON.stringify(predictions, null, 2);
document.body.append(pre);
}
run();
</script>
我想要做的就是将脚本转换为可以在节点js中运行的脚本,如下所示:
import * as tf from "@tensorflow/tfjs";
import * as automl from "@tensorflow/tfjs-automl";
async function run() {
const model = await tf.automl.loadImageClassification("model.json");
const image = document.createElement("img");
image.src =
"https://storage.googleapis.com/tfjs-testing/tfjs-automl/img_classification/daisy.jpg";
const predictions = await model.classify(image);
console.log(predictions);
}
run();
然后我用node --experimental-modules index.js
运行脚本,但失败:
(node:24163) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'loadImageClassification' of undefined
我也尝试过require
:
const tf = require("@tensorflow/tfjs");
const automl = require("@tensorflow/tfjs-automl");
async function run() {
const model = await tf.automl.loadImageClassification("model.json");
const image = document.createElement("img");
image.src =
"https://storage.googleapis.com/tfjs-testing/tfjs-automl/img_classification/daisy.jpg";
const predictions = await model.classify(image);
console.log(predictions);
}
run();
我不得不从"type": "module"
中删除package.json
并使用node index index.js
运行。它给出了相同的错误。
我还尝试了不捕获require
:
require("@tensorflow/tfjs");
require("@tensorflow/tfjs-automl");
async function run() {
const model = await tf.automl.loadImageClassification("model.json");
const image = document.createElement("img");
image.src =
"https://storage.googleapis.com/tfjs-testing/tfjs-automl/img_classification/daisy.jpg";
const predictions = await model.classify(image);
console.log(predictions);
}
run();
运行此命令时,出现错误:(node:24211) UnhandledPromiseRejectionWarning: ReferenceError: tf is not defined
。
这似乎很明显,但是有一种方法可以执行<script src=
的操作,但是在节点中,即引入外部脚本,以便我的脚本可以查看和使用外部的变量/方法。脚本?
答案 0 :(得分:1)
对于任何想在节点上运行张量流预测的人:
const tf = require("@tensorflow/tfjs-node");
const automl = require("@tensorflow/tfjs-automl");
const fs = require("fs");
const model_url = "<your-model-url>";
const image_path = process.argv.slice(2)[0];
if (!image_path) {
throw new Error("missing argument: path to image");
}
const image = fs.readFileSync(image_path);
const decoded_image = tf.node.decodeJpeg(image);
async function run() {
const model = await automl.loadImageClassification(model_url);
const predictions = await model.classify(decoded_image);
console.log(predictions);
}
run().catch(console.error);
答案 1 :(得分:0)
起初,我误解了您的问题。根据我现在所知道的,您不需要使用github问题中所述的tfjs节点,而是tfjs节点
https://us04web.zoom.us/j/74533735324?pwd=d2NkcENZS3Q2MUtEeGFjc2V6TUowdz09
原因是默认情况下tfjs设计为在浏览器上运行。我仍然认为以下信息可能对将来的某些人有所帮助。
这是NodeJS的官方资源 https://nodejs.dev/differences-between-nodejs-and-the-browser
脚本失败的原因可能与多种原因有关。这是我的想法:
npm install --save @tensorflow/tfjs-automl @tensorflow/tfjs
tfjs可能需要先捆绑在一起,然后才能在浏览器中使用。这意味着仅凭一个简单的nodejs应用程序还不够,而是您需要做一个简单的设置将模块和脚本捆绑到一个脚本中,然后就可以在浏览器中使用。
您确实在正确使用模块,但是浏览器不知道如何加载它们,可能是因为您的浏览器不理解“导入”语法。尽管不太可能,但我只是想着要这样。
您的设置很好,但是您没有正确使用tensorflow库。从你告诉我的评论。我感到也许loadImageClassification
方法或classify
方法都没有收到他们期望的参数。请记住,loadImageClassification需要一个tensorflow在分类期间将使用的json文件的URL。我还发现,如果不设置宽度和高度,那么在动态创建图像时,tfjs会遇到很多麻烦,如此处所述:
Requested texture size [0x0] is invalid. error when i am loading image in browser
我浏览了tfjs示例,发现其中一个实际上看起来像您在此处发布的示例: https://github.com/tensorflow/tfjs/tree/master/tfjs-automl/demo/img_classification
我也自己在github个人资料中进行了类似的设置