当我将Image添加到分类器中进行训练时,它将引发一些异常。我们正在使用ml5.js的Mobilenet模型,在该模型中,当我们调用train()方法时。
let features = ml5.featureExtractor('MobileNet');
const classifier = features.classification();
console.log("setup classifier DONE", classifier);
var img2;
console.log("adding images");
const gorra = new Image();
gorra.src = "https://ml5js.org/docs/assets/img/bird.jpg";
gorra.width = 224;
gorra.height = 224;
console.log("adding images DONE", gorra);
img2 = new Image();
img2.src = "{!$Resource.cat}"
img2.width = 224;
img2.height = 224;
console.log(img2);
var img3;
img3 = new Image();
img3.src = "{!$Resource.car}"
img3.width = 224;
img3.height = 224;
console.log(img3);
console.log("setup classifier");
var img4;
img4 = new Image();
img4.src = "{!$Resource.car1}"
img4.width = 224;
img4.height = 224;
console.log(img4);
console.log("setup classifier");
console.log("adding example image...");
const ex = classifier.addImage(document.getElementById('imgshow'), "Gorra");
console.log("adding ex image DONE!...", ex);
const ex1 = classifier.addImage(img2, "Gorra");
console.log("adding ex1 image DONE!...", ex1);
const ex2 = classifier.addImage(img3, "car");
console.log("adding ex1 image DONE!...", ex2);
const ex3 = classifier.addImage(img4, "car");
console.log("adding ex1 image DONE!...", ex3);
console.log('claasifier'+classifier);
console.log("Training");
// const trainer ;
setTimeout(function(){ const trainer = classifier.train(); console.log("Training DONE", trainer);}, 30000);
在添加图片后,无论何时train()运行,它都会引发此错误 enter image description here 引用了Mobilnet.js库,我已突出显示了导致该错误的行 enter image description here
请告诉我,我们如何解决这个问题。
答案 0 :(得分:0)
您需要为.train()
函数提供回调。
根据ml5js的文档,.train()
函数的回调不是是可选的。
您可以替换火车声明
const trainer = classifier.train();
带有以下代码。
const trainer = classifier.train(function(lossValue) {
console.log('Loss is', lossValue)
});
这应该可以解决问题。