{"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)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\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\nimport zipfile\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":"2021-11-25T02:36:38.342720Z","iopub.execute_input":"2021-11-25T02:36:38.343044Z","iopub.status.idle":"2021-11-25T02:36:42.381736Z","shell.execute_reply.started":"2021-11-25T02:36:38.342955Z","shell.execute_reply":"2021-11-25T02:36:42.380955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing\n\nBefore we can commence any form of Deep Learning, we will have to sort the images into training and testing folders. This is done in order to faciliate the ImageDataGenerator from Keras.","metadata":{}},{"cell_type":"code","source":"root = '/kaggle/input/dogs-vs-cats'\nroot_temp = '/kaggle/working/'","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:36:42.383433Z","iopub.execute_input":"2021-11-25T02:36:42.383839Z","iopub.status.idle":"2021-11-25T02:36:42.388000Z","shell.execute_reply.started":"2021-11-25T02:36:42.383801Z","shell.execute_reply":"2021-11-25T02:36:42.387108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Removes train and test folders\ntry:\n    os.rmdir(os.path.join(root_temp,'train'))\n    os.rmdir(os.path.join(root_temp,'test1'))\n    os.rmdir(os.path.join(root_temp,'val'))\nexcept OSError as error:\n    pass","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:36:42.389254Z","iopub.execute_input":"2021-11-25T02:36:42.389551Z","iopub.status.idle":"2021-11-25T02:36:42.398363Z","shell.execute_reply.started":"2021-11-25T02:36:42.389516Z","shell.execute_reply":"2021-11-25T02:36:42.397575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Unzips files\nwith zipfile.ZipFile(os.path.join(root,'train.zip'), 'r') as zip_ref:\n    zip_ref.extractall(os.path.join(root_temp))\nwith zipfile.ZipFile(os.path.join(root,'test1.zip'), 'r') as zip_ref:\n    zip_ref.extractall(os.path.join(root_temp))","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:36:42.400745Z","iopub.execute_input":"2021-11-25T02:36:42.401420Z","iopub.status.idle":"2021-11-25T02:37:00.075085Z","shell.execute_reply.started":"2021-11-25T02:36:42.401380Z","shell.execute_reply":"2021-11-25T02:37:00.074261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Confirm number of images\nprint('total number in training folder ', len(os.listdir(os.path.join(root_temp,'train'))))\nprint('total number in test folder ', len(os.listdir(os.path.join(root_temp,'test1'))))","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.077189Z","iopub.execute_input":"2021-11-25T02:37:00.077661Z","iopub.status.idle":"2021-11-25T02:37:00.105352Z","shell.execute_reply.started":"2021-11-25T02:37:00.077622Z","shell.execute_reply":"2021-11-25T02:37:00.104586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Confirm images in\ntrain_images = [fname for fname in os.listdir(os.path.join(root_temp,'train'))]\ntest_images = [fname for fname in os.listdir(os.path.join(root_temp,'test1'))]","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.106776Z","iopub.execute_input":"2021-11-25T02:37:00.107254Z","iopub.status.idle":"2021-11-25T02:37:00.133869Z","shell.execute_reply.started":"2021-11-25T02:37:00.107202Z","shell.execute_reply":"2021-11-25T02:37:00.133228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('total number in training set ', len(train_images))\nprint('total number in test set ', len(test_images))","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.134961Z","iopub.execute_input":"2021-11-25T02:37:00.135158Z","iopub.status.idle":"2021-11-25T02:37:00.141026Z","shell.execute_reply.started":"2021-11-25T02:37:00.135135Z","shell.execute_reply":"2021-11-25T02:37:00.140163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(train_images).head()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.142433Z","iopub.execute_input":"2021-11-25T02:37:00.143016Z","iopub.status.idle":"2021-11-25T02:37:00.165889Z","shell.execute_reply.started":"2021-11-25T02:37:00.142978Z","shell.execute_reply":"2021-11-25T02:37:00.165233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(test_images).head()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.167140Z","iopub.execute_input":"2021-11-25T02:37:00.167424Z","iopub.status.idle":"2021-11-25T02:37:00.176789Z","shell.execute_reply.started":"2021-11-25T02:37:00.167390Z","shell.execute_reply":"2021-11-25T02:37:00.175868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cat = []\ntrain_dog = []\nfor image in train_images:\n    if 'cat' in image:\n        train_cat.append(image)\n    else: train_dog.append(image)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.180612Z","iopub.execute_input":"2021-11-25T02:37:00.180933Z","iopub.status.idle":"2021-11-25T02:37:00.190667Z","shell.execute_reply.started":"2021-11-25T02:37:00.180896Z","shell.execute_reply":"2021-11-25T02:37:00.189659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('total number of cats in training set ', len(train_cat))\nprint('total number of dogs in training set ', len(train_dog))\nprint('total number of images in test set ', len(test_images))","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.192103Z","iopub.execute_input":"2021-11-25T02:37:00.192462Z","iopub.status.idle":"2021-11-25T02:37:00.202649Z","shell.execute_reply.started":"2021-11-25T02:37:00.192424Z","shell.execute_reply":"2021-11-25T02:37:00.201870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    os.mkdir(os.path.join(root_temp,'train','cat'))\n    os.mkdir(os.path.join(root_temp,'train','dog'))\nexcept OSError:\n    pass","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.203991Z","iopub.execute_input":"2021-11-25T02:37:00.205412Z","iopub.status.idle":"2021-11-25T02:37:00.210976Z","shell.execute_reply.started":"2021-11-25T02:37:00.205360Z","shell.execute_reply":"2021-11-25T02:37:00.210031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil \n\nfor image in train_cat:\n    shutil.move(os.path.join(root_temp,'train',image),os.path.join(root_temp,'train','cat',image))\n    \nfor image in train_dog:\n    shutil.move(os.path.join(root_temp,'train',image),os.path.join(root_temp,'train','dog',image))\n\nroot_train = os.path.join(root_temp,'train') \ntrain_cat = os.path.join(root_train,'cat')\ntrain_dog = os.path.join(root_train,'dog')\n\nroot_val = os.path.join(root_temp,'val')\nval_cat = os.path.join(root_val,'cat')\nval_dog = os.path.join(root_val,'dog')\n\nroot_test = os.path.join(root_temp,'test1')\n\ntry:\n    os.mkdir(root_val)\n    os.mkdir(val_cat)\n    os.mkdir(val_dog)\nexcept OSError:\n    pass","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.212266Z","iopub.execute_input":"2021-11-25T02:37:00.212643Z","iopub.status.idle":"2021-11-25T02:37:00.985363Z","shell.execute_reply.started":"2021-11-25T02:37:00.212606Z","shell.execute_reply":"2021-11-25T02:37:00.984448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index in range(1500):\n    #moves first 1500 images from /kaggle/working/train/cat to /kaggle/working/val/cat \n    shutil.move(os.path.join(root_temp,'train','cat',os.listdir(os.path.join(root_temp,'train','cat'))[index]),val_cat)\n    shutil.move(os.path.join(root_temp,'train','dog',os.listdir(os.path.join(root_temp,'train','dog'))[index]),val_dog)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:00.986635Z","iopub.execute_input":"2021-11-25T02:37:00.986897Z","iopub.status.idle":"2021-11-25T02:37:19.994627Z","shell.execute_reply.started":"2021-11-25T02:37:00.986864Z","shell.execute_reply":"2021-11-25T02:37:19.993797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#confirms the number of cats and dogs in folders\nprint('total number of cats in training folder ', len(os.listdir(train_cat)))\nprint('total number of dogs in training folder ', len(os.listdir(train_dog)))\nprint('total number of cats in validation folder ', len(os.listdir(val_cat)))\nprint('total number of dogs in validation folder ', len(os.listdir(val_dog)))\nprint('total number of iamges in testing folder ', len(os.listdir(root_test)))","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:19.996247Z","iopub.execute_input":"2021-11-25T02:37:19.996512Z","iopub.status.idle":"2021-11-25T02:37:20.026580Z","shell.execute_reply.started":"2021-11-25T02:37:19.996478Z","shell.execute_reply":"2021-11-25T02:37:20.025644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_gen = ImageDataGenerator(rescale = (1./255))\ntest_gen = ImageDataGenerator(rescale = (1./255))","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:20.028109Z","iopub.execute_input":"2021-11-25T02:37:20.028400Z","iopub.status.idle":"2021-11-25T02:37:21.025261Z","shell.execute_reply.started":"2021-11-25T02:37:20.028365Z","shell.execute_reply":"2021-11-25T02:37:21.024420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(root_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:21.026853Z","iopub.execute_input":"2021-11-25T02:37:21.027129Z","iopub.status.idle":"2021-11-25T02:37:21.059876Z","shell.execute_reply.started":"2021-11-25T02:37:21.027096Z","shell.execute_reply":"2021-11-25T02:37:21.059155Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(root_test))","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:21.061164Z","iopub.execute_input":"2021-11-25T02:37:21.061592Z","iopub.status.idle":"2021-11-25T02:37:21.075405Z","shell.execute_reply.started":"2021-11-25T02:37:21.061556Z","shell.execute_reply":"2021-11-25T02:37:21.074616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = os.path.join(root_test,'test')\ntry :\n    os.mkdir(test_dir)\nexcept: OSError\npass\n\nfrom pathlib import Path\n\n#root test = /kaggle/working/test1\n#test_dir = /kaggle/working/test1/test\nfor file in os.listdir(root_test):\n    if 'test' not in file:\n        shutil.move(root_test+'/'+file, test_dir+'/'+file)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:21.076957Z","iopub.execute_input":"2021-11-25T02:37:21.077480Z","iopub.status.idle":"2021-11-25T02:37:21.424351Z","shell.execute_reply.started":"2021-11-25T02:37:21.077444Z","shell.execute_reply":"2021-11-25T02:37:21.423454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(os.path.join(root_test,'test')))","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:21.425959Z","iopub.execute_input":"2021-11-25T02:37:21.426285Z","iopub.status.idle":"2021-11-25T02:37:21.441374Z","shell.execute_reply.started":"2021-11-25T02:37:21.426230Z","shell.execute_reply":"2021-11-25T02:37:21.440682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_flow = train_gen.flow_from_directory(root_train, \n                                           target_size = (150,150),\n                                          batch_size=25,\n                                          class_mode = 'binary')\nval_flow = test_gen.flow_from_directory(root_val, \n                                        target_size = (150,150),\n                                        batch_size=25,\n                                        class_mode = 'binary')\ntest_flow = test_gen.flow_from_directory(root_test, \n                                        target_size = (150,150),\n                                        batch_size=25,\n                                        class_mode = 'binary',\n                                        shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:21.442621Z","iopub.execute_input":"2021-11-25T02:37:21.442949Z","iopub.status.idle":"2021-11-25T02:37:22.445680Z","shell.execute_reply.started":"2021-11-25T02:37:21.442910Z","shell.execute_reply":"2021-11-25T02:37:22.444940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desired_accuracy = 0.95\n\nclass my_callback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self, epochs, log ={}):\n        if (log.get('accuracy')>desired_accuracy):\n            print('\\Accuracy of {} reached, stopping training '.format(desired_accuracy))\n            self.model.stop_training = True\n\nstop_training_callback = my_callback","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:22.446827Z","iopub.execute_input":"2021-11-25T02:37:22.447406Z","iopub.status.idle":"2021-11-25T02:37:22.454342Z","shell.execute_reply.started":"2021-11-25T02:37:22.447366Z","shell.execute_reply":"2021-11-25T02:37:22.453669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Modelling Time\n\nbasic_cnn = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation='relu'),\n    tf.keras.layers.Dense(1, activation='sigmoid')\n])","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:22.455821Z","iopub.execute_input":"2021-11-25T02:37:22.456350Z","iopub.status.idle":"2021-11-25T02:37:25.042158Z","shell.execute_reply.started":"2021-11-25T02:37:22.456314Z","shell.execute_reply":"2021-11-25T02:37:25.041434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_cnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:25.043499Z","iopub.execute_input":"2021-11-25T02:37:25.043952Z","iopub.status.idle":"2021-11-25T02:37:25.057323Z","shell.execute_reply.started":"2021-11-25T02:37:25.043913Z","shell.execute_reply":"2021-11-25T02:37:25.056092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basic_cnn.compile(loss = 'binary_crossentropy', metrics =['acc'], optimizer = tf.keras.optimizers.RMSprop(learning_rate = 1e-4))\n\nbasic_cnn_history = basic_cnn.fit_generator(train_flow,\n                   validation_data = val_flow,\n                   steps_per_epoch = 100,\n                   epochs = 50,\n                   validation_steps = 50)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:37:25.058599Z","iopub.execute_input":"2021-11-25T02:37:25.058869Z","iopub.status.idle":"2021-11-25T02:48:19.467563Z","shell.execute_reply.started":"2021-11-25T02:37:25.058834Z","shell.execute_reply":"2021-11-25T02:48:19.466790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_results(model_history):\n    acc = model_history.history['acc']\n    val_acc = model_history.history['val_acc']\n    loss = model_history.history['loss']\n    val_loss = model_history.history['val_loss']\n\n    epochs = range(len(acc))\n\n    plt.plot(epochs, acc, 'r', label='Training accuracy')\n    plt.plot(epochs, val_acc, 'b', label='Validation accuracy')\n    plt.title('Training and validation accuracy')\n\n    plt.figure()\n\n    plt.plot(epochs, loss, 'r', label='Training Loss')\n    plt.plot(epochs, val_loss, 'b', label='Validation Loss')\n    plt.title('Training and validation loss')\n    plt.legend()\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:48:19.469107Z","iopub.execute_input":"2021-11-25T02:48:19.469373Z","iopub.status.idle":"2021-11-25T02:48:19.476408Z","shell.execute_reply.started":"2021-11-25T02:48:19.469340Z","shell.execute_reply":"2021-11-25T02:48:19.475736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_results(basic_cnn_history)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:48:19.477841Z","iopub.execute_input":"2021-11-25T02:48:19.478495Z","iopub.status.idle":"2021-11-25T02:48:19.894903Z","shell.execute_reply.started":"2021-11-25T02:48:19.478458Z","shell.execute_reply":"2021-11-25T02:48:19.894143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Transfer Learning\npre_trainned_model = tf.keras.applications.InceptionV3(include_top = False,\n                                                       weights = 'imagenet',\n                                                       input_shape = (150,150,3))\n\nfor layer in pre_trainned_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:48:19.899459Z","iopub.execute_input":"2021-11-25T02:48:19.899668Z","iopub.status.idle":"2021-11-25T02:48:22.227071Z","shell.execute_reply.started":"2021-11-25T02:48:19.899644Z","shell.execute_reply":"2021-11-25T02:48:22.226258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_trainned_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:48:22.228321Z","iopub.execute_input":"2021-11-25T02:48:22.228584Z","iopub.status.idle":"2021-11-25T02:48:22.376293Z","shell.execute_reply.started":"2021-11-25T02:48:22.228551Z","shell.execute_reply":"2021-11-25T02:48:22.373448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_layer = pre_trainned_model.get_layer('mixed9_1')\n\npretrainned_last_output = last_layer.output\n\nx = tf.keras.layers.Flatten()(pretrainned_last_output)\nx = tf.keras.layers.Dense(512, activation = 'relu')(x)\nx = tf.keras.layers.Dropout(0.2)(x)\nx = tf.keras.layers.Dense(1, activation = 'sigmoid')(x)\n\nnew_model = tf.keras.models.Model(pre_trainned_model.input , x)\n\nnew_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:48:22.377373Z","iopub.execute_input":"2021-11-25T02:48:22.377610Z","iopub.status.idle":"2021-11-25T02:48:22.557973Z","shell.execute_reply.started":"2021-11-25T02:48:22.377578Z","shell.execute_reply":"2021-11-25T02:48:22.557341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desired_accuracy = 0.90\nclass my_callback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self, epochs, log={}):\n        if (log.get('val_acc')>desired_accuracy):\n            print('\\nDesired validation accuracy reached, stop training')\n            self.model.stop_training = True\ncallbacks = my_callback","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:48:22.559085Z","iopub.execute_input":"2021-11-25T02:48:22.559484Z","iopub.status.idle":"2021-11-25T02:48:22.565200Z","shell.execute_reply.started":"2021-11-25T02:48:22.559445Z","shell.execute_reply":"2021-11-25T02:48:22.564503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model.compile(loss='binary_crossentropy', metrics = ['acc'], optimizer = tf.keras.optimizers.RMSprop(learning_rate = 1e-4))\nnew_model_history = new_model.fit_generator(train_flow,\n                                           validation_data = val_flow,\n                                           steps_per_epoch = 100,\n                                           epochs = 5,\n                                           validation_steps = 50,\n#                                            callbacks = [callbacks]\n                                           )","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:48:22.566292Z","iopub.execute_input":"2021-11-25T02:48:22.567151Z","iopub.status.idle":"2021-11-25T02:49:51.081948Z","shell.execute_reply.started":"2021-11-25T02:48:22.567053Z","shell.execute_reply":"2021-11-25T02:49:51.081156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model_history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:49:51.083555Z","iopub.execute_input":"2021-11-25T02:49:51.084199Z","iopub.status.idle":"2021-11-25T02:49:51.091203Z","shell.execute_reply.started":"2021-11-25T02:49:51.084157Z","shell.execute_reply":"2021-11-25T02:49:51.090285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = new_model_history.history['acc']\nval_acc = new_model_history.history['val_acc']\nloss = new_model_history.history['loss']\nval_loss = new_model_history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\n\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:49:51.092859Z","iopub.execute_input":"2021-11-25T02:49:51.094096Z","iopub.status.idle":"2021-11-25T02:49:51.505776Z","shell.execute_reply.started":"2021-11-25T02:49:51.094057Z","shell.execute_reply":"2021-11-25T02:49:51.505046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_layer = pre_trainned_model.get_layer('mixed7')\n\npretrainned_last_output = last_layer.output\n\nx = tf.keras.layers.Flatten()(pretrainned_last_output)\nx = tf.keras.layers.Dense(512, activation = 'relu')(x)\nx = tf.keras.layers.Dropout(0.2)(x)\nx = tf.keras.layers.Dense(1, activation = 'sigmoid')(x)\n\nnew_model_mixed7 = tf.keras.models.Model(pre_trainned_model.input , x)\n\nnew_model_mixed7.compile(loss='binary_crossentropy', metrics = ['acc'], optimizer = tf.keras.optimizers.RMSprop(learning_rate = 1e-4))\nnew_model_mixed7_history = new_model.fit_generator(train_flow,\n                                                    validation_data = val_flow,\n                                                    steps_per_epoch = 100,\n                                                    epochs = 5,\n                                                    validation_steps = 50,\n                                                  #callbacks = [callbacks]\n                                                  )","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:49:51.507294Z","iopub.execute_input":"2021-11-25T02:49:51.507772Z","iopub.status.idle":"2021-11-25T02:51:34.003761Z","shell.execute_reply.started":"2021-11-25T02:49:51.507736Z","shell.execute_reply":"2021-11-25T02:51:34.002958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_results(new_model_mixed7_history)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:51:34.005446Z","iopub.execute_input":"2021-11-25T02:51:34.005866Z","iopub.status.idle":"2021-11-25T02:51:34.437799Z","shell.execute_reply.started":"2021-11-25T02:51:34.005830Z","shell.execute_reply":"2021-11-25T02:51:34.437072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_layer = pre_trainned_model.get_layer('mixed1')\n\npretrainned_last_output = last_layer.output\n\nx = tf.keras.layers.Flatten()(pretrainned_last_output)\nx = tf.keras.layers.Dense(256, activation = 'relu')(x)\nx = tf.keras.layers.Dropout(0.2)(x)\nx = tf.keras.layers.Dense(1, activation = 'sigmoid')(x)\n\nnew_model_mixed1 = tf.keras.models.Model(pre_trainned_model.input , x)\n\nnew_model_mixed1.compile(loss='binary_crossentropy', metrics = ['acc'], optimizer = tf.keras.optimizers.RMSprop(learning_rate = 1e-4))\nnew_model_mixed1_history = new_model.fit_generator(train_flow,\n                                                   validation_data = val_flow,\n                                                   steps_per_epoch = 100,\n                                                   epochs = 5,\n                                                   validation_steps = 50,\n                                                  #callbacks = [callbacks]\n                                                  )","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:51:34.439355Z","iopub.execute_input":"2021-11-25T02:51:34.439862Z","iopub.status.idle":"2021-11-25T02:53:16.926759Z","shell.execute_reply.started":"2021-11-25T02:51:34.439823Z","shell.execute_reply":"2021-11-25T02:53:16.925905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets try another application namely VGG\nfrom tensorflow.keras.applications import VGG16","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:53:16.929526Z","iopub.execute_input":"2021-11-25T02:53:16.929985Z","iopub.status.idle":"2021-11-25T02:53:16.934307Z","shell.execute_reply.started":"2021-11-25T02:53:16.929944Z","shell.execute_reply":"2021-11-25T02:53:16.933572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrain_vgg = VGG16(include_top = False,\n                    input_shape = (150,150,3),\n                    weights = 'imagenet')\n","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:53:16.935475Z","iopub.execute_input":"2021-11-25T02:53:16.935832Z","iopub.status.idle":"2021-11-25T02:53:17.597512Z","shell.execute_reply.started":"2021-11-25T02:53:16.935796Z","shell.execute_reply":"2021-11-25T02:53:17.596638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in pretrain_vgg.layers:\n    layer.trainable = False\n\npretrain_vgg.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:53:17.598850Z","iopub.execute_input":"2021-11-25T02:53:17.599124Z","iopub.status.idle":"2021-11-25T02:53:17.616966Z","shell.execute_reply.started":"2021-11-25T02:53:17.599089Z","shell.execute_reply":"2021-11-25T02:53:17.616129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrain_vgg_output =  pretrain_vgg.get_layer('block5_pool').output \n\nx = tf.keras.layers.Flatten()(pretrain_vgg_output)\nx = tf.keras.layers.Dense(512,activation = 'relu')(x)\nx = tf.keras.layers.Dropout(0.2)(x)\nx = tf.keras.layers.Dense(1,activation = 'sigmoid')(x)\n\npretrain_vgg_model = tf.keras.models.Model(pretrain_vgg.input, x)\npretrain_vgg_model.compile(loss = 'binary_crossentropy',\n                          metrics=['acc'],\n                          optimizer = tf.keras.optimizers.RMSprop(lr=0.00001))\npretrain_vgg_history = pretrain_vgg_model.fit_generator(train_flow,\n                                                       validation_data = val_flow,\n                                                       steps_per_epoch = 100,\n                                                       epochs = 10,\n                                                       validation_steps = 50)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:53:17.618077Z","iopub.execute_input":"2021-11-25T02:53:17.618384Z","iopub.status.idle":"2021-11-25T02:56:23.545799Z","shell.execute_reply.started":"2021-11-25T02:53:17.618348Z","shell.execute_reply":"2021-11-25T02:56:23.545061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_results(pretrain_vgg_history)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:56:23.547330Z","iopub.execute_input":"2021-11-25T02:56:23.547577Z","iopub.status.idle":"2021-11-25T02:56:23.920235Z","shell.execute_reply.started":"2021-11-25T02:56:23.547543Z","shell.execute_reply":"2021-11-25T02:56:23.919558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(root_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:56:23.921521Z","iopub.execute_input":"2021-11-25T02:56:23.921925Z","iopub.status.idle":"2021-11-25T02:56:23.927788Z","shell.execute_reply.started":"2021-11-25T02:56:23.921889Z","shell.execute_reply":"2021-11-25T02:56:23.926994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_flow","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:56:23.929170Z","iopub.execute_input":"2021-11-25T02:56:23.929563Z","iopub.status.idle":"2021-11-25T02:56:23.938083Z","shell.execute_reply.started":"2021-11-25T02:56:23.929527Z","shell.execute_reply":"2021-11-25T02:56:23.937402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = pretrain_vgg_model.predict(test_flow)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:56:23.939534Z","iopub.execute_input":"2021-11-25T02:56:23.940027Z","iopub.status.idle":"2021-11-25T02:57:05.318360Z","shell.execute_reply.started":"2021-11-25T02:56:23.939989Z","shell.execute_reply":"2021-11-25T02:57:05.317523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predictions)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:57:05.319803Z","iopub.execute_input":"2021-11-25T02:57:05.320055Z","iopub.status.idle":"2021-11-25T02:57:05.325051Z","shell.execute_reply.started":"2021-11-25T02:57:05.320024Z","shell.execute_reply":"2021-11-25T02:57:05.324374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = [1 if pred > 0.5 else 0 for pred in predictions]","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:57:05.326254Z","iopub.execute_input":"2021-11-25T02:57:05.326742Z","iopub.status.idle":"2021-11-25T02:57:05.357983Z","shell.execute_reply.started":"2021-11-25T02:57:05.326687Z","shell.execute_reply":"2021-11-25T02:57:05.357204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:57:05.359328Z","iopub.execute_input":"2021-11-25T02:57:05.359762Z","iopub.status.idle":"2021-11-25T02:57:05.369634Z","shell.execute_reply.started":"2021-11-25T02:57:05.359728Z","shell.execute_reply":"2021-11-25T02:57:05.368793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#label_ids = test_flow.filenames","metadata":{"execution":{"iopub.status.busy":"2021-11-25T03:01:39.403684Z","iopub.execute_input":"2021-11-25T03:01:39.403970Z","iopub.status.idle":"2021-11-25T03:01:39.408630Z","shell.execute_reply.started":"2021-11-25T03:01:39.403938Z","shell.execute_reply":"2021-11-25T03:01:39.407609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_ids = [fname for fname in os.listdir(test_dir)]","metadata":{"execution":{"iopub.status.busy":"2021-11-25T03:02:04.387983Z","iopub.execute_input":"2021-11-25T03:02:04.388289Z","iopub.status.idle":"2021-11-25T03:02:04.401086Z","shell.execute_reply.started":"2021-11-25T03:02:04.388248Z","shell.execute_reply":"2021-11-25T03:02:04.400388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_ids[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-25T03:02:05.498725Z","iopub.execute_input":"2021-11-25T03:02:05.499368Z","iopub.status.idle":"2021-11-25T03:02:05.508409Z","shell.execute_reply.started":"2021-11-25T03:02:05.499330Z","shell.execute_reply":"2021-11-25T03:02:05.507505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\ndata = zip(*(label_ids, predictions))\n\ndf = pd.DataFrame(data, columns = ['id' , 'label'])","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:57:58.914284Z","iopub.execute_input":"2021-11-25T02:57:58.915088Z","iopub.status.idle":"2021-11-25T02:57:58.929485Z","shell.execute_reply.started":"2021-11-25T02:57:58.915047Z","shell.execute_reply":"2021-11-25T02:57:58.928507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.set_index('id')","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:58:02.846799Z","iopub.execute_input":"2021-11-25T02:58:02.847060Z","iopub.status.idle":"2021-11-25T02:58:02.854348Z","shell.execute_reply.started":"2021-11-25T02:58:02.847032Z","shell.execute_reply":"2021-11-25T02:58:02.853033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-25T02:58:05.501090Z","iopub.execute_input":"2021-11-25T02:58:05.501783Z","iopub.status.idle":"2021-11-25T02:58:05.509692Z","shell.execute_reply.started":"2021-11-25T02:58:05.501743Z","shell.execute_reply":"2021-11-25T02:58:05.508962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-25T03:02:27.967623Z","iopub.execute_input":"2021-11-25T03:02:27.968249Z","iopub.status.idle":"2021-11-25T03:02:27.983999Z","shell.execute_reply.started":"2021-11-25T03:02:27.968188Z","shell.execute_reply":"2021-11-25T03:02:27.982846Z"},"trusted":true},"execution_count":null,"outputs":[]}]}