-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
spatial_transformer.py
170 lines (142 loc) · 6.7 KB
/
spatial_transformer.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from keras.layers.core import Layer
import tensorflow as tf
import numpy as np
class SpatialTransformer(Layer):
"""Spatial Transformer Layer
Implements a spatial transformer layer as described in [1]_.
Borrowed from [2]_:
downsample_fator : float
A value of 1 will keep the orignal size of the image.
Values larger than 1 will down sample the image. Values below 1 will
upsample the image.
example image: height= 100, width = 200
downsample_factor = 2
output image will then be 50, 100
References
----------
.. [1] Spatial Transformer Networks
Max Jaderberg, Karen Simonyan, Andrew Zisserman, Koray Kavukcuoglu
Submitted on 5 Jun 2015
.. [2] https://github.com/skaae/transformer_network/blob/master/transformerlayer.py
.. [3] https://github.com/EderSantana/seya/blob/keras1/seya/layers/attention.py
"""
def __init__(self,
localization_net,
output_size,
**kwargs):
self.locnet = localization_net
self.output_size = output_size
super(SpatialTransformer, self).__init__(**kwargs)
def build(self, input_shape):
self.locnet.build(input_shape)
self.trainable_weights = self.locnet.trainable_weights
# self.constraints = self.locnet.constraints
def compute_output_shape(self, input_shape):
output_size = self.output_size
return (None,
int(output_size[0]),
int(output_size[1]),
int(input_shape[-1]))
def call(self, X, mask=None):
affine_transformation = self.locnet.call(X)
output = self._transform(affine_transformation, X, self.output_size)
return output
def _repeat(self, x, num_repeats):
ones = tf.ones((1, num_repeats), dtype='int32')
x = tf.reshape(x, shape=(-1,1))
x = tf.matmul(x, ones)
return tf.reshape(x, [-1])
def _interpolate(self, image, x, y, output_size):
batch_size = tf.shape(image)[0]
height = tf.shape(image)[1]
width = tf.shape(image)[2]
num_channels = tf.shape(image)[3]
x = tf.cast(x , dtype='float32')
y = tf.cast(y , dtype='float32')
height_float = tf.cast(height, dtype='float32')
width_float = tf.cast(width, dtype='float32')
output_height = output_size[0]
output_width = output_size[1]
x = .5*(x + 1.0)*(width_float)
y = .5*(y + 1.0)*(height_float)
x0 = tf.cast(tf.floor(x), 'int32')
x1 = x0 + 1
y0 = tf.cast(tf.floor(y), 'int32')
y1 = y0 + 1
max_y = tf.cast(height - 1, dtype='int32')
max_x = tf.cast(width - 1, dtype='int32')
zero = tf.zeros([], dtype='int32')
x0 = tf.clip_by_value(x0, zero, max_x)
x1 = tf.clip_by_value(x1, zero, max_x)
y0 = tf.clip_by_value(y0, zero, max_y)
y1 = tf.clip_by_value(y1, zero, max_y)
flat_image_dimensions = width*height
pixels_batch = tf.range(batch_size)*flat_image_dimensions
flat_output_dimensions = output_height*output_width
base = self._repeat(pixels_batch, flat_output_dimensions)
base_y0 = base + y0*width
base_y1 = base + y1*width
indices_a = base_y0 + x0
indices_b = base_y1 + x0
indices_c = base_y0 + x1
indices_d = base_y1 + x1
flat_image = tf.reshape(image, shape=(-1, num_channels))
flat_image = tf.cast(flat_image, dtype='float32')
pixel_values_a = tf.gather(flat_image, indices_a)
pixel_values_b = tf.gather(flat_image, indices_b)
pixel_values_c = tf.gather(flat_image, indices_c)
pixel_values_d = tf.gather(flat_image, indices_d)
x0 = tf.cast(x0, 'float32')
x1 = tf.cast(x1, 'float32')
y0 = tf.cast(y0, 'float32')
y1 = tf.cast(y1, 'float32')
area_a = tf.expand_dims(((x1 - x) * (y1 - y)), 1)
area_b = tf.expand_dims(((x1 - x) * (y - y0)), 1)
area_c = tf.expand_dims(((x - x0) * (y1 - y)), 1)
area_d = tf.expand_dims(((x - x0) * (y - y0)), 1)
output = tf.add_n([area_a*pixel_values_a,
area_b*pixel_values_b,
area_c*pixel_values_c,
area_d*pixel_values_d])
return output
def _meshgrid(self, height, width):
x_linspace = tf.linspace(-1., 1., width)
y_linspace = tf.linspace(-1., 1., height)
x_coordinates, y_coordinates = tf.meshgrid(x_linspace, y_linspace)
x_coordinates = tf.reshape(x_coordinates, shape=(1, -1))
y_coordinates = tf.reshape(y_coordinates, shape=(1, -1))
ones = tf.ones_like(x_coordinates)
indices_grid = tf.concat([x_coordinates, y_coordinates, ones], 0)
return indices_grid
def _transform(self, affine_transformation, input_shape, output_size):
batch_size = tf.shape(input_shape)[0]
height = tf.shape(input_shape)[1]
width = tf.shape(input_shape)[2]
num_channels = tf.shape(input_shape)[3]
affine_transformation = tf.reshape(affine_transformation, shape=(batch_size,2,3))
affine_transformation = tf.reshape(affine_transformation, (-1, 2, 3))
affine_transformation = tf.cast(affine_transformation, 'float32')
width = tf.cast(width, dtype='float32')
height = tf.cast(height, dtype='float32')
output_height = output_size[0]
output_width = output_size[1]
indices_grid = self._meshgrid(output_height, output_width)
indices_grid = tf.expand_dims(indices_grid, 0)
indices_grid = tf.reshape(indices_grid, [-1]) # flatten?
indices_grid = tf.tile(indices_grid, tf.stack([batch_size]))
indices_grid = tf.reshape(indices_grid, tf.stack([batch_size, 3, -1]))
# transformed_grid = tf.batch_matmul(affine_transformation, indices_grid)
transformed_grid = tf.matmul(affine_transformation, indices_grid)
x_s = tf.slice(transformed_grid, [0, 0, 0], [-1, 1, -1])
y_s = tf.slice(transformed_grid, [0, 1, 0], [-1, 1, -1])
x_s_flatten = tf.reshape(x_s, [-1])
y_s_flatten = tf.reshape(y_s, [-1])
transformed_image = self._interpolate(input_shape,
x_s_flatten,
y_s_flatten,
output_size)
transformed_image = tf.reshape(transformed_image, shape=(batch_size,
output_height,
output_width,
num_channels))
return transformed_image