gpt4 book ai didi

tensorflow - 不能使用 Tensorflow.js predict() 函数

转载 作者:行者123 更新时间:2023-12-03 19:42:53 33 4
gpt4 key购买 nike

我已经训练了我自己的图形模型。我想在浏览器上使用它。这是我的代码:

async function predict() {
const model = await tf.loadGraphModel('./model/model.json');
let img = document.getElementById('test');
var example = tf.browser.fromPixels(img);
example = example.expandDims(0);
const output = await model.predict(example).data();
console.log(output);
}

当我运行它时,它在控制台上出现此错误:
Uncaught (in promise) Error: This execution contains the node 'SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/Exit_4', which has the dynamic op 'Exit'. Please use model.executeAsync() instead. Alternatively, to avoid the dynamic ops, specify the inputs [SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/TensorArrayStack_2/TensorArrayGatherV3]
at t.compile (tfjs:2)
at t.execute (tfjs:2)
at t.execute (tfjs:2)
at predict ((index):85)
at /websites/optik2/async http://localhost/websites/optik2/:96

我需要 predict()功能, executeAsync()不是那么好。

编辑

好的,我正在使用 executeAsync现在为 @Jason Mayes说。但它返回了一些这样的值:
t {kept: false, isDisposedInternal: false, shape: Array(3), dtype: "float32", size: 1200, …}
rank: 3
isDisposed: false
kept: false
isDisposedInternal: false
shape: (3) [1, 300, 4]
dtype: "float32"
size: 1200
strides: (2) [1200, 4]
dataId: {}
id: 2198
rankType: "3"
scopeId: 3545
__proto__: Object

我怎样才能得到这个的边界框?

最佳答案

const output = await model.executeAsync(data)的输出长度是多少? ?

您应该在 output 中寻找这些形状。 ;

output[X] = detection_boxes   // shape: [1, x, 4]  x: number of bounding boxes
output[Y] = detection_scores // shape: [1, x] x: number of scores
output[Z] = detection_classes // shape: [1, x] x: number of classes

然后您可以通过以下方式获取预测;
const boxes = output[0].dataSync()
const scores = output[1].arraySync()
const classes = output[2].dataSync()

然后,您可以通过执行此操作使用所有预测的边界框构建一个预测对象;
buildDetectedObjects(scores, threshold, imageWidth, imageHeight, boxes, classes, classesDir) {
const detectionObjects = []
scores.forEach((score, i) => {
if (score > threshold) {
const bbox = [];
const minY = boxes[i * 4] * imageHeight;
const minX = boxes[i * 4 + 1] * imageWidth;
const maxY = boxes[i * 4 + 2] * imageHeight;
const maxX = boxes[i * 4 + 3] * imageWidth;
bbox[0] = minX;
bbox[1] = minY;
bbox[2] = maxX - minX;
bbox[3] = maxY - minY;

detectionObjects.push({
class: classes[i],
label: classesDir[classes[i]].name,
score: score.toFixed(4),
bbox: bbox
})
}
})

return detectionObjects
}
classesDir是一本包含培训类(class)的字典;
let classesDir = {
1: {
name: 'Class name 1',
id: 1,
},
2: {
name: 'Class name 2',
id: 2,
}
}

预测对象将是一个包含对象的数组;
[{
bbox:[x,y,width,height],
class: X,
label: class name,
score: 0.XYZ
},
{
bbox:[x,y,width,height],
class: X,
label: class name,
score: 0.XYZ
}]

关于tensorflow - 不能使用 Tensorflow.js predict() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60516751/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com