DETECTION OF TUBERS WITH CONVOLUTIONAL NEURAL NETWORKS

PERFORMANCE IN THE TEST SET

Import packages and functions

In [1]:
# Import packages
%matplotlib inline
from PIL import Image
import numpy as np
import os
from skimage.color import gray2rgb
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
!pip install tensorflow
!pip install keras
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, GaussianNoise, BatchNormalization, GlobalAveragePooling2D
from keras.layers import Conv2D, MaxPooling2D
from keras import Sequential
from keras.optimizers import Adam
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
from keras.preprocessing import image
from keras.models import Model
from keras import backend as K
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_auc_score
!pip install git+https://github.com/raghakot/keras-vis.git --upgrade
from vis.visualization import visualize_cam, visualize_saliency, overlay
from keras import activations
from matplotlib import pyplot as plt
import matplotlib.cm as cm
import zipfile
from keras.models import model_from_json
import matplotlib as mpl
Requirement already satisfied: tensorflow in c:\programdata\anaconda3\lib\site-packages (1.12.0)
Requirement already satisfied: absl-py>=0.1.6 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (0.7.0)
Requirement already satisfied: protobuf>=3.6.1 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (3.6.1)
Requirement already satisfied: keras-applications>=1.0.6 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (1.0.6)
Requirement already satisfied: numpy>=1.13.3 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (1.14.3)
Requirement already satisfied: grpcio>=1.8.6 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (1.18.0)
Requirement already satisfied: gast>=0.2.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (0.2.2)
Requirement already satisfied: wheel>=0.26 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (0.32.3)
Requirement already satisfied: astor>=0.6.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (0.7.1)
Requirement already satisfied: tensorboard<1.13.0,>=1.12.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (1.12.2)
Requirement already satisfied: keras-preprocessing>=1.0.5 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (1.0.5)
Requirement already satisfied: termcolor>=1.1.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (1.1.0)
Requirement already satisfied: six>=1.10.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow) (1.11.0)
Requirement already satisfied: setuptools in c:\programdata\anaconda3\lib\site-packages (from protobuf>=3.6.1->tensorflow) (40.6.3)
Requirement already satisfied: h5py in c:\programdata\anaconda3\lib\site-packages (from keras-applications>=1.0.6->tensorflow) (2.7.1)
Requirement already satisfied: werkzeug>=0.11.10 in c:\programdata\anaconda3\lib\site-packages (from tensorboard<1.13.0,>=1.12.0->tensorflow) (0.14.1)
Requirement already satisfied: markdown>=2.6.8 in c:\programdata\anaconda3\lib\site-packages (from tensorboard<1.13.0,>=1.12.0->tensorflow) (3.0.1)
You are using pip version 19.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Requirement already satisfied: keras in c:\programdata\anaconda3\lib\site-packages (2.2.4)
Requirement already satisfied: six>=1.9.0 in c:\programdata\anaconda3\lib\site-packages (from keras) (1.11.0)
Requirement already satisfied: keras-preprocessing>=1.0.5 in c:\programdata\anaconda3\lib\site-packages (from keras) (1.0.5)
Requirement already satisfied: pyyaml in c:\programdata\anaconda3\lib\site-packages (from keras) (3.12)
Requirement already satisfied: numpy>=1.9.1 in c:\programdata\anaconda3\lib\site-packages (from keras) (1.14.3)
Requirement already satisfied: scipy>=0.14 in c:\programdata\anaconda3\lib\site-packages (from keras) (1.1.0)
Requirement already satisfied: h5py in c:\programdata\anaconda3\lib\site-packages (from keras) (2.7.1)
Requirement already satisfied: keras-applications>=1.0.6 in c:\programdata\anaconda3\lib\site-packages (from keras) (1.0.6)
You are using pip version 19.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
C:\ProgramData\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
Collecting git+https://github.com/raghakot/keras-vis.git
  Cloning https://github.com/raghakot/keras-vis.git to c:\users\tempch~1.021\appdata\local\temp\pip-req-build-afpxhknq
