0% found this document useful (0 votes)
14 views5 pages

dog and cat img classification.ipynb - Colab

Uploaded by

hasini.thota.04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

dog and cat img classification.ipynb - Colab

Uploaded by

hasini.thota.04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

!mkdir -p ~/.

kaggle
!cp kaggle.json ~/.kaggle/

!kaggle datasets download -d salader/dogs-vs-cats

import zipfile
zip_ref = zipfile.ZipFile('/content/dogs-vs-cats.zip', 'r')
zip_ref.extractall('/content')
zip_ref.close()

import tensorflow as tf
from tensorflow import keras
from keras import Sequential
from keras.layers import Dense,Conv2D,MaxPooling2D,Flatten, BatchNormalization, Dropout

train_ds = keras.utils.image_dataset_from_directory(
directory = '/content/train',
labels="inferred",
label_mode="int",#assigns 0 for cat and 1 for dogs
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),
)

Found 20000 files belonging to 2 classes.


Found 5000 files belonging to 2 classes.

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)
# Dataset is ready
# CNN model
# Contains 3 layers
# First layer - 32 filters, Second layer - 64 filters, Third layer - 128 filters

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'))

model.summary()
Model: "sequential_3"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ conv2d_9 (Conv2D) │ (None, 254, 254, 32) │ 896 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ batch_normalization_9 │ (None, 254, 254, 32) │ 128 │
│ (BatchNormalization) │ │ │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ max_pooling2d_9 (MaxPooling2D) │ (None, 127, 127, 32) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_10 (Conv2D) │ (None, 125, 125, 64) │ 18,496 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ batch_normalization_10 │ (None, 125, 125, 64) │ 256 │
│ (BatchNormalization) │ │ │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ max_pooling2d_10 (MaxPooling2D) │ (None, 62, 62, 64) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ conv2d_11 (Conv2D) │ (None, 60, 60, 128) │ 73,856 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ batch_normalization_11 │ (None, 60, 60, 128) │ 512 │
│ (BatchNormalization) │ │ │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ max_pooling2d_11 (MaxPooling2D) │ (None, 30, 30, 128) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ flatten_3 (Flatten) │ (None, 115200) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_9 (Dense) │ (None, 128) │ 14,745,728 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dropout_6 (Dropout) │ (None, 128) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_10 (Dense) │ (None, 64) │ 8,256 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dropout_7 (Dropout) │ (None, 64) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_11 (Dense) │ (None, 1) │ 65 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘

model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

history=model.fit(train_ds,epochs=10,validation_data=validation_ds)

Epoch 1/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 66s 84ms/step - accuracy: 0.5642 - loss: 2.9611 - val_accuracy: 0.6682 - val_loss: 0.6055
Epoch 2/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 51s 81ms/step - accuracy: 0.6814 - loss: 0.5940 - val_accuracy: 0.7160 - val_loss: 0.5469
Epoch 3/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 80s 79ms/step - accuracy: 0.7408 - loss: 0.5248 - val_accuracy: 0.7642 - val_loss: 0.4932
Epoch 4/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 83s 80ms/step - accuracy: 0.7867 - loss: 0.4594 - val_accuracy: 0.7652 - val_loss: 0.4840
Epoch 5/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 82s 81ms/step - accuracy: 0.8223 - loss: 0.3985 - val_accuracy: 0.7996 - val_loss: 0.4521
Epoch 6/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 82s 80ms/step - accuracy: 0.8445 - loss: 0.3536 - val_accuracy: 0.7688 - val_loss: 0.5311
Epoch 7/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 54s 86ms/step - accuracy: 0.8825 - loss: 0.2690 - val_accuracy: 0.7700 - val_loss: 0.7174
Epoch 8/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 82s 86ms/step - accuracy: 0.9285 - loss: 0.1817 - val_accuracy: 0.8040 - val_loss: 0.6982
Epoch 9/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 49s 79ms/step - accuracy: 0.9482 - loss: 0.1387 - val_accuracy: 0.8240 - val_loss: 0.7026
Epoch 10/10
625/625 ━━━━━━━━━━━━━━━━━━━━ 84s 82ms/step - accuracy: 0.9643 - loss: 0.0989 - val_accuracy: 0.8272 - val_loss: 0.8087

from google.colab import drive


drive.mount('/content/drive')

Mounted at /content/drive

import cv2
test_img = cv2.imread('/content/dog2.jpg')
plt.imshow(test_img)

<matplotlib.image.AxesImage at 0x7d9ebe0cabf0>
test_img.shape

(430, 710, 3)

test_img = cv2.resize(test_img,(256,256))

test_input = test_img.reshape((1,256,256,3))

#1 means dog, 0 means cat


model.predict(test_input)

1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 917ms/step


array([[1.]], dtype=float32)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy