This repository was archived by the owner on Nov 30, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththeano_funcs.py
executable file
·250 lines (182 loc) · 7.27 KB
/
theano_funcs.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
import numpy as np
import theano
import theano.tensor as T
from Lasagne.lasagne.updates import nesterov_momentum
from Lasagne.lasagne.layers import get_all_params
from Lasagne.lasagne.layers import get_output
def create_localization_train_func(layers, lr=0.01, mntm=0.9):
X = T.tensor4('X')
X_batch = T.tensor4('X_batch')
Y = T.tensor4('Y')
Y_batch = T.tensor4('Y_batch')
Z = T.btensor4('Z')
Z_batch = T.btensor4('Z_batch')
_, X_hat = get_output([layers['loc'], layers['trans']], X, deterministic=False)
# compute the loss between the transformed image and the given target
# train_loss = T.mean(
# T.mean(T.sqr(X_hat - Y), axis=1)
# )
# train_loss = T.mean(T.sqr(X_hat - Y) * Z)
train_loss = T.sum(T.sqr(X_hat - Y) * Z) / Z.sum()
params = get_all_params([layers['loc'], layers['trans']], trainable=True)
updates = nesterov_momentum(train_loss, params, lr, mntm)
train_func = theano.function(
inputs=[theano.In(X_batch), theano.In(Y_batch), theano.In(Z_batch)],
outputs=[train_loss, X_hat],
updates=updates,
givens={X: X_batch, Y: Y_batch, Z: Z_batch,},
)
return train_func
def create_localization_valid_func(layers):
X = T.tensor4('X')
X_batch = T.tensor4('X_batch')
Y = T.tensor4('Y')
Y_batch = T.tensor4('Y_batch')
Z = T.btensor4('Z')
Z_batch = T.btensor4('Z_batch')
_, X_hat = get_output([layers['loc'], layers['trans']], X, deterministic=True)
# compute the loss between the transformed image and the given target
# valid_loss = T.mean(
# T.mean(T.sqr(X_hat - Y), axis=1)
# )
# valid_loss = T.mean(T.sqr(X_hat - Y) * Z)
valid_loss = T.sum(T.sqr(X_hat - Y) * Z) / Z.sum()
valid_func = theano.function(
inputs=[theano.In(X_batch), theano.In(Y_batch), theano.In(Z_batch)],
outputs=[valid_loss, X_hat],
updates=None,
givens={X: X_batch, Y: Y_batch, Z: Z_batch,},
)
return valid_func
def create_localization_infer_func(layers):
X = T.tensor4('X')
X_batch = T.tensor4('X_batch')
M, X_hat = get_output([layers['loc'], layers['trans']], X, deterministic=True)
infer_func = theano.function(
inputs=[theano.In(X_batch)],
outputs=[M, X_hat],
updates=None,
givens={X: X_batch,},
)
return infer_func
def create_localization_test_func(layers):
X_full = T.tensor4('X_full')
X_full_batch = T.tensor4('X_full_batch')
X_down = T.tensor4('X_down')
X_down_batch = T.tensor4('X_down_batch')
X_hat_down, X_hat_full = get_output(
[layers['trans'], layers['trans_full']],
inputs={layers['in']: X_down, layers['in_full']: X_full},
deterministic=True,
)
test_func = theano.function(
inputs=[theano.In(X_down_batch), theano.In(X_full_batch)],
outputs=[X_hat_down, X_hat_full],
givens={X_down: X_down_batch, X_full: X_full_batch,},
)
return test_func
def create_segmentation_train_func(layers, lr=0.01, mntm=0.9):
X = T.tensor4('X')
X_batch = T.tensor4('X_batch')
Y = T.tensor4('Y')
Y_batch = T.tensor4('Y_batch')
X_hat = get_output(layers['seg_out'], X, deterministic=False)
# train_loss = T.mean(
# T.mean(T.sqr(X_hat - Y), axis=1)
# )
train_loss = T.mean(T.nnet.binary_crossentropy(T.clip(X_hat, 1e-15, 1 - 1e-15), Y))
params = get_all_params(layers['seg_out'], trainable=True)
updates = nesterov_momentum(train_loss, params, lr, mntm)
train_func = theano.function(
inputs=[theano.In(X_batch), theano.In(Y_batch)],
outputs=[train_loss, X_hat],
updates=updates,
givens={X: X_batch, Y: Y_batch,},
)
return train_func
def create_segmentation_valid_func(layers):
X = T.tensor4('X')
X_batch = T.tensor4('X_batch')
Y = T.tensor4('Y')
Y_batch = T.tensor4('Y_batch')
X_hat = get_output(layers['seg_out'], X, deterministic=True)
# valid_loss = T.mean(
# T.mean(T.sqr(X_hat - Y), axis=1)
# )
valid_loss = T.mean(T.nnet.binary_crossentropy(T.clip(X_hat, 1e-15, 1 - 1e-15), Y))
valid_func = theano.function(
inputs=[theano.In(X_batch), theano.In(Y_batch)],
outputs=[valid_loss, X_hat],
updates=None,
givens={X: X_batch, Y: Y_batch,},
)
return valid_func
def create_segmentation_infer_func(layers):
X = T.tensor4('X')
X_batch = T.tensor4('X_batch')
X_hat = get_output(layers['seg_out'], X, deterministic=True)
infer_func = theano.function(
inputs=[theano.In(X_batch)], outputs=X_hat, updates=None, givens={X: X_batch,},
)
return infer_func
def create_segmentation_func(layers):
X = T.tensor4('X')
X_batch = T.tensor4('X_batch')
# final segmentation
S = get_output(layers['seg_out'], X, deterministic=True)
infer_func = theano.function(
inputs=[theano.In(X_batch)], outputs=S, updates=None, givens={X: X_batch,},
)
return infer_func
def test_localization_funcs():
from wbia_curvrank import localization
print('testing localization')
print(' building model')
layers = localization.build_model((None, 3, 256, 256), downsample=2)
print(' compiling training function')
loc_train_func = create_localization_train_func(layers)
print(' compiling validation function')
loc_valid_func = create_localization_valid_func(layers)
print(' compiling inference function')
loc_infer_func = create_localization_infer_func(layers)
X = np.random.random((16, 3, 256, 256)).astype(np.float32)
Y = np.random.random((16, 3, 128, 128)).astype(np.float32)
Z = np.random.randint(0, 2, (16, 3, 128, 128)).astype(np.int32)
print(' forward/backward pass')
train_loss, _ = loc_train_func(X, Y, Z)
print(' train loss = %.6f' % (train_loss))
print(' forward pass with loss')
valid_loss, _ = loc_valid_func(X, Y, Z)
print(' valid loss = %.6f' % (valid_loss))
print(' forward pass without loss')
M, X_hat = loc_infer_func(X)
print('M.shape = %r, X_hat.shape = %r' % (M.shape, X_hat.shape))
print('done testing localization')
def test_segmentation_funcs():
from wbia_curvrank import segmentation
print('testing segmentation')
print(' building model')
layers = segmentation.build_model((None, 3, 128, 128))
print(' compiling training function')
seg_train_func = create_segmentation_train_func(layers)
print(' compiling validation function')
seg_valid_func = create_segmentation_valid_func(layers)
print(' compiling inference function')
seg_infer_func = create_segmentation_infer_func(layers)
X = np.random.random((16, 3, 128, 128)).astype(np.float32)
Y = np.random.random((16, 3, 128, 128)).astype(np.float32)
print(' forward/backward pass')
train_loss, _ = seg_train_func(X, Y)
print(' train loss = %.6f' % (train_loss))
print(' forward pass with loss')
valid_loss, _ = seg_valid_func(X, Y)
print(' valid loss = %.6f' % (valid_loss))
print(' forward pass without loss')
X_hat = seg_infer_func(X)
print('X_hat.shape = %r' % (X_hat.shape,))
print('done testing segmentation')
if __name__ == '__main__':
test_localization_funcs()
test_segmentation_funcs()