numpy - 了解 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);
解决方案
您在这里混合了两个不同的概念,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]。
推荐阅读
- kubernetes - 最小活动/备用服务数
- python - 如何在python中以最佳方式使用多线程和多处理?
- javascript - 使用 Redux 和 Hooks 将函数作为参数传递
- validation - Grails 4 hasone 没有正确验证
- unity3d - 保存技能树
- linux - 如何从 Bash 中的字符串计算上个月和上年
- c# - UWP WebView - OnScriptNotify 失败的侦听器事件
- python - Pandas - make new column with mean values of the part of another column
- java - How to get rid of white spaces in the list of an array?
- ruby-on-rails - FactoryBot on model with unique validation ... can't rerun tests