Importing the dataset¶
In [1]:
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/MedNIST.zip"
!unzip -qo "MedNIST.zip"
!rm "MedNIST.zip"
Allocating test and train directories¶
In [2]:
test_dir = "Medical/Medical_test"
train_dir = "Medical/Medical_train"
Importing libraries¶
In [3]:
import os
import numpy as np
import pandas as pd
import random, datetime, os, shutil, math
Defining a function for preparation of the test dataset¶
In [4]:
def prep_test_data(med, train_dir, test_dir):
pop = os.listdir(train_dir+'/'+med)
test_data=random.sample(pop, 2000)
#print(test_data)
for f in test_data:
shutil.copy(train_dir+'/'+med+'/'+f, test_dir+'/'+med+'/')
In [5]:
for medi in os.listdir(train_dir):
prep_test_data(medi, train_dir, test_dir)
In [6]:
target_classes = os.listdir(train_dir)
num_classes = len(target_classes)
print('Number of target classes:', num_classes)
print(list(enumerate(target_classes)))
In [7]:
target_classes = os.listdir(test_dir)
num_classes = len(target_classes)
print('Number of target classes:', num_classes)
print(list(enumerate(target_classes)))
In [8]:
#shutil.rmtree("Medical/Medical_test/.ipynb_checkpoints")
Assigning training and test set distributions¶
In [9]:
training_set_distribution = [len(os.listdir(os.path.join(train_dir, dir))) for dir in os.listdir(train_dir)]
testing_set_distribution = [len(os.listdir(os.path.join(test_dir, dir))) for dir in os.listdir(test_dir)]
Defining a function to display sample image¶
In [10]:
def show_mri(med):
num = len(med)
if num == 0:
return None
rows = int(math.sqrt(num))
cols = (num+1)//rows
f, axs = plt.subplots(rows, cols)
fig = 0
for b in med:
img = image.load_img(b)
row = fig // cols
col = fig % cols
axs[row, col].imshow(img)
fig += 1
plt.show()
In [11]:
import matplotlib.pyplot as plt
from matplotlib.image import imread
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
In [12]:
dir_name = os.path.join(train_dir,"AbdomenCT")
all_images = [os.path.join(dir_name, fname) for fname in os.listdir(dir_name)]
show_mri(all_images[:9])
Image Pre-processing¶
In [13]:
image_size = (32, 32, 3)
datagen=ImageDataGenerator(rescale = 1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
)
In [14]:
training_set=datagen.flow_from_directory(train_dir,
target_size=image_size[:2],
batch_size=32,
class_mode='categorical',
shuffle=False
#color_mode='rgb'
)
In [15]:
validation_set=datagen.flow_from_directory(test_dir,
target_size=image_size[:2],
batch_size=32,
class_mode='categorical',
shuffle=False
)
Importing and using Callbacks¶
In [16]:
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.utils import plot_model
In [17]:
#!pip install pydot
#!pip install graphviz
#!pip install pydotplus
#!sudo apt-get install graphviz
In [18]:
es = EarlyStopping(monitor='val_acc', mode='max', verbose=1, patience=7)
filepath = "modelMedicalMNIST.h5"
ckpt = ModelCheckpoint(filepath, monitor='acc', verbose=1, save_best_only=True, mode='max')
rlp = ReduceLROnPlateau(monitor='acc', patience=3, verbose=1)
Defining CNN model¶
In [19]:
def cnn(image_size, num_classes):
classifier = Sequential()
classifier.add(Conv2D(64, (5, 5), input_shape=image_size, activation='relu', padding='same'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())
classifier.add(Dense(num_classes, activation = 'softmax'))
classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
return classifier
neuralnetwork_cnn = cnn(image_size, num_classes)
neuralnetwork_cnn.summary()
#plot_model(neuralnetwork_cnn, show_shapes=True)
Using Fit()¶
In [20]:
history = neuralnetwork_cnn.fit_generator(
generator=training_set, validation_data=validation_set,
callbacks=[es, ckpt, rlp], epochs = 5,
)
Displaying loss and accuracy graph¶
In [21]:
fig, ax = plt.subplots(figsize=(20, 6))
pd.DataFrame(history.history).iloc[:, :-1].plot(ax=ax)
Out[21]:
Predicting on validation set¶
In [22]:
batch_size=32
pred=neuralnetwork_cnn.predict_generator(validation_set,steps=306/batch_size)
predicted_class_indices=np.argmax(pred,axis=1)
In [23]:
labels = (validation_set.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]
Displaying the category of image and predicted image¶
In [24]:
filenames=validation_set.filenames[0]
results=pd.DataFrame({"Filename":filenames,
"Predictions":predictions})
In [25]:
display(results.head(50))
DeepCC¶
In [27]:
!deepCC modelMedicalMNIST.h5
In [ ]: