-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgeometric_transformation_module.py
More file actions
187 lines (159 loc) · 6.76 KB
/
geometric_transformation_module.py
File metadata and controls
187 lines (159 loc) · 6.76 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import tensorflow as tf
#import numpy as np
import pdb
"""
--------------------------------------------
Spatial Transformer - based python module
--------------------------------------------
Adapted from:
.. [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
from https://github.com/oarriaga/spatial_transformer_networks/blob/master/src/spatial_transformer.py
"""
def perform_proj_transformation(image_batch, Ht, output_size, mask=None):
##
## Perform the projective warping transformation.
##
aug_images = proj_transform(Ht, image_batch, output_size )
return aug_images
def perform_aff_transformation(image_batch, Ht, output_size, mask=None):
##
## Perform the affine warping transformation.
##
aug_images = aff_transform(Ht, image_batch, output_size)
return aug_images
def _repeat( 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( 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 = _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( 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, [-1])
y_coordinates = tf.reshape(y_coordinates, [-1])
ones = tf.ones_like(x_coordinates)
indices_grid = tf.concat([x_coordinates, y_coordinates, ones], 0)
return indices_grid
def proj_transform(proj_transformation, input_shape, output_size ):
#
# changed to take a projective transform
#
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]
#
proj_transformation = tf.reshape(proj_transformation, shape=(batch_size,3,3))
#
proj_transformation = tf.reshape(proj_transformation, (-1, 3, 3))
proj_transformation = tf.cast(proj_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 = _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, (batch_size, 3, -1))
#
transformed_grid = tf.matmul(proj_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 = _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
def aff_transform(affine_transformation, input_shape, output_size):
#
# original ST affine transformation
#
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 = _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, (batch_size, 3, -1))
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 = _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))
#pdb.set_trace()
return transformed_image