-
Notifications
You must be signed in to change notification settings - Fork 0
/
Training_For_Axis3.py
339 lines (261 loc) · 9.58 KB
/
Training_For_Axis3.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding: utf-8 -*-
"""Training of Axis1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/18_X2k6liqJEu5r1RPP7skab-ZvwUl-lu
"""
import random
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
import tensorflow as tf
import keras
import keras.backend as K
from keras.utils import to_categorical
from keras import metrics
from keras.models import Model, load_model
from keras.layers import Input, BatchNormalization, Activation, Dense, Dropout,Maximum
from keras.layers.core import Lambda, RepeatVector, Reshape
from keras.layers.convolutional import Conv2D, Conv2DTranspose,Conv3D,Conv3DTranspose
from keras.layers.pooling import MaxPooling2D, GlobalMaxPool2D,MaxPooling3D
from keras.layers.merge import concatenate, add
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from skimage.io import imread, imshow, concatenate_images
from skimage.transform import resize
from sklearn.utils import class_weight
from keras.callbacks import ModelCheckpoint
from keras.callbacks import CSVLogger
from keras.callbacks import EarlyStopping
import os
from skimage.io import imread, imshow, concatenate_images
from skimage.transform import resize
# from medpy.io import load
import numpy as np
#import cv2
import nibabel as nib
from PIL import Image
def conv_block(input_mat,num_filters,kernel_size,batch_norm):
X = Conv2D(num_filters,kernel_size=(kernel_size,kernel_size),strides=(1,1),padding='same')(input_mat)
if batch_norm:
X = BatchNormalization()(X)
X = Activation('relu')(X)
X = Conv2D(num_filters,kernel_size=(kernel_size,kernel_size),strides=(1,1),padding='same')(X)
if batch_norm:
X = BatchNormalization()(X)
X = Activation('relu')(X)
return X
def Unet(input_img, n_filters = 16, dropout = 0.2, batch_norm = True):
c1 = conv_block(input_img,n_filters,3,batch_norm)
p1 = MaxPooling2D(pool_size=(2, 2), strides=2)(c1)
p1 = Dropout(dropout)(p1)
c2 = conv_block(p1,n_filters*2,3,batch_norm);
p2 = MaxPooling2D(pool_size=(2,2) ,strides=2)(c2)
p2 = Dropout(dropout)(p2)
c3 = conv_block(p2,n_filters*4,3,batch_norm);
p3 = MaxPooling2D(pool_size=(2,2) ,strides=2)(c3)
p3 = Dropout(dropout)(p3)
c4 = conv_block(p3,n_filters*8,3,batch_norm);
p4 = MaxPooling2D(pool_size=(2,2) ,strides=2)(c4)
p4 = Dropout(dropout)(p4)
c5 = conv_block(p4,n_filters*16,3,batch_norm);
u6 = Conv2DTranspose(n_filters*8, (3,3), strides=(2, 2), padding='same')(c5);
u6 = concatenate([u6,c4]);
c6 = conv_block(u6,n_filters*8,3,batch_norm)
c6 = Dropout(dropout)(c6)
u7 = Conv2DTranspose(n_filters*4,(3,3),strides = (2,2) , padding= 'same')(c6);
u7 = concatenate([u7,c3]);
c7 = conv_block(u7,n_filters*4,3,batch_norm)
c7 = Dropout(dropout)(c7)
u8 = Conv2DTranspose(n_filters*2,(3,3),strides = (2,2) , padding='same')(c7);
u8 = concatenate([u8,c2]);
c8 = conv_block(u8,n_filters*2,3,batch_norm)
c8 = Dropout(dropout)(c8)
u9 = Conv2DTranspose(n_filters,(3,3),strides = (2,2) , padding='same')(c8);
u9 = concatenate([u9,c1]);
c9 = conv_block(u9,n_filters,3,batch_norm)
outputs = Conv2D(4, (1, 1), activation='softmax')(c9)
model = Model(inputs=input_img, outputs=outputs)
return model
def standardize(image):
standardized_image = np.zeros(image.shape)
#
# iterate over the `z` dimension
for z in range(image.shape[2]):
# get a slice of the image
# at channel c and z-th dimension `z`
image_slice = image[:,:,z]
# subtract the mean from image_slice
centered = image_slice - np.mean(image_slice)
# divide by the standard deviation (only if it is different from zero)
if(np.std(centered)!=0):
centered = centered/np.std(centered)
# update the slice of standardized image
# with the scaled centered and scaled image
standardized_image[:, :, z] = centered
### END CODE HERE ###
return standardized_image
def dice_coef(y_true, y_pred, epsilon=0.00001):
"""
Dice = (2*|X & Y|)/ (|X|+ |Y|)
= 2*sum(|A*B|)/(sum(A^2)+sum(B^2))
ref: https://arxiv.org/pdf/1606.04797v1.pdf
"""
axis = (0,1,2)
dice_numerator = 2. * K.sum(y_true * y_pred, axis=axis) + epsilon
dice_denominator = K.sum(y_true*y_true, axis=axis) + K.sum(y_pred*y_pred, axis=axis) + epsilon
return K.mean((dice_numerator)/(dice_denominator))
def dice_coef_loss(y_true, y_pred):
return 1-dice_coef(y_true, y_pred)
input_img = Input((240,240,4))
model = Unet(input_img,32,0.14,True)
learning_rate = 0.00095
#epochs = 5000
decay_rate = 0.0000002
model.compile(optimizer=Adam(lr=learning_rate, decay = decay_rate), loss=dice_coef_loss, metrics=[dice_coef])
model.summary()
path = '/content/drive/MyDrive/BRATS2018TRAIN/HGG'
all_images = os.listdir(path)
#print(len(all_images))
all_images.sort()
data = np.zeros((240,240,155,4))
image_data2=np.zeros((240,240,155))
loss_hist = []
accu_hist = []
epoch_wise_loss = []
epoch_wise_accu = []
for epochs in range(5):
epoch_loss = 0
epoch_accu = 0
for image_num in range(180):
x_to = []
y_to = []
print(epochs)
print(image_num)
# data preprocessing starts here
x = all_images[image_num]
print(x)
folder_path = path + '/' + x;
modalities = os.listdir(folder_path)
modalities.sort()
#data = []
w = 0
for j in range(len(modalities)):
#print(modalities[j])
image_path = folder_path + '/' + modalities[j]
if not(image_path.find('seg.nii') == -1):
img = nib.load(image_path);
image_data2 = img.get_data()
image_data2 = np.asarray(image_data2)
print("Entered ground truth")
else:
img = nib.load(image_path);
image_data = img.get_data()
image_data = np.asarray(image_data)
image_data = standardize(image_data)
data[:,:,:,w] = image_data
print("Entered modality")
w = w+1
print(data.shape)
print(image_data2.shape)
'''
reshaped_data=data[56:184,75:203,13:141,:]
reshaped_data=reshaped_data.reshape(1,128,128,128,4)
reshaped_image_data2=image_data2[56:184,75:203,13:141]
reshaped_image_data2=reshaped_image_data2.reshape(1,128,128,128)
reshaped_image_data2[reshaped_image_data2==4] = 3
hello = reshaped_image_data2.flatten()
#y_to = keras.utils.to_categorical(y_to,num_classes=2)
print(reshaped_image_data2.shape)
#print(hello[hello==3].shape)
print("Number of classes",np.unique(hello))
class_weights = class_weight.compute_class_weight('balanced',np.unique(hello),hello)
print(class_weights)
'''
for slice_no in range(0,155):
a = slice_no
X = data[:,:,slice_no,:]
Y = image_data2[:,:,slice_no]
# imgplot = plt.imshow(X[:,:,2])
# plt.show(block=False)
# plt.pause(0.3)
# plt.close()
# imgplot = plt.imshow(Y)
# plt.show(block=False)
# plt.pause(0.3)
# plt.close()
if(X.any()!=0 and Y.any()!=0 and len(np.unique(Y)) == 4):
#print(slice_no)
x_to.append(X)
y_to.append(Y)
if len(y_to)>=44:
break;
#reshaped_image_data2 = to_categorical(reshaped_image_data2, num_classes = 4)
#print(reshaped_data.shape)
#print(reshaped_image_data2.shape)
#print(type(reshaped_data))
x_to = np.asarray(x_to)
y_to = np.asarray(y_to)
print(x_to.shape)
print(y_to.shape)
y_to[y_to==4] = 3
#y_to = one_hot_encode(y_to)
#y_to[y_to==2] = 1
#y_to[y_to==1] = 1
#y_to[y_to==0] = 0
print(y_to.shape)
from sklearn.utils import shuffle
x_to,y_to = shuffle(x_to,y_to)
hello = y_to.flatten()
#print(hello[hello==3].shape)
print("Number of classes",np.unique(hello))
class_weights = class_weight.compute_class_weight('balanced',np.unique(hello),hello)
#class_weights.insert(3,0)
print("class_weights",class_weights)
y_to = keras.utils.to_categorical(y_to,num_classes=4)
history = model.fit(x=x_to,y=y_to, epochs = 1 , batch_size = 44 )
print(history.history['loss'])
epoch_loss += history.history['loss'][0]
epoch_accu += history.history['dice_coef'][0]
loss_hist.append(history.history['loss'])
accu_hist.append(history.history['dice_coef'])
model.save('/content/drive/MyDrive/models/2dincr_4class_axis3.h5')
epoch_loss = epoch_loss/180
epoch_accu = epoch_accu/180
epoch_wise_loss.append(epoch_loss)
epoch_wise_accu.append(epoch_accu)
plt.plot(epoch_wise_loss)
plt.title('Model_loss vs epochs')
plt.ylabel('Loss')
plt.xlabel('epochs')
s = '/content/drive/MyDrive/SAVEFIG/epochwise_loss_incraxis3' + str(epochs)
plt.savefig(s)
plt.show()
plt.close()
plt.plot(epoch_wise_accu)
plt.title('Model_Accuracy vs epochs')
plt.ylabel('Accuracy')
plt.xlabel('epochs')
s = '/content/drive/MyDrive/SAVEFIG/epochwise_accu_incraxis3' + str(epochs)
plt.savefig(s)
plt.show()
plt.close()
plt.plot(accu_hist)
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
s = '/content/drive/MyDrive/SAVEFIG/accuracy_plot_incraxis3' + str(epochs)
plt.savefig(s)
plt.show()
plt.close()
plt.plot(loss_hist)
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
s = '/content/drive/MyDrive/SAVEFIG/loss_plot_incraxis3' + str(epochs)
plt.savefig(s)
plt.show()
plt.close()
model.save('/content/drive/MyDrive/models/2dincr_4class_axis3.h5')