使用ImageURL训练Tensorflowjs程序
Use Image URL to train Tensorflowjs program
我正在尝试使用互联网上的图像来尝试训练我的网络。我正在使用 Image() 对象来创建图像并将它们传递给 tensorflow。据我所知,Image() returns 一个 HTMLImageElement,但是,我仍然收到以下错误:
Error: pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement, ImageData in browser, or OffscreenCanvas, ImageData in webworker or {data: Uint32Array, width: number, height: number}, but was Image
下面是我的代码 运行:
const tf = require("@tensorflow/tfjs");
require("@tensorflow/tfjs-node")
const mobilenetModule = require("@tensorflow-models/mobilenet");
const knnClassifier = require("@tensorflow-models/knn-classifier");
const { Image } = require("canvas");
const classifier = knnClassifier.create();
const urls = ["https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Solid_white.svg/2048px-Solid_white.svg.png", "https://stone.co.nz/wp-content/uploads/2020/06/Iconic-Black.jpg", "https://media.tarkett-image.com/large/TH_25094225_25187225_001.jpg"];
async function start() {
const mobilenet = await mobilenetModule.load();
const pic0 = new Image();
pic0.src = urls[0];
pic0.onload = () => {
const img0 = tf.browser.fromPixels(pic0);
const logits0 = mobilenet.infer(img0, true);
classifier.addExample(logits0, 0);
}
const pic1 = new Image();
pic1.src = urls[1];
pic1.onload = () => {
const img1 = tf.browser.fromPixels(pic1);
const logits1 = mobilenet.infer(img1, true);
classifier.addExample(logits1, 1);
}
const checkPic = new Image();
checkPic.src = urls[2];
checkPic.onload = () => {
const x = tf.browser.fromPixels(checkPic);
const xlogits = mobilenet.infer(x, true);
const p = classifier.predictClass(xlogits);
console.log(p);
}
}
start();
请注意我是js/nodejs新手
似乎是 TFJS 团队的疏忽,因此无法识别 Image
类型。
改为这样做:
const pic0 = document.createElement('image')
这将创建与 Image 元素“几乎”相同的 HTMLImageElement,但具有与 TF 期望的不同的签名
最好将 TFJS Git 报告为一个问题,它应该很容易解决
我正在尝试使用互联网上的图像来尝试训练我的网络。我正在使用 Image() 对象来创建图像并将它们传递给 tensorflow。据我所知,Image() returns 一个 HTMLImageElement,但是,我仍然收到以下错误:
Error: pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement, ImageData in browser, or OffscreenCanvas, ImageData in webworker or {data: Uint32Array, width: number, height: number}, but was Image
下面是我的代码 运行:
const tf = require("@tensorflow/tfjs");
require("@tensorflow/tfjs-node")
const mobilenetModule = require("@tensorflow-models/mobilenet");
const knnClassifier = require("@tensorflow-models/knn-classifier");
const { Image } = require("canvas");
const classifier = knnClassifier.create();
const urls = ["https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Solid_white.svg/2048px-Solid_white.svg.png", "https://stone.co.nz/wp-content/uploads/2020/06/Iconic-Black.jpg", "https://media.tarkett-image.com/large/TH_25094225_25187225_001.jpg"];
async function start() {
const mobilenet = await mobilenetModule.load();
const pic0 = new Image();
pic0.src = urls[0];
pic0.onload = () => {
const img0 = tf.browser.fromPixels(pic0);
const logits0 = mobilenet.infer(img0, true);
classifier.addExample(logits0, 0);
}
const pic1 = new Image();
pic1.src = urls[1];
pic1.onload = () => {
const img1 = tf.browser.fromPixels(pic1);
const logits1 = mobilenet.infer(img1, true);
classifier.addExample(logits1, 1);
}
const checkPic = new Image();
checkPic.src = urls[2];
checkPic.onload = () => {
const x = tf.browser.fromPixels(checkPic);
const xlogits = mobilenet.infer(x, true);
const p = classifier.predictClass(xlogits);
console.log(p);
}
}
start();
请注意我是js/nodejs新手
似乎是 TFJS 团队的疏忽,因此无法识别 Image
类型。
改为这样做:
const pic0 = document.createElement('image')
这将创建与 Image 元素“几乎”相同的 HTMLImageElement,但具有与 TF 期望的不同的签名
最好将 TFJS Git 报告为一个问题,它应该很容易解决