Requirement already satisfied, skipping upgrade: keras in c:\programdata\anaconda3\lib\site-packages (from keras-vis==0.4.1) (2.2.4)
Requirement already satisfied, skipping upgrade: six in c:\programdata\anaconda3\lib\site-packages (from keras-vis==0.4.1) (1.11.0)
Requirement already satisfied, skipping upgrade: scikit-image in c:\programdata\anaconda3\lib\site-packages (from keras-vis==0.4.1) (0.13.1)
Requirement already satisfied, skipping upgrade: matplotlib in c:\programdata\anaconda3\lib\site-packages (from keras-vis==0.4.1) (2.2.2)
Requirement already satisfied, skipping upgrade: h5py in c:\programdata\anaconda3\lib\site-packages (from keras-vis==0.4.1) (2.7.1)
Requirement already satisfied, skipping upgrade: scipy>=0.14 in c:\programdata\anaconda3\lib\site-packages (from keras->keras-vis==0.4.1) (1.1.0)
Requirement already satisfied, skipping upgrade: numpy>=1.9.1 in c:\programdata\anaconda3\lib\site-packages (from keras->keras-vis==0.4.1) (1.14.3)
Requirement already satisfied, skipping upgrade: keras-applications>=1.0.6 in c:\programdata\anaconda3\lib\site-packages (from keras->keras-vis==0.4.1) (1.0.6)
Requirement already satisfied, skipping upgrade: pyyaml in c:\programdata\anaconda3\lib\site-packages (from keras->keras-vis==0.4.1) (3.12)
Requirement already satisfied, skipping upgrade: keras-preprocessing>=1.0.5 in c:\programdata\anaconda3\lib\site-packages (from keras->keras-vis==0.4.1) (1.0.5)
Requirement already satisfied, skipping upgrade: networkx>=1.8 in c:\programdata\anaconda3\lib\site-packages (from scikit-image->keras-vis==0.4.1) (2.1)
Requirement already satisfied, skipping upgrade: pillow>=2.1.0 in c:\programdata\anaconda3\lib\site-packages (from scikit-image->keras-vis==0.4.1) (5.1.0)
Requirement already satisfied, skipping upgrade: PyWavelets>=0.4.0 in c:\programdata\anaconda3\lib\site-packages (from scikit-image->keras-vis==0.4.1) (0.5.2)
Requirement already satisfied, skipping upgrade: cycler>=0.10 in c:\programdata\anaconda3\lib\site-packages (from matplotlib->keras-vis==0.4.1) (0.10.0)
Requirement already satisfied, skipping upgrade: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\programdata\anaconda3\lib\site-packages (from matplotlib->keras-vis==0.4.1) (2.2.0)
Requirement already satisfied, skipping upgrade: python-dateutil>=2.1 in c:\programdata\anaconda3\lib\site-packages (from matplotlib->keras-vis==0.4.1) (2.7.3)
Requirement already satisfied, skipping upgrade: pytz in c:\programdata\anaconda3\lib\site-packages (from matplotlib->keras-vis==0.4.1) (2018.4)
Requirement already satisfied, skipping upgrade: kiwisolver>=1.0.1 in c:\programdata\anaconda3\lib\site-packages (from matplotlib->keras-vis==0.4.1) (1.0.1)
Requirement already satisfied, skipping upgrade: decorator>=4.1.0 in c:\programdata\anaconda3\lib\site-packages (from networkx>=1.8->scikit-image->keras-vis==0.4.1) (4.3.0)
Requirement already satisfied, skipping upgrade: setuptools in c:\programdata\anaconda3\lib\site-packages (from kiwisolver>=1.0.1->matplotlib->keras-vis==0.4.1) (40.6.3)
Building wheels for collected packages: keras-vis
  Building wheel for keras-vis (setup.py): started
  Building wheel for keras-vis (setup.py): finished with status 'done'
  Stored in directory: C:\Users\TEMPCH~1.021\AppData\Local\Temp\pip-ephem-wheel-cache-8oz3u7ma\wheels\c5\ae\e7\b34d1cb48b1898f606a5cce08ebc9521fa0588f37f1e590d9f
