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')