{"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":"## Importing Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras import layers,models\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.densenet import DenseNet121\nimport warnings\nwarnings.simplefilter(\"ignore\")\nfrom PIL import Image\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-09T04:31:47.099168Z","iopub.execute_input":"2022-09-09T04:31:47.100318Z","iopub.status.idle":"2022-09-09T04:31:53.544162Z","shell.execute_reply.started":"2022-09-09T04:31:47.100215Z","shell.execute_reply":"2022-09-09T04:31:53.543041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport random\nimport base64\nimport seaborn as sn\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom PIL import Image\nfrom io import BytesIO\nfrom IPython.display import HTML\n\n\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\n\n\nimport numpy as np\nimport os,cv2,random,time,shutil,csv\nimport tensorflow as tf\nfrom matplotlib import pyplot as plt\nfrom matplotlib import image as mpimg\nfrom tqdm import tqdm\nimport json,os,cv2,keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model\nfrom keras.layers import BatchNormalization,Dense,GlobalAveragePooling2D,Lambda,Dropout,InputLayer,Input\nfrom tensorflow.keras.utils import to_categorical,plot_model\nfrom tensorflow.keras.preprocessing.image import load_img\n\n\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import RMSprop,Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import VGG16","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:53.546194Z","iopub.execute_input":"2022-09-09T04:31:53.546767Z","iopub.status.idle":"2022-09-09T04:31:54.297228Z","shell.execute_reply.started":"2022-09-09T04:31:53.546738Z","shell.execute_reply":"2022-09-09T04:31:54.296197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport random\nimport base64\nimport seaborn as sn\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom io import BytesIO\nfrom IPython.display import HTML\n","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:54.298695Z","iopub.execute_input":"2022-09-09T04:31:54.299092Z","iopub.status.idle":"2022-09-09T04:31:54.305508Z","shell.execute_reply.started":"2022-09-09T04:31:54.299033Z","shell.execute_reply":"2022-09-09T04:31:54.304354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font\n      size=\"5\">**Import Data**</font>","metadata":{}},{"cell_type":"code","source":"import os\nDir = '../input/dog-breed-identification'\nos.listdir(Dir)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:54.309296Z","iopub.execute_input":"2022-09-09T04:31:54.310199Z","iopub.status.idle":"2022-09-09T04:31:54.321425Z","shell.execute_reply.started":"2022-09-09T04:31:54.310157Z","shell.execute_reply":"2022-09-09T04:31:54.320515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('../input/dog-breed-identification/train')))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:54.323079Z","iopub.execute_input":"2022-09-09T04:31:54.323444Z","iopub.status.idle":"2022-09-09T04:31:54.766658Z","shell.execute_reply.started":"2022-09-09T04:31:54.323408Z","shell.execute_reply":"2022-09-09T04:31:54.765698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('../input/dog-breed-identification/test')))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:54.768220Z","iopub.execute_input":"2022-09-09T04:31:54.768622Z","iopub.status.idle":"2022-09-09T04:31:55.132141Z","shell.execute_reply.started":"2022-09-09T04:31:54.768588Z","shell.execute_reply":"2022-09-09T04:31:55.131079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df = pd.read_csv('../input/dog-breed-identification/labels.csv')\nlabels_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:55.133379Z","iopub.execute_input":"2022-09-09T04:31:55.133763Z","iopub.status.idle":"2022-09-09T04:31:55.177438Z","shell.execute_reply.started":"2022-09-09T04:31:55.133718Z","shell.execute_reply":"2022-09-09T04:31:55.176528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Analysis","metadata":{}},{"cell_type":"code","source":"Breed_num= pd.unique(labels_df['breed'])\nprint(len(Breed_num))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:55.178963Z","iopub.execute_input":"2022-09-09T04:31:55.179847Z","iopub.status.idle":"2022-09-09T04:31:55.193221Z","shell.execute_reply.started":"2022-09-09T04:31:55.179812Z","shell.execute_reply":"2022-09-09T04:31:55.192070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font\n      size=\"4\">**Images per Breed**</font>","metadata":{}},{"cell_type":"code","source":"each_label = labels_df.groupby(\"breed\").count()\neach_label = each_label.rename(columns = {\"id\" : \"count\"})\neach_label = each_label.sort_values(\"count\", ascending=False)\neach_label","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:55.194634Z","iopub.execute_input":"2022-09-09T04:31:55.195299Z","iopub.status.idle":"2022-09-09T04:31:55.216047Z","shell.execute_reply.started":"2022-09-09T04:31:55.195265Z","shell.execute_reply":"2022-09-09T04:31:55.215044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plotting the Breed VS Image count Distribution","metadata":{}},{"cell_type":"code","source":"ax=pd.value_counts(labels_df['breed'],ascending=True).plot(kind='barh',\n                                                       fontsize=\"40\",\n                                                       title=\"Class Distribution\",\n                                                       figsize=(50,100))\nax.set(xlabel=\"Images per class\", ylabel=\"Classes\")\nax.xaxis.label.set_size(20)\nax.yaxis.label.set_size(20)\nax.title.set_size(30)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:55.220554Z","iopub.execute_input":"2022-09-09T04:31:55.220807Z","iopub.status.idle":"2022-09-09T04:31:58.996327Z","shell.execute_reply.started":"2022-09-09T04:31:55.220784Z","shell.execute_reply":"2022-09-09T04:31:58.995404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.round((labels_df['breed'].value_counts()/len(labels_df['breed']))*100, 2)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:58.997255Z","iopub.execute_input":"2022-09-09T04:31:58.997573Z","iopub.status.idle":"2022-09-09T04:31:59.013093Z","shell.execute_reply.started":"2022-09-09T04:31:58.997545Z","shell.execute_reply":"2022-09-09T04:31:59.012056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels_df)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:59.014427Z","iopub.execute_input":"2022-09-09T04:31:59.014942Z","iopub.status.idle":"2022-09-09T04:31:59.020906Z","shell.execute_reply.started":"2022-09-09T04:31:59.014910Z","shell.execute_reply":"2022-09-09T04:31:59.020121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# paths\npath = r'../input/'\nlabels_csv_path = path + 'labels.csv'\nsample_submission_csv_path = path + 'sample_submission.csv'\nsubmission_csv_path =  '../input/sample_submission.csv'\ntrain_path = path + 'train'\ntest_path = path + 'test'\n\nnumber_of_epochs = 10\n\nprint(f'Constants are set. Fine tuning takes {number_of_epochs} epochs.')\n","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:59.022278Z","iopub.execute_input":"2022-09-09T04:31:59.022869Z","iopub.status.idle":"2022-09-09T04:31:59.032318Z","shell.execute_reply.started":"2022-09-09T04:31:59.022821Z","shell.execute_reply":"2022-09-09T04:31:59.031303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading a Single Image","metadata":{}},{"cell_type":"code","source":"id_ = labels_df.loc[0, 'id']\nbreed = labels_df.loc[0, 'breed']\nfile = id_+'.jpg'\nimg = cv2.imread(path+'../input/dog-breed-identification/train/000bec180eb18c7604dcecc8fe0dba07.jpg')\nprint('Shape:', img.shape)\nfig, ax = plt.subplots(1, 1, figsize=(7, 7))\nax.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\nax.set_xticklabels([])\nax.set_yticklabels([])\nax.set_title(breed)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:59.033731Z","iopub.execute_input":"2022-09-09T04:31:59.034345Z","iopub.status.idle":"2022-09-09T04:31:59.326677Z","shell.execute_reply.started":"2022-09-09T04:31:59.034311Z","shell.execute_reply":"2022-09-09T04:31:59.325874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_colwidth', -1)\n\ndef get_thumbnail(path):\n    i = Image.open(path)\n    i.thumbnail((150, 150), Image.LANCZOS)\n    return i\n\ndef image_base64(im):\n    if isinstance(im, str):\n        im = get_thumbnail(im)\n    with BytesIO() as buffer:\n        im.save(buffer, 'jpeg')\n        return base64.b64encode(buffer.getvalue()).decode()\n\ndef image_formatter(im):\n    return f'<img src=\"data:image/jpeg;base64,{image_base64(im)}\">'","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:59.328001Z","iopub.execute_input":"2022-09-09T04:31:59.328949Z","iopub.status.idle":"2022-09-09T04:31:59.337170Z","shell.execute_reply.started":"2022-09-09T04:31:59.328913Z","shell.execute_reply":"2022-09-09T04:31:59.335863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id='../input/dog-breed-identification/train/000bec180eb18c7604dcecc8fe0dba07.jpg'\ndogs = pd.read_csv('../input/dog-breed-identification/labels.csv')\ndogs = dogs.sample(20)\ndogs['file'] = dogs.id.map(lambda id: f'../input/dog-breed-identification/train/{id}.jpg')\ndogs['image'] = dogs.file.map(lambda f: get_thumbnail(f))\ndogs.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:59.338988Z","iopub.execute_input":"2022-09-09T04:31:59.339358Z","iopub.status.idle":"2022-09-09T04:31:59.644272Z","shell.execute_reply.started":"2022-09-09T04:31:59.339324Z","shell.execute_reply":"2022-09-09T04:31:59.643416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HTML(dogs[['breed', 'image']].to_html(formatters={'image': image_formatter}, escape=False))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:59.645818Z","iopub.execute_input":"2022-09-09T04:31:59.646169Z","iopub.status.idle":"2022-09-09T04:31:59.673117Z","shell.execute_reply.started":"2022-09-09T04:31:59.646135Z","shell.execute_reply":"2022-09-09T04:31:59.672033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_examples(category = 'scottish_deerhound'):\n    \"\"\" Plot 5 images of a given category \"\"\"\n    \n    fig, axs = plt.subplots(1, 5, figsize=(25, 20))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    axs = axs.ravel()\n    temp = labels_df[labels_df['breed']==category].copy()\n    temp.index = range(len(temp.index))\n    for i in range(5):\n        id_ = temp.loc[i, 'id']\n        breed = temp.loc[i, 'breed']\n        file = id_+'.jpg'\n        img = cv2.imread(path+'../input/dog-breed-identification/train/'+file)\n        axs[i].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n        axs[i].set_title(breed)\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:59.674341Z","iopub.execute_input":"2022-09-09T04:31:59.675201Z","iopub.status.idle":"2022-09-09T04:31:59.685224Z","shell.execute_reply.started":"2022-09-09T04:31:59.675168Z","shell.execute_reply":"2022-09-09T04:31:59.684204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:31:59.688283Z","iopub.execute_input":"2022-09-09T04:31:59.689314Z","iopub.status.idle":"2022-09-09T04:32:00.467597Z","shell.execute_reply.started":"2022-09-09T04:31:59.689287Z","shell.execute_reply":"2022-09-09T04:32:00.466758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples('otterhound')","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:00.468979Z","iopub.execute_input":"2022-09-09T04:32:00.469540Z","iopub.status.idle":"2022-09-09T04:32:01.269532Z","shell.execute_reply.started":"2022-09-09T04:32:00.469505Z","shell.execute_reply":"2022-09-09T04:32:01.267666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples('golden_retriever')","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:01.270827Z","iopub.execute_input":"2022-09-09T04:32:01.271765Z","iopub.status.idle":"2022-09-09T04:32:02.341753Z","shell.execute_reply.started":"2022-09-09T04:32:01.271728Z","shell.execute_reply":"2022-09-09T04:32:02.340953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Label Encoding","metadata":{}},{"cell_type":"code","source":"label_encoder = preprocessing.LabelEncoder()\n  \n# Encode labels in column 'species'.\nlabels_df['breed_label']= label_encoder.fit_transform(labels_df['breed'])\n  \nlabels_df['breed_label'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:02.343233Z","iopub.execute_input":"2022-09-09T04:32:02.343944Z","iopub.status.idle":"2022-09-09T04:32:02.359141Z","shell.execute_reply.started":"2022-09-09T04:32:02.343909Z","shell.execute_reply":"2022-09-09T04:32:02.357809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df[labels_df['breed']=='dingo'].head()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:02.360755Z","iopub.execute_input":"2022-09-09T04:32:02.361315Z","iopub.status.idle":"2022-09-09T04:32:02.376906Z","shell.execute_reply.started":"2022-09-09T04:32:02.361282Z","shell.execute_reply":"2022-09-09T04:32:02.375903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Preprocessing","metadata":{}},{"cell_type":"code","source":"def image_preprocessing(image, image_size):\n    \"\"\" Image Preprocessing \"\"\"\n\n    # Load Image\n    readFlag=cv2.COLOR_BGR2GRAY\n    #image = np.asarray(bytearray(resp.read()), dtype=\"uint8\")\n    #image = cv2.imdecode(image, readFlag)\n    image_gray = cv2.cvtColor(image, readFlag)\n    \n    # Crop Image\n    mid_row = int(image_gray.shape[0]/2)\n    mid_col = int(image_gray.shape[1]/2)\n    if image_gray.shape[0]>image_gray.shape[1]:\n        image_cropped = image_gray[mid_row-mid_col:mid_row+mid_col,\n                                   0:image_gray.shape[1]]\n    else:\n        image_cropped = image_gray[0:image_gray.shape[0],\n                                   mid_col-mid_row:mid_col+mid_row]\n    \n    # Rescale Image\n    image_rescale = cv2.resize(image_cropped,\n                               dsize=(image_size, image_size),\n                               interpolation=cv2.INTER_AREA)\n    return image_rescale\n\ndef plot_befor_after(image):\n    \"\"\" Compare original and prepared image \"\"\"\n    \n    fig, axs = plt.subplots(1, 2, figsize=(15, 10))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    axs = axs.ravel()\n    # Plot Original Image\n    axs[0].imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n    axs[0].set_title('original shape: '+str(image.shape))\n    # Image Preprocessing\n    image_rescale = image_preprocessing(image, image_size)\n    # Plot Prepared Image\n    axs[1].imshow(image_rescale, cmap='gray')\n    axs[1].set_title('rescaled shape: '+str(image_rescale.shape))\n    for i in range(2):\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:02.378528Z","iopub.execute_input":"2022-09-09T04:32:02.379248Z","iopub.status.idle":"2022-09-09T04:32:02.390845Z","shell.execute_reply.started":"2022-09-09T04:32:02.379212Z","shell.execute_reply":"2022-09-09T04:32:02.389826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 128","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:02.393956Z","iopub.execute_input":"2022-09-09T04:32:02.394357Z","iopub.status.idle":"2022-09-09T04:32:02.404472Z","shell.execute_reply.started":"2022-09-09T04:32:02.394237Z","shell.execute_reply":"2022-09-09T04:32:02.403550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = 0\nid_ = labels_df.loc[row, 'id']\nbreed = labels_df.loc[row, 'breed']\nfile = id_+'.jpg'\nimage = cv2.imread(path+'../input/dog-breed-identification/train/'+file)\nprint('Shape:', image.shape)\nplot_befor_after(image)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:02.406029Z","iopub.execute_input":"2022-09-09T04:32:02.406322Z","iopub.status.idle":"2022-09-09T04:32:02.772129Z","shell.execute_reply.started":"2022-09-09T04:32:02.406298Z","shell.execute_reply":"2022-09-09T04:32:02.771315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = 12\nid_ = labels_df.loc[row, 'id']\nbreed = labels_df.loc[row, 'breed']\nfile = id_+'.jpg'\nimage = cv2.imread(path+'../input/dog-breed-identification/train/'+file)\nprint('Shape:', image.shape)\nplot_befor_after(image)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:02.773494Z","iopub.execute_input":"2022-09-09T04:32:02.773983Z","iopub.status.idle":"2022-09-09T04:32:03.134910Z","shell.execute_reply.started":"2022-09-09T04:32:02.773952Z","shell.execute_reply":"2022-09-09T04:32:03.133987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read all Images","metadata":{}},{"cell_type":"code","source":"def prepare_data(path, data, image_size):\n    \"\"\" Read all images into a numpy array \"\"\"\n    \n    X = np.empty((len(data), image_size, image_size), dtype=np.uint8)\n    for row in data.index:\n        id_ = data.loc[row, 'id']\n        file = id_ + '.jpg'\n        image = cv2.imread(path+file)\n        image_rescaled = image_preprocessing(image, image_size)\n        X[row, :, :] = image_rescaled\n    X = X.astype('float32')/255\n    return X","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:03.142388Z","iopub.execute_input":"2022-09-09T04:32:03.143437Z","iopub.status.idle":"2022-09-09T04:32:03.150194Z","shell.execute_reply.started":"2022-09-09T04:32:03.143399Z","shell.execute_reply":"2022-09-09T04:32:03.149245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df = pd.read_csv('../input/dog-breed-identification/labels.csv')\nsamp_subm = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nprint(f'The shape of the labels: {labels_df.shape}')","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:32:03.152762Z","iopub.execute_input":"2022-09-09T04:32:03.153891Z","iopub.status.idle":"2022-09-09T04:32:03.634133Z","shell.execute_reply.started":"2022-09-09T04:32:03.153861Z","shell.execute_reply":"2022-09-09T04:32:03.633121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = prepare_data(path+'../input/dog-breed-identification/train/', labels_df, image_size)\nX_test = prepare_data(path+'../input/dog-breed-identification/test/', samp_subm, image_size)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:37:05.197366Z","iopub.execute_input":"2022-09-09T04:37:05.197943Z","iopub.status.idle":"2022-09-09T04:38:56.845748Z","shell.execute_reply.started":"2022-09-09T04:37:05.197897Z","shell.execute_reply":"2022-09-09T04:38:56.844464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Encode the Labels","metadata":{}},{"cell_type":"code","source":"y_train = labels_df['breed']\ny_train = pd.get_dummies(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:40:48.698280Z","iopub.execute_input":"2022-09-09T04:40:48.699018Z","iopub.status.idle":"2022-09-09T04:40:48.715130Z","shell.execute_reply.started":"2022-09-09T04:40:48.698980Z","shell.execute_reply":"2022-09-09T04:40:48.714119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split Train Data","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.2, random_state=2021)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:41:00.381312Z","iopub.execute_input":"2022-09-09T04:41:00.382019Z","iopub.status.idle":"2022-09-09T04:41:00.555704Z","shell.execute_reply.started":"2022-09-09T04:41:00.381983Z","shell.execute_reply":"2022-09-09T04:41:00.554584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Shape train data:', X_train.shape)\nprint('Shape val data:', X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:41:03.874220Z","iopub.execute_input":"2022-09-09T04:41:03.874652Z","iopub.status.idle":"2022-09-09T04:41:03.881373Z","shell.execute_reply.started":"2022-09-09T04:41:03.874618Z","shell.execute_reply":"2022-09-09T04:41:03.880011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reshape Data","metadata":{}},{"cell_type":"code","source":"X_train = X_train.reshape(-1,image_size,image_size,1)\nX_val = X_val.reshape(-1,image_size,image_size,1)\nX_test = X_test.reshape(-1,image_size,image_size,1)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:59:07.652058Z","iopub.execute_input":"2022-09-09T04:59:07.652801Z","iopub.status.idle":"2022-09-09T04:59:07.679730Z","shell.execute_reply.started":"2022-09-09T04:59:07.652766Z","shell.execute_reply":"2022-09-09T04:59:07.678136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Shape train data:', X_train.shape)\nprint('Shape val data:', X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:41:25.858781Z","iopub.execute_input":"2022-09-09T04:41:25.859220Z","iopub.status.idle":"2022-09-09T04:41:25.866154Z","shell.execute_reply.started":"2022-09-09T04:41:25.859184Z","shell.execute_reply":"2022-09-09T04:41:25.865158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"image_size = (128, 128)\nbatch_size = 32\n\ntrain_df = tf.keras.preprocessing.image_dataset_from_directory(\n    \"../input/dog-breed-identification\",\n    validation_split=0.2,\n    subset=\"training\",\n    seed=1337,\n    image_size=image_size,\n    batch_size=batch_size,\n)\nval_df = tf.keras.preprocessing.image_dataset_from_directory(\n    \"../input/dog-breed-identification\",\n    validation_split=0.2,\n    subset=\"validation\",\n    seed=1337,\n    image_size=image_size,\n    batch_size=batch_size,\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:53:46.480554Z","iopub.execute_input":"2022-09-09T04:53:46.481808Z","iopub.status.idle":"2022-09-09T04:54:17.108304Z","shell.execute_reply.started":"2022-09-09T04:53:46.481759Z","shell.execute_reply":"2022-09-09T04:54:17.107314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning","metadata":{}},{"cell_type":"markdown","source":"## 1. Efficient Net","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG19\nfrom keras.models import Model\nfrom keras.layers import Dense,Dropout\nfrom keras.backend import pool2d\nfrom keras.layers.pooling import GlobalAveragePooling2D","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:42:12.834935Z","iopub.execute_input":"2022-09-09T04:42:12.835903Z","iopub.status.idle":"2022-09-09T04:42:12.840492Z","shell.execute_reply.started":"2022-09-09T04:42:12.835864Z","shell.execute_reply":"2022-09-09T04:42:12.839257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.efficientnet import EfficientNetB3\ndef create_model():\n    model = models.Sequential()\n    model.add(EfficientNetB3(include_top = False, weights = 'imagenet',\n                             input_shape = (128, 128, 3),  drop_connect_rate=0.3))\n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Flatten())\n    model.add(layers.Dense(256, activation = \"relu\"))\n    model.add(layers.Dropout(0.3))\n    model.add(layers.Dense(1, activation='softmax'))\n    \n    loss = tf.keras.losses.CategoricalCrossentropy(\n        label_smoothing=0.0001,\n        name='categorical_crossentropy'\n    )\n    model.compile(optimizer = Adam(lr = 1e-4),\n                  loss = loss,\n                  metrics = [\"categorical_accuracy\"])\n    return model\n\nmodel = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:33:06.154476Z","iopub.execute_input":"2022-09-09T05:33:06.154853Z","iopub.status.idle":"2022-09-09T05:33:10.223745Z","shell.execute_reply.started":"2022-09-09T05:33:06.154823Z","shell.execute_reply":"2022-09-09T05:33:10.222577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T04:42:31.706171Z","iopub.execute_input":"2022-09-09T04:42:31.707192Z","iopub.status.idle":"2022-09-09T04:42:32.811582Z","shell.execute_reply.started":"2022-09-09T04:42:31.707147Z","shell.execute_reply":"2022-09-09T04:42:32.810495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 50\nbatch_size = 32\n\n\nmodel_history = model.fit(X_train, y_train,\n                    epochs=epochs,\n                    batch_size=batch_size,\n                    validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:01:13.870580Z","iopub.execute_input":"2022-09-09T05:01:13.871417Z","iopub.status.idle":"2022-09-09T05:29:39.717041Z","shell.execute_reply.started":"2022-09-09T05:01:13.871372Z","shell.execute_reply":"2022-09-09T05:29:39.716114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.Densenet21","metadata":{}},{"cell_type":"code","source":"#model_defined = EfficientNetB3(include_top = False, weights = 'imagenet',input_shape = (img_height, img_width, 3),  drop_connect_rate=0.3)\n#model_defined =  tf.keras.applications.ResNet101(include_top=True, weights=\"imagenet\",input_tensor=None,input_shape=None,pooling=None)\nmodel_defined =  tf.keras.applications.DenseNet121(include_top=False,weights=\"imagenet\",input_tensor=None,input_shape=None,pooling=None,classes=1000)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T06:04:01.838980Z","iopub.execute_input":"2022-09-09T06:04:01.840010Z","iopub.status.idle":"2022-09-09T06:04:04.272485Z","shell.execute_reply.started":"2022-09-09T06:04:01.839974Z","shell.execute_reply":"2022-09-09T06:04:04.271500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.efficientnet import EfficientNetB3\ndef create_model():\n    model.add (model_defined)\n    \n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Flatten())\n    model.add(layers.Dense(256, activation = \"relu\"))\n    model.add(layers.Dropout(0.3))\n    model.add(layers.Dense(1, activation='softmax'))\n    \n    loss = tf.keras.losses.CategoricalCrossentropy(\n        label_smoothing=0.0001,\n        name='categorical_crossentropy'\n    )\n    model.compile(optimizer = Adam(lr = 1e-4),\n                  loss = loss,\n                  metrics = [\"categorical_accuracy\"])\n    return model\n\nmodel = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T06:04:10.203999Z","iopub.execute_input":"2022-09-09T06:04:10.205056Z","iopub.status.idle":"2022-09-09T06:04:10.257020Z","shell.execute_reply.started":"2022-09-09T06:04:10.205017Z","shell.execute_reply":"2022-09-09T06:04:10.255549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 50\nbatch_size = 32\n\n\nmodel_history = model.fit(X_train, y_train,\n                    epochs=epochs,\n                    batch_size=batch_size,\n                    validation_data=(X_val, y_val))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Resnet50","metadata":{}},{"cell_type":"code","source":"model_defined =  tf.keras.applications.ResNet101(include_top=True, weights=\"imagenet\",input_tensor=None,input_shape=None,pooling=None)","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:58:01.035295Z","iopub.execute_input":"2022-09-09T05:58:01.036049Z","iopub.status.idle":"2022-09-09T05:58:04.738111Z","shell.execute_reply.started":"2022-09-09T05:58:01.036012Z","shell.execute_reply":"2022-09-09T05:58:04.737121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.efficientnet import EfficientNetB3\ndef create_model():\n    model = models.Sequential()\n    model.add(model_defined)\n    #model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Flatten())\n    model.add(layers.Dense(256, activation = \"relu\"))\n    model.add(layers.Dropout(0.3))\n    model.add(layers.Dense(120, activation='softmax'))\n    \n    loss = tf.keras.losses.CategoricalCrossentropy(\n        label_smoothing=0.0001,\n        name='categorical_crossentropy'\n    )\n    model.compile(optimizer = Adam(lr = 1e-4),\n                  loss = loss,\n                  metrics = [\"categorical_accuracy\"])\n    return model\n\nmodel = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:58:07.299944Z","iopub.execute_input":"2022-09-09T05:58:07.300311Z","iopub.status.idle":"2022-09-09T05:58:08.099680Z","shell.execute_reply.started":"2022-09-09T05:58:07.300282Z","shell.execute_reply":"2022-09-09T05:58:08.098766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 50\nbatch_size = 32\n\n\nmodel_history = model.fit(X_train, y_train,\n                    epochs=epochs,\n                    batch_size=batch_size,\n                    validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:58:11.678922Z","iopub.execute_input":"2022-09-09T05:58:11.679974Z","iopub.status.idle":"2022-09-09T05:58:12.713241Z","shell.execute_reply.started":"2022-09-09T05:58:11.679919Z","shell.execute_reply":"2022-09-09T05:58:12.710080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot Between Accuracy & Loss","metadata":{}},{"cell_type":"code","source":"loss = model_history.history['loss']\nloss_val = model_history.history['val_loss']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, loss, label='loss_train')\nplt.plot(epochs, loss_val, label='loss_val')\nplt.title('value of the loss function')\nplt.xlabel('epochs')\nplt.ylabel('value of the loss function')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:51:01.145725Z","iopub.execute_input":"2022-09-09T05:51:01.146286Z","iopub.status.idle":"2022-09-09T05:51:01.370260Z","shell.execute_reply.started":"2022-09-09T05:51:01.146252Z","shell.execute_reply":"2022-09-09T05:51:01.369322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = model_history.history['categorical_accuracy']\nacc_val = model_history.history['val_categorical_accuracy']\nepochs = range(1, len(loss)+1)\nplt.plot(epochs, acc, label='accuracy_train')\nplt.plot(epochs, acc_val,  label='accuracy_val')\nplt.title('accuracy')\nplt.xlabel('epochs')\nplt.ylabel('value of accuracy')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:53:08.413189Z","iopub.execute_input":"2022-09-09T05:53:08.413566Z","iopub.status.idle":"2022-09-09T05:53:08.628956Z","shell.execute_reply.started":"2022-09-09T05:53:08.413537Z","shell.execute_reply":"2022-09-09T05:53:08.627972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_history.history.keys()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:54:10.493980Z","iopub.execute_input":"2022-09-09T05:54:10.494483Z","iopub.status.idle":"2022-09-09T05:54:10.503322Z","shell.execute_reply.started":"2022-09-09T05:54:10.494442Z","shell.execute_reply":"2022-09-09T05:54:10.502335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ntrain_acc = model_history.history[\"categorical_accuracy\"]\nval_acc = model_history.history[\"val_categorical_accuracy\"]\nepochs = range(1, len(train_acc)+1)\nplt.plot(epochs, train_acc, \"bo\", label = \"Training Accuracy\")\nplt.plot(epochs, val_acc, \"b\", label = \"Validation Accuracy\")\nplt.title(\"Training and Validation Accuracy\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:54:13.101550Z","iopub.execute_input":"2022-09-09T05:54:13.101905Z","iopub.status.idle":"2022-09-09T05:54:13.331994Z","shell.execute_reply.started":"2022-09-09T05:54:13.101876Z","shell.execute_reply":"2022-09-09T05:54:13.331043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(8,6))\ntrain_loss = model_history.history[\"loss\"]\nval_loss = model_history.history[\"val_loss\"]\nepochs = range(1, len(train_loss)+1)\nplt.plot(epochs, train_loss, \"bo\", label = \"Training Loss\")\nplt.plot(epochs, val_loss, \"b\", label = \"Validation Loss\")\nplt.title(\"Training and Validation Loss\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-09-09T05:54:31.470784Z","iopub.execute_input":"2022-09-09T05:54:31.471172Z","iopub.status.idle":"2022-09-09T05:54:31.717561Z","shell.execute_reply.started":"2022-09-09T05:54:31.471140Z","shell.execute_reply":"2022-09-09T05:54:31.716491Z"},"trusted":true},"execution_count":null,"outputs":[]}]}