# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/ronniesong0809/chestX-ray/blob/master/chestX_ray.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="gqWWnLRWENCi" colab_type="code" colab={}
# !echo '{"username":"ronniesong0809","key":"269788090750b1eb7d55a04d86886708"}' > kaggle.json
# !cp /content/kaggle.json /root/.kaggle/kaggle.json

# + id="r4f3T6XZaqqj" colab_type="code" outputId="7b2e4784-9790-40b6-8f49-7ee4e0b316d2" colab={"base_uri": "https://localhost:8080/", "height": 34}
# !cp /content/kaggle.json /root/.kaggle/kaggle.json
# !chmod 600 /root/.kaggle/kaggle.json
# !kaggle config set -n path -v/content

# + id="WZi-s11tAfzP" colab_type="code" outputId="e9c8f715-fc0c-4996-9e51-6d4b11b0b3a5" colab={"base_uri": "https://localhost:8080/", "height": 68}
# !kaggle datasets download -d paultimothymooney/chest-xray-pneumonia

# + id="H6ZhhFl19DN-" colab_type="code" colab={}
# !unzip datasets/paultimothymooney/chest-xray-pneumonia/chest-xray-pneumonia.zip

# + id="bUHTcnt37Tuy" colab_type="code" colab={}
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
# %matplotlib inline

# + id="b8LbRxUn7XcB" colab_type="code" outputId="5d91fcc0-1d81-48fa-f0a5-19669bcc2831" colab={"base_uri": "https://localhost:8080/", "height": 34}
import keras
from keras.models import Sequential
from keras.layers import Conv2D,MaxPool2D,Dense,Dropout,Softmax,Input,Flatten, SeparableConv2D
from keras.optimizers import Adam,RMSprop,SGD
from keras.layers.merge import add
from keras.layers import Dense, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D, BatchNormalization
from keras.layers import BatchNormalization
from keras.metrics import categorical_accuracy
from keras.preprocessing.image import ImageDataGenerator

from sklearn.metrics import roc_auc_score,roc_curve,accuracy_score,recall_score

# from tensorflow import set_random_seed
import tensorflow as tf

# + id="tkSP5sFf7ed3" colab_type="code" colab={}
os.environ['PYTHONHASHSEED'] = "0"
np.random.seed(1)
# set_random_seed(2)
tf.random.set_seed(2)

# + [markdown] id="xAMgZSovBAt1" colab_type="text"
# ## Model 1: Simple CNN

