<jupyter_start><jupyter_text><jupyter_code>import keras
import tensorflow as tf 
from keras.models import Sequential
from keras.layers import GlobalAveragePooling2D
from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import Dense
from keras.layers import Dropout, Input
from keras.layers import BatchNormalization
import cv2
import matplotlib.pyplot as plt


vgg16_base_model = VGG16(input_shape=(180,180,3),include_top=False,weights='imagenet')

from google.colab import drive
drive.mount('/content/drive')
vgg16_base_model.summary()
vgg16_model = Sequential([
        vgg16_base_model,
        GlobalAveragePooling2D(),
        Dense(512, activation="relu"),
        BatchNormalization(),
        Dropout(0.6),
        Dense(128, activation="relu"),
        BatchNormalization(),
        Dropout(0.4),
        Dense(64,activation="relu"),
        BatchNormalization(),
        Dropout(0.3),
        Dense(1,activation="sigmoid")
    ])
    
model = Sequential()
model.add(vgg16_model.layers[0])
model.compile(
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)
from keras.preprocessing.image import ImageDataGenerator

image_generator = ImageDataGenerator(
    rotation_range=20,
    width_shift_range=0.1,
    shear_range=0.1,
    zoom_range=0.1,
    samplewise_center=True,
    samplewise_std_normalization=True
)
train = image_generator.flow_from_directory('/content/drive/MyDrive/xraysdata/chest_xray/train', 
                                            batch_size=8, 
                                            shuffle=True, 
                                            class_mode='binary',
                                            target_size=(180, 180))

validation = image_generator.flow_from_directory('/content/drive/MyDrive/xraysdata/chest_xray/val', 
                                                batch_size=1, 
                                                shuffle=False, 
                                                class_mode='binary',
                                                target_size=(180, 180))

test = image_generator.flow_from_directory('/content/drive/MyDrive/xraysdata/chest_xray/test', 
                                            batch_size=1, 
                                            shuffle=False, 
                                            class_mode='binary',
                                            target_size=(180, 180))
opt = tf.keras.optimizers.Adam(learning_rate=0.001)
METRICS = ['accuracy',tf.keras.metrics.Precision(name='precision'),tf.keras.metrics.Recall(name='recall')]
vgg16_model.compile(optimizer=opt,loss='binary_crossentropy',metrics=METRICS)
r = vgg16_model.fit(train,
          epochs=10 ,
          validation_data=validation,
          steps_per_epoch=100,
          validation_steps=25)

plt.figure(figsize=(12, 8))

plt.subplot(2, 2, 1)
plt.plot(r.history['loss'], label='Loss')
plt.plot(r.history['val_loss'], label='Val_Loss')
plt.legend()
plt.title('Loss Evolution')

plt.subplot(2, 2, 2)
plt.plot(r.history['accuracy'], label='Accuracy')
plt.plot(r.history['val_accuracy'], label='Val_Accuracy')
plt.legend()
plt.title('Accuracy Evolution')
evaluation =vgg16_model.evaluate(test)
print(f"Test Accuracy: {evaluation[1] * 100:.2f}%")

evaluation = vgg16_model.evaluate(train)
print(f"Train Accuracy: {evaluation[1] * 100:.2f}%")<jupyter_output>624/624 [==============================] - 220s 353ms/step - loss: 601.6246 - accuracy: 0.7949 - precision: 0.8541 - recall: 0.8103
Test Accuracy: 79.49%
652/652 [==============================] - 1711s 3s/step - loss: 544.5919 - accuracy: 0.8248 - precision: 0.9729 - recall: 0.7861
Train Accuracy: 82.48%
{'fraction_non_alphanumeric': 0.1319068255687974, 'fraction_numerical': 0.04929577464788732, 'mean_word_length': 2.5441458733205375, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '475391', 'license': 'MIT', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1089, 'n_words': 173, 'path': 'existingvgg.ipynb', 'repo_name': 'honey-06-dev/Python', 'size': '3692'}