Is the input of `tf.image.resize_images` must have static shape?

前端 未结 3 1784
一整个雨季
一整个雨季 2021-01-24 03:51

I run the code below, it raises an ValueError: \'images\' contains no shape. Therefore I have to add the line behind # to set the static shape, but

3条回答
  •  时光说笑
    2021-01-24 04:10

    The issue here actually comes from the fact that tf.image.decode_image doesn't return the shape of the image. This was explained in these two GitHub issues: issue1, issue2.

    The problem comes from the fact that tf.image.decode_image also handles .gif, which returns a 4D tensor, whereas .jpg and .png return 3D images. Therefore, the correct shape cannot be returned.


    The solution is to simply use tf.image.decode_jpeg or tf.image.decode_png (both work the same and can be used on .png and .jpg images).

    def _decode_image(filename):
        image_string = tf.read_file(filename)
        image_decoded = tf.image.decode_jpeg(image_string, channels=3)
        image = tf.cast(image_decoded, tf.float32)
        image_resized = tf.image.resize_images(image, [224, 224])
    
        return image_resized
    

提交回复
热议问题