# + id="YWbxCW1WA_0w" colab_type="code" colab={}
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3), activation="relu", padding="same", input_shape=(64,64,1)))
model.add(Conv2D(filters=32, kernel_size=(3,3), activation="relu", padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(rate=0.25))

model.add(Conv2D(filters=64, kernel_size=(3,3), activation="relu", padding="same"))
model.add(Conv2D(filters=64, kernel_size=(3,3), activation="relu", padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(rate=0.25))

model.add(Flatten())
model.add(Dense(1024,activation="relu"))
model.add(BatchNormalization())
model.add(Dropout(rate=0.4))
model.add(Dense(2, activation="softmax"))

# + [markdown] id="WIkjg-atC3OL" colab_type="text"
# ## Model 2: VGG16

# + id="-eEMxIhAC56O" colab_type="code" colab={}
# from keras.layers import SeparableConv2D, ZeroPadding2D

# model = Sequential()
# model.add(Conv2D(64, (3,3), activation='relu', padding='same', name='Conv1_1', input_shape=(224,224,1)))
# model.add(Conv2D(64, (3,3), activation="relu", padding="same"))
# # model.add(BatchNormalization())
# model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding="SAME"))

# model.add(SeparableConv2D(128, (3,3), activation='relu', padding='same'))
# model.add(SeparableConv2D(128, (3,3), activation='relu', padding='same'))
# # model.add(BatchNormalization())
# model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding="SAME"))

# model.add(SeparableConv2D(256, (3,3), activation='relu', padding='same'))
# model.add(SeparableConv2D(256, (3,3), activation='relu', padding='same'))
# model.add(SeparableConv2D(256, (3,3), activation='relu', padding='same'))
# # model.add(BatchNormalization())
# model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding="SAME"))

# model.add(SeparableConv2D(512, (3,3), activation='relu', padding='same'))
# model.add(SeparableConv2D(512, (3,3), activation='relu', padding='same'))
# model.add(SeparableConv2D(512, (3,3), activation='relu', padding='same'))
# # model.add(BatchNormalization())
# model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding="SAME"))

# model.add(SeparableConv2D(512, (3,3), activation='relu', padding='same'))
# model.add(SeparableConv2D(512, (3,3), activation='relu', padding='same'))
# model.add(SeparableConv2D(512, (3,3), activation='relu', padding='same'))
# # model.add(BatchNormalization())
# model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding="SAME"))

# model.add(Flatten())
# model.add(Dense(4096, activation='relu'))
# model.add(Dense(4096, activation='relu'))
# # model.add(Dropout(0.7))
# model.add(Dense(1000, activation='relu'))
# # model.add(Dropout(0.5))

# model.add(Dense(2, activation='softmax'))

# + id="LblRJecABHLW" colab_type="code" outputId="3ef23511-c999-4d60-e166-5e790afe86ad" colab={"base_uri": "https://localhost:8080/", "height": 663}
model.summary()

# + id="2r5sGSNP7hBc" colab_type="code" outputId="6322f418-59bf-472f-a378-9e1740bfd65f" colab={"base_uri": "https://localhost:8080/", "height": 68}
gen = ImageDataGenerator()
train_batches = gen.flow_from_directory("chest_xray/chest_xray/train", model.input_shape[1:3], color_mode="grayscale", shuffle=True, seed=1, batch_size=16)
valid_batches = gen.flow_from_directory("chest_xray/chest_xray/val",   model.input_shape[1:3], color_mode="grayscale", shuffle=True, seed=1, batch_size=16)
test_batches  = gen.flow_from_directory("chest_xray/chest_xray/test",  model.input_shape[1:3], color_mode="grayscale", shuffle=False, batch_size=8)

# + [markdown] id="l7RSffCOBxVp" colab_type="text"
# ## **Experiment 1.** learning rate = 0.01

# + id="D8i4D_TRBKS7" colab_type="code" outputId="abc60bb6-7fa6-48e9-e754-c864226c02e3" colab={"base_uri": "https://localhost:8080/", "height": 136}
model.compile(Adam(lr=0.001),loss="categorical_crossentropy", metrics=["accuracy"])
model.fit_generator(train_batches, validation_data=valid_batches, epochs=3)

# + id="R3GIQE86UbUM" colab_type="code" outputId="5ab99b6a-47e8-4572-ed36-250afd4b8598" colab={"base_uri": "https://localhost:8080/", "height": 272}
p = model.predict_generator(test_batches, verbose=True)
pre = pd.DataFrame(p)
pre["filename"] = test_batches.filenames
pre["label"] = (pre["filename"].str.contains("PNEUMONIA")).apply(int)
pre['pre'] = (pre[1]>0.5).apply(int)
print(pre)

# + id="3GlXF49nUdnz" colab_type="code" outputId="0c73b3cd-c8f2-4d03-f4a7-5908f4ae619d" colab={"base_uri": "https://localhost:8080/", "height": 34}
recall_score(pre["label"], pre["pre"])

# + id="w4mL0WESUe6A" colab_type="code" outputId="b52fc11a-03bf-45ec-9591-3b85ddd1d9b0" colab={"base_uri": "https://localhost:8080/", "height": 34}
roc_auc_score(pre["label"],pre[1])

# + id="ebdrbMPlUgWQ" colab_type="code" outputId="3d308110-88b3-4979-d695-105864615d19" colab={"base_uri": "https://localhost:8080/", "height": 296}
tpr,fpr,thres = roc_curve(pre["label"],pre[1])
roc = pd.DataFrame([tpr,fpr]).T
roc.plot(x=0,y=1)

# + [markdown] id="KKHHXKBIUg3F" colab_type="text"
# ## **Experiment 2.** learning rate = 0.001

# + id="gYZ8JDFFUZVt" colab_type="code" outputId="468e3341-c09c-4f13-8288-7b8977da4f89" colab={"base_uri": "https://localhost:8080/", "height": 136}
model.compile(Adam(lr=0.001),loss="categorical_crossentropy", metrics=["accuracy"])
model.fit_generator(train_batches, validation_data=valid_batches, epochs=3)

# + id="lGb9mTUHBeRy" colab_type="code" outputId="b4284156-78ac-4c7f-d2ba-9605e308a0cf" colab={"base_uri": "https://localhost:8080/", "height": 272}
p = model.predict_generator(test_batches, verbose=True)
pre = pd.DataFrame(p)
pre["filename"] = test_batches.filenames
pre["label"] = (pre["filename"].str.contains("PNEUMONIA")).apply(int)
pre['pre'] = (pre[1]>0.5).apply(int)
print(pre)

# + id="1SeACpnKBgfz" colab_type="code" outputId="7fb904c0-11f3-4365-a77b-da2b99e72cea" colab={"base_uri": "https://localhost:8080/"}
recall_score(pre["label"], pre["pre"])

# + id="Z56dibIqG4o4" colab_type="code" outputId="2800cb90-54d3-44b3-9f07-844e25faf19b" colab={"base_uri": "https://localhost:8080/"}
roc_auc_score(pre["label"],pre[1])

# + id="SXbGksFkG3pA" colab_type="code" outputId="54a40e3c-e7d7-4b21-e849-acad78f5cd75" colab={"base_uri": "https://localhost:8080/", "height": 296}
tpr,fpr,thres = roc_curve(pre["label"],pre[1])
roc = pd.DataFrame([tpr,fpr]).T
roc.plot(x=0,y=1)

# + [markdown] id="_x4qqbHqBv2l" colab_type="text"
# ## **Experiment 3.** learning rate = 0.0001

# + id="8p1pCeFiBL8e" colab_type="code" outputId="b8a3984a-01fa-4d1d-e111-cd9f09fe745b" colab={"base_uri": "https://localhost:8080/", "height": 136}
model.compile(Adam(lr=0.0001), loss="categorical_crossentropy", metrics=["accuracy"])
model.fit_generator(train_batches, validation_data=valid_batches, epochs=3)

# + id="y8pcUn4UBub9" colab_type="code" outputId="7c9dd651-7add-4f09-a172-611c6e79742d" colab={"base_uri": "https://localhost:8080/", "height": 34}
p = model.predict_generator(test_batches, verbose=True)
pre = pd.DataFrame(p)
pre["filename"] = test_batches.filenames
pre["label"] = (pre["filename"].str.contains("PNEUMONIA")).apply(int)
pre['pre'] = (pre[1]>0.5).apply(int)

# + id="IeiPJQpdBmJm" colab_type="code" outputId="4ba40536-e982-49d3-914f-0f9b148d24df" colab={"base_uri": "https://localhost:8080/"}
recall_score(pre["label"],pre["pre"])

# + id="y27upvTRTxAK" colab_type="code" outputId="b8aecbea-c29f-407a-fd56-a81b06eb841a" colab={"base_uri": "https://localhost:8080/"}
roc_auc_score(pre["label"],pre[1])

# + id="fGbBJpzrTyOK" colab_type="code" outputId="94848fe9-0929-4455-d3f4-7bef14bb9d03" colab={"base_uri": "https://localhost:8080/", "height": 296}
tpr,fpr,thres = roc_curve(pre["label"],pre[1])
roc = pd.DataFrame([tpr,fpr]).T
roc.plot(x=0,y=1)

StarcoderdataJupyterScriptsDedupFiltered