-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.py
199 lines (151 loc) · 5.13 KB
/
main.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
import random
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
import tensorflow as tf
import keras.backend as K
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 models import Unet_with_slice,Unet_with_inception,Inception,Unet
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
from utils import dice_coef_loss,dice_coef,one_hot_encode,standardize
#checkpoint = ModelCheckpoint('new/weights.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
#earlystopping = EarlyStopping(monitor = 'val_loss', verbose = 1,min_delta = 0.01, patience = 3, mode = 'min')
#callbacks_list = [checkpoint, earlystopping]
input_img = Input((240, 240, 4))
model = Unet(input_img,16,0.1,True)
learning_rate = 0.001
epochs = 5000
decay_rate = learning_rate / epochs
model.compile(optimizer=Adam(lr=learning_rate, decay = decay_rate), loss=dice_coef_loss, metrics=[dice_coef])
model.summary()
# data preprocessing starts here
path = '../BRATS2017/Brats17TrainingData/HGG'
all_images = os.listdir(path)
#print(len(all_images))
all_images.sort()
data = np.zeros((240,240,155,4))
x_to = []
y_to = []
for i in range(1):
print(i)
x = all_images[i]
print(x)
folder_path = path + '/' + x;
modalities = os.listdir(folder_path)
modalities.sort()
#data = []
w = 0
for j in range(len(modalities)-1):
#print(modalities[j])
image_path = folder_path + '/' + modalities[j]
if(image_path[-7:-1] + image_path[-1] == 'seg.nii'):
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)
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.reshape(240,240,1))
imgplot = plt.imshow(X[:,:,0])
plt.show(block=False)
plt.pause(100)
plt.close()
imgplot = plt.imshow(Y)
plt.show(block=False)
plt.pause(3)
plt.close()
for l in range(4):
img = Image.fromarray(X[:,:,l])
img2 = img.rotate(45)
rotated = np.asarray(img2)
X[:,:,l] = rotated
img = Image.fromarray(Y)
img2 = img.rotate(45)
rotated = np.asarray(img2)
Y = rotated
x_to.append(X)
y_to.append(Y.reshape(240,240,1))
# for l in range(4):
# img = Image.fromarray(X[:,:,l])
# img2 = img.rotate(45)
# rotated = np.asarray(img2)
# X[:,:,l] = rotated
# img = Image.fromarray(Y)
# img2 = img.rotate(45)
# rotated = np.asarray(img2)
# Y = rotated
# x_to.append(X)
# y_to.append(Y.reshape(240,240,1))
# imgplot = plt.imshow(X[:,:,0])
# plt.show(block=False)
# plt.pause(3)
# plt.close()
# imgplot = plt.imshow(Y)
# plt.show(block=False)
# plt.pause(3)
# plt.close()
#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)
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] = 1 #since label 4 was missing in Brats dataset , changing all labels 4 to 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)
#y_to = y_to.reshape(240,240,1)
model.fit(x=x_to, y=y_to, batch_size=20, epochs=50)
model.save('2class.h5')