{"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":"markdown","source":"\n\n# Import libraries.\n","metadata":{"id":"zqrYqv734KAo"}},{"cell_type":"code","source":"# Core\nimport numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport glob\nimport random\nimport os\nimport cv2\n#tesor fow & keras\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.regularizers import l2     \nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom keras.models import load_model\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Flatten, Dense,BatchNormalization,Dropout,Input\nfrom keras.models import Sequential, Model\nfrom keras.layers import Conv2D,GlobalMaxPooling2D\nfrom tensorflow.keras.applications import  Xception,VGG16,InceptionResNetV2\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n#cnn\nfrom tensorflow.keras import datasets, layers, models\n\nfrom keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda\nfrom keras.engine.base_layer import Layer\nfrom sklearn.metrics import classification_report,confusion_matrix\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import f1_score\nfrom sklearn.metrics import recall_score\nfrom sklearn.metrics import precision_score","metadata":{"id":"1eb99db7","execution":{"iopub.status.busy":"2022-03-28T18:07:03.578126Z","iopub.execute_input":"2022-03-28T18:07:03.578511Z","iopub.status.idle":"2022-03-28T18:07:12.541979Z","shell.execute_reply.started":"2022-03-28T18:07:03.578391Z","shell.execute_reply":"2022-03-28T18:07:12.540660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 42\nnp.random.seed =seed","metadata":{"id":"4DTeuvL_TphS","execution":{"iopub.status.busy":"2022-03-28T18:07:12.545327Z","iopub.execute_input":"2022-03-28T18:07:12.545695Z","iopub.status.idle":"2022-03-28T18:07:12.551292Z","shell.execute_reply.started":"2022-03-28T18:07:12.545648Z","shell.execute_reply":"2022-03-28T18:07:12.550442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '../input/happy-whale-and-dolphin/'\n","metadata":{"id":"qcaudVrD0uQ2","execution":{"iopub.status.busy":"2022-03-28T18:07:12.552572Z","iopub.execute_input":"2022-03-28T18:07:12.552933Z","iopub.status.idle":"2022-03-28T18:07:12.576423Z","shell.execute_reply.started":"2022-03-28T18:07:12.552902Z","shell.execute_reply":"2022-03-28T18:07:12.575653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bussiness Task\nDeveloping  a model to match individual whales and dolphins by unique—but often subtle—characteristics of their natural markings. \n\nI hope you find this NoteBook helpful and some <span style=\"color:red;\">**UPVOTES**</span> would be appreciated.\n\n\n","metadata":{"id":"JE_1ztaHWq4h"}},{"cell_type":"markdown","source":"# Data Preperation.\n1.   read csv file to get all data about train image \n2.   Check Null data \n3.   clear duplicate data\n\n\n\n","metadata":{"id":"5ssSyiLDdTlP"}},{"cell_type":"code","source":"train_df = pd.read_csv(image_path+'train.csv')\ntrain_df.head(10)","metadata":{"id":"m-6-gfsOPUQp","outputId":"18363c61-4a77-468e-e2e0-4262e325987b","execution":{"iopub.status.busy":"2022-03-28T18:07:12.578978Z","iopub.execute_input":"2022-03-28T18:07:12.579624Z","iopub.status.idle":"2022-03-28T18:07:12.728207Z","shell.execute_reply.started":"2022-03-28T18:07:12.579572Z","shell.execute_reply":"2022-03-28T18:07:12.727499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"id":"X8NoGOg2QUAf","outputId":"d25be8e0-8e87-449e-99c1-78ef58f64453","execution":{"iopub.status.busy":"2022-03-28T18:07:12.729435Z","iopub.execute_input":"2022-03-28T18:07:12.730102Z","iopub.status.idle":"2022-03-28T18:07:12.772452Z","shell.execute_reply.started":"2022-03-28T18:07:12.730056Z","shell.execute_reply":"2022-03-28T18:07:12.771299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"id":"tfOr0lWYQZzO","outputId":"112fed3b-0a88-44dd-c21e-16e87af59eda","execution":{"iopub.status.busy":"2022-03-28T18:07:12.774076Z","iopub.execute_input":"2022-03-28T18:07:12.774571Z","iopub.status.idle":"2022-03-28T18:07:12.781162Z","shell.execute_reply.started":"2022-03-28T18:07:12.774510Z","shell.execute_reply":"2022-03-28T18:07:12.780244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum().sort_values(ascending=False)","metadata":{"id":"EwIAPpXYDFvp","outputId":"ebe13066-9a68-4d24-8884-acd53309f360","execution":{"iopub.status.busy":"2022-03-28T18:07:12.782769Z","iopub.execute_input":"2022-03-28T18:07:12.783465Z","iopub.status.idle":"2022-03-28T18:07:12.818099Z","shell.execute_reply.started":"2022-03-28T18:07:12.783426Z","shell.execute_reply":"2022-03-28T18:07:12.817259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop_duplicates(inplace=True)","metadata":{"id":"w-ldTB09HqYh","execution":{"iopub.status.busy":"2022-03-28T18:07:12.819526Z","iopub.execute_input":"2022-03-28T18:07:12.820034Z","iopub.status.idle":"2022-03-28T18:07:12.863927Z","shell.execute_reply.started":"2022-03-28T18:07:12.819999Z","shell.execute_reply":"2022-03-28T18:07:12.862932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols =train_df.columns\ncolours = ['#4624b8', '#d080b6'] # specify the orange  - yellow is missing. blue is not missing.\nsns.heatmap(train_df[cols].isnull(), cmap=sns.color_palette(colours))","metadata":{"id":"qCJGgdehDFrc","outputId":"9e5902cd-b9a2-46b1-8101-74e1aceb8076","execution":{"iopub.status.busy":"2022-03-28T18:07:12.865422Z","iopub.execute_input":"2022-03-28T18:07:12.865806Z","iopub.status.idle":"2022-03-28T18:07:13.514400Z","shell.execute_reply.started":"2022-03-28T18:07:12.865766Z","shell.execute_reply":"2022-03-28T18:07:13.513592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Get Unique data for each column in  Data Frame** ","metadata":{"id":"kLIX3arSHAJJ"}},{"cell_type":"code","source":"sorted(train_df['species'].unique())","metadata":{"id":"9qEwfe61GhZT","outputId":"73a5f278-2016-49cf-f945-b6eb2eac6a07","execution":{"iopub.status.busy":"2022-03-28T18:07:13.517202Z","iopub.execute_input":"2022-03-28T18:07:13.518135Z","iopub.status.idle":"2022-03-28T18:07:13.530559Z","shell.execute_reply.started":"2022-03-28T18:07:13.518092Z","shell.execute_reply":"2022-03-28T18:07:13.529583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From above we found that there are some coliumn name need to be corrected.","metadata":{"id":"gOxDIXaeD4xb"}},{"cell_type":"code","source":"train_df['species'].replace({\n                          \"pilot_whale\": \"short_finned_pilot_whale\",\n                          \"globis\": \"short_finned_pilot_whale\",\n                          \"bottlenose_dolpin\": \"bottlenose_dolphin\",\n                          \"kiler_whale\": \"killer_whale\"}, inplace=True)\n","metadata":{"id":"4zbL_AbtGhb_","execution":{"iopub.status.busy":"2022-03-28T18:07:13.532495Z","iopub.execute_input":"2022-03-28T18:07:13.532817Z","iopub.status.idle":"2022-03-28T18:07:13.556454Z","shell.execute_reply.started":"2022-03-28T18:07:13.532777Z","shell.execute_reply":"2022-03-28T18:07:13.555725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train =train_df['species'].count()\ntotalspecies=train_df['species'].nunique()\ntotalindividual_id= train_df['individual_id'].nunique()\nprint(f'Total train images         : {total_train}')\nprint(f'Total train species        : {totalspecies}')\nprint(f'Total train individualId   : {totalindividual_id}')","metadata":{"id":"KGEv2llKGhfU","outputId":"4a8ad289-268c-4dd5-c447-af00b5628a79","execution":{"iopub.status.busy":"2022-03-28T18:07:13.557637Z","iopub.execute_input":"2022-03-28T18:07:13.558395Z","iopub.status.idle":"2022-03-28T18:07:13.584157Z","shell.execute_reply.started":"2022-03-28T18:07:13.558352Z","shell.execute_reply":"2022-03-28T18:07:13.583281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = list(train_df['individual_id'].unique())","metadata":{"id":"fwGXJZGzVJ2s","execution":{"iopub.status.busy":"2022-03-28T18:07:13.585328Z","iopub.execute_input":"2022-03-28T18:07:13.585568Z","iopub.status.idle":"2022-03-28T18:07:13.595704Z","shell.execute_reply.started":"2022-03-28T18:07:13.585522Z","shell.execute_reply":"2022-03-28T18:07:13.594727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nsns.countplot(x='species',data=train_df,palette='flare')\nplt.xticks(rotation=90)\nplt.show()","metadata":{"id":"X4MDmrD6OOIH","outputId":"5ef03dc7-c9d5-40c4-b4fc-e8d1c3b7aea4","execution":{"iopub.status.busy":"2022-03-28T18:07:13.597090Z","iopub.execute_input":"2022-03-28T18:07:13.597465Z","iopub.status.idle":"2022-03-28T18:07:13.996512Z","shell.execute_reply.started":"2022-03-28T18:07:13.597434Z","shell.execute_reply":"2022-03-28T18:07:13.995537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\nfig = px.sunburst(train_df,path=['species'])\nfig.show()","metadata":{"id":"Q82Ubr6ZQM7O","outputId":"68e6e93c-a8c3-4fb0-f7f0-421bf086d741","execution":{"iopub.status.busy":"2022-03-28T18:07:13.998085Z","iopub.execute_input":"2022-03-28T18:07:13.998320Z","iopub.status.idle":"2022-03-28T18:07:17.714903Z","shell.execute_reply.started":"2022-03-28T18:07:13.998293Z","shell.execute_reply":"2022-03-28T18:07:17.713847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Whale analysis and Visualization 🐳\n1.  collect all types of whales \n2.  collect all walles data  \n3.  whale statistics \n4.  whales Visualize ","metadata":{"id":"2o0uMS8tWk74"}},{"cell_type":"code","source":"#  all whales types\nallwales = [data for data in train_df['species'].unique()  if not \"dolphin\" in data and  not \"dolpin\" in data]\n#get all data for each  type\nwhales_df = train_df[train_df['species'].isin(allwales)]\nwhales_df.head(10)","metadata":{"id":"SQzgdqac_dhw","outputId":"302d9ab8-88ce-4de1-921e-f3c448098d64","execution":{"iopub.status.busy":"2022-03-28T18:07:17.716905Z","iopub.execute_input":"2022-03-28T18:07:17.717231Z","iopub.status.idle":"2022-03-28T18:07:17.749748Z","shell.execute_reply.started":"2022-03-28T18:07:17.717185Z","shell.execute_reply":"2022-03-28T18:07:17.748520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='species',data=whales_df,palette='flare')\nplt.xticks(rotation=90)\nplt.show()","metadata":{"id":"BDn6oDyhALHp","outputId":"469fa80e-4772-4904-9e2d-9a7e5dcf03d4","execution":{"iopub.status.busy":"2022-03-28T18:07:17.751196Z","iopub.execute_input":"2022-03-28T18:07:17.752143Z","iopub.status.idle":"2022-03-28T18:07:18.043809Z","shell.execute_reply.started":"2022-03-28T18:07:17.752081Z","shell.execute_reply":"2022-03-28T18:07:18.042917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\nfig = px.sunburst(whales_df,path=['species'])\nfig.show()","metadata":{"id":"u5w0kx1KOTrK","outputId":"268848f7-4c56-4894-d149-bf522f742420","execution":{"iopub.status.busy":"2022-03-28T18:07:18.044965Z","iopub.execute_input":"2022-03-28T18:07:18.045197Z","iopub.status.idle":"2022-03-28T18:07:18.714740Z","shell.execute_reply.started":"2022-03-28T18:07:18.045169Z","shell.execute_reply":"2022-03-28T18:07:18.713962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**whale statistics**","metadata":{"id":"pk51iW2WB6Ou"}},{"cell_type":"code","source":"total = len(allwales)\nprint(f' total train whale is         :    {total}')\nwhale_images = whales_df['image'].nunique()\nprint(f' train whale images  is       : {whale_images}')\nwhale_ids=whales_df['individual_id'].nunique()\nprint(f' train whale individualId  is : {whale_ids}')","metadata":{"id":"mW_IBHcQAyh-","outputId":"a26d3fd6-619e-4ff2-bafa-b3e244c1021c","execution":{"iopub.status.busy":"2022-03-28T18:07:18.715761Z","iopub.execute_input":"2022-03-28T18:07:18.716634Z","iopub.status.idle":"2022-03-28T18:07:18.740657Z","shell.execute_reply.started":"2022-03-28T18:07:18.716590Z","shell.execute_reply":"2022-03-28T18:07:18.739925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Notes \n \n\n*   humpback_whale & beluga has most count of image in data set \n*   pygemy_killer_wall has low image in whale data set\n*  there are variation of image of whaels between its types.\n\n","metadata":{"id":"OFiApRcjIosB"}},{"cell_type":"markdown","source":"# Dolphin analysis and Visualization 🐬\n1.  collect all types of Dolphin \n2.  collect all Dolphin data  \n3.  Dolphin statistics \n4.  Dolphin Visualize ","metadata":{"id":"XP0D5co7GABB"}},{"cell_type":"code","source":"# get all dolphin types\nall_dolphine = [data for data in train_df['species'].unique()  if  \"dolphin\" in data or  \"dolpin\" in data]\n#get all data for each specific type\ndolphin_df = train_df[train_df['species'].isin(all_dolphine)]\ndolphin_df.head(10)","metadata":{"id":"Gbku2xKTFpiG","outputId":"bc2ff63c-a5b2-40b1-a6f8-3e02f91a53c0","execution":{"iopub.status.busy":"2022-03-28T18:07:18.741649Z","iopub.execute_input":"2022-03-28T18:07:18.742475Z","iopub.status.idle":"2022-03-28T18:07:18.766678Z","shell.execute_reply.started":"2022-03-28T18:07:18.742431Z","shell.execute_reply":"2022-03-28T18:07:18.765742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='species',data=dolphin_df,palette='flare')\nplt.xticks(rotation=90)\nplt.show()","metadata":{"id":"gsWP1AXL9FtS","outputId":"41504259-9c56-40e6-d588-3c91e2f7a00f","execution":{"iopub.status.busy":"2022-03-28T18:07:18.767789Z","iopub.execute_input":"2022-03-28T18:07:18.768455Z","iopub.status.idle":"2022-03-28T18:07:18.989242Z","shell.execute_reply.started":"2022-03-28T18:07:18.768372Z","shell.execute_reply":"2022-03-28T18:07:18.988216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\nfig = px.sunburst(dolphin_df,path=['species'])\nfig.show()","metadata":{"id":"7Ch6cJ_3OeyQ","outputId":"24b1e25e-031f-452c-d9e8-92c92ed92db2","execution":{"iopub.status.busy":"2022-03-28T18:07:18.990601Z","iopub.execute_input":"2022-03-28T18:07:18.990830Z","iopub.status.idle":"2022-03-28T18:07:19.366466Z","shell.execute_reply.started":"2022-03-28T18:07:18.990804Z","shell.execute_reply":"2022-03-28T18:07:19.365339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = len(all_dolphine)\nprint(f' total train Dolphin is         :    {total}')\ndolphin_images = dolphin_df['image'].nunique()\nprint(f' train dolphin images  is       : {dolphin_images}')\nwhale_ids=dolphin_df['individual_id'].nunique()\nprint(f' train dolphin individualId  is : {whale_ids}')","metadata":{"id":"2vsHJN_16Kcq","outputId":"a76b1e46-e760-468f-a389-6903ed61f397","execution":{"iopub.status.busy":"2022-03-28T18:07:19.367818Z","iopub.execute_input":"2022-03-28T18:07:19.368734Z","iopub.status.idle":"2022-03-28T18:07:19.385667Z","shell.execute_reply.started":"2022-03-28T18:07:19.368698Z","shell.execute_reply":"2022-03-28T18:07:19.384743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Notes \n \n\n*   humpback_whale bottlenose_dolphin of image in data set .\n*   images of dolphine types  is very low which mean that it is difficult to diffreniate between dopline types so we need to increase its images \n\n","metadata":{"id":"lEzQCMosKJuU"}},{"cell_type":"markdown","source":"# loading images  🐬vs 🐳","metadata":{"id":"931w2fM0Q3wu"}},{"cell_type":"code","source":"\nplt.figure(figsize=(20,15))\nplt.suptitle(\"Train Images\", fontsize=20)\npath =image_path+'/'+'train_images/'\ncounter =0\nfor i,img in enumerate(train_df['image'])  :\n        plt.subplot(5,7,i+1)\n        full_image= plt.imread(path+img)\n        plt.xticks([])\n        plt.yticks([])\n        plt.grid(False)\n        plt.xlabel(train_df['species'][i])\n        plt.imshow(full_image, cmap=plt.cm.binary) \n        if i == 34:\n            break","metadata":{"id":"fOLq0LLW6SQj","outputId":"03c2f1cc-4c13-4dfe-f27a-2fd2a1a167ff","execution":{"iopub.status.busy":"2022-03-28T18:07:19.387082Z","iopub.execute_input":"2022-03-28T18:07:19.387851Z","iopub.status.idle":"2022-03-28T18:07:43.463077Z","shell.execute_reply.started":"2022-03-28T18:07:19.387804Z","shell.execute_reply":"2022-03-28T18:07:43.461746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Buliding CNN Model\n\n**i will use Data augmentation,as it  is a set of techniques to artificially increase the amount of data by generating new data points from existing data. This includes making small changes to data or using deep learning models to generate new data points.** \n\n","metadata":{"id":"5O1NZpjrijnM"}},{"cell_type":"code","source":"batch_size = 64\n#create image generator for images \nimage_gen = ImageDataGenerator(\n                                 rescale = 1./255,\n                                  shear_range = 0.2,\n                                  zoom_range = 0.5,\n                                  height_shift_range=0.2,\n                                  width_shift_range=0.2,\n                                  fill_mode='nearest',\n                                   horizontal_flip=True,\n                                   rotation_range = 20,\n                               validation_split=0.2 \n                               )\n\n# Create Image Data Generator for Test/Validation Set\ntest_data_gen = ImageDataGenerator(  \n                                   rescale = 1./255,\n                                  shear_range = 0.2,\n                                  zoom_range = 0.5,\n                                  height_shift_range=0.2,\n                                  width_shift_range=0.2,\n                                  fill_mode='nearest',\n                                   horizontal_flip=True,\n                                   rotation_range = 20,\n                                   validation_split=0.2)        \ntrain = image_gen.flow_from_dataframe(\n      train_df,\n      path,\n      x_col='image',\n      y_col='individual_id',\n      target_size=(64,64),\n      class_mode='categorical',\n      shuffle=True, \n      batch_size=batch_size,\n      labels =labels,\n      subset = \"training\"\n      )\nvalidate = test_data_gen.flow_from_dataframe(\n      train_df,\n      path,\n      x_col='image',\n      y_col='individual_id',\n      target_size=(64,64),\n      class_mode='categorical',\n      shuffle=True, \n      batch_size=batch_size,\n      subset = \"validation\",\n      labels =labels\n      )\n","metadata":{"id":"p7jQbFP742OG","outputId":"16e6663f-1a36-41cd-d0e0-2d1525f91060","execution":{"iopub.status.busy":"2022-03-28T18:07:43.464520Z","iopub.execute_input":"2022-03-28T18:07:43.465039Z","iopub.status.idle":"2022-03-28T18:08:43.291369Z","shell.execute_reply.started":"2022-03-28T18:07:43.465003Z","shell.execute_reply":"2022-03-28T18:08:43.288476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get shape of train data \nfor train_img , train_label in train :\n    print('image shape ',train_img.shape)\n    print('label  shape ',train_label.shape)\n    break ","metadata":{"id":"OCLV-gLvLE5z","outputId":"a318a876-5985-4235-8ec9-2f52048d9570","execution":{"iopub.status.busy":"2022-03-28T18:08:43.293698Z","iopub.execute_input":"2022-03-28T18:08:43.294355Z","iopub.status.idle":"2022-03-28T18:08:48.307430Z","shell.execute_reply.started":"2022-03-28T18:08:43.294301Z","shell.execute_reply":"2022-03-28T18:08:48.306397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get shape of train data \nfor v_img , v_label in validate :\n    print('image shape ',v_img.shape)\n    print('label  shape ',v_label.shape)\n    break ","metadata":{"id":"wN3Oz9z1LdhH","outputId":"88bd01a5-f720-4117-fe08-96688535f5f3","execution":{"iopub.status.busy":"2022-03-28T18:08:48.308739Z","iopub.execute_input":"2022-03-28T18:08:48.308974Z","iopub.status.idle":"2022-03-28T18:08:53.797378Z","shell.execute_reply.started":"2022-03-28T18:08:48.308947Z","shell.execute_reply":"2022-03-28T18:08:53.795997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model = Sequential()\ncnn_model = models.Sequential()\ncnn_model.add(layers.Conv2D(64,(3,3),padding ='Same',activation = 'relu',input_shape=(64,64,3)))\ncnn_model.add(layers.MaxPooling2D(2,2))\ncnn_model.add(layers.Conv2D(64,(3,3) ,padding ='same',activation='relu'))\ncnn_model.add(layers.MaxPooling2D(2,2))\ncnn_model.add(layers.Conv2D(128,(3,3),padding ='same',activation='relu'))\ncnn_model.add(layers.MaxPooling2D(2,2)) \ncnn_model.add(layers.Conv2D(128,(3,3) ,padding ='same',activation='relu'))\ncnn_model.add(layers.MaxPooling2D(2,2)) \ncnn_model.add(layers.Conv2D(256,(3,3) ,padding ='same',activation='relu'))\ncnn_model.add(layers.MaxPooling2D(2,2)) \ncnn_model.add(BatchNormalization())\ncnn_model.add(layers.Conv2D(256,(3,3) ,padding ='same',activation='relu'))\ncnn_model.add(layers.MaxPooling2D(2,2)) \ncnn_model.add(BatchNormalization())\ncnn_model.summary()\n# cnn_model.add(Dropout(0.2))\n","metadata":{"id":"QfsDiDZE42Qz","outputId":"dea697b0-7044-4605-fae0-a5bd32978550","execution":{"iopub.status.busy":"2022-03-28T18:08:53.801943Z","iopub.execute_input":"2022-03-28T18:08:53.803130Z","iopub.status.idle":"2022-03-28T18:08:54.067294Z","shell.execute_reply.started":"2022-03-28T18:08:53.803082Z","shell.execute_reply":"2022-03-28T18:08:54.066423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ncnn_model.add(layers.Flatten())\ncnn_model.add(layers.Dense(1024, activation='relu'))\ncnn_model.add(BatchNormalization())\n# cnn_model.add(Dropout(0.7))\ncnn_model.add(layers.Dense(512, activation='relu'))\ncnn_model.add(BatchNormalization())\n# cnn_model.add(Dropout(0.3))\ncnn_model.add(layers.Dense(15587, activation ='softmax'))\ncnn_model.summary()\n","metadata":{"id":"S92wdti_cHH6","outputId":"1eac9ecf-4ec5-4e9e-c64e-3a54191642b6","execution":{"iopub.status.busy":"2022-03-28T18:08:54.068714Z","iopub.execute_input":"2022-03-28T18:08:54.068980Z","iopub.status.idle":"2022-03-28T18:08:54.247740Z","shell.execute_reply.started":"2022-03-28T18:08:54.068948Z","shell.execute_reply":"2022-03-28T18:08:54.246971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'],)","metadata":{"id":"EMoCr59t42Td","execution":{"iopub.status.busy":"2022-03-28T18:08:54.249083Z","iopub.execute_input":"2022-03-28T18:08:54.249415Z","iopub.status.idle":"2022-03-28T18:08:54.594817Z","shell.execute_reply.started":"2022-03-28T18:08:54.249370Z","shell.execute_reply":"2022-03-28T18:08:54.593572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"  **1-Defining Callbacks**\n\n*   A callback is an object that can perform actions at various stages of training (e.g. at the start or end of an epoch, before or after a single batch, etc)\n\n\n**2-Reduce Learning Rate on Plateau**\n*   Is used to reduce the learning rate when a metric has stopped improving.\n\n","metadata":{"id":"ubaCfhsU_6QF"}},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping,ReduceLROnPlateau\nearly = EarlyStopping(monitor=\"loss\", mode=\"min\",min_delta = 0,\n                          patience = 10,\n                          verbose = 1,\n                          restore_best_weights = True)\nlearning_rate_reduction = ReduceLROnPlateau(monitor='loss', patience = 2, verbose=1,factor=0.3, min_lr=0.000001)\ncallbacks_list = [ early, learning_rate_reduction]","metadata":{"id":"uxLfTTyhFnFy","execution":{"iopub.status.busy":"2022-03-28T18:08:54.596626Z","iopub.execute_input":"2022-03-28T18:08:54.596874Z","iopub.status.idle":"2022-03-28T18:08:54.604411Z","shell.execute_reply.started":"2022-03-28T18:08:54.596846Z","shell.execute_reply":"2022-03-28T18:08:54.603371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training model\nn_training_samples = len(train)\nn_validation_samples = len(validate)\nhistory = cnn_model.fit(\n    train,\n    epochs=100,\n    validation_data=validate,\n    validation_steps=n_validation_samples//batch_size,\n    steps_per_epoch =n_training_samples//batch_size,\n    shuffle = True,\n    callbacks=callbacks_list\n    )","metadata":{"id":"CKSuE6FQ42WE","outputId":"46116f77-ab1e-4613-ba28-28f6a3077ae8","execution":{"iopub.status.busy":"2022-03-28T18:08:54.605702Z","iopub.execute_input":"2022-03-28T18:08:54.606021Z","iopub.status.idle":"2022-03-28T19:05:52.239176Z","shell.execute_reply.started":"2022-03-28T18:08:54.605977Z","shell.execute_reply":"2022-03-28T19:05:52.235728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nThanks for   <span style=\"color:red;\">**Reviewing**</span>   My kernel also, if you have any comments or suggestion please raise it","metadata":{}}]}