forked from daviddao/spatial-transformer-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_interp.py
66 lines (54 loc) · 2.63 KB
/
example_interp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from scipy import ndimage
import tensorflow as tf
from spatial_transformer import AffineTransformer
import numpy as np
import scipy.misc
# Input image retrieved from:
# https://raw.githubusercontent.com/skaae/transformer_network/master/cat.jpg
im = ndimage.imread('data/cat.jpg')
im = im / 255.
im = im.astype('float32')
# input batch
batch_size = 1
batch = np.expand_dims(im, axis=0)
batch = np.tile(batch, [batch_size, 1, 1, 1])
# input placeholder
x = tf.placeholder(tf.float32, [batch_size, im.shape[0], im.shape[1], im.shape[2]])
# Let the output size of the affine transformer be quarter of the image size.
outsize = (int(im.shape[0]/4), int(im.shape[1]/4))
stl_bilinear = AffineTransformer(outsize, interp_method='bilinear')
stl_bicubic = AffineTransformer(outsize, interp_method='bicubic')
# Identity transform
initial = np.array([[1.0, 0, 0.0], [0, 1.0, 0.0]]).astype('float32')
initial = initial.flatten()
# %% Run session
with tf.Session() as sess:
with tf.device("/cpu:0"):
with tf.variable_scope('spatial_transformer'):
theta = initial + tf.zeros([batch_size, stl_bilinear.param_dim])
result_bilinear = stl_bilinear.transform(x, theta)
result_bicubic = stl_bicubic.transform(x, theta)
result_bilinear_tf = tf.image.resize_bilinear(x, outsize, align_corners=True)
result_bicubic_tf = tf.image.resize_bicubic(x, outsize, align_corners=True)
#result_bilinear_tf = tf.image.resize_bilinear(x, outsize)
#result_bicubic_tf = tf.image.resize_bicubic(x, outsize)
sess.run(tf.global_variables_initializer())
result_bilinear_ = sess.run(result_bilinear, feed_dict={x: batch})
result_bicubic_ = sess.run(result_bicubic, feed_dict={x: batch})
result_bilinear_tf_ = sess.run(result_bilinear_tf, feed_dict={x: batch})
result_bicubic_tf_ = sess.run(result_bicubic_tf, feed_dict={x: batch})
# save our result
scipy.misc.imsave('interp_bilinear_stn.png', result_bilinear_[0])
scipy.misc.imsave('interp_bicubic_stn.png', result_bicubic_[0])
# save tf.image result
scipy.misc.imsave('interp_bilinear_tf.png', result_bilinear_tf_[0])
scipy.misc.imsave('interp_bicubic_tf.png', result_bicubic_tf_[0])
# save differences
diff_bilinear = np.abs(result_bilinear_[0] - result_bilinear_tf_[0])
diff_bilinear = diff_bilinear - np.amin(diff_bilinear)
diff_bilinear = diff_bilinear/(0.0001 + np.amax(diff_bilinear))
scipy.misc.imsave('interp_diff_bilinear.png', diff_bilinear)
diff_bicubic = np.abs(result_bicubic_[0] - result_bicubic_tf_[0])
diff_bicubic = diff_bicubic - np.amin(diff_bicubic)
diff_bicubic = diff_bicubic/(0.0001 + np.amax(diff_bicubic))
scipy.misc.imsave('interp_diff_bicubic.png', diff_bicubic)