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
test1.dcm
test2.dcm
test3.dcm
test4.dcm
test5.dcm
test6.dcm
test_dicoms = glob.glob('*.dcm')
test_dicoms
['test2.dcm', 'test5.dcm', 'test1.dcm', 'test4.dcm', 'test6.dcm', 'test3.dcm']
a = pydicom.dcmread(test_dicoms[0])
for i in test_dicoms:
    ds = pydicom.dcmread(i)
    print(ds.StudyDescription, ds.Modality, ds.PatientPosition)
Cardiomegaly DX AP
No Finding CT PA
No Finding DX PA
No Finding DX PA
No Finding DX XX
Effusion DX AP

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')
Load file test1.dcm ...
<matplotlib.image.AxesImage at 0x7fab6bf97c10>
plt.imshow(check_dicom('test4.dcm'), cmap='gray')
Load file test4.dcm ...
<matplotlib.image.AxesImage at 0x7fab6bf1e8d0>
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)
Loaded model from disk
Load file test1.dcm ...
1/1 [==============================] - 1s 1s/step
[[0.43534777]] ['Pneumoinia Abscent']
Load file test2.dcm ...
1/1 [==============================] - 1s 553ms/step
[[0.4329888]] ['Pneumoinia Abscent']
Load file test3.dcm ...
1/1 [==============================] - 1s 567ms/step
[[0.4371698]] ['Pneumoinia Abscent']
Load file test4.dcm ...
1/1 [==============================] - 1s 609ms/step
[[0.43534777]] ['Pneumoinia Abscent']
Load file test5.dcm ...
test5.dcm not suitable
Load file test6.dcm ...
test6.dcm not suitable
# 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)
Loaded model from disk
Load file test1.dcm ...
1/1 [==============================] - 1s 945ms/step
[[0.49144208]] ['Pneumoinia Abscent']
Load file test2.dcm ...
1/1 [==============================] - 1s 557ms/step
[[0.48717716]] ['Pneumoinia Abscent']
Load file test3.dcm ...
1/1 [==============================] - 1s 569ms/step
[[0.4967311]] ['Pneumoinia Abscent']
Load file test4.dcm ...
1/1 [==============================] - 1s 559ms/step
[[0.49144208]] ['Pneumoinia Abscent']
Load file test5.dcm ...
test5.dcm not suitable
Load file test6.dcm ...
test6.dcm not suitable