-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification_lung_ct_scans_3d_analysis.py
More file actions
304 lines (199 loc) · 7.62 KB
/
classification_lung_ct_scans_3d_analysis.py
File metadata and controls
304 lines (199 loc) · 7.62 KB
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
import os
import zipfile
import numpy as nump
import tensorflow as tflow
import random
from scipy import ndimage
import nibabel as nib
from tensorflow import keras as tkeras
from tensorflow.keras import layers as klayers
url = "https://github.com/hasibzunair/3D-image-classification-tutorial/releases/download/v0.2/CT-0.zip"
filename = os.path.join(os.getcwd(), "CT-0.zip")
tkeras.utils.get_file(filename, url)
url = "https://github.com/hasibzunair/3D-image-classification-tutorial/releases/download/v0.2/CT-23.zip"
filename = os.path.join(os.getcwd(), "CT-23.zip")
tkeras.utils.get_file(filename, url)
os.makedirs("MosMedData")
with zipfile.ZipFile("CT-0.zip", "r") as z_fp:
z_fp.extractall("./MosMedData/")
with zipfile.ZipFile("CT-23.zip", "r") as z_fp:
z_fp.extractall("./MosMedData/")
def read_nifti_file(filepath):
scan = nib.load(filepath)
scan = scan.get_fdata()
return scan
def normalize(volume):
min = -1000
max = 400
volume[volume < min] = min
volume[volume > max] = max
volume = (volume - min) / (max - min)
volume = volume.astype("float32")
return volume
def resize_volume(img):
desired_depth = 64
desired_width = 128
desired_height = 128
current_depth = img.shape[-1]
current_width = img.shape[0]
current_height = img.shape[1]
depth = current_depth / desired_depth
width = current_width / desired_width
height = current_height / desired_height
depth_factor = 1 / depth
width_factor = 1 / width
height_factor = 1 / height
img = ndimage.rotate(img, 90, reshape=False)
img = ndimage.zoom(img, (width_factor, height_factor, depth_factor), order=1)
return img
def process_scan(path):
volume = read_nifti_file(path)
volume = normalize(volume)
volume = resize_volume(volume)
return volume
normal_scan_paths = [
os.path.join(os.getcwd(), "MosMedData/CT-0", x)
for x in os.listdir("MosMedData/CT-0")
]
abnormal_scan_paths = [
os.path.join(os.getcwd(), "MosMedData/CT-23", x)
for x in os.listdir("MosMedData/CT-23")
]
print("CT scans with normal lung tissue: " + str(len(normal_scan_paths)))
print("CT scans with abnormal lung tissue: " + str(len(abnormal_scan_paths)))
abnormal_scans = nump.array([process_scan(path) for path in abnormal_scan_paths])
normal_scans = nump.array([process_scan(path) for path in normal_scan_paths])
abnormal_labels = nump.array([1 for _ in range(len(abnormal_scans))])
normal_labels = nump.array([0 for _ in range(len(normal_scans))])
x_train = nump.concatenate((abnormal_scans[:70], normal_scans[:70]), axis=0)
y_train = nump.concatenate((abnormal_labels[:70], normal_labels[:70]), axis=0)
x_val = nump.concatenate((abnormal_scans[70:], normal_scans[70:]), axis=0)
y_val = nump.concatenate((abnormal_labels[70:], normal_labels[70:]), axis=0)
print(
"Number of samples in train and validation are %d and %d."
% (x_train.shape[0], x_val.shape[0])
)
@tflow.function
def rotate(volume):
def scipy_rotate(volume):
angles = [-20, -10, -5, 5, 10, 20]
angle = random.choice(angles)
volume = ndimage.rotate(volume, angle, reshape=False)
volume[volume < 0] = 0
volume[volume > 1] = 1
return volume
augmented_volume = tflow.numpy_function(scipy_rotate, [volume], tflow.float32)
return augmented_volume
def train_preprocessing(volume, label):
volume = rotate(volume)
volume = tflow.expand_dims(volume, axis=3)
return volume, label
def validation_preprocessing(volume, label):
volume = tflow.expand_dims(volume, axis=3)
return volume, label
train_loader = tflow.data.Dataset.from_tensor_slices((x_train, y_train))
validation_loader = tflow.data.Dataset.from_tensor_slices((x_val, y_val))
batch_size = 2
train_dataset = (
train_loader.shuffle(len(x_train))
.map(train_preprocessing)
.batch(batch_size)
.prefetch(2)
)
validation_dataset = (
validation_loader.shuffle(len(x_val))
.map(validation_preprocessing)
.batch(batch_size)
.prefetch(2)
)
import matplotlib.pyplot as plt
data = train_dataset.take(1)
images, labels = list(data)[0]
images = images.numpy()
image = images[0]
print("Dimension of the CT scan is:", image.shape)
plt.imshow(nump.squeeze(image[:, :, 30]), cmap="gray")
def plot_slices(num_rows, num_columns, width, height, data):
data = nump.rot90(nump.array(data))
data = nump.transpose(data)
data = nump.reshape(data, (num_rows, num_columns, width, height))
rows_data, columns_data = data.shape[0], data.shape[1]
heights = [slc[0].shape[0] for slc in data]
widths = [slc.shape[1] for slc in data[0]]
fig_width = 12.0
fig_height = fig_width * sum(heights) / sum(widths)
f, axarr = plt.subplots(
rows_data,
columns_data,
figsize=(fig_width, fig_height),
gridspec_kw={"height_ratios": heights},
)
for i in range(rows_data):
for j in range(columns_data):
axarr[i, j].imshow(data[i][j], cmap="gray")
axarr[i, j].axis("off")
plt.subplots_adjust(wspace=0, hspace=0, left=0, right=1, bottom=0, top=1)
plt.show()
plot_slices(4, 10, 128, 128, image[:, :, :40])
def get_model(width=128, height=128, depth=64):
inputs = tkeras.Input((width, height, depth, 1))
x = klayers.Conv3D(filters=64, kernel_size=3, activation="relu")(inputs)
x = klayers.MaxPool3D(pool_size=2)(x)
x = klayers.BatchNormalization()(x)
x = klayers.Conv3D(filters=64, kernel_size=3, activation="relu")(x)
x = klayers.MaxPool3D(pool_size=2)(x)
x = klayers.BatchNormalization()(x)
x = klayers.Conv3D(filters=128, kernel_size=3, activation="relu")(x)
x = klayers.MaxPool3D(pool_size=2)(x)
x = klayers.BatchNormalization()(x)
x = klayers.Conv3D(filters=256, kernel_size=3, activation="relu")(x)
x = klayers.MaxPool3D(pool_size=2)(x)
x = klayers.BatchNormalization()(x)
x = klayers.GlobalAveragePooling3D()(x)
x = klayers.Dense(units=512, activation="relu")(x)
x = klayers.Dropout(0.3)(x)
outputs = klayers.Dense(units=1, activation="sigmoid")(x)
model = tkeras.Model(inputs, outputs, name="3dcnn")
return model
model = get_model(width=128, height=128, depth=64)
model.summary()
initial_learning_rate = 0.0001
lr_schedule = tkeras.optimizers.schedules.ExponentialDecay(
initial_learning_rate, decay_steps=100000, decay_rate=0.96, staircase=True
)
model.compile(
loss="binary_crossentropy",
optimizer=tkeras.optimizers.Adam(learning_rate=lr_schedule),
metrics=["acc"],
)
checkpoint_cb = tkeras.callbacks.ModelCheckpoint(
"3d_image_classification.h5", save_best_only=True
)
early_stopping_cb = tkeras.callbacks.EarlyStopping(monitor="val_acc", patience=15)
epochs = 100
model.fit(
train_dataset,
validation_data=validation_dataset,
epochs=epochs,
shuffle=True,
verbose=2,
callbacks=[checkpoint_cb, early_stopping_cb],
)
fig, ax = plt.subplots(1, 2, figsize=(20, 3))
ax = ax.ravel()
for i, metric in enumerate(["acc", "loss"]):
ax[i].plot(model.history.history[metric])
ax[i].plot(model.history.history["val_" + metric])
ax[i].set_title("Model {}".format(metric))
ax[i].set_xlabel("epochs")
ax[i].set_ylabel(metric)
ax[i].legend(["train", "val"])
model.load_weights("3d_image_classification.h5")
prediction = model.predict(nump.expand_dims(x_val[0], axis=0))[0]
scores = [1 - prediction[0], prediction[0]]
class_names = ["normal", "abnormal"]
for score, name in zip(scores, class_names):
print(
"This model is %.2f percent confident that CT scan is %s"
% ((100 * score), name)
)