Press "Enter" to skip to content

解决Tensorflow ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object …

本站内容均来自兴趣收集,如不慎侵害的您的相关权益,请留言告知,我们将尽快删除.谢谢.

问题描述

 

在将一个数组送入tensorflow训练时,报错如下:

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

数组元素为数组,每个数组元素的shape不一致,示例如下:

 

cropImg[0].shape = (13, 13, 3)
cropImg[1].shape = (14, 13, 3)
cropImg[2].shape = (12, 13, 3)

 

环境

 

python 3.7.9

 

tensorflow 2.6.0

 

keras 2.6.0

 

解决方法

 

stackoverlow上有许多类似的报错,大概意思都是数据类型错误,转换的数据类型非
报错中括号里的数据类型,如:

 

Unsupported object type numpy.ndarray
指cropImg数组元素不是numpy.ndarray类型。

 

博主非常不解,尝试了许多方法,都显示cropImg数组元素数据类型为numpy.ndarray,但错误一直存在。

 

后来突然转念,在生成cropImg数组时,有一个warning:

 

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  cropImg_ar = np.array(img_list)

 

cropImg数组元素为shape不一致的数组,这说明cropImg数组元素类型实际上为object
,会不会是tensorflow不接受object类型的数据导致的?

 

将cropImg数组元素转换为shape一致后,问题解决。

 

参考链接

 

https://stackoverflow.com/questions/62570936/valueerror-failed-to-convert-a-numpy-array-to-a-tensor-unsupported-object-type

 

https://stackoverflow.com/questions/58636087/tensorflow-valueerror-failed-to-convert-a-numpy-array-to-a-tensor-unsupporte

https://blog.csdn.net/liveshow021_jxb/article/details/112752145

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注