图片使用resize_image_with_crop_or_pad方法报错问题

在学习tensorflow的过程中遇到一个图片预处理的问题:

具体代码如下:

image_raw_data = tf.gfile.FastGFile("./dog.jpeg",'rb').read()


with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)
    print (img_data.get_shape())
    # 输出解码之后的三维矩阵。
    #print(img_data.eval())
    img_data.set_shape([560, 735, 3])
    print (img_data)
    ?img_data.set_shape
    print(img_data.get_shape())
    
with tf.Session() as sess:
    croped = tf.image.resize_image_with_crop_or_pad(img_data,1000,1000)
    padded = tf.image.resize_image_with_crop_or_pad(img_data,3000,3000)
    plt.imshow(croped.eval())
    plt.show()
    plt.imshow(padded.eval())
    plt.show()

该段程序在ipython中运行报如下错误:

InvalidArgumentError: Expected size[1] in [0, 551], but got 600
	 [[Node: resize_image_with_crop_or_pad_44/crop_to_bounding_box/Slice = Slice[Index=DT_INT32, T=DT_UINT8, _device="/job:localhost/replica:0/task:0/device:CPU:0"](resize_image_with_crop_or_pad_44/ExpandDims, resize_image_with_crop_or_pad_44/crop_to_bounding_box/stack, resize_image_with_crop_or_pad_44/crop_to_bounding_box/stack_1)]]

上述报错英文暂时不太能看明白,但是经过自己的测试找到了原因,

上述的图片实际像素为551X735的大小。在这里我们是设置成了560X735,这个时候在对图片数据调用resize_image_with_crop_or_pad(img_data,1000,1000)则会报错,将上述img_data.set_shape([560, 735, 3])改成img_data.set_shape([551, 735, 3])(即大小在图片实际大小范围之内),则报错解除。




你可能感兴趣的:(tensor,flow使用问题,tensorflow)