{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-04T02:37:43.159802Z","iopub.execute_input":"2022-05-04T02:37:43.160114Z","iopub.status.idle":"2022-05-04T02:37:43.191253Z","shell.execute_reply.started":"2022-05-04T02:37:43.160032Z","shell.execute_reply":"2022-05-04T02:37:43.190544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Contents\nIn This NoteBook , we will follow these \n\n1. Explore the example data of Dogs vs. Cats\n1. Build and train a neural network to classify between the two pets\n1. Evaluate the training and validation accuracy","metadata":{}},{"cell_type":"markdown","source":"# Explore the data of Dogs vs. Cats","metadata":{}},{"cell_type":"code","source":"import os\nimport zipfile\nimport pandas as pd\nfrom tqdm import tqdm\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom tensorflow.keras.optimizers import RMSprop\n","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:37:43.193055Z","iopub.execute_input":"2022-05-04T02:37:43.193548Z","iopub.status.idle":"2022-05-04T02:37:48.489618Z","shell.execute_reply.started":"2022-05-04T02:37:43.193488Z","shell.execute_reply":"2022-05-04T02:37:48.488817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"work_path = './cats_and_dogs_filtered'\nos.mkdir(work_path)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:37:48.490935Z","iopub.execute_input":"2022-05-04T02:37:48.491207Z","iopub.status.idle":"2022-05-04T02:37:48.498597Z","shell.execute_reply.started":"2022-05-04T02:37:48.491172Z","shell.execute_reply":"2022-05-04T02:37:48.497031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlocal_zip = '../input/dogs-vs-cats/test1.zip'\nzip_ref = zipfile.ZipFile(local_zip,'r')\nzip_ref.extractall(work_path)\n\nlocal_zip = '../input/dogs-vs-cats/train.zip'\nzip_ref = zipfile.ZipFile(local_zip,'r')\nzip_ref.extractall(work_path)\n\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:37:48.500875Z","iopub.execute_input":"2022-05-04T02:37:48.501592Z","iopub.status.idle":"2022-05-04T02:38:04.653582Z","shell.execute_reply.started":"2022-05-04T02:37:48.501551Z","shell.execute_reply":"2022-05-04T02:38:04.652819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = os.path.join(work_path, 'train')\ntest_path = os.path.join(work_path, 'test1')","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:04.654973Z","iopub.execute_input":"2022-05-04T02:38:04.655225Z","iopub.status.idle":"2022-05-04T02:38:04.659586Z","shell.execute_reply.started":"2022-05-04T02:38:04.655188Z","shell.execute_reply":"2022-05-04T02:38:04.658751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.DataFrame({'image_name':os.listdir(train_path)})\ntrain_df['label'] =train_df['image_name'].apply(lambda x: x.split('.')[0])\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:04.661378Z","iopub.execute_input":"2022-05-04T02:38:04.661890Z","iopub.status.idle":"2022-05-04T02:38:04.715872Z","shell.execute_reply.started":"2022-05-04T02:38:04.661856Z","shell.execute_reply":"2022-05-04T02:38:04.715197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame({'image_name':os.listdir(test_path)})\ntest_df['label'] =test_df['image_name'].apply(lambda x: x.split('.')[0])\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:04.717055Z","iopub.execute_input":"2022-05-04T02:38:04.717522Z","iopub.status.idle":"2022-05-04T02:38:04.743287Z","shell.execute_reply.started":"2022-05-04T02:38:04.717468Z","shell.execute_reply":"2022-05-04T02:38:04.742511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tidying up the data\n","metadata":{}},{"cell_type":"markdown","source":"### Arrange Training in sub-directories","metadata":{}},{"cell_type":"code","source":"dog_path_train = os.path.join(train_path, 'dog')\nos.mkdir(dog_path_train)\ndog_df_train = train_df[train_df.label=='dog']\nfor n in tqdm(dog_df_train.image_name):\n    os.rename((os.path.join(train_path, n)), (os.path.join(dog_path_train, n)))","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:04.744668Z","iopub.execute_input":"2022-05-04T02:38:04.744991Z","iopub.status.idle":"2022-05-04T02:38:05.062974Z","shell.execute_reply.started":"2022-05-04T02:38:04.744952Z","shell.execute_reply":"2022-05-04T02:38:05.061456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_path_train = os.path.join(train_path, 'cat')\nos.mkdir(cat_path_train)\ncat_df_train = train_df[train_df.label=='cat']\nfor n in tqdm(cat_df_train.image_name):\n    os.rename((os.path.join(train_path, n)), (os.path.join(cat_path_train, n)))","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:05.064652Z","iopub.execute_input":"2022-05-04T02:38:05.064942Z","iopub.status.idle":"2022-05-04T02:38:05.366596Z","shell.execute_reply.started":"2022-05-04T02:38:05.064901Z","shell.execute_reply":"2022-05-04T02:38:05.365355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### check subdirectories","metadata":{}},{"cell_type":"code","source":"# # check subdirectories\nbase_dir = './cats_and_dogs_filtered'\n\nprint(' Contents of base directory')\nprint(os.listdir(base_dir))\n\nprint('\\n Contents of Train directory')\ntrain_path = f'{base_dir}/train'\nprint(os.listdir(train_path))\n\nprint('\\n Contents of validation directory')\nprint(os.listdir(test_path)[:5])","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:05.369560Z","iopub.execute_input":"2022-05-04T02:38:05.369777Z","iopub.status.idle":"2022-05-04T02:38:05.384423Z","shell.execute_reply.started":"2022-05-04T02:38:05.369746Z","shell.execute_reply":"2022-05-04T02:38:05.383730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### assign each dir to a varibles","metadata":{}},{"cell_type":"code","source":"# assign each dir to a varibles\ntrain_dir = os.path.join(base_dir,'train')\nvalidation_dir = os.path.join(base_dir,'test1')\n\ntrain_cats_dir = os.path.join(train_dir,'cat')\ntrain_dogs_dir = os.path.join(train_dir,'dog')","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:05.385651Z","iopub.execute_input":"2022-05-04T02:38:05.386061Z","iopub.status.idle":"2022-05-04T02:38:05.391819Z","shell.execute_reply.started":"2022-05-04T02:38:05.386023Z","shell.execute_reply":"2022-05-04T02:38:05.390089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# see the filenames look like \ntrain_cats_names = os.listdir(train_cats_dir)\ntrain_dogs_names = os.listdir(train_dogs_dir)\n\nprint(train_cats_names[:5])\nprint(train_dogs_names[:5])","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:05.393354Z","iopub.execute_input":"2022-05-04T02:38:05.393888Z","iopub.status.idle":"2022-05-04T02:38:05.413102Z","shell.execute_reply.started":"2022-05-04T02:38:05.393832Z","shell.execute_reply":"2022-05-04T02:38:05.412413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### number of cats and dogs","metadata":{}},{"cell_type":"code","source":"# total number of cats and dogs in training set and validation set\nprint(f'numbers of cats in training set = {len(train_cats_names)}')\nprint(f'numbers of dogs in training set = {len(train_dogs_names)}')\nprint(f'numbers of cats and dogs in validation set = {len(os.listdir(validation_dir))}')","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:05.414212Z","iopub.execute_input":"2022-05-04T02:38:05.414954Z","iopub.status.idle":"2022-05-04T02:38:05.428338Z","shell.execute_reply.started":"2022-05-04T02:38:05.414917Z","shell.execute_reply":"2022-05-04T02:38:05.427566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Take a look at a few images","metadata":{}},{"cell_type":"code","source":"# take a look at a few images\n%matplotlib inline\n\nnrows = 4\nncols = 4\n\npic_index = 0\n\nfig = plt.gcf()\nfig.set_size_inches(nrows*4,ncols*4)\n\nnext_cat_pic = [os.path.join(train_cats_dir,fname) for fname in train_cats_names[pic_index:pic_index+8]]\n\nnext_dog_pic = [os.path.join(train_dogs_dir,fname) for fname in train_dogs_names[pic_index:pic_index+8]]\n\nfor i ,img_path in enumerate(next_cat_pic+next_dog_pic):\n    sp = plt.subplot(nrows,ncols,i+1)\n    sp.axis('off')\n    \n    img = mpimg.imread(img_path)\n    plt.imshow(img)\nplt.show()    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:05.429555Z","iopub.execute_input":"2022-05-04T02:38:05.429862Z","iopub.status.idle":"2022-05-04T02:38:06.794875Z","shell.execute_reply.started":"2022-05-04T02:38:05.429826Z","shell.execute_reply":"2022-05-04T02:38:06.792571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building a model from scratch","metadata":{}},{"cell_type":"code","source":"def create_model():\n\n  model = tf.keras.models.Sequential([ \n    tf.keras.layers.Conv2D(16,(3,3), activation = 'relu', input_shape=(150,150,3)),\n    tf.keras.layers.MaxPooling2D(2,2),\n\n    tf.keras.layers.Conv2D(32,(3,3), activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n\n    tf.keras.layers.Conv2D(64,(3,3), activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation = 'relu'),\n    tf.keras.layers.Dense(1, activation='sigmoid')\n  ])\n\n  \n  model.compile(optimizer=RMSprop(lr=0.001),\n                loss='binary_crossentropy',\n                metrics=['accuracy']) \n    \n  return model\n","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:06.795976Z","iopub.execute_input":"2022-05-04T02:38:06.796213Z","iopub.status.idle":"2022-05-04T02:38:06.806475Z","shell.execute_reply.started":"2022-05-04T02:38:06.796181Z","shell.execute_reply":"2022-05-04T02:38:06.805667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:06.807660Z","iopub.execute_input":"2022-05-04T02:38:06.808437Z","iopub.status.idle":"2022-05-04T02:38:09.440013Z","shell.execute_reply.started":"2022-05-04T02:38:06.808400Z","shell.execute_reply":"2022-05-04T02:38:09.439312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ImageDataGenrator","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(rescale=1./255,\n      rotation_range=40,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      shear_range=0.2,\n      zoom_range=0.2,\n      horizontal_flip=True,\n      fill_mode='nearest',\n      validation_split=0.2\n                                  ) # set validation split\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150,150),\n    batch_size=50,\n    class_mode='binary',\n    subset='training'\n) # set as training data\n\nvalidation_generator = train_datagen.flow_from_directory(\n    train_dir, # same directory as training data\n    target_size=(150, 150),\n    batch_size=50,\n    class_mode='binary',\n    subset='validation') # set as validation data\n","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:09.441446Z","iopub.execute_input":"2022-05-04T02:38:09.441940Z","iopub.status.idle":"2022-05-04T02:38:11.054334Z","shell.execute_reply.started":"2022-05-04T02:38:09.441902Z","shell.execute_reply":"2022-05-04T02:38:11.053565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class mycallback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self,epoch,logs={}):\n        if(logs.get('val_accuracy')>=0.90):\n            self.model.stop_training = True\n            \ncallback = mycallback()\n            \n            ","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:11.055643Z","iopub.execute_input":"2022-05-04T02:38:11.056047Z","iopub.status.idle":"2022-05-04T02:38:11.061862Z","shell.execute_reply.started":"2022-05-04T02:38:11.056008Z","shell.execute_reply":"2022-05-04T02:38:11.061133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Training","metadata":{}},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    steps_per_epoch = train_generator.samples//50,#batch_size, \n    epochs = 30,\n    verbose=1,\n    validation_data = validation_generator,\n    validation_steps = validation_generator.samples//50,#batch_size,\n    callbacks=[callback]\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T02:38:11.064309Z","iopub.execute_input":"2022-05-04T02:38:11.064564Z","iopub.status.idle":"2022-05-04T04:03:52.517565Z","shell.execute_reply.started":"2022-05-04T02:38:11.064532Z","shell.execute_reply":"2022-05-04T04:03:52.516771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Validation accuracy","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nplt.figure(figsize=(8, 8))\nplt.subplot(2, 1, 1)\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.ylabel('Accuracy')\nplt.ylim([min(plt.ylim()),1])\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 1, 2)\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.ylabel('Cross Entropy')\nplt.ylim([0,1.0])\nplt.title('Training and Validation Loss')\nplt.xlabel('epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:03:52.518936Z","iopub.execute_input":"2022-05-04T04:03:52.519190Z","iopub.status.idle":"2022-05-04T04:03:52.851415Z","shell.execute_reply.started":"2022-05-04T04:03:52.519155Z","shell.execute_reply":"2022-05-04T04:03:52.850666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n## **UP Vote 🥺**\nThanks , love you bro 👍\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}