-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwarp.py
160 lines (148 loc) · 5.16 KB
/
warp.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
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
# Code from https://github.com/Ryo-Ito/spatial_transformer_network
def _repeat(base_indices, n_repeats):
base_indices = tf.matmul(
tf.reshape(base_indices, [-1, 1]),
tf.ones([1, n_repeats], dtype='int32'))
return tf.reshape(base_indices, [-1])
def _interpolate2d(imgs, x, y):
n_batch = tf.shape(imgs)[0]
xlen = tf.shape(imgs)[1]
ylen = tf.shape(imgs)[2]
n_channel = tf.shape(imgs)[3]
#print(xlen, ylen, n_channel)
x = tf.to_float(x)
y = tf.to_float(y)
xlen_f = tf.to_float(xlen)
ylen_f = tf.to_float(ylen)
zero = tf.zeros([], dtype='int32')
max_x = tf.cast(xlen - 1, 'int32')
max_y = tf.cast(ylen - 1, 'int32')
# scale indices from [-1, 1] to [0, xlen/ylen]
x = (x + 1.) * (xlen_f - 1.) * 0.5
y = (y + 1.) * (ylen_f - 1.) * 0.5
# do sampling
x0 = tf.cast(tf.floor(x), 'int32')
x1 = x0 + 1
y0 = tf.cast(tf.floor(y), 'int32')
y1 = y0 + 1
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)
base = _repeat(tf.range(n_batch) * xlen * ylen, ylen * xlen)
base_x0 = base + x0 * ylen
base_x1 = base + x1 * ylen
index00 = base_x0 + y0
index01 = base_x0 + y1
index10 = base_x1 + y0
index11 = base_x1 + y1
# use indices to lookup pixels in the flat image and restore
# n_channel dim
imgs_flat = tf.reshape(imgs, [-1, n_channel])
imgs_flat = tf.to_float(imgs_flat)
I00 = tf.gather(imgs_flat, index00)
I01 = tf.gather(imgs_flat, index01)
I10 = tf.gather(imgs_flat, index10)
I11 = tf.gather(imgs_flat, index11)
# and finally calculate interpolated values
dx = x - tf.to_float(x0)
dy = y - tf.to_float(y0)
w00 = tf.expand_dims((1. - dx) * (1. - dy), 1)
w01 = tf.expand_dims((1. - dx) * dy, 1)
w10 = tf.expand_dims(dx * (1. - dy), 1)
w11 = tf.expand_dims(dx * dy, 1)
output = tf.add_n([w00*I00, w01*I01, w10*I10, w11*I11]) #this is a must to provide gradient!!!!
#output = I00
# reshape
output = tf.reshape(output, [n_batch, xlen, ylen, n_channel])
return output, index00, base, x0, y0
#'''
def batch_warp2d(imgs, mappings):
"""
warp image using mapping function
I(x) -> I(phi(x))
phi: mapping function
Parameters
----------
imgs : tf.Tensor
images to be warped
[n_batch, xlen, ylen, n_channel]
mapping : tf.Tensor
grids representing mapping function
[n_batch, xlen, ylen, 2]
Returns
-------
output : tf.Tensor
warped images
[n_batch, xlen, ylen, n_channel]
"""
n_batch = tf.shape(imgs)[0]
coords = tf.reshape(mappings, [n_batch, 2, -1]) #this seems to be the problem.
x_coords = tf.slice(coords, [0, 0, 0], [-1, 1, -1])
y_coords = tf.slice(coords, [0, 1, 0], [-1, 1, -1])
x_coords_flat = tf.reshape(x_coords, [-1])
y_coords_flat = tf.reshape(y_coords, [-1])
output, index00, base, x0, y0 = _interpolate2d(imgs, x_coords_flat, y_coords_flat)
#return output, index00, base, x0, y0, coords, x_coords, y_coords
return output
def batch_warp2d_2(imgs, mappings_x, mappings_y):
"""
warp image using mapping function
I(x) -> I(phi(x))
phi: mapping function
Parameters
----------
imgs : tf.Tensor
images to be warped
[n_batch, xlen, ylen, n_channel]
mapping : tf.Tensor
grids representing mapping function
[n_batch, xlen, ylen, 2]
Returns
-------
output : tf.Tensor
warped images
[n_batch, xlen, ylen, n_channel]
"""
n_batch = tf.shape(imgs)[0]
#coords = tf.reshape(mappings, [n_batch, 2, -1]) #this seems to be the problem.
#x_coords = tf.slice(coords, [0, 0, 0], [-1, 1, -1])
#y_coords = tf.slice(coords, [0, 1, 0], [-1, 1, -1])
#x_coords_flat = tf.reshape(x_coords, [-1])
#y_coords_flat = tf.reshape(y_coords, [-1])
x_coords = tf.reshape(mappings_x, [-1])
y_coords = tf.reshape(mappings_y, [-1])
output, index00, base, x0, y0 = _interpolate2d(imgs, x_coords, y_coords)
return output
def batch_warp2d_2_test(imgs, mappings_x, mappings_y):
"""
warp image using mapping function
I(x) -> I(phi(x))
phi: mapping function
Parameters
----------
imgs : tf.Tensor
images to be warped
[n_batch, xlen, ylen, n_channel]
mapping : tf.Tensor
grids representing mapping function
[n_batch, xlen, ylen, 2]
Returns
-------
output : tf.Tensor
warped images
[n_batch, xlen, ylen, n_channel]
"""
n_batch = tf.shape(imgs)[0]
#coords = tf.reshape(mappings, [n_batch, 2, -1]) #this seems to be the problem.
#x_coords = tf.slice(coords, [0, 0, 0], [-1, 1, -1])
#y_coords = tf.slice(coords, [0, 1, 0], [-1, 1, -1])
#x_coords_flat = tf.reshape(x_coords, [-1])
#y_coords_flat = tf.reshape(y_coords, [-1])
x_coords = tf.reshape(mappings_x, [-1])
y_coords = tf.reshape(mappings_y, [-1])
output, index00, base, x0, y0 = _interpolate2d(imgs, x_coords, y_coords)
return output, index00, base, x0, y0, x_coords, y_coords