Successfully built keras-vis
Installing collected packages: keras-vis
  Found existing installation: keras-vis 0.4.1
    Uninstalling keras-vis-0.4.1:
      Successfully uninstalled keras-vis-0.4.1
Successfully installed keras-vis-0.4.1
You are using pip version 19.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

FIRST PART: DATA INGESTION

Import original images from local computer

We used Magnetic Resonance Imaging (MRI) scans from 114 patients with tuberous sclerosis complex (TSC) and from 114 patients with structurally normal MRI (controls).

For each MRI, we manually selected representative axial T2 and T2 FLAIR slices with tubers (in patients with TSC) and with normal findings (in controls). These axial slices were converted to deidentified .jpg images.

We created three folders per TSC and three folders for controls: TSCtrain (566 images), TSCval (130 images), and TSCtest (210 images) and Controltrain (561 images), Controlval (118 images), and Controltest (226 images). Individual patients belonged to only one of the categories (none of the patients had images in different folders).

For the model development part done in a cloud computer environment we only used the train and validation subset. We selected the model with lowest binary cross-entropy error in the validation set as the best model. The best model (InceptionV3) was saved and its performance was evaluated in the local computer on the test set (data not seen previously by the model).

In [2]:
# Set the figure size
mpl.rcParams['figure.figsize'] = (16,10)
In [3]:
# Unzip files
with zipfile.ZipFile("Controltest.zip","r") as zip_ref:
    zip_ref.extractall()
with zipfile.ZipFile("TSCtest.zip","r") as zip_ref:
    zip_ref.extractall()

Path to original images folder

In [4]:
# Path to the folder with the original images
pathtoimagesControltest = './Controltest/'

pathtoimagesTSCtest = './TSCtest/'

SECOND PART: IMPORTATION OF FINAL DATA

Import images and labels for the test set

In [5]:
## CONTROLS

# Define the image size
image_size = (224, 224)

# Read in the test images for controls
Controltest_images = []
Controltest_dir = pathtoimagesControltest
Controltest_files = os.listdir(Controltest_dir)
# For each image
for f in Controltest_files:
  # Open the image
  img = Image.open(Controltest_dir + f)
  # Resize the image so that it has a size 224x224
  img = img.resize(image_size)
  # Transform into a numpy array
  img_arr = np.array(img)
  # Transform from 224x224 to 224x224x3
  if img_arr.shape == image_size:
        img_arr = np.expand_dims(img_arr, 3)
        img_arr = gray2rgb(img_arr[:, :, 0])
  # Add the image to the array of images      
  Controltest_images.append(img_arr)

# After having transformed all images, transform the list into a numpy array  
Controltest_X = np.array(Controltest_images)

# Create an array of labels (0 for controls)
Controltest_y = np.array([[0]*Controltest_X.shape[0]]).T



## TSC

# Read in the test images for TSC
TSCtest_images = []
TSCtest_dir = pathtoimagesTSCtest
TSCtest_files = os.listdir(TSCtest_dir)
# For each image
for f in TSCtest_files:
  # Open the image
  img = Image.open(TSCtest_dir + f)
  # Resize the image so that it has a size 224x224
  img = img.resize(image_size)
  # Transform into a numpy array
  img_arr = np.array(img)
  # Transform from 224x224 to 224x224x3
  if img_arr.shape == image_size:
        img_arr = np.expand_dims(img_arr, 3)
        img_arr = gray2rgb(img_arr[:, :, 0])
  # Add the image to the array of images      
  TSCtest_images.append(img_arr)

# After having transformed all images, transform the list into a numpy array  
TSCtest_X = np.array(TSCtest_images)

# Create an array of labels (1 for TSC)
TSCtest_y = np.array([[1]*TSCtest_X.shape[0]]).T


## MERGE CONTROLS AND TSC

