This tutorial was written for the
Please go here to submit comments and report problems
This example of 75 images (in png format) of chest x-rays and abdomen x-rays and builds a deep convolutional classifier. The paper is available here https://link.springer.com/article/10.1007/s10278-018-0079-6
!pip install keras
!pip install tensorflow
Requirement already satisfied: keras in /home/idies/miniconda3/lib/python3.5/site-packages (2.2.4) Requirement already satisfied: keras-preprocessing>=1.0.5 in /home/idies/miniconda3/lib/python3.5/site-packages (from keras) (1.0.9) Requirement already satisfied: h5py in /home/idies/miniconda3/lib/python3.5/site-packages (from keras) (2.8.0) Requirement already satisfied: six>=1.9.0 in /home/idies/miniconda3/lib/python3.5/site-packages (from keras) (1.11.0) Requirement already satisfied: pyyaml in /home/idies/miniconda3/lib/python3.5/site-packages (from keras) (3.13) Requirement already satisfied: scipy>=0.14 in /home/idies/miniconda3/lib/python3.5/site-packages (from keras) (1.1.0) Requirement already satisfied: keras-applications>=1.0.6 in /home/idies/miniconda3/lib/python3.5/site-packages (from keras) (1.0.7) Requirement already satisfied: numpy>=1.9.1 in /home/idies/miniconda3/lib/python3.5/site-packages (from keras) (1.15.2) twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed. You are using pip version 10.0.1, however version 19.0.3 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Collecting tensorflow Downloading https://files.pythonhosted.org/packages/ca/f2/0931c194bb98398017d52c94ee30e5e1a4082ab6af76e204856ff1fdb33e/tensorflow-1.13.1-cp35-cp35m-manylinux1_x86_64.whl (92.5MB) 100% |################################| 92.5MB 260kB/s eta 0:00:01 | 1.5MB 53.9MB/s eta 0:00:02 | 4.4MB 49.2MB/s eta 0:00:029MB 46.6MB/s eta 0:00:02 11% |### | 10.6MB 40.4MB/s eta 0:00:03% |##### | 15.4MB 49.0MB/s eta 0:00:02 18% |###### | 17.6MB 58.2MB/s eta 0:00:02 | 20.9MB 51.8MB/s eta 0:00:02 25% |######## | 23.4MB 53.4MB/s eta 0:00:02 | 25.9MB 45.0MB/s eta 0:00:02[K 31% |########## | 29.0MB 51.9MB/s eta 0:00:02[K 34% |########### | 31.9MB 56.5MB/s eta 0:00:02[K 37% |############ | 34.8MB 49.9MB/s eta 0:00:02####### | 37.0MB 44.4MB/s eta 0:00:02[K 43% |############# | 40.1MB 47.9MB/s eta 0:00:02 48% |############### | 45.1MB 42.6MB/s eta 0:00:02% |################ | 47.3MB 55.3MB/s eta 0:00:01[K 65% |#################### | 60.3MB 53.7MB/s eta 0:00:01[K 70% |###################### | 65.5MB 49.0MB/s eta 0:00:01[K 73% |####################### | 68.0MB 54.3MB/s eta 0:00:01[K 84% |########################## | 78.0MB 48.3MB/s eta 0:00:01 86% |########################### | 80.4MB 52.1MB/s eta 0:00:01[K 89% |############################ | 82.6MB 56.8MB/s eta 0:00:01[K 92% |############################# | 85.8MB 55.9MB/s eta 0:00:01 Collecting tensorflow-estimator<1.14.0rc0,>=1.13.0 (from tensorflow) Downloading https://files.pythonhosted.org/packages/bb/48/13f49fc3fa0fdf916aa1419013bb8f2ad09674c275b4046d5ee669a46873/tensorflow_estimator-1.13.0-py2.py3-none-any.whl (367kB) 100% |################################| 368kB 22.8MB/s ta 0:00:01 Collecting gast>=0.2.0 (from tensorflow) Downloading https://files.pythonhosted.org/packages/4e/35/11749bf99b2d4e3cceb4d55ca22590b0d7c2c62b9de38ac4a4a7f4687421/gast-0.2.2.tar.gz Requirement already satisfied: wheel>=0.26 in /home/idies/miniconda3/lib/python3.5/site-packages (from tensorflow) (0.31.1) Requirement already satisfied: six>=1.10.0 in /home/idies/miniconda3/lib/python3.5/site-packages (from tensorflow) (1.11.0) Collecting protobuf>=3.6.1 (from tensorflow) Downloading https://files.pythonhosted.org/packages/81/59/c7b0815a78fd641141f24a6ece878293eae6bf1fce40632a6ab9672346aa/protobuf-3.7.1-cp35-cp35m-manylinux1_x86_64.whl (1.2MB) 100% |################################| 1.2MB 18.5MB/s ta 0:00:01 Collecting termcolor>=1.1.0 (from tensorflow) Downloading https://files.pythonhosted.org/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz Collecting tensorboard<1.14.0,>=1.13.0 (from tensorflow) Downloading https://files.pythonhosted.org/packages/0f/39/bdd75b08a6fba41f098b6cb091b9e8c7a80e1b4d679a581a0ccd17b10373/tensorboard-1.13.1-py3-none-any.whl (3.2MB) 100% |################################| 3.2MB 13.0MB/s ta 0:00:01 0:00:01 Requirement already satisfied: keras-preprocessing>=1.0.5 in /home/idies/miniconda3/lib/python3.5/site-packages (from tensorflow) (1.0.9) Requirement already satisfied: keras-applications>=1.0.6 in /home/idies/miniconda3/lib/python3.5/site-packages (from tensorflow) (1.0.7) Requirement already satisfied: numpy>=1.13.3 in /home/idies/miniconda3/lib/python3.5/site-packages (from tensorflow) (1.15.2) Collecting grpcio>=1.8.6 (from tensorflow) Downloading https://files.pythonhosted.org/packages/0e/fd/e6696e5b115f328c382dd88414168e2b918cb7153b59dc9228d3c15e356c/grpcio-1.19.0-cp35-cp35m-manylinux1_x86_64.whl (10.8MB) 100% |################################| 10.8MB 5.8MB/s eta 0:00:01 | 266kB 49.5MB/s eta 0:00:01 | 2.5MB 54.2MB/s eta 0:00:01B/s eta 0:00:01###### | 7.5MB 43.3MB/s eta 0:00:01 Collecting astor>=0.6.0 (from tensorflow) Downloading https://files.pythonhosted.org/packages/35/6b/11530768cac581a12952a2aad00e1526b89d242d0b9f59534ef6e6a1752f/astor-0.7.1-py2.py3-none-any.whl Collecting absl-py>=0.1.6 (from tensorflow) Downloading https://files.pythonhosted.org/packages/da/3f/9b0355080b81b15ba6a9ffcf1f5ea39e307a2778b2f2dc8694724e8abd5b/absl-py-0.7.1.tar.gz (99kB) 100% |################################| 102kB 23.8MB/s a 0:00:01 Collecting mock>=2.0.0 (from tensorflow-estimator<1.14.0rc0,>=1.13.0->tensorflow) Downloading https://files.pythonhosted.org/packages/e6/35/f187bdf23be87092bd0f1200d43d23076cee4d0dec109f195173fd3ebc79/mock-2.0.0-py2.py3-none-any.whl (56kB) 100% |################################| 61kB 14.0MB/s ta 0:00:01 Requirement already satisfied: setuptools in /home/idies/miniconda3/lib/python3.5/site-packages (from protobuf>=3.6.1->tensorflow) (40.2.0) Collecting markdown>=2.6.8 (from tensorboard<1.14.0,>=1.13.0->tensorflow) Downloading https://files.pythonhosted.org/packages/f5/e4/d8c18f2555add57ff21bf25af36d827145896a07607486cc79a2aea641af/Markdown-3.1-py2.py3-none-any.whl (87kB) 100% |################################| 92kB 21.9MB/s ta 0:00:01 Requirement already satisfied: werkzeug>=0.11.15 in /home/idies/miniconda3/lib/python3.5/site-packages (from tensorboard<1.14.0,>=1.13.0->tensorflow) (0.14.1) Requirement already satisfied: h5py in /home/idies/miniconda3/lib/python3.5/site-packages (from keras-applications>=1.0.6->tensorflow) (2.8.0) Collecting pbr>=0.11 (from mock>=2.0.0->tensorflow-estimator<1.14.0rc0,>=1.13.0->tensorflow) Downloading https://files.pythonhosted.org/packages/14/09/12fe9a14237a6b7e0ba3a8d6fcf254bf4b10ec56a0185f73d651145e9222/pbr-5.1.3-py2.py3-none-any.whl (107kB) 100% |################################| 112kB 24.5MB/s ta 0:00:01 Building wheels for collected packages: gast, termcolor, absl-py Running setup.py bdist_wheel for gast ... done Stored in directory: /home/idies/.cache/pip/wheels/5c/2e/7e/a1d4d4fcebe6c381f378ce7743a3ced3699feb89bcfbdadadd Running setup.py bdist_wheel for termcolor ... done Stored in directory: /home/idies/.cache/pip/wheels/7c/06/54/bc84598ba1daf8f970247f550b175aaaee85f68b4b0c5ab2c6 Running setup.py bdist_wheel for absl-py ... done Stored in directory: /home/idies/.cache/pip/wheels/ee/98/38/46cbcc5a93cfea5492d19c38562691ddb23b940176c14f7b48 Successfully built gast termcolor absl-py twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed. Installing collected packages: pbr, mock, absl-py, tensorflow-estimator, gast, protobuf, termcolor, grpcio, markdown, tensorboard, astor, tensorflow Successfully installed absl-py-0.7.1 astor-0.7.1 gast-0.2.2 grpcio-1.19.0 markdown-3.1 mock-2.0.0 pbr-5.1.3 protobuf-3.7.1 tensorboard-1.13.1 tensorflow-1.13.1 tensorflow-estimator-1.13.0 termcolor-1.1.0 You are using pip version 10.0.1, however version 19.0.3 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D
from keras.models import Model
from keras.optimizers import Adam
# dimensions of our images.
img_width, img_height = 299, 299
train_data_dir = './data/IMG/Open_I_abd_vs_CXRs/TRAIN' #location of training data
validation_data_dir = './data/IMG/Open_I_abd_vs_CXRs/VAL' #location of validation data
# number of samples used for determining the samples_per_epoch
nb_train_samples = 65
nb_validation_samples = 10
epochs = 20
batch_size = 5
train_datagen = ImageDataGenerator(
rescale=1./255, # normalize pixel values to [0,1]
shear_range=0.2,
zoom_range=0.2,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
val_datagen = ImageDataGenerator(
rescale=1./255) # normalize pixel values to [0,1]
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
validation_generator = train_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
Using TensorFlow backend.
Found 65 images belonging to 2 classes. Found 10 images belonging to 2 classes.
import sys
from PIL import Image
sys.modules['Image'] = Image
from PIL import Image
print(Image.__file__)
import Image
print(Image.__file__)
/home/idies/miniconda3/lib/python3.5/site-packages/PIL/Image.py /home/idies/miniconda3/lib/python3.5/site-packages/PIL/Image.py
base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3))
WARNING:tensorflow:From /home/idies/miniconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 87916544/87910968 [==============================] - 5s 0us/step
model_top = Sequential()
model_top.add(GlobalAveragePooling2D(input_shape=base_model.output_shape[1:], data_format=None)),
model_top.add(Dense(256, activation='relu'))
model_top.add(Dropout(0.5))
model_top.add(Dense(1, activation='sigmoid'))
model = Model(inputs=base_model.input, outputs=model_top(base_model.output))
model.compile(optimizer=Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08,decay=0.0), loss='binary_crossentropy', metrics=['accuracy'])
WARNING:tensorflow:From /home/idies/miniconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version. Instructions for updating: Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
history = model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
WARNING:tensorflow:From /home/idies/miniconda3/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version. Instructions for updating: Use tf.cast instead. Epoch 1/20 13/13 [==============================] - 38s 3s/step - loss: 0.4865 - acc: 0.7538 - val_loss: 0.1829 - val_acc: 1.0000 Epoch 2/20 13/13 [==============================] - 20s 2s/step - loss: 0.1206 - acc: 0.9692 - val_loss: 0.0244 - val_acc: 1.0000 Epoch 3/20 13/13 [==============================] - 20s 2s/step - loss: 0.1501 - acc: 0.9538 - val_loss: 0.0163 - val_acc: 1.0000 Epoch 4/20 13/13 [==============================] - 20s 2s/step - loss: 0.1402 - acc: 0.9385 - val_loss: 0.0054 - val_acc: 1.0000 Epoch 5/20 13/13 [==============================] - 19s 1s/step - loss: 0.0197 - acc: 1.0000 - val_loss: 0.0044 - val_acc: 1.0000 Epoch 6/20 13/13 [==============================] - 19s 1s/step - loss: 0.0314 - acc: 0.9846 - val_loss: 0.0020 - val_acc: 1.0000 Epoch 7/20 13/13 [==============================] - 19s 1s/step - loss: 0.0235 - acc: 1.0000 - val_loss: 0.0015 - val_acc: 1.0000 Epoch 8/20 13/13 [==============================] - 19s 1s/step - loss: 0.0224 - acc: 0.9846 - val_loss: 0.0022 - val_acc: 1.0000 Epoch 9/20 13/13 [==============================] - 19s 1s/step - loss: 0.1544 - acc: 0.9077 - val_loss: 0.0036 - val_acc: 1.0000 Epoch 10/20 13/13 [==============================] - 19s 1s/step - loss: 0.0564 - acc: 0.9846 - val_loss: 0.0353 - val_acc: 1.0000 Epoch 11/20 13/13 [==============================] - 19s 1s/step - loss: 0.1560 - acc: 0.9231 - val_loss: 0.0273 - val_acc: 1.0000 Epoch 12/20 13/13 [==============================] - 19s 1s/step - loss: 0.2866 - acc: 0.9538 - val_loss: 0.0077 - val_acc: 1.0000 Epoch 13/20 13/13 [==============================] - 19s 1s/step - loss: 0.0494 - acc: 0.9846 - val_loss: 0.0035 - val_acc: 1.0000 Epoch 14/20 13/13 [==============================] - 19s 1s/step - loss: 0.0542 - acc: 0.9846 - val_loss: 7.1194e-04 - val_acc: 1.0000 Epoch 15/20 13/13 [==============================] - 20s 2s/step - loss: 0.1190 - acc: 0.9692 - val_loss: 0.0102 - val_acc: 1.0000 Epoch 16/20 13/13 [==============================] - 20s 2s/step - loss: 0.1796 - acc: 0.9538 - val_loss: 0.0098 - val_acc: 1.0000 Epoch 17/20 13/13 [==============================] - 19s 1s/step - loss: 0.1472 - acc: 0.9538 - val_loss: 0.0026 - val_acc: 1.0000 Epoch 18/20 13/13 [==============================] - 19s 1s/step - loss: 0.1824 - acc: 0.9077 - val_loss: 0.0370 - val_acc: 1.0000 Epoch 19/20 13/13 [==============================] - 19s 1s/step - loss: 0.0726 - acc: 1.0000 - val_loss: 0.0185 - val_acc: 1.0000 Epoch 20/20 13/13 [==============================] - 19s 1s/step - loss: 0.0414 - acc: 0.9846 - val_loss: 0.0038 - val_acc: 1.0000
import matplotlib.pyplot as plt
print(history.history.keys())
plt.figure()
plt.plot(history.history['acc'], 'orange', label='Training accuracy')
plt.plot(history.history['val_acc'], 'blue', label='Validation accuracy')
plt.plot(history.history['loss'], 'red', label='Training loss')
plt.plot(history.history['val_loss'], 'green', label='Validation loss')
plt.legend()
plt.show()
dict_keys(['val_acc', 'acc', 'val_loss', 'loss'])
import numpy as np
from keras.preprocessing import image
img_path='./data/IMG/Open_I_abd_vs_CXRs/TEST/chest2.png' #change to location of chest x-ray
img_path2='./data/IMG/Open_I_abd_vs_CXRs/TEST/abd2.png' #change to location of abd x-ray
img = image.load_img(img_path, target_size=(img_width, img_height))
img2 = image.load_img(img_path2, target_size=(img_width, img_height))
plt.imshow(img)
plt.show()
img = image.img_to_array(img)
x = np.expand_dims(img, axis=0) * 1./255
score = model.predict(x)
print('Predicted:', score, 'Chest X-ray' if score < 0.5 else 'Abd X-ray')
plt.imshow(img2)
plt.show()
img = image.img_to_array(img2)
x = np.expand_dims(img2, axis=0) * 1./255
score2 = model.predict(x)
print('Predicted:', score2, 'Chest X-ray' if score2 < 0.5 else 'Abd X-ray')
Predicted: [[0.00021414]] Chest X-ray
Predicted: [[0.9998468]] Abd X-ray