首页 > 解决方案 > 了解 Tensorflow 对象检测模型的 INDArray 维度重塑

问题描述

尝试将 Tensorflow 训练的模型加载到 Deeplearning4J 中,但出现以下错误:

IllegalStateException: Invalid array shape: cannot associate an array with shape [38880] with a placeholder of shape [-1, -1, -1, 3]:shape is wrong rank or does not match on one or more dimensions
var arr: INDArray = Nd4j.create(data) //.reshape(1, -1, -1, 3);
arr = Nd4j.pile(arr, arr)
sd.associateArrayWithVariable(arr, sd.variables.get(0))

Python模型是这样加载的:

# Load image using OpenCV and
# expand image dimensions to have shape: [1, None, None, 3]
# i.e. a single-column array, where each item in the column has the pixel RGB value
image = cv2.imread(PATH_TO_IMAGE)
image_expanded = np.expand_dims(image, axis=0)

如果您知道,请解释任何问题:

1) 就 Python 数组而言 [1, None, None, 3] 是什么意思

2) Python 中的 np.expand_dims(image, axis=0) 是什么意思

3) Deeplearning4J 重塑(1, -1, -1, 3);

标签: numpytensorflowreshapedeeplearning4jfaster-rcnn

解决方案


您在这里混合了两个不同的概念,TF 占位符和命令式 numpy-like reshape。

在您的情况下,模型需要 4D 输入张量,形状为 [-1, -1, -1, 3]。对于人类,它可以翻译为 [Any, Any, Any, 3]。但是你试图用形状为 [38880] 的张量来喂它,排名 1。

现在回答你的问题。

1) 见上文。-1 被视为“任何”。

2)这个函数增加1作为维度。即如果你有 [38880],axis=0 处的 expand_dims 将使其变为 [1, 38880]

3) 不,那是错误的。你不应该使用它作为你的形状。您那里有一些图像,因此您应该指定图像具有的适当尺寸,即 [1, 800, 600, 3]。


推荐阅读