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

问题描述

在将一个数组送入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//valueerror-failed-to-convert-a-numpy-array-to-a-tensor-unsupported-object-type
https://stackoverflow.com/questions//tensorflow-valueerror-failed-to-convert-a-numpy-array-to-a-tensor-unsupporte
https://blog.csdn.net/liveshow021_jxb/article/details/

网站名称:解决Tensorflow ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)
URL地址:https://www.cdcxhl.com/article24/dsojcce.html

成都网站建设公司_创新互联,为您提供全网营销推广品牌网站设计用户体验网站导航网站排名企业建站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化