readme: ---
  license: apache-2.0
  ---
  # -*- coding: utf-8 -*-
  '''
  Original file is located at
  https://colab.research.google.com/drive/1HrNm5UMZr2Zjmze_HKW799p6LAHM8BTa
  '''
  
  
  
  from google.colab import files
  files.upload()
  
  !pip install kaggle
  
  !cp kaggle.json ~/.kaggle/
  
  !chmod 600 ~/.kaggle/kaggle.json
  
  !kaggle datasets download 'shaunthesheep/microsoft-catsvsdogs-dataset'
  
  !unzip microsoft-catsvsdogs-dataset
  
  import tensorflow as tf
  from tensorflow.keras.preprocessing.image import ImageDataGenerator
  
  image_dir='/content/PetImages/Cat'
  
  !mkdir train_folder
  
  !mkdir test_folder
  
  import os
  path='/content/train_folder/'
  dir='upside_down'
  dir2='normal'
  training_normal= os.path.join(path, dir2)
  training_upside= os.path.join(path, dir)
  os.mkdir(training_normal)
  os.mkdir(training_upside)
  
  #creating classes directories
  path='/content/test_folder/'
  dir='upside_down'
  dir2='normal'
  training_normal= os.path.join(path, dir2)
  training_upside= os.path.join(path, dir)
  os.mkdir(training_normal)
  os.mkdir(training_upside)
  
  
  
  #copying only the cat images to my train folder
  fnames = ['{}.jpg'.format(i) for i in range(2000)]
  for fname in fnames:
      src = os.path.join('/content/PetImages/Cat', fname)
      dst = os.path.join('/content/train_folder/normal', fname)
      shutil.copyfile(src, dst)
  
  import os 
  import shutil
  fnames = ['{}.jpg'.format(i) for i in range(2000, 4000)]
  for fname in fnames:
      src = os.path.join('/content/PetImages/Cat', fname)
      dst = os.path.join('/content/test_folder/normal', fname)
      shutil.copyfile(src, dst)
  
  from scipy import ndimage, misc
  from PIL import Image
  import numpy as np
  import matplotlib.pyplot as plt
  import imageio
  import os
  import cv2
  
  #inverting Training Images
  outPath = '/content/train_folder/upside_down'
  path ='/content/train_folder/normal'
  
      # iterate through the names of contents of the folder
  for image_path in os.listdir(path):
  
          # create the full input path and read the file
          input_path = os.path.join(path, image_path)
          image_to_rotate =plt.imread(input_path)
  
          # rotate the image
          rotated =  np.flipud(image_to_rotate)
  
          # create full output path, 'example.jpg' 
          # becomes 'rotate_example.jpg', save the file to disk
          fullpath = os.path.join(outPath, 'rotated_'+image_path)
          imageio.imwrite(fullpath, rotated)
  
  #nverting images for Validation
  outPath = '/content/test_folder/upside_down'
  path ='/content/test_folder/normal'
  
      # iterate through the names of contents of the folder
  for image_path in os.listdir(path):
  
          # create the full input path and read the file
          input_path = os.path.join(path, image_path)
          image_to_rotate =plt.imread(input_path)
  
          # rotate the image
          rotated =  np.flipud(image_to_rotate)
  
          # create full output path, 'example.jpg' 
          # becomes 'rotate_example.jpg', save the file to disk
          fullpath = os.path.join(outPath, 'rotated_'+image_path)
          imageio.imwrite(fullpath, rotated)
  
  ima='/content/train_folder/inverted/rotated_1001.jpg'
  image=plt.imread(ima)
  plt.imshow(image)
  # visualize the the figure
  plt.show()
  
  
  
  train_dir='/content/train_folder'
  train_gen=ImageDataGenerator(rescale=1./255)
  train_images= train_gen.flow_from_directory(
    train_dir,
    target_size=(250,250),
    batch_size=50,
    class_mode='binary'
  )
  
  validation_dir='/content/test_folder'
  test_gen=ImageDataGenerator(rescale=1./255)
  test_images= test_gen.flow_from_directory(
    validation_dir,
    target_size=(250,250),
    batch_size=50,
    class_mode='binary'
  )
  
  model=tf.keras.Sequential([
                             tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(250,250,3)),
                             tf.keras.layers.MaxPooling2D(2,2),
                             tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
                             tf.keras.layers.MaxPooling2D(2,2),
                             tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
                             tf.keras.layers.MaxPooling2D(2,2),
                             tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
                             tf.keras.layers.MaxPooling2D(2,2),
                             tf.keras.layers.Flatten(),
                             tf.keras.layers.Dense(512, activation='relu'),
                             tf.keras.layers.Dense(1, activation='sigmoid')
  
  ])
  
  from tensorflow.keras.optimizers import RMSprop
  model.compile(optimizer=RMSprop(learning_rate=0.001), loss=tf.keras.losses.BinaryCrossentropy(), metrics=['acc'])
  
  
  
  history=model.fit(train_images, validation_data=test_images, epochs=5, steps_per_epoch=40 )
  
  