Machine Learning Project
Classification of Pet’s Face(Dog vs Cats)
https://notesgallery.com/machine-learning-projects/ Youtube video link at the end
Step 1
Download Datasets from Kaggle.https://www.kaggle.com/datasets/salader/dogs-vs-cats
Step 2
Open Google Colab in browser https://colab.research.google.com/–>Go to file–>Create new notebook–>Rename notebook by clicking here.
Step 3
Go to Runtime–>Change runtime–>Hardware accelerator-->GPU–>save
Step 4
Goto profile in Kaggle–>Click account–>slide and get API token
a file named kaggle.jason will be download in your device.
Step 5
Upload kaggle.jason file in google colab and then rename it.
Step 6
Go to Kaggle (Dogs and cats datasets)–>click on 3 dots–>click Copy API command
Step 7 : run this two commands first
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
Step 8
copy API command in Google Colab Notebook with exclamation mark
!kaggle datasets download -d salader/dogs-vs-cats
Step 9: unzip file
import zipfile
zip_ref = zipfile.ZipFile(‘/content/dogs-vs-cats.zip’, ‘r’)
zip_ref.extractall(‘/content’)
zip_ref.close()
click here to refresh
refresh again after running the program
and you get your train and test data
Step 10
import tensorflow as tf
from tensorflow import keras
from keras import Sequential
from keras.layers import Dense,Conv2D,MaxPooling2D,Flatten,BatchNormalization,Dropout
Step 11
#generators
train_ds = keras.utils.image_dataset_from_directory(
directory = ‘/content/train’,
labels=’inferred’,
label_mode = ‘int’,
batch_size = 32,
image_size = (256,256)
)
validation_ds = keras.utils.image_dataset_from_directory(
directory = ‘/content/test’,
labels=’inferred’,
label_mode = ‘int’,
batch_size = 32,
image_size = (256,256)
)
Step 12
#Normalize
def process(image,label):
image = tf.cast(image/255. ,tf.float32)
return image,label
train_ds = train_ds.map(process)
validation_ds = validation_ds.map(process)
Step 13
# create CNN model
model = Sequential()
model.add(Conv2D(32,kernel_size=(3,3),padding=’valid’,activation=’relu’,input_shape=(256,256,3)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2),strides=2,padding=’valid’))
model.add(Conv2D(64,kernel_size=(3,3),padding=’valid’,activation=’relu’))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2),strides=2,padding=’valid’))
model.add(Conv2D(128,kernel_size=(3,3),padding=’valid’,activation=’relu’))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2),strides=2,padding=’valid’))
model.add(Flatten())
model.add(Dense(128,activation=’relu’))
model.add(Dropout(0.1))
model.add(Dense(64,activation=’relu’))
model.add(Dropout(0.1))
model.add(Dense(1,activation=’sigmoid’))
Step 14
Step 15
model.compile(optimizer=’adam’,loss=’binary_crossentropy’,metrics=[‘accuracy’])
Step 16
history = model.fit(train_ds,epochs=10,validation_data=validation_ds)
Step 17
import matplotlib.pyplot as plt
plt.plot(history.history[‘accuracy’],color=’red’,label=’train’)
plt.plot(history.history[‘val_accuracy’],color=’blue’,label=’validation’)
plt.legend()
plt.show()
Step 18
plt.plot(history.history[‘loss’],color=’red’,label=’train’)
plt.plot(history.history[‘val_loss’],color=’blue’,label=’validation’)
plt.legend()
plt.show()
Step 19
Testing of model starts here.
download image of dog and cat from google
open it in gooogle colab
copy path of image –>inside single quote(watch you tube video ,link provided at the end )
Step 20
import cv2
Step 21
test_img = cv2.imread(‘/content/256px-Canis_lupus_familiaris.001_-_Monfero.jpg‘)
Step 22
Step 23
Step 24
test_img = cv2.imread(‘/content/256px-Domestic_cat_in_Poland.jpg‘)
Step 25
NOTE: you can test any image of cat or dog downloaded from google.
Conclusion
Once you understand the classification model, then you are able to make models like classification of face for security,
classification of bird species, classification of lung cancer etc….
Youtube video link!