Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate images at different angles randomly in tensorflow

I know that I can rotate images in tensorflow using tf.contrib.image.rotate. But suppose I want to apply the rotation randomly at an angle between -0.3 and 0.3 in radians as follows:

images = tf.contrib.image.rotate(images, tf.random_uniform(shape=[batch_size], minval=-0.3, maxval=0.3, seed=mseed), interpolation='BILINEAR')

So far this will work fine. But the problem arises when the batch size changes on the last iteration and I got an error. So how to fix this code and make it work in all case scenarios? Please note that the inputs images are fed using tf.data.Dataset api.

Any help is much appreciated!!

like image 730
I. A Avatar asked Nov 13 '18 03:11

I. A


People also ask

How do I rotate an image in Tensorflow?

For rotating an image or a batch of images counter-clockwise by multiples of 90 degrees, you can use tf. image. rot90(image,k=1,name=None) . k denotes the number of 90 degrees rotations you want to make.


1 Answers

You can't feed tf.contrib.image.rotate with an angles tensor.

But if you inspect the source code you can see it just makes a bunch of argument validations, and then:

image_height = math_ops.cast(array_ops.shape(images)[1],
                             dtypes.float32)[None]
image_width = math_ops.cast(array_ops.shape(images)[2],
                            dtypes.float32)[None]
output = transform(
    images,
    angles_to_projective_transforms(angles, image_height, image_width),
                                    interpolation=interpolation)

tf.contrib.image.transform() receives a projective transform matrix. tf.contrib.image.angles_to_projective_transforms() generates projective transforms from the rotation angles.

Both accept tensors as arguments, so you can just call the underlying functions.


Here is an example using MNIST

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# load mnist
from tensorflow.examples.tutorials.mnist
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)

# Tensorflow random angle rotation
input_size = mnist.train.images.shape[1]
side_size = int(np.sqrt(input_size))

dataset = tf.placeholder(tf.float32, [None, input_size])
images = tf.reshape(dataset, (-1, side_size, side_size, 1))
random_angles = tf.random.uniform(shape = (tf.shape(images)[0], ), minval = -np
    .pi / 4, maxval = np.pi / 4)

rotated_images = tf.contrib.image.transform(
    images,
    tf.contrib.image.angles_to_projective_transforms(
        random_angles, tf.cast(tf.shape(images)[1], tf.float32), tf.cast(tf
            .shape(images)[2], tf.float32)
    ))

# Run and Print
sess = tf.Session()
result = sess.run(rotated_images, feed_dict = {
    dataset: mnist.train.images,
})

original = np.reshape(mnist.train.images * 255, (-1, side_size, side_size)).astype(
    np.uint8)
rotated = np.reshape(result * 255, (-1, side_size, side_size)).astype(np.uint8)


# Print 10 random samples
fig, axes = plt.subplots(2, 10, figsize = (15, 4.5))
choice = np.random.choice(range(len(mnist.test.labels)), 10)
for k in range(10):
    axes[0][k].set_axis_off()
axes[0][k].imshow(original[choice[k, ]], interpolation = 'nearest', \
    cmap = 'gray')
axes[1][k].set_axis_off()
axes[1][k].imshow(rotated[choice[k, ]], interpolation = 'nearest', \
    cmap = 'gray')

enter image description here

like image 180
xvan Avatar answered Nov 09 '22 10:11

xvan