Project - Mini Project
# To import needed libraries.
import numpy as np
import random
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import zipfile
import os
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
%matplotlib inline
import shutil
# Print File Paths in a Directory Using os.walk() Function in Python
for dirname, _, filenames in os.walk("D:/Codings/My Practices/Week 6_Mini Project"):
for filename in filenames:
print(os.path.join(dirname, filename))
# unzips the file and extracts all its contents to the current working directory.
with zipfile.ZipFile('D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle.zip', 'r') as z:
z.extractall()
# Define the paths to the directories
dir1_path = "D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/training_set/training_set/cats"
dir2_path = "D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/training_set/training_set/dogs"
# Create a new directory to save the files in
new_dir_path = "D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/training_set/training_set/train"
if not os.path.exists(new_dir_path):
os.makedirs(new_dir_path)
# Move all files from dir1_path to the new directory
for file_name in os.listdir(dir1_path):
src = os.path.join(dir1_path, file_name)
dst = os.path.join(new_dir_path, file_name)
shutil.copy(src, dst)
# Move all files from dir2_path to the new directory
for file_name in os.listdir(dir2_path):
src = os.path.join(dir2_path, file_name)
dst = os.path.join(new_dir_path, file_name)
shutil.copy(src, dst)
# Create two lists of filenames and labels
filenames= os.listdir('D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/training_set/training_set/train')
labels=[]
for name in filenames:
list_of_split_name= name.split('.')[0]
if list_of_split_name == 'dog':
labels.append('dog')
else:
labels.append('cat')
# Shuffle the filenames and labels
combined = list(zip(filenames, labels))
random.shuffle(combined)
filenames[:], labels[:] = zip(*combined)
# Creating a Pandas DataFrame from Lists of Filenames and Labels
df = pd.DataFrame({'filename': filenames, 'label': labels})
df.head(20)
# returns the count of unique values in the 'label' column of the DataFrame 'df'.
df['label'].value_counts()
# Displays images from a list in 4rows x 5columns format.
fig, axes = plt.subplots(4, 5, figsize=(20, 10))
plt.subplots_adjust(hspace=0.5, wspace=0)
for i, ax in enumerate(axes.ravel()):
sample = filenames[i]
image = tf.keras.preprocessing.image.load_img('D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/training_set/training_set/train/' + sample)
ax.imshow(image)
ax.set_title('dog' if labels[i]=='dog' else 'cat')
plt.show()
# Define directory variable
directory = 'D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/training_set/training_set/train/'
# split data into Train and Validation using train_test_split
train_df, valid_df = train_test_split(df , test_size= 0.3 , random_state= 42, stratify=df['label'], shuffle=True)
train_df= train_df.reset_index(drop=True)
valid_df= valid_df.reset_index(drop=True)
# Data Augmentation and Image Generator Setup for Image Classification Training
train_data= keras.preprocessing.image.ImageDataGenerator(rescale=1./255 ,
rotation_range=20,
horizontal_flip=True,
vertical_flip=True
)
directory = 'D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/training_set/training_set/train/'
train_generator=train_data.flow_from_dataframe( dataframe=train_df,
directory=directory,
target_size=(224, 224),
x_col="filename",
y_col="label",
color_mode="rgb",
class_mode="binary",
batch_size=32,
seed = 42,
shuffle=True,
validate_filenames=True
)
# Image Data Generator Setup for Image Classification Validation or Testing
valid_data=keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
directory = 'D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/training_set/training_set/train/'
valid_generator=valid_data.flow_from_dataframe( dataframe=valid_df,
directory=directory,
target_size=(224, 224),
x_col="filename",
y_col="label",
color_mode="rgb",
class_mode="binary",
batch_size=32,
seed = 42,
shuffle=True,
validate_filenames=True
)
source_dir = "D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/training_set/training_set/train"
dest_dir = "D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/test_set/test_set/test"
# Create destination directory if it doesn't exist
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
# Copy all files from source directory to destination directory
for filename in os.listdir(source_dir):
shutil.copy(os.path.join(source_dir, filename), dest_dir)
# Creating DataFrame for Test Image Files and Counting the Number of Samples
filenames = os.listdir("D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/test_set/test_set/test")
test_df = pd.DataFrame({'filename' : filenames})
samples = test_df.shape[0]
test_data=keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
test_generator=train_data.flow_from_dataframe( dataframe=test_df,
directory="D:/Codings/My Practices/Week 6_Mini Project/dogs_cats_kaggle/test_set/test_set/test",
target_size=(224, 224),
x_col="filename",
y_col=None,
class_mode=None,
batch_size=32,
seed = 42
)
# Image Data Generator Setup for Image Classification Testing or Inference
from tensorflow.keras import applications
base_model= tf.keras.applications.MobileNetV2(weights='imagenet', include_top=False, input_shape=(224,224,3) )
base_model.trainable= False
flatten_layer= keras.layers.Flatten()
output=keras.layers.Dense(1, activation= 'sigmoid')
model=keras.Sequential([base_model,flatten_layer,output])
model.summary()
# Printing the Summary of a Pre-Trained Convolutional Neural Network (CNN) Architecture
base_model.summary()
# Callbacks Setup for Model Checkpointing
checkpoint_filepath =('./model.h5')
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(filepath = checkpoint_filepath,
monitor = 'val_loss',
mode = 'min',
save_best_only = True)
early_stopping= tf.keras.callbacks.EarlyStopping(monitor='val_accuracy',
mode='max',
patience=5,
restore_best_weights= True,
)
model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])
# Training and Evaluation of a Convolutional Neural Network (CNN) Model on Image Classification Data Using Keras in Python
history= model.fit(train_generator, steps_per_epoch = train_generator.samples // 32,validation_data=valid_generator,validation_steps = valid_generator.samples // 32 ,epochs=10,callbacks=[model_checkpoint_callback, early_stopping] )
# Training and Validation Loss Visualization for a Deep Learning Model in Python.
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# Prediction on Test Data Using Trained Model in Keras
predict = model.predict(test_generator, steps=np.ceil(samples/32))
# Adding Predicted Categories to Test DataFrame for a Binary Classification Problem
test_df['category'] = np.argmax(predict, axis=-1)
test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })
# Creating a Submission File for Image Classification Challenge
submission_df = test_df.copy()
submission_df['id'] = submission_df['filename'].str.split('.').str[0]
submission_df['label'] = submission_df['category']
submission_df.drop(['filename', 'category'], axis=1, inplace=True)
submission_df.to_csv('submission.csv', index=False)
- The code first imports necessary libraries such as NumPy, TensorFlow, Keras, Pandas, and Matplotlib. It then prints out the file paths in the specified directory using the os.walk() function in Python.
- Afterwards, its extract the contents of the training data zip file using zipfile.ZipFile and extractall() method. It then defines the paths to the directories where the images of cats and dogs are stored, creates a new directory to save the files in, and moves all files from 'dir1_path' and 'dir2_path' to the new directory.
- Next, it creates two lists of filenames and labels, shuffles them, and creates a Pandas DataFrame from them. The code then returns the count of unique values in the 'label' column of the DataFrame.
- Plot a grid of 20 images using Matplotlib to check if the images have been loaded correctly.It also displays images from a list in 4 rows x 5 columns format.
- Split the DataFrame df into training and validation sets using train_test_split() function from scikit-learn. It resets the index of the training and validation sets, and sets up data augmentation and image generator for image classification training.
- Creating a ImageDataGenerator object for training data and set various parameters like rescaling, rotation range, and flipping.
- Create a flow of training data using flow_from_dataframe() method and set various parameters like target size, batch size, and validation filenames.
- Similarly, create a ImageDataGenerator object for validation data and a flow of validation data using flow_from_dataframe() method.
- Get the list of all filenames in the 'test' directory and create a DataFrame test_df containing only the filenames.
- Create a flow of test data using flow_from_dataframe() method without any labels.
- Import the MobileNetV2 model from Tensorflow Keras and set its weights to 'imagenet', and set to base_model.trainable is False.
- Define the neural network architecture by adding a flatten layer and a dense output layer with sigmoid activation function to the base model using Sequential class from Keras.
- Compile the model using this function model.compile().and it takes as input the adam optimizer, and binary grossentropy loss function, and accuracy metric.
- Train the model using fit() method with training and validation generators, and set callbacks for ModelCheckpoint and EarlyStopping.
Plot the loss curves for training and validation data using Matplotlib. - Make predictions on test data using predict() method and get the predicted category values.
- Modify the DataFrame test_df to add a new column 'category' containing the predicted label values (1 for dog, 0 for cat).
- Finally, Create a new DataFrame submission_df containing only the id and label columns, and save it as a CSV file for submission.
i have uploaded the training file(images) only. Because, Due to upload file size limitation.