Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TF.js在浏览器中以model.json格式加载对象检测模型_Javascript_Tensorflow_Tensorflow.js_Object Detection Api - Fatal编程技术网

Javascript TF.js在浏览器中以model.json格式加载对象检测模型

Javascript TF.js在浏览器中以model.json格式加载对象检测模型,javascript,tensorflow,tensorflow.js,object-detection-api,Javascript,Tensorflow,Tensorflow.js,Object Detection Api,我使用Colab在python中训练了一个.pb对象检测模型,并使用。我需要在浏览器中加载这个模型(没有Node.js!),并在那里运行推理。 这是生成的“我的模型”文件夹的结构: model | - model.json | - labels.json | - group1-shard1of2.bin | - group1-shard2of2.bin 建议以下内容加载此类模型: const model = await tf.loadGraphModel('model/model.json')

我使用Colab在python中训练了一个.pb对象检测模型,并使用。我需要在浏览器中加载这个模型(没有Node.js!),并在那里运行推理。 这是生成的“我的模型”文件夹的结构:

model
| - model.json
| - labels.json
| - group1-shard1of2.bin
| - group1-shard2of2.bin
建议以下内容加载此类模型:

const model = await tf.loadGraphModel('model/model.json');

我正在使用
tf.loadGraphModel
函数。加载模型可以完美地工作,但当我尝试使用以下代码对其进行推理时:

// detect objects in the image.
const img = document.getElementById('img'); 
model.predict(img).then(predictions => {
    console.log('Predictions: ');
    console.log(predictions);
});
const tensor = tf.browser.fromPixels(img);
它抛出以下错误:

Uncaught (in promise) Error: The dict provided in model.execute(dict) has keys [...] that are not part of graph
at e.t.checkInputs (graph_executor.js:607)
at e.t.execute (graph_executor.js:193)
at e.t.execute (graph_model.js:338)
at e.t.predict (graph_model.js:291)
at predictImages (detector.php:39)
我是否使用了错误的加载函数,模型加载过程是否失败(即使它没有抛出任何错误?)或者推理函数是否错误? 提前感谢您的支持

编辑:在使用@edkeveke的建议将图像转换为张量后,首先使用以下代码:

// detect objects in the image.
const img = document.getElementById('img'); 
model.predict(img).then(predictions => {
    console.log('Predictions: ');
    console.log(predictions);
});
const tensor = tf.browser.fromPixels(img);
并使用以下公式运行推理:

model.predict(tensor.expandDims(0));
我收到了以下错误消息:

Uncaught (in promise) Error: This execution contains the node 'Preprocessor/map/while/Exit_2', which has the dynamic op 'Exit'. Please use model.executeAsync() instead. Alternatively, to avoid the dynamic ops, specify the inputs [Preprocessor/map/TensorArrayStack/TensorArrayGatherV3]
    at e.t.compile (graph_executor.js:162)
    at e.t.execute (graph_executor.js:212)
    at e.t.execute (graph_model.js:338)
    at e.t.predict (graph_model.js:291)
    at predictImages (detector.php:38)
model.predict()
替换为
model.executeAsync()
后,它返回的结果与我从对象检测模型得到的结果不同:

detector.php:40 (2) [e, e]0: e {kept: false, isDisposedInternal: false, shape: Array(3), dtype: "float32", size: 3834, …}1: e {kept: false, isDisposedInternal: false, shape: Array(4), dtype: "float32", size: 7668, …}length: 2__proto__: Array(0)
这是我目前为止的完整代码(使用PHP以HTML添加的图像):


异步函数predictImages(){//async
日志(“加载模型”);
//加载模型。
const model=await tf.loadGraphModel('model/model.json');
log(“模型加载”);
//预测所有图像
for(设i=0;i{
log('Predictions:');
控制台日志(预测);
});
}否则{
打破
}
}
}
预测图像();

模型。predict
需要一个张量,但它被赋予了一个HTMLImageElement。首先,需要从HTMLImageElement构造一个张量

const tensor = tf.browser.fromPixels(img)
然后,张量可以作为
模型的参数。预测

model.predict(tensor) // returns a 3d
最后但并非最不重要的是确保张量形状是模型所期望的形状(3d或4d)。如果模型期望4d,那么它应该是

model.predict(tensor.expandDims(0))

按照您的建议加载图像的工作是完美的,但是当我使用我在问题中添加的代码运行预测时,它返回以下内容:
detector.php:40(2)[e,e]0:e{keep:false,isDisposedInternal:false,shape:Array(3),dtype:“float32”,size:3834,}1:e{keep:false,isDisposedInternal:false,shape:Array(4),dtype:“float32”,size:7668,…}length:2__-proto__;u:Array(0)
我的代码有什么问题?代码没有问题<代码>预测
是张量数组。如果你想得到它的值,你需要做
预测[index].dataSync()
,索引为0或1,这取决于模型的输出。但是,如果希望返回数据保持与张量相同的形状,可以使用
arraySync
而不是
dataSync
,我认为这是另一个问题。您可以关闭此选项并打开一个新问题,显示您当前拥有的输出。您可以向上投票此答案并将其标记为已接受,因为它解决了您最初关于从图像预测的问题:)