<jupyter_start><jupyter_text><jupyter_code>import os
from tensorflow.keras import layers
from tensorflow.keras import Model


#Loading the InceptionV3 model from keras
from tensorflow.keras.applications.inception_v3 import InceptionV3


#instantiating the model with the desired input shape and ImageNet weights
pre_trained_model = InceptionV3(input_shape = (150, 150, 3), 
                                include_top = False, 
                                weights = "imagenet")


#Freezing the pre_trained_model layers for preventing it from training with the new dataset
for layer in pre_trained_model.layers:
  layer.trainable = False
  
pre_trained_model.summary()

#Extracting the desired filter bank (convolutional block) from the pre_trained_model

last_layer = pre_trained_model.get_layer('mixed7')

print('last layer output shape: ', last_layer.output_shape)

last_output = last_layer.output
#Building the model by adding the desired deep layers to the filter bank (convolutional block) which is extracted in the previous step

#importing the desired optimizer
from tensorflow.keras.optimizers import RMSprop

# Flatten the output of the last layer of the filter bank
x = layers.Flatten()(last_output)
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024, activation='relu')(x)
# Add a dropout rate of 0.2 to reduce overfitting
x = layers.Dropout(0.2)(x)
# Add a fully connected layer with 256 hidden units and ReLU activation
#x = layers.Dense(256, activation='relu')(x) 
# Add a fully connected layer with 64 hidden units and ReLU activation
#x = layers.Dense(32, activation='relu')(x)                 
# Add a final sigmoid layer for classification 
x = layers.Dense(1, activation='sigmoid')(x)           

model = Model( pre_trained_model.input, x) 

model.summary()

model.compile(optimizer = RMSprop(lr=0.0001), loss = 'binary_crossentropy', metrics = ['accuracy'])

#Downloading the partial cats_and_dogs dataset. The link is povided by <NAME>.

!wget --no-check-certificate \
        https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip \
       -O /tmp/cats_and_dogs_filtered.zip


import zipfile
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import random

local_zip = '//tmp/cats_and_dogs_filtered.zip'

zip_ref = zipfile.ZipFile(local_zip, 'r')

zip_ref.extractall('/tmp')
zip_ref.close()

# Defining the directory that contains the dataset
base_dir = '/tmp/cats_and_dogs_filtered'

train_dir = os.path.join( base_dir, 'train')
validation_dir = os.path.join( base_dir, 'validation')

#Defining the train and validation directoies for cat and dog 
train_cats_dir = os.path.join(train_dir, 'cats') 
train_dogs_dir = os.path.join(train_dir, 'dogs') 
validation_cats_dir = os.path.join(validation_dir, 'cats') 
validation_dogs_dir = os.path.join(validation_dir, 'dogs')

train_cat_fnames = os.listdir(train_cats_dir)
train_dog_fnames = os.listdir(train_dogs_dir)

#Finding the number of train and test images 
num_cat_trn_imgs = len([name for name in os.listdir(train_cats_dir)])
num_dog_trn_imgs = len([name for name in os.listdir(train_dogs_dir)])

num_cat_val_imgs = len([name for name in os.listdir(validation_cats_dir)])
num_dog_val_imgs = len([name for name in os.listdir(validation_dogs_dir)])


print('num_cat_trn_imgs =',num_cat_trn_imgs, 'num_dog_trn_imgs =',num_dog_trn_imgs,
      'ttl_num_trn_imgs = ', num_cat_trn_imgs+num_dog_trn_imgs)

print('num_cat_val_imgs =',num_cat_val_imgs, 'num_dog_val_imgs =',num_dog_val_imgs,
      'ttl_num_val_imgs = ', num_cat_val_imgs+num_dog_val_imgs)

#Displaying random images from the training dataset 
exmp_trn_cat_path = os.path.join(train_cats_dir,
                                         os.listdir(train_cats_dir)[random.randint(0,num_cat_trn_imgs-1)]) 
exmp_trn_cat = mpimg.imread(exmp_trn_cat_path)
                                    
plt.imshow(exmp_trn_cat)
plt.show()

exmp_trn_dog_path = os.path.join(train_dogs_dir,
                                         os.listdir(train_dogs_dir)[random.randint(0,num_dog_trn_imgs-1)])
exmp_trn_dog = mpimg.imread(exmp_trn_dog_path)
                                         
plt.imshow(exmp_trn_dog)
plt.show()


from tensorflow.keras.preprocessing.image import ImageDataGenerator

#Generating the train dataset with ImageDataGenerator and applying image augmentation to reduce overfitting
train_datagen = ImageDataGenerator(rescale = 1./255.,
                                   rotation_range = 40,
                                   width_shift_range = 0.2,
                                   height_shift_range = 0.2,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

#Normalizing the test images
test_datagen = ImageDataGenerator( rescale = 1/255. )

#Using flow_from_directory to create the train data in batches of 20 images
train_generator = train_datagen.flow_from_directory(train_dir,
                                                    batch_size = 20,
                                                    class_mode = 'binary', 
                                                    target_size = (150, 150))     

#Using flow_from_directory to create the test data in batches of 20 images
validation_generator =  test_datagen.flow_from_directory( validation_dir,
                                                          batch_size  = 20,
                                                          class_mode  = 'binary', 
                                                          target_size = (150, 150))
#Fitting the model

history = model.fit(train_generator, validation_data = validation_generator, steps_per_epoch = 100, epochs = 20, validation_steps = 50, verbose = 1)
#plotting the loss and accuracy VS epochs 

import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.legend(loc=0)
plt.figure()


plt.show()
#Testing the trained model on images outside the cats_and_dogs dataset
import numpy as np

from google.colab import files
from keras.preprocessing import image

uploaded=files.upload()

for fn in uploaded.keys():
 
  # predicting images
  path='/content/' + fn
  img=image.load_img(path, target_size=(150, 150))
  
  y=image.img_to_array(img)
  y=np.expand_dims(y/255, axis=0)
  images = np.vstack([y])
  
  classes = model.predict(images, batch_size=10)
  
  print(classes[0])
  
  if classes[0]>0.5:
    print(fn + " is a dog")
    
  else:
    print(fn + " is a cat")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08797440744510687, 'fraction_numerical': 0.01555911007706849, 'mean_word_length': 3.0578171091445427, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '610445', 'license': 'Apache-2.0', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2140, 'n_words': 534, 'path': 'Cat_vs_Dog.ipynb', 'repo_name': 'Arazsh/Cat-or-Dog-Transfer-Learning', 'size': '6887'}