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

sun*_*tch 5 tensorflow tensorflow.js

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

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);
    }
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它在控制台上出现此错误:

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
Run Code Online (Sandbox Code Playgroud)

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

编辑

好吧,我使用的是executeAsync现在作为@Jason梅斯说。但它返回了一些这样的值:

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
Run Code Online (Sandbox Code Playgroud)

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

小智 9

输出的长度是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
Run Code Online (Sandbox Code Playgroud)

然后您可以通过以下方式获取预测;

const boxes = output[0].dataSync()
const scores = output[1].arraySync()
const classes = output[2].dataSync()
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过执行此操作使用所有预测的边界框构建一个预测对象;

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
  }
Run Code Online (Sandbox Code Playgroud)

classesDir 是一本包含培训课程的字典;

let classesDir = {
    1: {
        name: 'Class name 1',
        id: 1,
    },
    2: {
        name: 'Class name 2',
        id: 2,
    }
}
Run Code Online (Sandbox Code Playgroud)

预测对象将是一个包含对象的数组;

[{
  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
}]
Run Code Online (Sandbox Code Playgroud)