AI for Healthcare (Part 3)
Part 1 Inference and Validation for 2D Imaging
import numpy as np
import pandas as pd
import pydicom
%matplotlib inline
import matplotlib.pyplot as plt
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import model_from_json
import glob
from skimage.transform import resize
!ls | grep .dcm
test_dicoms = glob.glob('*.dcm')
test_dicoms
a = pydicom.dcmread(test_dicoms[0])
for i in test_dicoms:
ds = pydicom.dcmread(i)
print(ds.StudyDescription, ds.Modality, ds.PatientPosition)
In the check_dicom function, you need to check for image position, image type(modality), and body part on ALL .dcm files so that you can know which files are valid to be used for your algorithm (as a hint 3 of the files should not be useable).
Suggestion:- In your cell block where you show the probabilities of the presence of Pneumonia presence instead of showing 1 you can print Pneumonia present and instead of 0 print Pneumonia absent so that the reader may easily understand the output of your results.
# This function reads in a .dcm file, checks the important fields for our device, and returns a numpy array
# of just the imaging data
def check_dicom(filename):
# todo
print('Load file {} ...'.format(filename))
ds = pydicom.dcmread(filename)
# if ds.Modality == 'DX' and ds.PatientPosition in ['AP', 'PA'] and ds.StudyDescription not in ['Cardiomegaly']:
if ds.Modality == 'DX' and ds.PatientPosition in ['AP', 'PA']:
img = ds.pixel_array
return img
else:
print(f'{filename} not suitable')
# This function takes the numpy array output by check_dicom and
# runs the appropriate pre-processing needed for our model input
def preprocess_image(img,img_size):
# todo
# proc_img = (img - img_mean)/img_std
# proc_img = resize(proc_img, img_size)
idg = ImageDataGenerator()
img = resize(img, IMG_SIZE)
proc_img = idg.flow(
img,
y=None,
batch_size=32,
shuffle=True,
sample_weight=None,
seed=None,
save_to_dir=None,
save_prefix="",
save_format="png",
subset=None,)
return proc_img
# This function loads in our trained model w/ weights and compiles it
def load_model(model_path, weight_path):
# todo
json_file = open(model_path, 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(weight_path)
print("Loaded model from disk")
return loaded_model
# This function uses our device's threshold parameters to predict whether or not
# the image shows the presence of pneumonia using our trained model
def predict_image(model, img, thresh):
# todo
prediction_prob = model.predict(img, verbose = True)
prediction = ['Pneumoinia Present' if i[0] > thresh else 'Pneumoinia Abscent' for i in prediction_prob]
return prediction_prob,prediction
plt.imshow(check_dicom('test1.dcm'), cmap='gray')
plt.imshow(check_dicom('test4.dcm'), cmap='gray')
test_dicoms = ['test1.dcm','test2.dcm','test3.dcm','test4.dcm','test5.dcm','test6.dcm']
model_path = 'my_model.json'
weight_path = 'xray_class_my_model.best.hdf5'
IMG_SIZE=(1,224,224,3) # This might be different if you did not use vgg16
# img_mean =
# img_std = # loads the std dev image value they used during training preprocessing
my_model = load_model(model_path, weight_path)
thresh = 0.5
X = []
# use the .dcm files to test your prediction
for i in test_dicoms:
img = np.array([])
img = check_dicom(i)
if img is None:
continue
img_proc = preprocess_image(img,IMG_SIZE)
X.append(img_proc)
pred_proba, pred = predict_image(my_model,img_proc,thresh)
print(pred_proba, pred)
# Using the Second Model
test_dicoms = ['test1.dcm','test2.dcm','test3.dcm','test4.dcm','test5.dcm','test6.dcm']
model_path = 'my_model2.json'
weight_path = 'xray_class_my_model2.best.hdf5'
IMG_SIZE=(1,224,224,3) # This might be different if you did not use vgg16
# img_mean =
# img_std = # loads the std dev image value they used during training preprocessing
my_model = load_model(model_path, weight_path)
thresh = 0.5
X = []
# use the .dcm files to test your prediction
for i in test_dicoms:
img = np.array([])
img = check_dicom(i)
if img is None:
continue
img_proc = preprocess_image(img,IMG_SIZE)
X.append(img_proc)
pred_proba, pred = predict_image(my_model,img_proc,thresh)
print(pred_proba, pred)