TensorFlow报错Input to reshape is a tensor with XXX values, but the requested shape ...

完整报错代码:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 1600 values, but the requested shape requires a multiple of 4800

分析+定位+解决: 

分析错误:张量reshape不对,实际输入元素(值)个数与所需矩阵元素个数不一致,所以报错

举个例子(个人理解):

lable = tf.Variable([1,2,3,4,5,6,7,8])
reshape2 = tf.reshape(lable,shape = [2,5])
with tf.Session() as sess:
    print(sess.run(reshape2))

8个元素非要reshape成2行5列10个元素,元素不够就报错了:

ValueError: Cannot reshape a tensor with 8 elements to shape [2,5] (10 elements) for 'Reshape_1' (op: 'Reshape') with input shapes: [8], [2] and with input tensors computed as partial shapes: input[1] = [2,5].


所以解决方法就是跟着报错寻找定位:

最后追溯到代码

image_data = tf.gfile.FastGFile("./1.png", 'rb').read()

decode_image = tf.image.decode_png(image_data,1)

decode_image = tf.image.convert_image_dtype(decode_image, tf.float32)

image = tf.reshape(decode_image, [-1, 20, 80, 3])

很明显问题在哪了,读取图片时

tf.image.decode_png(image_data,1)

不小心将通道取了1(copy过来的,你懂得),改了就行了

 

 

好像之前举的例子不太合适,新增加一个例子:

图片形状为(200px * 100px)

读取图片

image_data = tf.gfile.FastGFile("./test4.jpg", 'rb').read()
decode_image = tf.image.decode_png(image_data,3)

reshape此时如果

image = tf.reshape(decode_image, [-1, 200, 50, 3])

 

第一次读取图片后形成的矩阵元素个数为[200*100*3] 60000个,而我们想改为[200*50*3] 30000个元素,那么就会报错:

 

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 60000 values, but the requested shape requires a multiple of 30000

 

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值