# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="jz-VjHEgUhcp"
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator


# + colab={"base_uri": "https://localhost:8080/"} id="UF62NPvUr0ot" outputId="8a0e28b3-a421-4dfb-ce49-c28657fac864"
# !pip install tensorflow.gpu

# + colab={"base_uri": "https://localhost:8080/"} id="6ltuCuJ-sOlS" outputId="9e90c3b7-60fe-41d0-b3f2-85f3543a433a"
# !nvidia-smi

# + [markdown] id="0mKSsp5je-Bi"
# **Preprocessing the Training set**
#
#

# + colab={"base_uri": "https://localhost:8080/"} id="KwzWNStQajoT" outputId="a531f493-fc69-40b1-bfd8-558e0927101e"
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
training_set = train_datagen.flow_from_directory(
        '/content/drive/My Drive/dataset/training_set',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

# + [markdown] id="i82uqIaWfYoA"
# Preprocessing the Test set

# + colab={"base_uri": "https://localhost:8080/"} id="WMWxV7A7bFzC" outputId="525a8436-5cb3-445d-e951-6eb2f61a4826"
test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
        '/content/drive/MyDrive/dataset/test_set',
        target_size=(64, 64),
        batch_size=32,
        class_mode='binary')

# + id="fYbHUKuMcX8b"
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten

# + colab={"base_uri": "https://localhost:8080/"} id="YYBApycmhWFN" outputId="49992947-3c91-44d3-bfdf-5d4cd3471fcc"
#we have already resized our shape to 64x64 
128*128*3

# + id="ABlhCj62cw6S"
model = Sequential()

model.add(Conv2D(filters=32,kernel_size=(3,3),input_shape=(64,64,3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))

model.add(Conv2D(filters=32,kernel_size=(3,3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))

model.add(Flatten())

model.add(Dense(128,activation='relu'))


model.add(Dense(1,activation='sigmoid'))


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

# + id="TzaxveV0somX"
from tensorflow.keras.callbacks import EarlyStopping

# + id="F90TwFMMstS0"
early_stopping = EarlyStopping(monitor='val_loss',patience=2)

# + colab={"base_uri": "https://localhost:8080/"} id="cr4EOydxd5Qq" outputId="df713fd5-0144-44ff-9186-54e9bd8ca3d4"
model.fit(training_set,epochs=30,validation_data=(test_set),callbacks=[early_stopping])

# + id="iFAQTlw702ag"
import pandas as pd

# + id="Kt1UXk9omjNj"
metrics = pd.DataFrame(model.history.history)


# + colab={"base_uri": "https://localhost:8080/", "height": 300} id="SyMXjDaV0uhz" outputId="1d0f3034-4811-409b-eef1-7b8f4a19f581"
metrics

# + colab={"base_uri": "https://localhost:8080/"} id="WK7YGEQT08KN" outputId="b6420306-1651-4b24-bb59-7d671dd36c57"
metrics.columns

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="xmKsEKUx1Fed" outputId="1b5f4130-1b2e-4066-ef9a-12b498291447"
metrics[['accuracy', 'val_accuracy']].plot()

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="xvkmBpmv1J_J" outputId="74d0a779-d593-4e01-a235-f30744f28b4f"
metrics[['loss', 'val_loss']].plot()

# + colab={"base_uri": "https://localhost:8080/"} id="OsrMN6O71QcD" outputId="6e796b1a-bf22-45e2-da60-4061abbda0b3"
model.evaluate(training_set,verbose=0)

# + [markdown] id="Imvh2N1U3KUb"
# Making a single prediction

# + id="u8Ik7cUt1dBK"
import numpy as np
from keras.preprocessing import image

test_image = image.load_img('/content/drive/MyDrive/dataset/single_prediction/cat_or_dog_1.jpg', target_size=(64,64) )
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)

result = model.predict(test_image)

training_set.class_indices
if result[0][0] == 1:
  prediction = 'dog'
else:
  prediction = 'cat'
 
   

# + colab={"base_uri": "https://localhost:8080/"} id="4Tt2E_Y52Ovm" outputId="31553f76-e70f-47d2-f14f-8c82dca6f1b3"
print(prediction)

# + [markdown] id="tQVraeJP6p2W"
# Here we can see that our model successfully predicted the result as dog
#
# ---
#
#

# + [markdown] id="6mUBZizq64fA"
# Make another prediction

# + id="jIXN0tcc2Vcf"
test_image = image.load_img('/content/drive/MyDrive/dataset/single_prediction/cat_or_dog_2.jpg', target_size=(64,64) )
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)

result = model.predict(test_image)

training_set.class_indices
if result[0][0] == 1:
  prediction = 'dog'
else:
  prediction = 'cat'


# + colab={"base_uri": "https://localhost:8080/"} id="Kas6_iDU2eyw" outputId="f3862713-536e-4b50-e764-bed21ad22b7a"
print(prediction)

# + [markdown] id="fe82Gr6W7KSL"
# Here we can see that our model successfully predicted the result as cat
#

# + [markdown] id="f-SuQSDL70XH"
# **Making predictions by taking random image from test data and predicting the result.**

# + id="a6WnFbWf7y4N"
test_image = image.load_img('/content/drive/MyDrive/dataset/single_prediction/cat.4533.jpg', target_size=(64,64) )
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)

result = model.predict(test_image)

training_set.class_indices
if result[0][0] == 1:
  prediction = 'dog'
else:
  prediction = 'cat'


# + colab={"base_uri": "https://localhost:8080/"} id="oTvQzTyU9hZo" outputId="8663d6b2-4a34-4552-c9af-107728e7216d"
print(prediction)

# + [markdown] id="Uo3sD4n9-33k"
# We can see that our was able to predict the correct resut

# + id="BMXRRW05QDdd"
test_image2 = image.load_img('/content/drive/MyDrive/dataset/single_prediction/cat.4326.jpg', target_size=(64,64) )
test_image2 = image.img_to_array(test_image2)
test_image2 = np.expand_dims(test_image2, axis=0)

result = model.predict(test_image2)

training_set.class_indices
if result[0][0] == 1:
  prediction = 'dog'
else:
  prediction = 'cat'


# + colab={"base_uri": "https://localhost:8080/"} id="zqy1DWuzRQmW" outputId="3bbeaae1-852b-4ae9-e38e-9282a4ad50a3"
print(prediction)

# + id="sqwIjYrbRgHG"
test_image3 = image.load_img('/content/drive/MyDrive/dataset/single_prediction/dog.4067.jpg', target_size=(64,64) )
test_image3 = image.img_to_array(test_image3)
test_image3 = np.expand_dims(test_image3, axis=0)

result = model.predict(test_image3)

training_set.class_indices
if result[0][0] == 1:
  prediction = 'dog'
else:
  prediction = 'cat'


# + colab={"base_uri": "https://localhost:8080/"} id="9L4aaidfSA8U" outputId="1e071980-ac85-4449-954e-5f09bd7b6916"
print(prediction)

# + [markdown] id="bXBv5EVySIzv"
# We can see that our model is predicting the correct results on random image predictions

StarcoderdataJupyterScriptsDedupFiltered