完整报错代码:
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