From 08f90f0ad966842278cfa44a1018099975b49727 Mon Sep 17 00:00:00 2001 From: Ikki Date: Sun, 6 Oct 2024 23:02:27 +0530 Subject: [PATCH] added comments --- .../Models/MobileNetV2.ipynb | 119 +++++++++++++----- .../Models/ResNet50V2.ipynb | 91 +++++++++----- .../Models/VGG19.ipynb | 86 +++++++++---- 3 files changed, 211 insertions(+), 85 deletions(-) diff --git a/Deep_Learning/Dog Species Classification/Models/MobileNetV2.ipynb b/Deep_Learning/Dog Species Classification/Models/MobileNetV2.ipynb index 12f87eab11..e02c8d822d 100644 --- a/Deep_Learning/Dog Species Classification/Models/MobileNetV2.ipynb +++ b/Deep_Learning/Dog Species Classification/Models/MobileNetV2.ipynb @@ -40,19 +40,29 @@ }, "outputs": [], "source": [ + "# Import the OpenCV library for image processing\n", "import cv2\n", + "\n", + "# Import NumPy for numerical operations and array manipulation\n", "import numpy as np\n", + "\n", + "# Import Matplotlib for plotting and visualizing data\n", "import matplotlib.pyplot as plt\n", + "\n", + "# Import the OS module for interacting with the operating system\n", "import os\n", "\n", + "# Import TensorFlow library for building and training machine learning models\n", "import tensorflow as tf\n", + "\n", + "# Import ImageDataGenerator for data augmentation and image preprocessing\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", - "from tensorflow.keras.applications import MobileNetV2\n", - "from tensorflow.keras.applications.mobilenet_v2 import preprocess_input\n", - "from tensorflow.keras.models import Sequential\n", - "from tensorflow.keras.layers import Input, Lambda, GlobalAveragePooling2D, Dropout, Dense\n", + "\n", + "# Import the Adam optimizer for optimizing the model during training\n", "from tensorflow.keras.optimizers import Adam\n", - "from tensorflow.keras.callbacks import ModelCheckpoint" + "\n", + "# Import ModelCheckpoint callback for saving the model during training\n", + "from tensorflow.keras.callbacks import ModelCheckpoint\n" ] }, { @@ -181,8 +191,10 @@ } ], "source": [ + "# Iterate through each class folder path in the list of class folder paths\n", "for class_folder_path in class_folder_paths:\n", - " print('{0}:'.format(class_folder_path), ' ', len(os.listdir(class_folder_path)))" + " # Print the class folder path and the number of files (images) in that folder\n", + " print('{0}:'.format(class_folder_path), ' ', len(os.listdir(class_folder_path)))\n" ] }, { @@ -302,13 +314,25 @@ } ], "source": [ + "# Create a figure and a grid of subplots with 2 rows and 5 columns\n", "fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(15, 7))\n", + "\n", + "# Iterate over the flattened array of axes and their corresponding indices\n", "for i, ax in enumerate(axes.flat):\n", + " # Construct the directory path for each dog's training images\n", " dir = TRAIN_DIR + \"/\" + train_dogs[i] + \"/\"\n", + " \n", + " # Open the first image in the directory and display it in the current subplot\n", " ax.imshow(Image.open(dir + os.listdir(dir)[0]))\n", + " \n", + " # Set the title of the subplot to the dog's name\n", " ax.set_title(train_dogs[i])\n", + "\n", + "# Adjust the layout to prevent overlap between subplots\n", "plt.tight_layout()\n", - "plt.show()" + "\n", + "# Display the entire figure with the subplots\n", + "plt.show()\n" ] }, { @@ -361,35 +385,40 @@ } ], "source": [ - "train_data_gen = ImageDataGenerator(horizontal_flip = True,\n", - " rotation_range=20,\n", - " width_shift_range=0.1,\n", - " height_shift_range=0.1,\n", - " zoom_range=0.2)\n", + "# Import ImageDataGenerator for data augmentation and loading\n", + "train_data_gen = ImageDataGenerator(horizontal_flip=True, # Randomly flip images horizontally\n", + " rotation_range=20, # Randomly rotate images up to 20 degrees\n", + " width_shift_range=0.1, # Randomly shift images horizontally (10% of width)\n", + " height_shift_range=0.1, # Randomly shift images vertically (10% of height)\n", + " zoom_range=0.2) # Randomly zoom into images (up to 20%)\n", "\n", - "train_generator = train_data_gen.flow_from_directory(TRAIN_DIR,\n", - " target_size = (224, 224),\n", - " color_mode = 'rgb',\n", - " batch_size = 32,\n", - " class_mode ='categorical',\n", - " shuffle = True)\n", + "# Create a training data generator that loads images from the specified directory (TRAIN_DIR)\n", + "train_generator = train_data_gen.flow_from_directory(TRAIN_DIR, # Directory with training images\n", + " target_size=(224, 224), # Resize all images to 224x224 pixels\n", + " color_mode='rgb', # Use RGB color (3 channels)\n", + " batch_size=32, # Process images in batches of 32\n", + " class_mode='categorical',# Labels are categorical (multi-class)\n", + " shuffle=True) # Shuffle the data at the start of each epoch\n", "\n", + "# Create an image generator for validation data (no augmentation)\n", "val_data_gen = ImageDataGenerator()\n", "\n", - "val_generator = val_data_gen.flow_from_directory(VAL_DIR,\n", - " target_size = (224, 224),\n", - " color_mode = 'rgb',\n", - " batch_size = 32,\n", - " class_mode = 'categorical',\n", - " shuffle = False)\n", - "val_data_gen = ImageDataGenerator()\n", + "# Create a validation data generator from the specified directory (VAL_DIR)\n", + "val_generator = val_data_gen.flow_from_directory(VAL_DIR, # Directory with validation images\n", + " target_size=(224, 224), # Resize all images to 224x224 pixels\n", + " color_mode='rgb', # Use RGB color (3 channels)\n", + " batch_size=32, # Process images in batches of 32\n", + " class_mode='categorical',# Labels are categorical (multi-class)\n", + " shuffle=False) # Do not shuffle data (to maintain order)\n", + "\n", "\n", - "test_generator = val_data_gen.flow_from_directory(TEST_DIR,\n", - " target_size = (224, 224),\n", - " color_mode = 'rgb',\n", - " batch_size = 32,\n", - " class_mode = 'categorical',\n", - " shuffle = False)" + "# Create a test data generator from the specified directory (TEST_DIR)\n", + "test_generator = val_data_gen.flow_from_directory(TEST_DIR, # Directory with test images\n", + " target_size=(224, 224), # Resize all images to 224x224 pixels\n", + " color_mode='rgb', # Use RGB color (3 channels)\n", + " batch_size=32, # Process images in batches of 32\n", + " class_mode='categorical',# Labels are categorical (multi-class)\n", + " shuffle=False) # Do not shuffle data (to maintain order)" ] }, { @@ -550,15 +579,29 @@ } ], "source": [ + "# 'before_mobilenet' initializes a Sequential model.\n", + "# It takes images of shape (224, 224, 3) as input and \n", + "# applies the preprocessing function from MobileNetV2.\n", "before_mobilenet = Sequential([Input((224,224,3)),\n", " Lambda(preprocess_input)])\n", "\n", + "# 'mobilenet' loads the pre-trained MobileNetV2 model.\n", + "# The input shape is specified as (224, 224, 3) with RGB channels.\n", + "# 'include_top=False' means that we are not including the top classification \n", + "# layer of the pre-trained model, allowing us to customize the output for our task.\n", "mobilenet = MobileNetV2(input_shape = (224,224,3), include_top = False)\n", "\n", + "# 'after_mobilenet' is another Sequential model that takes the features \n", + "# from MobileNetV2, applies global average pooling, followed by a dropout \n", + "# layer (to prevent overfitting), and then a Dense layer to classify \n", + "# the output into 70 classes (for the dog breeds).\n", "after_mobilenet = Sequential([GlobalAveragePooling2D(),\n", " Dropout(0.2),\n", " Dense(70, activation = 'softmax')])\n", "\n", + "# Finally, the full model is built by chaining 'before_mobilenet',\n", + "# 'mobilenet', and 'after_mobilenet' sequentially. \n", + "# This creates an end-to-end model for dog breed classification.\n", "model = Sequential([before_mobilenet, mobilenet, after_mobilenet])" ] }, @@ -585,8 +628,12 @@ }, "outputs": [], "source": [ + "# Setting the optimizer as Adam with a learning rate of 0.00001\n", "opt = Adam(learning_rate=0.00001)\n", - "model.compile(optimizer = opt, loss = 'categorical_crossentropy', metrics = ['accuracy'])" + "\n", + "# Compiling the model with the Adam optimizer, categorical crossentropy loss (used for multi-class classification problems),\n", + "# and accuracy as the metric to evaluate model performance.\n", + "model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])" ] }, { @@ -969,10 +1016,18 @@ } ], "source": [ + "# Build the model with input shape (None, 224, 224, 3)\n", + "# 'None' indicates that the model can take a variable batch size, \n", + "# while '224, 224, 3' is the shape of each input image (224x224 RGB).\n", "model.build(((None, 224, 224, 3)))\n", "\n", + "# Display the summary of the model before MobileNet integration\n", "before_mobilenet.summary()\n", + "\n", + "# Display the summary of the MobileNet architecture\n", "mobilenet.summary()\n", + "\n", + "# Display the summary of the model after integrating MobileNet\n", "after_mobilenet.summary()" ] }, diff --git a/Deep_Learning/Dog Species Classification/Models/ResNet50V2.ipynb b/Deep_Learning/Dog Species Classification/Models/ResNet50V2.ipynb index fba87c0065..f393fcb34b 100644 --- a/Deep_Learning/Dog Species Classification/Models/ResNet50V2.ipynb +++ b/Deep_Learning/Dog Species Classification/Models/ResNet50V2.ipynb @@ -40,19 +40,29 @@ }, "outputs": [], "source": [ + "# Import the OpenCV library for image processing\n", "import cv2\n", + "\n", + "# Import NumPy for numerical operations and array manipulation\n", "import numpy as np\n", + "\n", + "# Import Matplotlib for plotting and visualizing data\n", "import matplotlib.pyplot as plt\n", + "\n", + "# Import the OS module for interacting with the operating system\n", "import os\n", "\n", + "# Import TensorFlow library for building and training machine learning models\n", "import tensorflow as tf\n", + "\n", + "# Import ImageDataGenerator for data augmentation and image preprocessing\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", - "from tensorflow.keras.applications.resnet_v2 import ResNet50V2, preprocess_input\n", "\n", - "from tensorflow.keras.models import Sequential\n", - "from tensorflow.keras.layers import Input, Lambda, GlobalAveragePooling2D, Dropout, Dense\n", + "# Import the Adam optimizer for optimizing the model during training\n", "from tensorflow.keras.optimizers import Adam\n", - "from tensorflow.keras.callbacks import ModelCheckpoint" + "\n", + "# Import ModelCheckpoint callback for saving the model during training\n", + "from tensorflow.keras.callbacks import ModelCheckpoint\n" ] }, { @@ -302,13 +312,25 @@ } ], "source": [ + "# Create a figure and a grid of subplots with 2 rows and 5 columns\n", "fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(15, 7))\n", + "\n", + "# Iterate over the flattened array of axes and their corresponding indices\n", "for i, ax in enumerate(axes.flat):\n", + " # Construct the directory path for each dog's training images\n", " dir = TRAIN_DIR + \"/\" + train_dogs[i] + \"/\"\n", + " \n", + " # Open the first image in the directory and display it in the current subplot\n", " ax.imshow(Image.open(dir + os.listdir(dir)[0]))\n", + " \n", + " # Set the title of the subplot to the dog's name\n", " ax.set_title(train_dogs[i])\n", + "\n", + "# Adjust the layout to prevent overlap between subplots\n", "plt.tight_layout()\n", - "plt.show()" + "\n", + "# Display the entire figure with the subplots\n", + "plt.show()\n" ] }, { @@ -361,35 +383,41 @@ } ], "source": [ - "train_data_gen = ImageDataGenerator(horizontal_flip = True,\n", - " rotation_range=20,\n", - " width_shift_range=0.1,\n", - " height_shift_range=0.1,\n", - " zoom_range=0.2)\n", + "# Create an instance of ImageDataGenerator for training data with data augmentation\n", + "train_data_gen = ImageDataGenerator(horizontal_flip=True, # Randomly flip images horizontally\n", + " rotation_range=20, # Randomly rotate images by up to 20 degrees\n", + " width_shift_range=0.1, # Randomly shift images horizontally by 10%\n", + " height_shift_range=0.1, # Randomly shift images vertically by 10%\n", + " zoom_range=0.2) # Randomly zoom into images by up to 20%\n", "\n", + "# Create a training data generator that reads images from the specified directory\n", "train_generator = train_data_gen.flow_from_directory(TRAIN_DIR,\n", - " target_size = (224, 224),\n", - " color_mode = 'rgb',\n", - " batch_size = 32,\n", - " class_mode ='categorical',\n", - " shuffle = True)\n", + " target_size=(224, 224), # Resize images to 224x224\n", + " color_mode='rgb', # Use RGB color mode\n", + " batch_size=32, # Set the batch size to 32\n", + " class_mode='categorical', # Use categorical labels for multi-class classification\n", + " shuffle=True) # Shuffle the data at each epoch\n", "\n", + "# Create an instance of ImageDataGenerator for validation data without augmentation\n", "val_data_gen = ImageDataGenerator()\n", "\n", + "# Create a validation data generator that reads images from the specified directory\n", "val_generator = val_data_gen.flow_from_directory(VAL_DIR,\n", - " target_size = (224, 224),\n", - " color_mode = 'rgb',\n", - " batch_size = 32,\n", - " class_mode = 'categorical',\n", - " shuffle = False)\n", - "val_data_gen = ImageDataGenerator()\n", + " target_size=(224, 224), # Resize images to 224x224\n", + " color_mode='rgb', # Use RGB color mode\n", + " batch_size=32, # Set the batch size to 32\n", + " class_mode='categorical', # Use categorical labels for multi-class classification\n", + " shuffle=False) # Do not shuffle the data for validation\n", + "\n", "\n", + "\n", + "# Create a test data generator that reads images from the specified directory\n", "test_generator = val_data_gen.flow_from_directory(TEST_DIR,\n", - " target_size = (224, 224),\n", - " color_mode = 'rgb',\n", - " batch_size = 32,\n", - " class_mode = 'categorical',\n", - " shuffle = False)" + " target_size=(224, 224), # Resize images to 224x224\n", + " color_mode='rgb', # Use RGB color mode\n", + " batch_size=32, # Set the batch size to 32\n", + " class_mode='categorical', # Use categorical labels for multi-class classification\n", + " shuffle=False) # Do not shuffle the data for testing\n" ] }, { @@ -495,9 +523,14 @@ } ], "source": [ + "# Retrieve the class indices (mapping of class labels to numeric indices) from the training generator\n", "labels = train_generator.class_indices\n", - "class_mapping = dict((v,k) for k,v in labels.items())\n", - "class_mapping" + "\n", + "# Create a dictionary that maps numeric indices back to class labels\n", + "class_mapping = dict((v, k) for k, v in labels.items())\n", + "\n", + "# Display the class mapping dictionary\n", + "class_mapping\n" ] }, { @@ -1419,6 +1452,8 @@ } ], "source": [ + "# Evaluate the performance of the trained model on the test dataset,\n", + "# which results in an accuracy of 96.28 percent\n", "model.evaluate(test_generator)" ] } diff --git a/Deep_Learning/Dog Species Classification/Models/VGG19.ipynb b/Deep_Learning/Dog Species Classification/Models/VGG19.ipynb index fbb25602ac..fd0e9d5763 100644 --- a/Deep_Learning/Dog Species Classification/Models/VGG19.ipynb +++ b/Deep_Learning/Dog Species Classification/Models/VGG19.ipynb @@ -40,15 +40,29 @@ }, "outputs": [], "source": [ + "# Import the OpenCV library for image processing\n", "import cv2\n", + "\n", + "# Import NumPy for numerical operations and array manipulation\n", "import numpy as np\n", + "\n", + "# Import Matplotlib for plotting and visualizing data\n", "import matplotlib.pyplot as plt\n", + "\n", + "# Import the OS module for interacting with the operating system\n", "import os\n", "\n", + "# Import TensorFlow library for building and training machine learning models\n", "import tensorflow as tf\n", + "\n", + "# Import ImageDataGenerator for data augmentation and image preprocessing\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", + "\n", + "# Import the Adam optimizer for optimizing the model during training\n", "from tensorflow.keras.optimizers import Adam\n", - "from tensorflow.keras.callbacks import ModelCheckpoint" + "\n", + "# Import ModelCheckpoint callback for saving the model during training\n", + "from tensorflow.keras.callbacks import ModelCheckpoint\n" ] }, { @@ -177,8 +191,10 @@ } ], "source": [ + "# Iterate through each class folder path in the list of class folder paths\n", "for class_folder_path in class_folder_paths:\n", - " print('{0}:'.format(class_folder_path), ' ', len(os.listdir(class_folder_path)))" + " # Print the class folder path and the number of files (images) in that folder\n", + " print('{0}:'.format(class_folder_path), ' ', len(os.listdir(class_folder_path)))\n" ] }, { @@ -298,13 +314,25 @@ } ], "source": [ + "# Create a figure and a grid of subplots with 2 rows and 5 columns\n", "fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(15, 7))\n", + "\n", + "# Iterate over the flattened array of axes and their corresponding indices\n", "for i, ax in enumerate(axes.flat):\n", + " # Construct the directory path for each dog's training images\n", " dir = TRAIN_DIR + \"/\" + train_dogs[i] + \"/\"\n", + " \n", + " # Open the first image in the directory and display it in the current subplot\n", " ax.imshow(Image.open(dir + os.listdir(dir)[0]))\n", + " \n", + " # Set the title of the subplot to the dog's name\n", " ax.set_title(train_dogs[i])\n", + "\n", + "# Adjust the layout to prevent overlap between subplots\n", "plt.tight_layout()\n", - "plt.show()" + "\n", + "# Display the entire figure with the subplots\n", + "plt.show()\n" ] }, { @@ -357,35 +385,41 @@ } ], "source": [ - "train_data_gen = ImageDataGenerator(horizontal_flip = True,\n", - " rotation_range=20,\n", - " width_shift_range=0.1,\n", - " height_shift_range=0.1,\n", - " zoom_range=0.2)\n", + "# Create an instance of ImageDataGenerator for training data with data augmentation\n", + "train_data_gen = ImageDataGenerator(horizontal_flip=True, # Randomly flip images horizontally\n", + " rotation_range=20, # Randomly rotate images by up to 20 degrees\n", + " width_shift_range=0.1, # Randomly shift images horizontally by 10%\n", + " height_shift_range=0.1, # Randomly shift images vertically by 10%\n", + " zoom_range=0.2) # Randomly zoom into images by up to 20%\n", "\n", + "# Create a training data generator that reads images from the specified directory\n", "train_generator = train_data_gen.flow_from_directory(TRAIN_DIR,\n", - " target_size = (224, 224),\n", - " color_mode = 'rgb',\n", - " batch_size = 32,\n", - " class_mode ='categorical',\n", - " shuffle = True)\n", + " target_size=(224, 224), # Resize images to 224x224\n", + " color_mode='rgb', # Use RGB color mode\n", + " batch_size=32, # Set the batch size to 32\n", + " class_mode='categorical', # Use categorical labels for multi-class classification\n", + " shuffle=True) # Shuffle the data at each epoch\n", "\n", + "# Create an instance of ImageDataGenerator for validation data without augmentation\n", "val_data_gen = ImageDataGenerator()\n", "\n", + "# Create a validation data generator that reads images from the specified directory\n", "val_generator = val_data_gen.flow_from_directory(VAL_DIR,\n", - " target_size = (224, 224),\n", - " color_mode = 'rgb',\n", - " batch_size = 32,\n", - " class_mode = 'categorical',\n", - " shuffle = False)\n", - "val_data_gen = ImageDataGenerator()\n", + " target_size=(224, 224), # Resize images to 224x224\n", + " color_mode='rgb', # Use RGB color mode\n", + " batch_size=32, # Set the batch size to 32\n", + " class_mode='categorical', # Use categorical labels for multi-class classification\n", + " shuffle=False) # Do not shuffle the data for validation\n", + "\n", + "\n", "\n", + "# Create a test data generator that reads images from the specified directory\n", "test_generator = val_data_gen.flow_from_directory(TEST_DIR,\n", - " target_size = (224, 224),\n", - " color_mode = 'rgb',\n", - " batch_size = 32,\n", - " class_mode = 'categorical',\n", - " shuffle = False)" + " target_size=(224, 224), # Resize images to 224x224\n", + " color_mode='rgb', # Use RGB color mode\n", + " batch_size=32, # Set the batch size to 32\n", + " class_mode='categorical', # Use categorical labels for multi-class classification\n", + " shuffle=False) # Do not shuffle the data for testing\n" ] }, { @@ -875,7 +909,9 @@ } ], "source": [ - "model.evaluate(test_generator)" + "# Evaluate the performance of the trained model on the test dataset,\n", + "# which results in an accuracy of 91.85 percent\n", + "model.evaluate(test_generator)\n" ] } ],