<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive/')
!pip install keras
 import keras
 from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
 from keras.models import Model, Sequential
 from keras.preprocessing.image import ImageDataGenerator
 from keras.models import load_model
 from keras.utils.vis_utils import plot_model
 import numpy as np
 import matplotlib.pyplot as plt
IMAGE_SIZE = (224,224)
BATCH_SIZE = 32
train_path = '/content/drive/MyDrive/Dataset/CT_COVID'
test_path = '/content/drive/MyDrive/Colab Notebooks/'
# DATA PREPARATION
train_datagen = ImageDataGenerator(rescale=1./255,
                             shear_range=0.1,
                             zoom_range=0.1,
                             horizontal_flip=True,
                             validation_split=0.2)
test_datagen = ImageDataGenerator(rescale=1./255)


train_generator = train_datagen.flow_from_directory(train_path, 
                                               target_size=(224,224),
                                               batch_size=BATCH_SIZE,
                                               color_mode='grayscale',
                                               class_mode='binary',
                                               shuffle=True,
                                               subset='training')

validation_generator = train_datagen.flow_from_directory(train_path, 
                                               target_size=IMAGE_SIZE,
                                               batch_size=BATCH_SIZE,
                                               color_mode='grayscale',
                                               class_mode='binary',
                                               shuffle=True,
                                               subset='validation')

test_generator = test_datagen.flow_from_directory(test_path,
                                                  target_size=(224, 224),
                                                  color_mode="grayscale",
                                                  classes=['test'],
                                                  class_mode='binary',
                                                  batch_size=1,
                                                  shuffle=False)
print(test_generator.filenames)
def VGG16():
  model = Sequential()
  model.add(Conv2D(16, 3, padding='same', activation='relu', input_shape=(224,224,1)))
  model.add(Conv2D(16, 3, padding='same', activation='relu'))
  model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
  model.add(Conv2D(32, 3, padding='same', activation='relu'))
  model.add(Conv2D(32, 3, padding='same', activation='relu'))
  model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
  model.add(Flatten())
  model.add(Dense(128, activation='relu'))
  model.add(Dropout(0.2))
  model.add(Dense(64, activation='relu'))
  model.add(Dropout(0.2))
  model.add(Dense(1, activation='sigmoid'))
  return model
model = VGG16()
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
initial_learning_rate = 0.0001
lr_schedule = keras.optimizers.schedules.ExponentialDecay(
    initial_learning_rate, decay_steps=100000, decay_rate=0.9, staircase=True
)
model.compile(optimizer=keras.optimizers.Adam(learning_rate=lr_schedule),
              loss='binary_crossentropy',
              metrics=['acc'])
checkpoint_cb = keras.callbacks.ModelCheckpoint(
    "COVID_VGG16_checkpoint.h5", save_best_only=True)
early_stopping_cb = keras.callbacks.EarlyStopping(monitor="val_acc", patience=15)
# TRAIN MODEL
history = model.fit(train_generator, 
                    validation_data=validation_generator,
                    shuffle=True,
                    epochs=20,
                    verbose=1,
                    callbacks=[checkpoint_cb, early_stopping_cb])
fig, ax = plt.subplots(1, 2, figsize=(20, 3))
ax = ax.ravel()

for i, metric in enumerate(["acc", "loss"]):
    ax[i].plot(model.history.history[metric])
    ax[i].plot(model.history.history["val_" + metric])
    ax[i].set_title("Model {}".format(metric))
    ax[i].set_xlabel("epochs")
    ax[i].set_ylabel(metric)
    ax[i].legend(["train", "val"])
model.evaluate(test_generator)
import os
os.chdir('/content/drive/MyDrive/Colab Notebooks')
model.save("COVID_VGG16_Model")         #Model folder contain .pb file
model.save("COVID_VGG16.h5")            #HDF5 format for custom use
new_model = load_model("/content/drive/MyDrive/Colab Notebooks/COVID_VGG16_Model", compile=True)
predictions = new_model.predict(test_generator)
for i in range(0, (test_generator.labels.shape[0])):
  if predictions[i] > 0.005:
    print(test_generator.filenames[i] + 'is normal')
  else:
    print(test_generator.filenames[i] + 'is COVID')<jupyter_output>test/covid-19-pneumonia-182.jpgis COVID
test/covid-19-pneumonia-30.jpgis COVID
test/covid-19-pneumonia-56.jpgis normal
test/covid-19-pneumonia-94.pngis COVID
test/normal-chest-ct-1 (1).jpgis COVID
test/normal-chest-ct-1 (2).jpgis normal
test/normal-chest-ct-1 (3).jpgis normal
test/normal-chest-ct-1 (4).pngis normal
test/normal-chest-ct-1 (5).jpgis normal
{'fraction_non_alphanumeric': 0.11400462962962964, 'fraction_numerical': 0.029128086419753087, 'mean_word_length': 2.190769230769231, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '153504', 'license': 'MIT', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1505, 'n_words': 241, 'path': 'COVID_VGG16.ipynb', 'repo_name': 'Minatozaki0911/COVID-TB_Classification_VGG16', 'size': '5184'}