{"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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2022-10-27T11:44:04.703959Z","iopub.execute_input":"2022-10-27T11:44:04.704395Z","iopub.status.idle":"2022-10-27T11:44:04.715819Z","shell.execute_reply.started":"2022-10-27T11:44:04.704359Z","shell.execute_reply":"2022-10-27T11:44:04.714406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = '0'></a>\n<h2 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color:#78ccdb ; color : #36609A; border-radius: 5px 5px; padding:3px;text-align:center; font-weight: bold\" >Table of Contents</h2> \n\n\n*  [1. Introduction](#1.0)\n*  [2. importing libraries](#2.0)\n*  [3. Data visualization](#3.0)\n*  [4. Classification with Vanila CNN](#4.0)\n*  [5. Data agumentaion, learning rate schedule, and Early stoping](#5.0)\n*  [6. Classification with ResNet](#6.0)\n*  [7. Final predictions on Test Set](#7.0)\n*  [8. Summary](#8.0)\n","metadata":{}},{"cell_type":"markdown","source":"<br>\n<br>\n<center><h2 style = \"font-family: garamond; font-size: 50px; font-style: normal; letter-spcaing: 3px; background-color:#78ccdb ; color :black; border-radius: 5px 5px; padding:10px;text-align:center; font-weight: bold\" >CAT OR DOG</h2> \n<br> \n<br>\n<center><div class = 'image'> <img style=\"float:center; border:10px solid #78ccdb; width:60%\" align=center src = https://th.bing.com/th/id/OIP.hJxaRY8ee56icejxgpomYwAAAA?w=237&h=158&c=7&r=0&o=5&dpr=1.3&pid=1.7> \n</div></center>\n<br>\n<br>\n<a href =\"https://th.bing.com/th/id/OIP.hJxaRY8ee56icejxgpomYwAAAA?w=237&h=158&c=7&r=0&o=5&dpr=1.3&pid=1.7\" style = \"font-size:20px,color: dimgrey, text-align:left,font-family:serif\"></a>\n<br>\n","metadata":{}},{"cell_type":"markdown","source":"<a id = '1.0'></a>\n<h2 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color:#78ccdb ; color : #36609A; border-radius: 5px 5px; padding:3px;text-align:center; font-weight: bold\" > introduction </h2> ","metadata":{}},{"cell_type":"markdown","source":"# 1.0 Convolutional Neural Network (CNN)","metadata":{}},{"cell_type":"markdown","source":"A **Convolutional Neural Network (ConvNet/CNN)** is a Deep Learning algorithm which can take in an input image, assign importance (learnable weights and biases) to various aspects/objects in the image and be able to differentiate one from the other. The pre-processing required in a ConvNet is much lower as compared to other classification algorithms. While in primitive methods filters are hand-engineered, with enough training, ConvNets have the ability to learn these filters/characteristics.","metadata":{}},{"cell_type":"markdown","source":"![CNN](https://i.imgur.com/jmV3oAz.jpg)","metadata":{}},{"cell_type":"markdown","source":"<a id = '1.0'></a>\n<h2 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color:#78ccdb ; color : #36609A; border-radius: 5px 5px; padding:3px;text-align:center; font-weight: bold\" > importing libraries </h2> ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport sys\nimport tensorflow as tf\nimport os\nimport sys\nimport pandas as pd\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# basic librareis\nimport zipfile\nimport glob\nimport os\nimport pandas as pd\nimport numpy as np\nfrom PIL import Image\n\n\n# plotting and visualizations\nimport matplotlib \nimport matplotlib.pyplot as plt \n%matplotlib inline\nimport seaborn as sns \nimport missingno as msno\n%matplotlib inline\nimport matplotlib.image as img\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import confusion_matrix\nimport plotly.graph_objects as go\nimport itertools\nimport plotly.express as px","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:04.717742Z","iopub.execute_input":"2022-10-27T11:44:04.718509Z","iopub.status.idle":"2022-10-27T11:44:04.735492Z","shell.execute_reply.started":"2022-10-27T11:44:04.718468Z","shell.execute_reply":"2022-10-27T11:44:04.734456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test1', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:04.737209Z","iopub.execute_input":"2022-10-27T11:44:04.737588Z","iopub.status.idle":"2022-10-27T11:44:18.72142Z","shell.execute_reply.started":"2022-10-27T11:44:04.737552Z","shell.execute_reply":"2022-10-27T11:44:18.719566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR_PATH = './train'\nfile_names = os.listdir(TRAIN_DIR_PATH)\nprint('There are {} number of images in directory.'.format(len(file_names)))","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:18.72507Z","iopub.execute_input":"2022-10-27T11:44:18.725616Z","iopub.status.idle":"2022-10-27T11:44:18.750858Z","shell.execute_reply.started":"2022-10-27T11:44:18.725561Z","shell.execute_reply":"2022-10-27T11:44:18.749611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_FOLDER_PATH = \"../working/train\"\nFILE_NAMES = os.listdir(IMAGE_FOLDER_PATH)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:18.752835Z","iopub.execute_input":"2022-10-27T11:44:18.753624Z","iopub.status.idle":"2022-10-27T11:44:18.777256Z","shell.execute_reply.started":"2022-10-27T11:44:18.753572Z","shell.execute_reply":"2022-10-27T11:44:18.775827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = list()\nfull_paths = list()\ntrain_cats_dir = list()\ntrain_dogs_dir = list()\n\n# finding each file's target\nfor file_name in FILE_NAMES:\n    target = file_name.split(\".\")[0] # target name\n    full_path = os.path.join(IMAGE_FOLDER_PATH, file_name)\n    \n    if(target == \"dog\"):\n        train_dogs_dir.append(full_path)\n    if(target == \"cat\"):\n        train_cats_dir.append(full_path)\n    \n    full_paths.append(full_path)\n    targets.append(target)\n\ndataset = pd.DataFrame() # make dataframe\ndataset['image_path'] = full_paths # file path\ndataset['target'] = targets # file's target","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:18.779026Z","iopub.execute_input":"2022-10-27T11:44:18.779479Z","iopub.status.idle":"2022-10-27T11:44:18.865608Z","shell.execute_reply.started":"2022-10-27T11:44:18.779441Z","shell.execute_reply":"2022-10-27T11:44:18.864228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:18.867494Z","iopub.execute_input":"2022-10-27T11:44:18.867874Z","iopub.status.idle":"2022-10-27T11:44:18.88019Z","shell.execute_reply.started":"2022-10-27T11:44:18.867842Z","shell.execute_reply":"2022-10-27T11:44:18.878598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"total data counts:\", dataset['target'].count())\ncounts = dataset['target'].value_counts()\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:18.881885Z","iopub.execute_input":"2022-10-27T11:44:18.882404Z","iopub.status.idle":"2022-10-27T11:44:18.90127Z","shell.execute_reply.started":"2022-10-27T11:44:18.882368Z","shell.execute_reply":"2022-10-27T11:44:18.8996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=px.pie(counts.head(10),values= 'target', names=dataset['target'].unique(),hole=0.425)\nfig.update_layout(title='Data Distribution of Data',font_size=15,title_x=0.45,annotations=[dict(text='Cat vs Dog',font_size=18, showarrow=False,height=800,width=700)])\nfig.update_traces(textfont_size=15,textinfo='percent')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:18.9029Z","iopub.execute_input":"2022-10-27T11:44:18.903611Z","iopub.status.idle":"2022-10-27T11:44:18.977155Z","shell.execute_reply.started":"2022-10-27T11:44:18.903572Z","shell.execute_reply":"2022-10-27T11:44:18.975865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = 5\ncols = 5\naxes = []\nfig=plt.figure(figsize=(20,10))\ni = 0\n\nfor a in range(rows*cols):\n    b = img.imread(train_cats_dir[i])\n    axes.append(fig.add_subplot(rows,cols,a+1))\n    plt.imshow(b)\n    i+=1\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:18.981195Z","iopub.execute_input":"2022-10-27T11:44:18.981676Z","iopub.status.idle":"2022-10-27T11:44:22.306628Z","shell.execute_reply.started":"2022-10-27T11:44:18.981638Z","shell.execute_reply":"2022-10-27T11:44:22.305264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = 5\ncols = 5\naxes = []\nfig=plt.figure(figsize=(20,10))\ni = 0\n\nfor a in range(rows*cols):\n    b = img.imread(train_dogs_dir[i])\n    axes.append(fig.add_subplot(rows,cols,a+1))\n    plt.imshow(b)\n    i+=1\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:22.308069Z","iopub.execute_input":"2022-10-27T11:44:22.308443Z","iopub.status.idle":"2022-10-27T11:44:26.676182Z","shell.execute_reply.started":"2022-10-27T11:44:22.308408Z","shell.execute_reply":"2022-10-27T11:44:26.674947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, test_val = train_test_split(dataset, test_size=0.2, stratify=dataset['target'], random_state=20)\ntest, val = train_test_split(test_val, test_size=0.5,  stratify=test_val['target'], random_state=20)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:26.677438Z","iopub.execute_input":"2022-10-27T11:44:26.678615Z","iopub.status.idle":"2022-10-27T11:44:26.724392Z","shell.execute_reply.started":"2022-10-27T11:44:26.678573Z","shell.execute_reply":"2022-10-27T11:44:26.723492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test_val.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:26.72573Z","iopub.execute_input":"2022-10-27T11:44:26.726637Z","iopub.status.idle":"2022-10-27T11:44:26.735744Z","shell.execute_reply.started":"2022-10-27T11:44:26.7266Z","shell.execute_reply":"2022-10-27T11:44:26.734253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_distribution =test['target'].value_counts()\ntarget_distribution.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:26.737446Z","iopub.execute_input":"2022-10-27T11:44:26.738664Z","iopub.status.idle":"2022-10-27T11:44:26.752647Z","shell.execute_reply.started":"2022-10-27T11:44:26.738609Z","shell.execute_reply":"2022-10-27T11:44:26.751362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('train set distribution:\\n',train['target'].value_counts())\nprint('\\nvalid set distribution:\\n',test['target'].value_counts())\nprint()\n\nplt.figure(figsize=(10,5))\nfig=px.pie(target_distribution.head(10),values= 'target', names=train['target'].unique(),hole=0.425)\nfig.update_layout(title='Data Distribution of Train Data in Pie Chart',font_size=15,title_x=0.45,annotations=[dict(text='Cat vs Dog',font_size=18, showarrow=False,height=800,width=700)])\nfig.update_traces(textfont_size=15,textinfo='percent')\nfig.show()\n\nfig=px.pie(target_distribution.head(10),values= 'target', names=test['target'].unique(),hole=0.425)\nfig.update_layout(title='Data Distribution of Train Data in Pie Chart',font_size=15,title_x=0.45,annotations=[dict(text='Cat vs Dog',font_size=18, showarrow=False,height=800,width=700)])\nfig.update_traces(textfont_size=15,textinfo='percent')\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:26.754775Z","iopub.execute_input":"2022-10-27T11:44:26.755292Z","iopub.status.idle":"2022-10-27T11:44:26.886203Z","shell.execute_reply.started":"2022-10-27T11:44:26.755241Z","shell.execute_reply":"2022-10-27T11:44:26.88505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.layers import Dropout, Flatten, Dense\nfrom keras.preprocessing.image import ImageDataGenerator\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:26.887689Z","iopub.execute_input":"2022-10-27T11:44:26.888043Z","iopub.status.idle":"2022-10-27T11:44:26.894703Z","shell.execute_reply.started":"2022-10-27T11:44:26.888011Z","shell.execute_reply":"2022-10-27T11:44:26.893312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FILTER_SIZE = 3\nNUM_FILTERS = 32\nINPUT_SIZE = 32\nMAXPOOL_SIZE = 2\nBATCH_SIZE = 16\nSTEPS_PER_EPOCH = 20000//BATCH_SIZE\nEPOCHS = 15","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:26.896436Z","iopub.execute_input":"2022-10-27T11:44:26.897135Z","iopub.status.idle":"2022-10-27T11:44:26.912455Z","shell.execute_reply.started":"2022-10-27T11:44:26.897096Z","shell.execute_reply":"2022-10-27T11:44:26.911071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32,(3,3), input_shape = (INPUT_SIZE,INPUT_SIZE,3),kernel_regularizer=tf.keras.regularizers.l2(0.01), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (MAXPOOL_SIZE,MAXPOOL_SIZE)))\n\nmodel.add(Conv2D(32,(3,3), input_shape = (INPUT_SIZE,INPUT_SIZE,3),kernel_regularizer=tf.keras.regularizers.l2(0.01), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (MAXPOOL_SIZE,MAXPOOL_SIZE)))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(units=128,activation ='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(units=1,activation = 'sigmoid'))\nmodel.compile(optimizer = 'adam', loss = 'binary_crossentropy',metrics=['accuracy'])\n\naug_gen = ImageDataGenerator(rescale = 1./255)\n                               \n                             \ntraining_set = aug_gen.flow_from_dataframe(train, x_col=\"image_path\",\n                                                     y_col=\"target\",target_size = (32,32),\n                                                          batch_size = 16,class_mode='binary')\n# Preprocessing the Test set\ntest_data_generator = ImageDataGenerator(rescale = 1./255)\ntest_set = test_data_generator.flow_from_dataframe(test,x_col=\"image_path\",\n                                                     y_col=\"target\",target_size = (32, 32),\n                                            batch_size = 16,\n                                            class_mode = 'binary')\n                                           \nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:26.913792Z","iopub.execute_input":"2022-10-27T11:44:26.914308Z","iopub.status.idle":"2022-10-27T11:44:27.281373Z","shell.execute_reply.started":"2022-10-27T11:44:26.914263Z","shell.execute_reply":"2022-10-27T11:44:27.280043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nearlystop_cb = EarlyStopping(patience=10, restore_best_weights=True)\nreduce_lr_cb = ReduceLROnPlateau(factor=0.5, patience=5, monitor='val_loss', min_lr=0.00001)\ncheckpoint_cb = ModelCheckpoint('model.h5', save_best_only=True)\n\ncallbacks = [earlystop_cb, reduce_lr_cb, checkpoint_cb]","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:27.28305Z","iopub.execute_input":"2022-10-27T11:44:27.283414Z","iopub.status.idle":"2022-10-27T11:44:27.290435Z","shell.execute_reply.started":"2022-10-27T11:44:27.283382Z","shell.execute_reply":"2022-10-27T11:44:27.288767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r=model.fit(x=training_set,validation_data =test_set,steps_per_epoch = STEPS_PER_EPOCH, epochs = EPOCHS, verbose = 1,callbacks=callbacks)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-27T11:44:27.291976Z","iopub.execute_input":"2022-10-27T11:44:27.292589Z","iopub.status.idle":"2022-10-27T12:09:40.335356Z","shell.execute_reply.started":"2022-10-27T11:44:27.292547Z","shell.execute_reply":"2022-10-27T12:09:40.333945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = r.history['accuracy']\nval_acc = r.history['val_accuracy']\nloss = r.history['loss']\nval_loss = r.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.figure(figsize=(15,5))\nplt.subplot(1,2,1)\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\n\n\nplt.subplot(1,2,2)\nplt.plot(epochs, loss, 'go', label='Training Loss')\nplt.plot(epochs, val_loss, 'g', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-27T12:09:40.337445Z","iopub.execute_input":"2022-10-27T12:09:40.337831Z","iopub.status.idle":"2022-10-27T12:09:40.763878Z","shell.execute_reply.started":"2022-10-27T12:09:40.337798Z","shell.execute_reply":"2022-10-27T12:09:40.7623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**using SVM**","metadata":{}},{"cell_type":"code","source":"# Part 1 - Data Preprocessing\n\n# Preprocessing the Training set\ntrain_datagen = ImageDataGenerator(rescale = 1./255,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   horizontal_flip = True)\ntraining_set = train_datagen.flow_from_dataframe(train, x_col=\"image_path\",\n                                                     y_col=\"target\",\n                                                 target_size = (64, 64),\n                                                 batch_size = 32,\n                                                 class_mode = 'binary')\n\n# Preprocessing the Test set\ntest_datagen = ImageDataGenerator(rescale = 1./255)\ntest_set = test_datagen.flow_from_dataframe(test, x_col=\"image_path\",\n                                                     y_col=\"target\",\n                                            target_size = (64, 64),\n                                            batch_size = 32,\n                                            class_mode = 'binary')\n# Initialising the CNN\ncnn = tf.keras.models.Sequential()\n\n# Step 1 - Convolution\ncnn.add(tf.keras.layers.Conv2D(filters=32,padding=\"same\",kernel_size=3, activation='relu', strides=2, input_shape=[64, 64, 3]))\n\n# Step 2 - Pooling\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))\n\n# Adding a second convolutional layer\ncnn.add(tf.keras.layers.Conv2D(filters=32,padding='same',kernel_size=3, activation='relu'))\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))\n\ncnn.add(tf.keras.layers.Conv2D(filters=32,padding='same',kernel_size=3, activation='relu'))\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))\n# Step 3 - Flattening\ncnn.add(tf.keras.layers.Flatten())\n\n# Step 4 - Full Connection\ncnn.add(tf.keras.layers.Dense(units=128, activation='relu'))\n\n# Step 5 - Output Layer\n#cnn.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))\n## For Binary Classification\ncnn.add(Dense(1, kernel_regularizer=tf.keras.regularizers.l2(0.01),activation\n             ='linear'))\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-27T12:09:40.766131Z","iopub.execute_input":"2022-10-27T12:09:40.766557Z","iopub.status.idle":"2022-10-27T12:09:41.155666Z","shell.execute_reply.started":"2022-10-27T12:09:40.766506Z","shell.execute_reply":"2022-10-27T12:09:41.154321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" #Part 3 - Training the CNN\n\n# Compiling the CNN\ncnn.compile(optimizer = 'adam', loss = 'hinge', metrics = ['accuracy'])\n\n# Training the CNN on the Training set and evaluating it on the Test set\nr=cnn.fit(x = training_set, validation_data = test_set, epochs = 20)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T12:09:41.157683Z","iopub.execute_input":"2022-10-27T12:09:41.158125Z","iopub.status.idle":"2022-10-27T12:46:45.037281Z","shell.execute_reply.started":"2022-10-27T12:09:41.15808Z","shell.execute_reply":"2022-10-27T12:46:45.036127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nfig,ax  = plt.subplots(2,1, figsize =(8,8), dpi = 100)\nfig.patch.set_facecolor('#f5f6f6')\n\naxes  = ax.ravel()\n\nfor ax in axes:\n    ax.set_facecolor('#f5f6f6')\n    for loc in ['right','top',]:\n        ax.spines[loc].set_visible(False)\n        \nhist1 = r.history\nEpochs =  range(len(hist1['loss']))\n\n## loss plot\nsns.lineplot(x = Epochs, y = hist1['val_loss'],  ax = axes[0], linewidth = 4)\nsns.lineplot(x = Epochs, y = hist1['loss'], ax  = axes[0], linewidth =4)\n\n\naxes[0].text(Epochs[-1]+0.25,hist1['val_loss'][-1],'Validation Loss',{'fontfamily':'serif', 'size':12, 'weight':'bold','color':\"black\"})\naxes[0].text(Epochs[-1]+0.25,hist1['loss'][-1] ,'Training Loss',{'fontfamily':'serif', 'size':12, 'weight':'bold','color':\"black\"})\n\n\n# accuracy plot\nsns.lineplot(x = Epochs, y = hist1['val_accuracy'],ax = axes[1],linewidth = 4)\nsns.lineplot(x = Epochs, y = hist1['accuracy'],ax = axes[1],linewidth =4)\naxes[1].text(Epochs[-1]+0.50,hist1['val_accuracy'][-1],'Validation Accuracy',{'fontfamily':'serif', 'size':12, 'weight':'bold','color':\"black\"})\naxes[1].text(Epochs[-1]+0.50,hist1['accuracy'][-1] ,'Training Accuracy',{'fontfamily':'serif', 'size':12, 'weight':'bold','color':\"black\"})\n\n\n\n        \nfig.text(0.75,-0.05,'© Made by RANIA/Kaggle',{'fontfamily':'serif', 'size':10,  'color':'black'})\nplt.tight_layout(h_pad = 5)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T12:51:21.446122Z","iopub.execute_input":"2022-10-27T12:51:21.44655Z","iopub.status.idle":"2022-10-27T12:51:22.063328Z","shell.execute_reply.started":"2022-10-27T12:51:21.446516Z","shell.execute_reply":"2022-10-27T12:51:22.062321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install visualkeras\nimport visualkeras\nvisualkeras.layered_view(model, legend=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T12:46:45.663253Z","iopub.execute_input":"2022-10-27T12:46:45.664495Z","iopub.status.idle":"2022-10-27T12:46:57.14919Z","shell.execute_reply.started":"2022-10-27T12:46:45.664444Z","shell.execute_reply":"2022-10-27T12:46:57.14771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = '5.0'></a>\n<h2 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color:#78ccdb ; color : #36609A; border-radius: 5px 5px; padding:3px;text-align:center; font-weight: bold\" >Data Agumentation</h2> ","metadata":{}},{"cell_type":"code","source":"def data_argumentation_show(n, grid_size):\n    sample_aug_map = ImageDataGenerator(\n            #zoom_range = 0.1,\n            rotation_range = 25,\n            horizontal_flip = True,\n            height_shift_range =0.2,\n            width_shift_range = 0.2,\n            fill_mode='nearest',\n            rescale = 1/255)\n    sample_data = sample_aug_map.flow_from_dataframe(\n            (train_df.sample(n)),\n           \n            x_col = 'image_path',\n            y_col = 'target',\n            target_size = (img_size, img_size),\n            class_mode = 'categorical')\n  \n  #subplot grid \n    fig = plt.figure(figsize = (10,10))\n    fig.patch.set_facecolor('#f5f6f6')\n    for i in range(0,grid_size*grid_size):\n        plt.subplot(grid_size,grid_size, i+1)\n        for x,y in sample_data:\n            img = x[0]\n            plt.imshow(img)\n            plt.axis('off')\n            break\n            plt.tight_layout()\n            del img\n    fig.show()\n\n     \n","metadata":{"execution":{"iopub.status.busy":"2022-10-27T12:46:57.151134Z","iopub.execute_input":"2022-10-27T12:46:57.151563Z","iopub.status.idle":"2022-10-27T12:46:57.164331Z","shell.execute_reply.started":"2022-10-27T12:46:57.151512Z","shell.execute_reply":"2022-10-27T12:46:57.162257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_argumentation_show(1, 5)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T12:46:57.165834Z","iopub.execute_input":"2022-10-27T12:46:57.166374Z","iopub.status.idle":"2022-10-27T12:46:58.850256Z","shell.execute_reply.started":"2022-10-27T12:46:57.166321Z","shell.execute_reply":"2022-10-27T12:46:58.849252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_argumentation_show(3, 5)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T12:46:58.855599Z","iopub.execute_input":"2022-10-27T12:46:58.856804Z","iopub.status.idle":"2022-10-27T12:47:01.184121Z","shell.execute_reply.started":"2022-10-27T12:46:58.856727Z","shell.execute_reply":"2022-10-27T12:47:01.182878Z"},"trusted":true},"execution_count":null,"outputs":[]}]}