concat1D 错误:张量 [23] 的等级必须与其余等级相同

Error in concat1D: rank of tensors[23] must be the same as the rank of the rest

张量流 1.12.0

我用我的数据集训练了 SSD_Mobilenet_V1_pnp 模型(使用 COCO 预训练)。

在 phyton 上工作 - 很好地检测物体

转换为 js。

在浏览器中 运行 出现错误:

tfjs@latest:2 Uncaught (in promise) Error: Error in concat1D: rank of tensors[23] must be the same as the rank of the rest (1)

    at assert (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest:2:5660)

    at https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest:2:60833

    at Array.forEach (native)

    at assertParamsConsistent (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest:2:60811)

    at concat_ (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest:2:223560)

    at concat (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest:2:68004)

    at executeOp (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest:2:800253)

    at executeOp (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest:2:804261)

    at p (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest:2:811525)

    at e.processStack (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest:2:811935)

我做错了什么?

我在 tfjs-node 中遇到了同样的问题,这个错误可能需要你遵循 https://github.com/tensorflow/tfjs-models/tree/master/coco-ssd#technical-details-for-advanced-users

注意:所有示例均使用 TypeScript,但很容易翻译成 JavaScript。

我详细介绍下面的 3 个步骤

1。删除后处理步骤

您需要从导出的模型中删除 post 个处理步骤

tensorflowjs_converter --input_format=tf_saved_model \
                   --output_node_names='Postprocessor/ExpandDims_1,Postprocessor/Slice' \
                   --saved_model_tags=serve \
                   ./saved_model \
                   ./web_model

这应该可以解决您的 rank of tensors[23] must be the same as the rank of the rest (1) 错误。

但是现在您的模型中没有任何 post 处理,因此您需要直接在 javascript 代码中进行处理。

2。使用单个 class NonMaxSuppression

想法是只保留 "best" 个框,并在 js

中进行过滤

首先,您需要找到 maxScores,请参见示例代码 here

然后您将需要使用 NonMaxSuppression 删除所有非最大分数。

example code and documentation

3。在 CPU 后端

上执行 NonMaxSuppression 操作

您将在 here and here using setBackend 函数中获得示例

注意:为了释放内存,您需要在更改后端之前tf.dispose and tf.Tensor.dispose张量