--- a +++ b/vgg16 & vgg19.ipynb @@ -0,0 +1,528 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "37e884ed", + "metadata": {}, + "outputs": [], + "source": [ + "# Importing necessary libraries\n", + "import os\n", + "import random\n", + "import shutil\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "from keras.preprocessing.image import ImageDataGenerator\n", + "from keras.applications import VGG16, VGG19, ResNet50, InceptionV3, Xception\n", + "from keras.models import Model\n", + "from keras.layers import Dense, GlobalAveragePooling2D\n", + "from keras.optimizers import Adam\n", + "from keras.callbacks import ModelCheckpoint\n", + "from sklearn.metrics import classification_report, confusion_matrix\n", + "\n", + "# Setting random seed for reproducibility\n", + "np.random.seed(42)\n", + "random.seed(42)\n", + "\n", + "# Define constants\n", + "NUM_CLASSES = 2\n", + "IMG_SIZE = (224, 224)\n", + "BATCH_SIZE = 32\n", + "TRAIN_DIR = 'train'\n", + "VAL_DIR = 'val'\n", + "TEST_DIR = 'test'\n", + "TRAIN_SPLIT = 0.7\n", + "VAL_SPLIT = 0.1\n", + "TEST_SPLIT = 0.2\n", + "test_samples = 280" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3a76b0e6", + "metadata": {}, + "outputs": [], + "source": [ + "# Define the base directory where the image data is located\n", + "BASE_DIR = r\"C:\\Users\\mohit\\Downloads\\DIP Assignment 2\\Data\"" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "e3142f03", + "metadata": {}, + "outputs": [], + "source": [ + "# Set the paths for training, validation, and test data\n", + "TRAIN_DIR = os.path.join(BASE_DIR, 'train')\n", + "VAL_DIR = os.path.join(BASE_DIR, 'val')\n", + "TEST_DIR = os.path.join(BASE_DIR, 'test')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "0bfbe95a", + "metadata": {}, + "outputs": [], + "source": [ + "# Load and preprocess the data\n", + "train_datagen = ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)\n", + "val_datagen = ImageDataGenerator(rescale=1./255)\n", + "test_datagen = ImageDataGenerator(rescale=1./255)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "77eeebb9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found 980 images belonging to 2 classes.\n", + "Found 140 images belonging to 2 classes.\n", + "Found 280 images belonging to 2 classes.\n" + ] + } + ], + "source": [ + "train_generator = train_datagen.flow_from_directory(TRAIN_DIR,target_size=IMG_SIZE,batch_size=BATCH_SIZE,class_mode='categorical')\n", + "\n", + "val_generator = val_datagen.flow_from_directory(VAL_DIR,target_size=IMG_SIZE,batch_size=BATCH_SIZE,class_mode='categorical')\n", + "\n", + "test_generator = test_datagen.flow_from_directory(TEST_DIR,target_size=IMG_SIZE,batch_size=BATCH_SIZE,class_mode='categorical',shuffle=False)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "221f07fe", + "metadata": {}, + "outputs": [], + "source": [ + "# Define function for creating transfer learning models\n", + "def create_transfer_model(base_model, num_classes):\n", + " x = base_model.output\n", + " x = GlobalAveragePooling2D()(x)\n", + " x = Dense(1024, activation='relu')(x)\n", + " predictions = Dense(num_classes, activation='softmax')(x)\n", + " model = Model(inputs=base_model.input, outputs=predictions)\n", + " return model\n", + "\n", + "# Define VGG16 model\n", + "vgg16_base = VGG16(include_top=False, weights='imagenet', input_shape=(IMG_SIZE[0], IMG_SIZE[1], 3))\n", + "vgg16_model = create_transfer_model(vgg16_base, NUM_CLASSES)\n", + "\n", + "# Define VGG19 model\n", + "vgg19_base = VGG19(include_top=False, weights='imagenet', input_shape=(IMG_SIZE[0], IMG_SIZE[1], 3))\n", + "vgg19_model = create_transfer_model(vgg19_base, NUM_CLASSES)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "4c47d0fd", + "metadata": {}, + "outputs": [], + "source": [ + "# Compile the models\n", + "optimizer = Adam(learning_rate=0.0001)\n", + "vgg16_model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\n", + "vgg19_model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "b1063daa", + "metadata": {}, + "outputs": [], + "source": [ + "# Define checkpoint callback to save the best model during training\n", + "checkpoint = ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "d9c6cb6b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\mohit\\AppData\\Local\\Temp\\ipykernel_25572\\2039408895.py:2: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.\n", + " vgg16_history = vgg16_model.fit_generator(train_generator, steps_per_epoch=train_generator.n // train_generator.batch_size, epochs=5, validation_data=val_generator, validation_steps=val_generator.n // val_generator.batch_size, callbacks=[checkpoint])\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/5\n", + "30/30 [==============================] - 269s 9s/step - loss: 0.6998 - accuracy: 0.5190 - val_loss: 0.7125 - val_accuracy: 0.5000\n", + "Epoch 2/5\n", + "30/30 [==============================] - 286s 10s/step - loss: 0.6402 - accuracy: 0.6118 - val_loss: 0.4709 - val_accuracy: 0.8281\n", + "Epoch 3/5\n", + "30/30 [==============================] - 470s 16s/step - loss: 0.2767 - accuracy: 0.9008 - val_loss: 0.1435 - val_accuracy: 0.9531\n", + "Epoch 4/5\n", + "30/30 [==============================] - 503s 17s/step - loss: 0.1493 - accuracy: 0.9473 - val_loss: 0.1466 - val_accuracy: 0.9609\n", + "Epoch 5/5\n", + "30/30 [==============================] - 504s 17s/step - loss: 0.1644 - accuracy: 0.9494 - val_loss: 0.0989 - val_accuracy: 0.9688\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\mohit\\AppData\\Local\\Temp\\ipykernel_25572\\2039408895.py:3: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.\n", + " vgg19_history = vgg19_model.fit_generator(train_generator, steps_per_epoch=train_generator.n // train_generator.batch_size, epochs=5, validation_data=val_generator, validation_steps=val_generator.n // val_generator.batch_size, callbacks=[checkpoint])\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/5\n", + "30/30 [==============================] - 570s 19s/step - loss: 0.7282 - accuracy: 0.5042 - val_loss: 0.6897 - val_accuracy: 0.5156\n", + "Epoch 2/5\n", + "30/30 [==============================] - 558s 19s/step - loss: 0.6453 - accuracy: 0.6983 - val_loss: 0.4191 - val_accuracy: 0.8672\n", + "Epoch 3/5\n", + "30/30 [==============================] - 544s 18s/step - loss: 0.3698 - accuracy: 0.8576 - val_loss: 0.1742 - val_accuracy: 0.9297\n", + "Epoch 4/5\n", + "30/30 [==============================] - 547s 18s/step - loss: 0.2672 - accuracy: 0.9040 - val_loss: 0.1718 - val_accuracy: 0.9375\n", + "Epoch 5/5\n", + "30/30 [==============================] - 354s 12s/step - loss: 0.2497 - accuracy: 0.9146 - val_loss: 0.2170 - val_accuracy: 0.8984\n" + ] + } + ], + "source": [ + "# Train the models\n", + "vgg16_history = vgg16_model.fit_generator(train_generator, steps_per_epoch=train_generator.n // train_generator.batch_size, epochs=5, validation_data=val_generator, validation_steps=val_generator.n // val_generator.batch_size, callbacks=[checkpoint])\n", + "vgg19_history = vgg19_model.fit_generator(train_generator, steps_per_epoch=train_generator.n // train_generator.batch_size, epochs=5, validation_data=val_generator, validation_steps=val_generator.n // val_generator.batch_size, callbacks=[checkpoint])" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "865704d7", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\mohit\\AppData\\Local\\Temp\\ipykernel_25572\\1392354301.py:2: UserWarning: `Model.evaluate_generator` is deprecated and will be removed in a future version. Please use `Model.evaluate`, which supports generators.\n", + " vgg16_scores = vgg16_model.evaluate_generator(test_generator, steps=test_samples // BATCH_SIZE)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "VGG16 Test Loss: 0.07796119153499603\n", + "VGG16 Test Accuracy: 0.97265625\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\mohit\\AppData\\Local\\Temp\\ipykernel_25572\\1392354301.py:6: UserWarning: `Model.evaluate_generator` is deprecated and will be removed in a future version. Please use `Model.evaluate`, which supports generators.\n", + " vgg19_scores = vgg19_model.evaluate_generator(test_generator, steps=test_samples // BATCH_SIZE)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "VGG19 Test Loss: 0.14393511414527893\n", + "VGG19 Test Accuracy: 0.94140625\n" + ] + } + ], + "source": [ + "# Evaluate the models on test data\n", + "vgg16_scores = vgg16_model.evaluate_generator(test_generator, steps=test_samples // BATCH_SIZE)\n", + "print(\"VGG16 Test Loss:\", vgg16_scores[0])\n", + "print(\"VGG16 Test Accuracy:\", vgg16_scores[1])\n", + "\n", + "vgg19_scores = vgg19_model.evaluate_generator(test_generator, steps=test_samples // BATCH_SIZE)\n", + "print(\"VGG19 Test Loss:\", vgg19_scores[0])\n", + "print(\"VGG19 Test Accuracy:\", vgg19_scores[1])" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "0a38cb63", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\mohit\\AppData\\Local\\Temp\\ipykernel_25572\\2218791254.py:8: UserWarning: `Model.predict_generator` is deprecated and will be removed in a future version. Please use `Model.predict`, which supports generators.\n", + " vgg16_predictions = vgg16_model.predict_generator(test_generator, steps=num_prediction_steps, verbose=1)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9/9 [==============================] - 23s 3s/step\n" + ] + } + ], + "source": [ + "# Get the number of test samples\n", + "num_test_samples = test_generator.n\n", + "\n", + "# Calculate the number of steps for prediction\n", + "num_prediction_steps = num_test_samples // test_generator.batch_size + 1\n", + "\n", + "# Generate predictions for all test samples\n", + "vgg16_predictions = vgg16_model.predict_generator(test_generator, steps=num_prediction_steps, verbose=1)\n", + "\n", + "# Convert predictions to class labels\n", + "vgg16_predicted_labels = np.argmax(vgg16_predictions, axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "38f80e7c", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\mohit\\AppData\\Local\\Temp\\ipykernel_25572\\903632277.py:8: UserWarning: `Model.predict_generator` is deprecated and will be removed in a future version. Please use `Model.predict`, which supports generators.\n", + " vgg19_predictions = vgg19_model.predict_generator(test_generator, steps=num_prediction_steps, verbose=1)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9/9 [==============================] - 30s 3s/step\n" + ] + } + ], + "source": [ + "# Get the number of test samples\n", + "num_test_samples = test_generator.n\n", + "\n", + "# Calculate the number of steps for prediction\n", + "num_prediction_steps = num_test_samples // test_generator.batch_size + 1\n", + "\n", + "# Generate predictions for all test samples\n", + "vgg19_predictions = vgg19_model.predict_generator(test_generator, steps=num_prediction_steps, verbose=1)\n", + "\n", + "# Convert predictions to class labels\n", + "vgg19_predicted_labels = np.argmax(vgg19_predictions, axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "c1afa985", + "metadata": {}, + "outputs": [], + "source": [ + "# Get true class labels\n", + "true_labels = test_generator.classes" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "5900fa6e", + "metadata": {}, + "outputs": [], + "source": [ + "# Calculate classification report\n", + "vgg16_report = classification_report(true_labels, vgg16_predicted_labels)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "67dfb41f", + "metadata": {}, + "outputs": [], + "source": [ + "# Calculate classification report\n", + "from sklearn.metrics import classification_report\n", + "\n", + "# Get the ground truth labels\n", + "ground_truth_labels = test_generator.classes\n", + "\n", + "# Get the predicted labels\n", + "vgg19_predicted_labels = np.argmax(vgg19_predictions, axis=1)\n", + "\n", + "# Calculate classification report\n", + "classification_report_vgg19 = classification_report(ground_truth_labels, vgg19_predicted_labels, zero_division=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "3e69c923", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "vgg16 Classification Report:\n", + " precision recall f1-score support\n", + "\n", + " 0 0.95 0.99 0.97 140\n", + " 1 0.99 0.94 0.96 140\n", + "\n", + " accuracy 0.96 280\n", + " macro avg 0.97 0.96 0.96 280\n", + "weighted avg 0.97 0.96 0.96 280\n", + "\n", + "vgg19 Classification Report:\n", + " precision recall f1-score support\n", + "\n", + " 0 0.87 0.99 0.93 140\n", + " 1 0.98 0.86 0.92 140\n", + "\n", + " accuracy 0.92 280\n", + " macro avg 0.93 0.92 0.92 280\n", + "weighted avg 0.93 0.92 0.92 280\n", + "\n" + ] + } + ], + "source": [ + "print(\"vgg16 Classification Report:\")\n", + "print(vgg16_report)\n", + "\n", + "print(\"vgg19 Classification Report:\")\n", + "print(classification_report_vgg19)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b52bd648", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/5\n", + "30/30 [==============================] - 608s 21s/step - loss: 0.1287 - accuracy: 0.9599\n", + "Epoch 2/5\n", + "30/30 [==============================] - 802s 27s/step - loss: 0.1328 - accuracy: 0.9620\n", + "Epoch 3/5\n", + "15/30 [==============>...............] - ETA: 6:36 - loss: 0.0728 - accuracy: 0.9854" + ] + } + ], + "source": [ + "import matplotlib.pyplot as plt\n", + "\n", + "# Train the VGG16 model and obtain the training history\n", + "vgg16_history = vgg16_model.fit(train_generator, steps_per_epoch=train_generator.n // train_generator.batch_size, epochs=5)\n", + "\n", + "# Plot the training loss curve\n", + "plt.plot(vgg16_history.history['loss'])\n", + "plt.title('vgg16 Training Loss')\n", + "plt.xlabel('Epoch')\n", + "plt.ylabel('Loss')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5f87edd6", + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "\n", + "# Train the VGG16 model and obtain the training history\n", + "vgg19_history = vgg19_model.fit(train_generator, steps_per_epoch=train_generator.n // train_generator.batch_size, epochs=10)\n", + "\n", + "# Plot the training loss curve\n", + "plt.plot(vgg19_history.history['loss'])\n", + "plt.title('vgg19 Training Loss')\n", + "plt.xlabel('Epoch')\n", + "plt.ylabel('Loss')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f66c546c", + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(vgg16_history.history['accuracy'])\n", + "plt.title('VGG16 Training Accuracy')\n", + "plt.xlabel('Epoch')\n", + "plt.ylabel('Accuracy')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba32e996", + "metadata": {}, + "outputs": [], + "source": [ + "plt.plot(vgg19_history.history['accuracy'])\n", + "plt.title('VGG19 Training Accuracy')\n", + "plt.xlabel('Epoch')\n", + "plt.ylabel('Accuracy')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b3533667", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}