-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtrain_vox.py
241 lines (184 loc) · 7.11 KB
/
train_vox.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
import cPickle as pickle
import matplotlib.pyplot as plt
import numpy as np
import h5py
import climin.stops
from climin import mathadapt as ma
from breze.learn.trainer import report
import ash
from ash import PocketTrainer
from conv3d.model import FCN
vis = False
params = False
stacked_filters = False
d_code = '96fcnmini'
param_file = 'params/params' + d_code + '.pkl'
f = h5py.File('data/data'+d_code+'.hdf5', 'r')
train_x = f['train_x']
train_y = f['train_y']
valid_x = f['valid_x']
valid_y = f['valid_y']
test_x = f['test_x']
test_y = f['test_y']
n_classes = 2
if d_code == '48fcnmini' or d_code == '48fcn' and stacked_filters:
nkerns_d = [16, 16, 32, 32, 64, 64]
fs_d = [(2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2)]
ps_d = ['no_pool', (2, 2, 2), 'no_pool', (2, 2, 2), 'no_pool', (2, 2, 2)]
strides_d = (1, 1, 1)
nkerns_u = [128, 128, 64, 64, 32, 32, 16, n_classes]
fs_u = [(2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (1, 1, 1)]
ps_u = ['no_pool', (2, 2, 2), 'no_pool', (2, 2, 2), 'no_pool', (2, 2, 2), 'no_pool', 'no_pool']
hidden_transfers_conv = ['rectifier']*6
hidden_transfers_upconv = ['rectifier']*7 + ['identity']
bm_down = ['same']*6
bm_up = ['same']*7 + ['valid']
loss_id = ash.fcn_cat_ce
padding = 0
elif d_code.startswith('48fcn'):
nkerns_d = [2, 16, 32]
fs_d = [(3, 3, 3), (3, 3, 3), (3, 3, 3)]
ps_d = [(2, 2, 2), (2, 2, 2), (2, 2, 2)]
strides_d = (1, 1, 1)
nkerns_u = [32, 32, 16, n_classes]
fs_u = [(3, 3, 3), (3, 3, 3), (3, 3, 3), (3, 3, 3)]
ps_u = [(2, 2, 2), (2, 2, 2), (2, 2, 2), 'no_pool']
hidden_transfers_conv = ['rectifier', 'rectifier', 'rectifier']
hidden_transfers_upconv = ['rectifier', 'rectifier', 'rectifier', 'identity']
bm_down = ['same', 'same', 'same']
bm_up = ['same', 'same', 'same', 'same']
loss_id = ash.fcn_cat_ce
padding = 0
elif d_code.startswith('9680fcn') and stacked_filters:
nkerns_d = [16, 16, 32, 32, 64, 64]
fs_d = [(2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2)]
ps_d = ['no_pool', (2, 2, 2), 'no_pool', (2, 2, 2), 'no_pool', (2, 2, 2)]
strides_d = (1, 1, 1)
nkerns_u = [128, 128, 64, 64, 32, 32, 16, n_classes]
fs_u = [(2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (2, 2, 2), (1, 1, 1)]
ps_u = ['no_pool', (2, 2, 2), 'no_pool', (2, 2, 2), 'no_pool', (2, 2, 2), 'no_pool', 'no_pool']
hidden_transfers_conv = ['rectifier']*6
hidden_transfers_upconv = ['rectifier']*7 + ['identity']
bm_down = ['same']*6
bm_up = ['valid']*2 + ['same']*5 + ['valid']
loss_id = ash.fcn_cat_ce
padding = 16
elif d_code.startswith('9680fcn'):
nkerns_d = [8, 16, 32]
fs_d = [(3, 3, 3), (3, 3, 3), (3, 3, 3)]
ps_d = [(2, 2, 2), (2, 2, 2), (2, 2, 2)]
strides_d = (1, 1, 1)
nkerns_u = [32, 32, 16, n_classes]
fs_u = [(3, 3, 3), (3, 3, 3), (3, 3, 3), (3, 3, 3)]
ps_u = [(2, 2, 2), (2, 2, 2), (2, 2, 2), 'no_pool']
hidden_transfers_conv = ['rectifier', 'rectifier', 'rectifier']
hidden_transfers_upconv = ['rectifier', 'rectifier', 'rectifier', 'identity']
bm_down = ['same', 'same', 'same']
bm_up = ['valid', 'same', 'same', 'same']
loss_id = ash.fcn_cat_ce
padding = 16
elif d_code.startswith('96fcn'):
nkerns_d = [8, 16, 32]
fs_d = [(3, 3, 3), (3, 3, 3), (3, 3, 3)]
ps_d = [(2, 2, 2), (2, 2, 2), (2, 2, 2)]
strides_d = (1, 1, 1)
nkerns_u = [32, 32, 16, n_classes]
fs_u = [(3, 3, 3), (3, 3, 3), (3, 3, 3), (3, 3, 3)]
ps_u = [(2, 2, 2), (2, 2, 2), (2, 2, 2), 'no_pool']
hidden_transfers_conv = ['rectifier', 'rectifier', 'rectifier']
hidden_transfers_upconv = ['rectifier', 'rectifier', 'rectifier', 'identity']
bm_down = ['same', 'same', 'same']
bm_up = ['same', 'same', 'same', 'same']
loss_id = ash.dice
padding = 0
else:
raise Exception('No such dataset.')
train_size, inpt_d, n_channels, inpt_h, inpt_w = train_x.shape
batch_size = 1
max_passes = 50
set_x = train_x
set_y = train_y
output_h = inpt_h-padding
output_w = inpt_w-padding
output_d = inpt_d-padding
if vis:
for i in range(set_x.shape[0]):
plt.imshow(set_x[i,inpt_d/2,0,:,:], cmap='Greys_r')
plt.show()
ty = np.reshape(set_y[i], (output_h,output_w,output_d,n_classes))
ty = ty.argmax(axis=3)
plt.imshow(ty[:,:,output_d/2], cmap='Greys_r')
plt.show()
inpt_dims = (inpt_h, inpt_w, inpt_d)
n_report = train_size / batch_size
max_iter = n_report * max_passes
print 'Train x shape: ', train_x.shape
print 'Train y shape: ', train_y.shape
print 'Valid x shape: ', valid_x.shape
print 'Valid y shape: ', valid_y.shape
print 'Test x shape: ', test_x.shape
print 'Test y shape: ', test_y.shape
print '\nmax iter: ', max_iter
print 'report frequency: every %i iterations\n' % n_report
stop = climin.stops.AfterNIterations(max_iter)
pause = climin.stops.ModuloNIterations(n_report)
optimizer = 'adam'
pkchu = FCN(
image_height=inpt_dims[0], image_width=inpt_dims[1],
image_depth=inpt_dims[2], n_channel=n_channels,
n_output=n_classes, n_hiddens_conv=nkerns_d,
down_filter_shapes=fs_d, hidden_transfers_conv=hidden_transfers_conv,
down_pools=ps_d, n_hiddens_upconv=nkerns_u,
up_filter_shapes=fs_u, hidden_transfers_upconv=hidden_transfers_upconv,
up_pools=ps_u, out_transfer='softmax', loss=loss_id,
optimizer=optimizer, batch_size=batch_size,
bm_up=bm_up, bm_down=bm_down,
max_iter=max_iter, implementation='dnn_conv3d', strides_d=strides_d
)
print '\nARCHITECTURE: '
print '\tFilters: ', fs_d
print '\tFeature maps: ', nkerns_d
print '\tPools: ', ps_d
print '\tUp-filters: ', fs_u
print '\tFeature maps: ', nkerns_u
print '\tUppools: ', ps_u
if not params:
rng = np.random.RandomState(123)
pkchu.parameters.data[...] = rng.normal(0, 0.01, pkchu.parameters.data.shape)
else:
with open(param_file, 'r') as f:
#with open('params/params9680fcnmini.pkl', 'r') as f:
pkchu.parameters.data[...] = pickle.load(f)
report_fun = report.OneLinePrinter(
['n_iter', 'runtime', 'loss', 'val_loss', 'test_avg'],
spaces=['4', '7.4f', '5.4f', '7.4f', '7.4f']
)
score_fun = ash.MinibatchScoreFCN(max_samples=batch_size, sample_dims=[0, 0])
data = {
'train':(train_x, train_y),
'val':(valid_x, valid_y),
'test':(test_x, test_y)
}
test_fun = ash.MinibatchTestFCN(max_samples=batch_size, sample_dims=[0, 0])
initial_err = ma.scalar(score_fun(pkchu.score, *data['train']))
print 'Initial train loss: %.4f' % initial_err
coach = PocketTrainer(
model=pkchu, data=data, stop=stop,
pause=pause, score_fun=score_fun,
report_fun=report_fun, test_fun=test_fun,
evaluate=True, test=True
)
coach.fit()
pkchu.parameters.data[...] = coach.best_pars
plt.plot(coach.losses)
plt.show()
for i in range(test_x.shape[0]):
plt.imshow(test_x[i, inpt_d/2, 0, :, :], cmap='Greys_r')
plt.show()
y = pkchu.predict(test_x[i:i+1])
y = np.reshape(y[0].as_numpy_array(), (output_h, output_w, output_d, n_classes))
y = y.argmax(axis=3)
plt.imshow(y[:, :, output_d/2], cmap='Greys_r')
plt.show()
with open(param_file,'w') as f:
pickle.dump(coach.best_pars, f)