解决Tensorflow ValueError: Failed to convert a NumPy array to a Tensor | Unsupported object type nump

解决Tensorflow ValueError: Failed to convert a NumPy array to a Tensor  | Unsupported object type nump

问题描述

在将一个数组送入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

解决方法

stackoverflow上有许多类似的报错,大概意思都是数据类型错误转换的数据类型非报错中括号里的数据类型,如:
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

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部