-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
train_res.py
278 lines (221 loc) · 9.94 KB
/
train_res.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import print_function
import os
from keras.models import Model
from keras.layers import (
Input,
Activation,
merge,
Dense,
Flatten
)
from keras.layers.convolutional import (
Convolution2D,
MaxPooling2D,
AveragePooling2D
)
from keras.layers.normalization import BatchNormalization
from keras.utils.visualize_util import plot
import cv2
import numpy as np
from keras.models import Model
from keras.layers import Input, merge, Convolution2D, MaxPooling2D, UpSampling2D, Dropout
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras import backend as K
from data import load_train_data, load_test_data
from train import dice_coef,dice_coef_loss
from skimage.transform import rotate, resize
from skimage import data
rows=160
cols=224
def preprocess(imgs, img_rows,img_cols):
imgs_p = np.ndarray((imgs.shape[0], imgs.shape[1], img_rows, img_cols), dtype=np.uint8)
for i in range(imgs.shape[0]):
imgs_p[i, 0] = cv2.resize(imgs[i, 0], (img_cols, img_rows), interpolation=cv2.INTER_CUBIC)
return imgs_p
def augmentation(image, imageB, org_width=160,org_height=224, width=190, height=262):
max_angle=20
image=resize(image,(width,height))
imageB=resize(imageB,(width,height))
angle=np.random.randint(max_angle)
if np.random.randint(2):
angle=-angle
image=rotate(image,angle,resize=True)
imageB=rotate(imageB,angle,resize=True)
xstart=np.random.randint(width-org_width)
ystart=np.random.randint(height-org_height)
image=image[xstart:xstart+org_width,ystart:ystart+org_height]
imageB=imageB[xstart:xstart+org_width,ystart:ystart+org_height]
if np.random.randint(2):
image=cv2.flip(image,1)
imageB=cv2.flip(imageB,1)
if np.random.randint(2):
imageB=cv2.flip(imageB,0)
# image=resize(image,(org_width,org_height))
return image,imageB
# print(image.shape)
# plt.imshow(image)
# plt.show()
# Helper to build a conv -> BN -> relu block
def _conv_bn_relu(nb_filter, nb_row, nb_col, subsample=(1, 1)):
def f(input):
conv = Convolution2D(nb_filter=nb_filter, nb_row=nb_row, nb_col=nb_col, subsample=subsample,
init="he_normal", border_mode="same")(input)
norm = BatchNormalization(mode=0, axis=1)(conv)
return Activation("relu")(norm)
return f
# Helper to build a BN -> relu -> conv block
# This is an improved scheme proposed in http://arxiv.org/pdf/1603.05027v2.pdf
def _bn_relu_conv(nb_filter, nb_row, nb_col, subsample=(1, 1)):
def f(input):
norm = BatchNormalization(mode=0, axis=1)(input)
activation = Activation("relu")(norm)
return Convolution2D(nb_filter=nb_filter, nb_row=nb_row, nb_col=nb_col, subsample=subsample,
init="he_normal", border_mode="same")(activation)
return f
# Bottleneck architecture for > 34 layer resnet.
# Follows improved proposed scheme in http://arxiv.org/pdf/1603.05027v2.pdf
# Returns a final conv layer of nb_filters * 4
def _bottleneck(nb_filters, init_subsample=(1, 1)):
def f(input):
conv_1_1 = _bn_relu_conv(nb_filters, 1, 1, subsample=init_subsample)(input)
conv_3_3 = _bn_relu_conv(nb_filters, 3, 3)(conv_1_1)
residual = _bn_relu_conv(nb_filters * 4, 1, 1)(conv_3_3)
return _shortcut(input, residual)
return f
# Basic 3 X 3 convolution blocks.
# Use for resnet with layers <= 34
# Follows improved proposed scheme in http://arxiv.org/pdf/1603.05027v2.pdf
def _basic_block(nb_filters, init_subsample=(1, 1)):
def f(input):
conv1 = _bn_relu_conv(nb_filters, 3, 3, subsample=init_subsample)(input)
residual = _bn_relu_conv(nb_filters, 3, 3)(conv1)
return _shortcut(input, residual)
return f
# Adds a shortcut between input and residual block and merges them with "sum"
def _shortcut(input, residual):
# Expand channels of shortcut to match residual.
# Stride appropriately to match residual (width, height)
# Should be int if network architecture is correctly configured.
stride_width = input._keras_shape[2] / residual._keras_shape[2]
stride_height = input._keras_shape[3] / residual._keras_shape[3]
equal_channels = residual._keras_shape[1] == input._keras_shape[1]
shortcut = input
# 1 X 1 conv if shape is different. Else identity.
if stride_width > 1 or stride_height > 1 or not equal_channels:
shortcut = Convolution2D(nb_filter=residual._keras_shape[1], nb_row=1, nb_col=1,
subsample=(stride_width, stride_height),
init="he_normal", border_mode="valid")(input)
return merge([shortcut, residual], mode="sum")
# Builds a residual block with repeating bottleneck blocks.
def _residual_block(block_function, nb_filters, repetations, is_first_layer=False):
def f(input):
for i in range(repetations):
init_subsample = (1, 1)
if i == 0 and not is_first_layer:
init_subsample = (2, 2)
input = block_function(nb_filters=nb_filters, init_subsample=init_subsample)(input)
return input
return f
def _up_block(block,mrge, nb_filters):
up = merge([Convolution2D(2*nb_filters, 2, 2, border_mode='same')(UpSampling2D(size=(2, 2))(block)), mrge], mode='concat', concat_axis=1)
# conv = Convolution2D(4*nb_filters, 1, 1, activation='relu', border_mode='same')(up)
conv = Convolution2D(nb_filters, 3, 3, activation='relu', border_mode='same')(up)
conv = Convolution2D(nb_filters, 3, 3, activation='relu', border_mode='same')(conv)
# conv = Convolution2D(4*nb_filters, 1, 1, activation='relu', border_mode='same')(conv)
# conv = Convolution2D(nb_filters, 3, 3, activation='relu', border_mode='same')(conv)
# conv = Convolution2D(nb_filters, 1, 1, activation='relu', border_mode='same')(conv)
# conv = Convolution2D(4*nb_filters, 1, 1, activation='relu', border_mode='same')(conv)
# conv = Convolution2D(nb_filters, 3, 3, activation='relu', border_mode='same')(conv)
# conv = Convolution2D(nb_filters, 1, 1, activation='relu', border_mode='same')(conv)
return conv
# http://arxiv.org/pdf/1512.03385v1.pdf
# 50 Layer resnet
def resnet():
input = Input(shape=(1, rows, cols))
nb_filters=4 # 5
conv1 = _conv_bn_relu(nb_filter=2*nb_filters, nb_row=7, nb_col=7, subsample=(2, 2))(input)
pool1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), border_mode="same")(conv1)
# Build residual blocks..
block_fn = _bottleneck
block1 = _residual_block(block_fn, nb_filters=2*nb_filters, repetations=3, is_first_layer=True)(pool1)
block2 = _residual_block(block_fn, nb_filters=2**2*nb_filters, repetations=4)(block1)
block3 = _residual_block(block_fn, nb_filters=2**3*nb_filters, repetations=6)(block2)
block4 = _residual_block(block_fn, nb_filters=2**4*nb_filters, repetations=3)(block3)
up5=_up_block(block4,block3,2**3*nb_filters)
up6=_up_block(up5,block2,2**2*nb_filters)
up7=_up_block(up6,block1,2*nb_filters)
up8=_up_block(up7,conv1,nb_filters)
conv10=Convolution2D(1,1,1,activation='sigmoid')(up8)
model = Model(input=input, output=conv10)
model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef])
return model
def main():
# import time
# start = time.time()
# model = resnet()
# duration = time.time() - start
# print("{} s to make model".format(duration))
# start = time.time()
# model.output
# duration = time.time() - start
# print("{} s to get output".format(duration))
# start = time.time()
# model.compile(loss="categorical_crossentropy", optimizer="sgd")
# duration = time.time() - start
# print("{} s to get compile".format(duration))
# current_dir = os.path.dirname(os.path.realpath(__file__))
# model_path = os.path.join(current_dir, "resnet_50.png")
# plot(model, to_file=model_path, show_shapes=True)
# exit()
# -----------------------------------------------------------------------------
print('-'*30)
print('Loading and preprocessing train data...')
print('-'*30)
imgs_train, imgs_mask_train = load_train_data()
imgs_train = preprocess(imgs_train, rows,cols)
imgs_mask_train = preprocess(imgs_mask_train, rows/2,cols/2)
imgs_train = imgs_train.astype('float32')
mean = imgs_train.mean(0)[np.newaxis,:] # mean for data centering
std = np.std(imgs_train) # std for data normalization
imgs_train -= mean
imgs_train /= std
imgs_mask_train = imgs_mask_train.astype('float32')
imgs_mask_train /= 255. # scale masks to [0, 1]
print('-'*30)
print('Creating and compiling model...')
print('-'*30)
model = resnet()
# model.load_weights('resnet.hdf5')
model_checkpoint = ModelCheckpoint('resnet.hdf5', monitor='loss',verbose=1, save_best_only=True)
# -----------------------------------------------------------------------
print('-'*30)
print('Fitting model...')
print('-'*30)
model.fit(imgs_train, imgs_mask_train, batch_size=32, nb_epoch=20, verbose=1, shuffle=True, callbacks=[model_checkpoint])
# for i in range(3):
# model.train(imgs_train[:3],imgs_mask_train[:3])
print('-'*30)
print('Loading and preprocessing test data...')
print('-'*30)
imgs_test, imgs_id_test = load_test_data()
imgs_test = preprocess(imgs_test, rows,cols)
imgs_test = imgs_test.astype('float32')
imgs_test -= mean
imgs_test /= std
print('-'*30)
print('Loading saved weights...')
print('-'*30)
model.load_weights('resnet.hdf5')
print('-'*30)
print('Predicting masks on test data...')
print('-'*30)
imgs_mask_test = model.predict(imgs_test, verbose=1)
np.save('imgs_mask_test.npy', imgs_mask_test)
print('-'*30)
print('Predicting masks on train data...')
print('-'*30)
imgs_mask_test = model.predict(imgs_train, verbose=1)
np.save('imgs_train_pred.npy', imgs_mask_test)
if __name__ == '__main__':
main()