# Train merge files
test_X = np.concatenate([Controltest_X, TSCtest_X])
test_y = np.vstack((Controltest_y, TSCtest_y))

# GPU expects values to be 32-bit floats
test_X = test_X.astype(np.float32)

# Rescale the values to be between 0 and 1
test_X /= 255.
In [6]:
# Shuffle in unison the test_X and the test_y array (123 is just a random number for reproducibility)
shuffled_test_X, shuffled_test_y = shuffle(test_X, test_y, random_state=123)
In [7]:
shuffled_test_X.shape
Out[7]:
(436, 224, 224, 3)
In [8]:
# Example of an image to make sure they were converted right
plt.imshow(shuffled_test_X[0])
plt.grid(b=None)
plt.xticks([])
plt.yticks([])
plt.show()
In [9]:
shuffled_test_y.shape
Out[9]:
(436, 1)
In [10]:
shuffled_test_y[0]
Out[10]:
array([0])

THIRD PART: EVALUATE NEURAL NETWORK PERFORMANCE IN THE TEST SET

Load the model

In [11]:
# load model
json_file = open('InceptionV3.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load weights into new model
model.load_weights("InceptionV3.h5")

Test the model with the test data

In [12]:
# Compile model
model.compile(optimizer = Adam(lr = 0.00025), loss = 'binary_crossentropy', metrics = ['accuracy'])

# Generate predictions on test data in the form of probabilities
testInceptionV3 = model.predict(shuffled_test_X, batch_size = 16)
testInceptionV3
Out[12]:
array([[2.43405538e-07],
       [9.99999881e-01],
       [9.01499987e-01],
       [4.52990451e-10],
       [1.00000000e+00],
       [9.99899387e-01],
       [9.99619007e-01],
       [9.53212930e-06],
       [4.36156202e-04],
       [1.00000000e+00],
       [2.19751910e-05],
       [9.12895121e-06],
       [1.44296419e-05],
       [5.76829016e-01],
       [9.99997020e-01],
       [1.00000000e+00],
       [4.29008162e-09],
       [2.01692616e-04],
       [5.70827183e-07],
       [4.94313269e-07],
       [1.83460597e-05],
       [9.99971628e-01],
       [9.97111917e-01],
       [1.99091883e-05],
       [1.75167008e-06],
       [1.36609728e-04],
       [9.44846867e-09],
       [1.00000000e+00],
       [9.99947190e-01],
       [1.24520564e-03],
       [4.99550079e-05],
       [1.52865258e-08],
       [9.99686122e-01],
       [9.99995589e-01],
       [1.00000000e+00],
       [9.92720379e-06],
       [9.99999642e-01],
       [1.13511658e-06],
       [1.47998717e-03],
       [3.86229454e-04],
       [6.26599899e-07],
       [8.90807883e-07],
       [1.00000000e+00],
       [1.95838993e-06],
       [1.00000000e+00],
       [9.99980927e-01],
       [9.99996781e-01],
       [9.99999523e-01],
       [6.67436933e-03],
       [9.99999523e-01],
       [9.36557569e-07],
       [5.17932852e-09],
       [9.09236431e-01],
       [9.99996305e-01],
       [9.14071803e-04],
       [1.00000000e+00],
       [1.00000000e+00],
       [8.78150695e-06],
       [3.37022470e-06],
       [9.99992490e-01],
       [9.99991417e-01],
       [1.00000000e+00],
       [9.99999523e-01],
       [9.99999166e-01],
       [9.99992847e-01],
       [9.93806243e-01],
       [9.99911427e-01],
       [9.99999285e-01],
       [2.57578836e-06],
       [1.00000000e+00],
       [7.39958807e-07],
       [2.38763096e-04],
       [9.99930143e-01],
       [8.50901358e-07],
       [7.58931128e-06],
       [2.78671962e-07],
       [9.15486282e-07],
       [9.99999881e-01],
       [1.00000000e+00],
       [9.99963403e-01],
       [5.66391009e-06],
       [9.99995828e-01],
       [9.99981165e-01],
       [9.99999285e-01],
       [8.26536953e-01],
       [3.68892361e-05],
       [9.72648412e-02],
       [1.38560108e-06],
       [1.57254465e-06],
       [8.71579596e-06],
       [1.93003507e-06],
       [9.99424815e-01],
       [9.99999285e-01],
       [9.99997735e-01],
       [9.99999523e-01],
       [9.12739233e-06],
       [1.05811214e-05],
       [9.92227197e-01],
       [2.85860624e-09],
       [4.96164648e-06],
       [9.98095214e-01],
       [1.93250726e-05],
       [9.99999642e-01],
       [9.89017606e-01],
       [9.99993443e-01],
       [4.88928572e-06],
       [2.08852771e-06],
       [9.84587669e-01],
       [1.00000000e+00],
       [9.61302996e-01],
       [2.12924377e-07],
       [7.80876599e-06],
       [9.99975801e-01],
       [3.46733532e-06],
       [9.99999881e-01],
       [9.99999762e-01],
       [9.99999642e-01],
       [1.18484129e-08],
       [1.39628838e-07],
       [3.12320911e-03],
       [1.08056511e-05],
       [1.09157270e-06],
       [9.99948502e-01],
       [2.00679619e-06],
       [2.67193955e-05],
       [9.99999523e-01],
       [9.99988556e-01],
       [3.09712448e-07],
       [2.04323107e-04],
       [9.99968529e-01],
       [9.99930263e-01],
       [1.10555891e-06],
       [2.86858437e-07],
       [5.82113522e-08],
       [3.30873195e-06],
       [9.99733865e-01],
       [7.01935530e-01],
       [7.75766154e-08],
       [1.51646545e-05],
       [9.98028219e-01],
       [9.99653816e-01],
       [1.39514407e-06],
       [3.93575903e-07],
       [8.68970744e-07],
       [2.34795016e-06],
       [4.43305134e-06],
       [1.40148870e-07],
       [1.91756294e-06],
       [9.99876142e-01],
       [3.27452653e-07],
       [9.99964237e-01],
       [9.99285042e-01],
       [9.98937666e-01],
       [9.99990225e-01],
       [4.59614603e-05],
       [1.84920264e-05],
       [9.99999762e-01],
       [9.99999166e-01],
       [2.33475760e-07],
       [8.30561135e-07],
       [1.93003507e-06],
       [1.00000000e+00],
       [9.97202158e-01],
       [9.99999404e-01],
       [9.99995470e-01],
       [6.63827814e-06],
       [9.14071803e-04],
       [9.99910235e-01],
       [9.99999881e-01],
       [2.52396205e-07],
       [2.16726025e-04],
       [8.53711356e-07],
       [1.37430770e-05],
       [9.17539467e-07],
       [4.24050599e-01],
       [9.99994993e-01],
       [1.00000000e+00],
       [9.99577820e-01],
       [7.07235992e-01],
       [5.10121281e-05],
       [6.43147578e-05],
       [5.72113204e-05],
       [9.99826849e-01],
       [9.99992371e-01],
       [9.99999166e-01],
       [9.99882936e-01],
       [1.24425620e-01],
       [1.00000000e+00],
       [1.00000000e+00],
       [4.83751194e-07],
       [4.85845567e-06],
       [9.99996543e-01],
       [9.99875784e-01],
       [6.56585897e-08],
       [8.15486431e-01],
       [9.99999881e-01],
       [9.75798775e-06],
       [2.12856685e-06],
       [8.52648668e-07],
       [2.96742019e-06],
       [1.00000000e+00],
       [9.99998808e-01],
       [9.98776019e-01],
       [8.50271078e-07],
       [1.93629785e-06],
       [4.82187497e-05],
       [2.22305898e-04],
       [2.57187960e-08],
       [1.25044053e-02],
       [9.99999881e-01],
       [2.10544349e-05],
       [3.59065707e-05],
       [9.99975324e-01],
       [7.83074302e-06],
       [9.99307990e-01],
       [9.99999762e-01],
       [9.99973655e-01],
       [9.99824822e-01],
       [1.00000000e+00],
       [3.56540431e-06],
       [8.40568703e-07],
       [9.99995232e-01],
       [9.96831357e-01],
       [9.99995232e-01],
       [1.09474081e-06],
       [9.99999762e-01],
       [1.72345608e-04],
       [1.66819867e-04],
       [9.99985456e-01],
       [7.48084188e-02],
       [1.00000000e+00],
       [3.37075704e-07],
       [9.99994159e-01],
       [1.29204607e-02],
       [9.86172259e-01],
       [9.99910116e-01],
       [3.13276428e-06],
       [9.99952435e-01],
       [1.03139042e-09],
       [9.99566138e-01],
       [4.64915189e-07],
       [4.03418198e-06],
       [1.88522893e-06],
       [9.70047832e-01],
       [9.91066754e-01],
       [2.72409161e-05],
       [9.99999881e-01],
       [9.99954939e-01],
       [9.99997735e-01],
       [9.99975443e-01],
       [1.95475714e-05],
       [3.57289969e-06],
       [1.82200802e-06],
       [8.45986302e-04],
       [2.64389143e-07],
       [9.99493241e-01],
       [1.44836463e-06],
       [1.96490092e-07],
       [1.85825613e-07],
       [9.91995692e-01],
       [4.30435364e-07],
       [9.99818981e-01],
       [3.33739627e-05],
       [1.74563820e-06],
       [9.99694347e-01],
       [9.99931455e-01],
       [9.99975801e-01],
       [3.48792696e-07],
       [4.01889774e-05],
       [6.46488774e-09],
       [1.00000000e+00],
       [7.20456956e-06],
       [9.99978065e-01],
       [1.77645159e-03],
       [2.81119696e-03],
       [1.00000000e+00],
       [6.66898050e-05],
       [9.99999762e-01],
       [7.37445635e-06],
       [9.99886155e-01],
       [6.85238274e-06],
       [8.86852922e-06],
       [8.43624651e-01],
       [3.45775130e-04],
       [6.96775686e-08],
       [9.99999762e-01],
       [9.99999404e-01],
       [9.99999166e-01],
       [3.21507215e-01],
       [4.58649563e-09],
       [9.99999881e-01],
       [4.08076419e-04],
       [9.99999642e-01],
       [9.99696016e-01],
       [9.99489903e-01],
       [9.97400165e-01],
       [9.99605000e-01],
       [9.97565627e-01],
       [1.94845404e-02],
       [9.99994993e-01],
       [9.30837359e-07],
       [8.11992228e-01],
       [9.78742123e-01],
       [2.46793911e-06],
       [2.13773137e-06],
       [1.65188010e-06],
       [3.94568360e-06],
       [4.02472389e-04],
       [9.73684311e-01],
       [3.09282466e-09],
       [1.22086448e-03],
       [3.01903196e-06],
       [9.99992490e-01],
       [9.99999881e-01],
       [9.99983430e-01],
       [9.99999762e-01],
       [5.92942251e-06],
       [9.99999881e-01],
       [1.00000000e+00],
       [9.99963522e-01],
       [9.99998927e-01],
       [9.99999046e-01],
       [5.62102741e-07],
       [1.79076937e-06],
       [8.21919031e-08],
       [4.37546561e-07],
       [3.32084738e-07],
       [9.99998450e-01],
       [9.76750195e-01],
       [9.99999523e-01],
       [2.38272651e-06],
       [9.27081227e-01],
       [9.99999523e-01],
       [5.61386605e-06],
       [3.55203269e-06],
       [1.00630233e-08],
       [4.31476423e-04],
       [4.68991348e-05],
       [9.81975257e-01],
       [9.98980224e-01],
       [1.81150390e-03],
       [9.90629852e-01],
       [9.99999762e-01],
       [1.00000000e+00],
       [2.26751974e-04],
       [1.44111155e-07],
       [6.75364499e-05],
       [3.24615456e-07],
       [8.94322568e-07],
       [9.99555886e-01],
       [1.08350559e-06],
       [9.99271095e-01],
       [9.99998450e-01],
       [7.84169060e-06],
       [5.04698073e-06],
       [5.59065654e-07],
       [9.00035083e-01],
       [9.99999881e-01],
       [9.99099135e-01],
       [1.75303072e-07],
       [6.80260062e-01],
       [9.98252094e-01],
       [7.89726151e-09],
       [3.72036266e-05],
       [1.26098812e-05],
       [3.97176668e-02],
       [9.97402489e-01],
       [9.99552786e-01],
       [9.99998689e-01],
       [9.99993563e-01],
       [9.40473080e-01],
       [9.99342978e-01],
       [9.99999881e-01],
       [2.09644483e-07],
       [1.00000000e+00],
       [1.98452153e-06],
       [9.99951959e-01],
       [1.00000000e+00],
       [3.36129091e-07],
       [4.34278995e-08],
       [9.99842882e-01],
       [9.99910712e-01],
       [8.94435644e-01],
       [4.05900801e-06],
       [9.99921918e-01],
       [4.03297972e-03],
       [2.25482807e-01],
       [2.82312249e-06],
       [9.99886990e-01],
       [9.99959588e-01],
       [4.49770170e-07],
       [3.15843174e-08],
       [7.77446330e-01],
       [6.27271652e-01],
       [1.16031920e-07],
       [9.99850869e-01],
       [1.00000000e+00],
       [1.12882018e-01],
       [9.57081199e-01],
       [2.17957041e-10],
       [3.01763976e-07],
       [9.99995947e-01],
       [2.31166996e-06],
       [9.99999642e-01],
       [1.58532259e-06],
       [9.99999404e-01],
       [2.36685587e-08],
       [9.99758542e-01],
       [9.99639750e-01],
       [3.81410814e-07],
       [3.48208164e-06],
       [1.00000000e+00],
       [9.99996781e-01],
       [5.10132959e-05],
       [9.99998093e-01],
       [9.99999046e-01],
       [4.70237137e-06],
       [2.89103070e-07],
       [4.08229243e-06],
       [3.96602900e-06],
       [4.96993437e-07],
       [3.00847347e-07],
       [2.31077274e-06],
       [2.96421528e-01],
       [2.83215655e-07],
       [7.59071327e-07],
       [1.20321975e-08],
       [5.44969794e-07],
       [1.21758749e-05],
       [1.76661597e-05],
       [5.80039853e-07],
       [9.99999881e-01],
       [2.34249607e-03],
       [9.99999762e-01],
       [9.99910831e-01],
       [9.99687791e-01]], dtype=float32)
In [13]:
# Generate the confusion matrix
y_true = shuffled_test_y
y_predInceptionV3 = testInceptionV3 > 0.5

confusion_matrix(y_true, y_predInceptionV3)
Out[13]:
array([[214,  12],
       [ 11, 199]], dtype=int64)
In [14]:
# Calculate accuracy in the test set
accuracy_InceptionV3 = (confusion_matrix(y_true, y_predInceptionV3)[0, 0] + confusion_matrix(y_true, y_predInceptionV3)[1, 1]) / (confusion_matrix(y_true, y_predInceptionV3)[0, 0] + confusion_matrix(y_true, y_predInceptionV3)[0, 1] + confusion_matrix(y_true, y_predInceptionV3)[1, 0] + confusion_matrix(y_true, y_predInceptionV3)[1, 1])
print('The accuracy in the test set is {}.'.format(accuracy_InceptionV3))
The accuracy in the test set is 0.9472477064220184.
In [15]:
# Calculate AUC in the test set
auc_testInceptionV3 = roc_auc_score(y_true, testInceptionV3)
print('The AUC in the test set is {}.'.format(auc_testInceptionV3))
The AUC in the test set is 0.989369995785925.