tensorflow: how to rotate an image for data augmentation?

后端 未结 7 2046
遇见更好的自我
遇见更好的自我 2020-12-09 05:27

In tensorflow, I would like to rotate an image from a random angle, for data augmentation. But I don\'t find this transformation in the tf.image module.

7条回答
  •  被撕碎了的回忆
    2020-12-09 05:43

    Update: see @astromme's answer below. Tensorflow now supports rotating images natively.

    What you can do while there is no native method in tensorflow is something like this:

    from PIL import Image
    sess = tf.InteractiveSession()
    
    # Pass image tensor object to a PIL image
    image = Image.fromarray(image.eval())
    
    # Use PIL or other library of the sort to rotate
    rotated = Image.Image.rotate(image, degrees)
    
    # Convert rotated image back to tensor
    rotated_tensor = tf.convert_to_tensor(np.array(rotated))
    

提交回复
热议问题