-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain.py
288 lines (196 loc) · 8.06 KB
/
train.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
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import os
import time
import pytz
import pandas as pd
import numpy as np
from datetime import datetime
DATA_DIR = '/data/cmpe257-02-fa2019/team-1-meerkats/rsna-intracranial-hemorrhage-detection/'
TIMESTAMP_BEGIN = str(datetime.now(pytz.timezone('America/Los_Angeles'))).replace(" ", "-")
MODEL_NAME = TIMESTAMP_BEGIN + '-ResNet50-conv+head-670k' # should change this every time!
WEIGHTS_DIR = 'weights/' + MODEL_NAME + '/'
TB_DIR = 'tensorboard-graphs/Graph-' + MODEL_NAME
os.mkdir(WEIGHTS_DIR)
os.mkdir(TB_DIR)
TB_FREQ = 67000
INPUT_SHAPE = (224, 224, 3)
# In[2]:
# dcm processing
def correct_dcm(dcm):
x = dcm.pixel_array + 1000
px_mode = 4096
x[x>=px_mode] = x[x>=px_mode] - px_mode
dcm.PixelData = x.tobytes()
dcm.RescaleIntercept = -1000
def window_image(dcm, window_center, window_width):
#handle the 12 bit values
if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):
correct_dcm(dcm)
img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept
img_min = window_center - window_width // 2
img_max = window_center + window_width // 2
img = np.clip(img, img_min, img_max)
return img
def window_and_scale_brain_subdural_soft(dcm):
#window images
brain_img = window_image(dcm, 40, 80)
subdural_img = window_image(dcm, 80, 200)
#soft_img = window_image(dcm, 40, 380)
bone_img = window_image(dcm, 600, 2800)
#scale images (0-1)
brain_img = (brain_img - 0) / 80
subdural_img = (subdural_img + 20) / 200
bone_img = (bone_img + 800) / 2800
# combine channels
return np.array([brain_img, subdural_img, bone_img]).transpose(1,2,0)
def old_window_and_scale(dcm):
brain_img = window_image(dcm, 40, 80)
subdural_img = window_image(dcm, 80, 200)
soft_img = window_image(dcm, 40, 380)
brain_img = (brain_img - 0) / 80
subdural_img = (subdural_img - (-20)) / 200
soft_img = (soft_img - (-150)) / 380
bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1,2,0)
return bsb_img
def read_trainset(filename=DATA_DIR+"stage_2_train.csv"):
df = pd.read_csv(filename)
df["Image"] = df["ID"].str.slice(stop=12)
df["Diagnosis"] = df["ID"].str.slice(start=13)
duplicates_to_remove = [
56346,56347,56348,56349,
56350,56351,1171830,1171831,
1171832,1171833,1171834,1171835,
3705312,3705313,3705314,3705315,
3705316,3705317,3842478,3842479,
3842480,3842481,3842482,3842483
]
df = df.drop(index=duplicates_to_remove)
df = df.reset_index(drop=True)
df = df.loc[:, ["Label", "Diagnosis", "Image"]]
df = df.set_index(['Image', 'Diagnosis']).unstack(level=-1)
return df
# In[3]:
import keras
import cv2
from math import ceil
import numpy as np
import pydicom
np.random.seed(2557)
def _read(path, desired_size):
"""Will be used in DataGenerator"""
dcm = pydicom.dcmread(path)
try:
img = window_and_scale_brain_subdural_soft(dcm)
img = cv2.resize(img, desired_size[:2], interpolation=cv2.INTER_LINEAR)
# Some dcms seem to be corrupted
except ValueError:
print('Error while parsing {}'.format(path))
img = np.ones(desired_size)
return img
class DataGenerator(keras.utils.Sequence):
def __init__(self, img_dir, image_IDs, labels_df, batch_size, img_size):
self.image_IDs = image_IDs
self.labels_df = labels_df
self.batch_size = batch_size
self.img_size = img_size
self.img_dir = img_dir
def __len__(self):
return int(ceil(len(self.image_IDs) / self.batch_size))
def __getitem__(self, index):
batch_ids = self.image_IDs[index*self.batch_size:(index+1)*self.batch_size]
X = np.empty((self.batch_size, *self.img_size))
Y = np.empty((self.batch_size, 6))
for i, ID in enumerate(batch_ids):
X[i,] = _read(self.img_dir+ID+".dcm", self.img_size)
Y[i,] = self.labels_df.loc[ID].values
return X, Y
# In[4]:
from sklearn.model_selection import train_test_split
df = read_trainset()
train_df, test_df = train_test_split(df,test_size=0.1, random_state=257)
traingen = DataGenerator(img_dir=DATA_DIR+'stage_2_train/',
image_IDs=train_df.index, #MAGIC
labels_df=train_df, #MAGIC
batch_size=16,
img_size=INPUT_SHAPE)
testgen = DataGenerator(img_dir=DATA_DIR+'stage_2_train/',
image_IDs=test_df.index, #MAGIC
labels_df=test_df, #MAGIC
batch_size=16,
img_size=INPUT_SHAPE)
# In[5]:
# custom loss function
from keras import backend as K
def weighted_log_loss(y_true, y_pred):
"""
Can be used as the loss function in model.compile()
---------------------------------------------------
"""
class_weights = np.array([1., 1., 1., 1., 1., 1.])
eps = K.epsilon()
y_pred = K.clip(y_pred, eps, 1.0-eps)
out = -( y_true * K.log( y_pred) * class_weights
+ (1.0 - y_true) * K.log(1.0 - y_pred) * class_weights)
return K.mean(out, axis=-1)
# custom performance metric
def correct_diagnoses(y_true, y_pred):
THRESHOLD = 0.5
p_thr = K.greater(y_pred, THRESHOLD)
y_true = K.cast(y_true, dtype='bool')
equals_t = K.equal(p_thr, y_true)
correct_rows = K.all(equals_t, axis=1)
correct_rows_int = K.cast(correct_rows, dtype='int32')
return K.sum(correct_rows_int)/K.shape(correct_rows_int)[0]
def correct_positive_diagnoses(y_true, y_pred):
THRESHOLD = 0.5
p_thr = K.greater(y_pred, THRESHOLD)
y_true = K.cast(y_true, dtype='bool')
pos_mask = K.any(y_true, axis=1) #patients with positive diagnoses
p_thr = p_thr[pos_mask]
y_true = y_true[pos_mask]
equals_t = K.equal(p_thr, y_true)
correct_rows = K.all(equals_t, axis=1)
correct_rows_float = K.cast(correct_rows, dtype='float32')
return K.sum(correct_rows_float)/(K.cast(K.shape(correct_rows_float)[0], dtype='float32')+K.epsilon())
def np_cpd(y_true, pred, thr=0.5): #numpy implementation of correct positive diagnoses
p_thr = pred > thr
pos_mask = np.any(y_true, axis=1)
p_thr = p_thr[pos_mask]
y_true = y_true[pos_mask]
p_correct = np.all(p_thr[:len(y_true)] == y_true[:len(p_thr)], axis=1)
return np.sum(p_correct)/(len(p_thr)+1e-15)
# In[6]:
import keras
#from keras.applications.vgg16 import VGG16
from keras.applications import ResNet50
from keras import layers
import numpy as np
#conv_base = VGG16(weights=None, input_shape=INPUT_SHAPE ,include_top=False)
conv_base = ResNet50(include_top=False, weights='imagenet', input_shape=INPUT_SHAPE)
#conv_base.load_weights('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5') #doesn't work otherwise without internet access
conv_base.trainable = True
model = keras.models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(6, activation='sigmoid'))
model.name = MODEL_NAME
model.compile(
#loss=weighted_log_loss, #custom loss
loss='binary_crossentropy',
#loss='categorical_crossentropy', # mutually exclusive
optimizer=keras.optimizers.Adam(lr=1e-5),
metrics=[correct_positive_diagnoses])
model.summary()
mc = keras.callbacks.ModelCheckpoint(filepath=WEIGHTS_DIR+MODEL_NAME+'-epoch={epoch:02d}-valid-loss={val_loss:.2f}.hdf5', monitor='loss', verbose=True, save_best_only=False, save_weights_only=False)
tb = keras.callbacks.TensorBoard(log_dir=TB_DIR, histogram_freq=0, update_freq=TB_FREQ,
write_graph=True, write_images=True) # about 10 checkpoints per epoch
hist = model.fit_generator(traingen,
validation_data = testgen,
epochs=5,
verbose=True,
use_multiprocessing=True,
workers=4,
callbacks=[mc, tb])