{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30616,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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 seaborn as sns\n\n%matplotlib inline\nimport matplotlib.pyplot as plt\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\nfile_path=[]\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    print(dirname)\n    for filename in filenames:\n        file_path.append(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\n\n#/kaggle/input/dog-breed-identification/sample_submission.csv\n#/kaggle/input/dog-breed-identification/labels.csv\n#/kaggle/input/dog-breed-identification/test/","metadata":{"_uuid":"8ec6a422-59a3-4952-9c22-2e4687b2ff54","_cell_guid":"c3bf0c63-a621-4935-a3e3-8da8f0823e4d","jupyter":{"outputs_hidden":false},"collapsed":false,"execution":{"iopub.status.busy":"2024-03-17T08:02:07.920754Z","iopub.execute_input":"2024-03-17T08:02:07.921731Z","iopub.status.idle":"2024-03-17T08:02:25.504550Z","shell.execute_reply.started":"2024-03-17T08:02:07.921684Z","shell.execute_reply":"2024-03-17T08:02:25.503455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. Train Deep learning to Classify dog breeds\n2. Use Transfer learning to classify Dog breeds","metadata":{"_uuid":"e07bbec5-0911-4e01-9583-292ae0bc340d","_cell_guid":"b6899f52-d601-4e2a-b0e0-a38e5cc00dca","trusted":true}},{"cell_type":"markdown","source":"**Deep Learning to Classify dog breeds**","metadata":{"_uuid":"f377b83c-6823-4c92-9bf8-75600ca5481c","_cell_guid":"8d22693f-1f7e-42db-a327-ec13a0247b90","trusted":true}},{"cell_type":"code","source":"#file_path[0:10]","metadata":{"_uuid":"e3f301ba-2bfe-4a08-a637-d715f6e2e972","_cell_guid":"e7496373-b393-4613-a74e-1706af34f67a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:25.506884Z","iopub.execute_input":"2024-03-17T08:02:25.507845Z","iopub.status.idle":"2024-03-17T08:02:25.512759Z","shell.execute_reply.started":"2024-03-17T08:02:25.507802Z","shell.execute_reply":"2024-03-17T08:02:25.511486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Load file and check sample data (.head)\nsample=pd.read_csv(file_path[0])\nsample.head()","metadata":{"_uuid":"e8fd118d-4c4f-4572-91d3-1a5cbba1762b","_cell_guid":"029231f7-41b7-4a91-996b-e32403d29adc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:25.514182Z","iopub.execute_input":"2024-03-17T08:02:25.514515Z","iopub.status.idle":"2024-03-17T08:02:26.095942Z","shell.execute_reply.started":"2024-03-17T08:02:25.514487Z","shell.execute_reply":"2024-03-17T08:02:26.094706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Load labels file of the dog and check if there are any nulls values (.info)\nlabels=pd.read_csv(file_path[1])\nlabels.info()","metadata":{"_uuid":"d2496ee7-2a2c-4e53-90d1-8183e99ca810","_cell_guid":"9a12a5f9-767d-44fc-8159-f85ef73d71cd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:26.098507Z","iopub.execute_input":"2024-03-17T08:02:26.098864Z","iopub.status.idle":"2024-03-17T08:02:26.222785Z","shell.execute_reply.started":"2024-03-17T08:02:26.098833Z","shell.execute_reply":"2024-03-17T08:02:26.221694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"_uuid":"c3749e5a-682b-440f-aa09-0be4fe6a02c3","_cell_guid":"726e4862-2464-4fb5-bf79-2757fc45ab3e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:26.224148Z","iopub.execute_input":"2024-03-17T08:02:26.224490Z","iopub.status.idle":"2024-03-17T08:02:26.234524Z","shell.execute_reply.started":"2024-03-17T08:02:26.224461Z","shell.execute_reply":"2024-03-17T08:02:26.233332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['breed'].value_counts()","metadata":{"_uuid":"9491024f-66a5-487d-af8a-1a700d731a31","_cell_guid":"6fb71941-4e6d-4057-9d3e-e6204d523f53","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:26.236055Z","iopub.execute_input":"2024-03-17T08:02:26.236416Z","iopub.status.idle":"2024-03-17T08:02:26.250599Z","shell.execute_reply.started":"2024-03-17T08:02:26.236380Z","shell.execute_reply":"2024-03-17T08:02:26.249487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check the distribution of the of the difference classes of dogs\nplt.figure(figsize=(10,20))\nax=sns.countplot(y=labels['breed'],order=labels['breed'].value_counts().index.tolist())\nax.bar_label(ax.containers[0]) #show counts on bar graph\nplt.tight_layout()\nplt.show","metadata":{"_uuid":"7d352003-d522-465a-84c2-e4d1c46d4d3c","_cell_guid":"6dec6c51-576f-418b-a7aa-48b1254ab8ca","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:26.252155Z","iopub.execute_input":"2024-03-17T08:02:26.252527Z","iopub.status.idle":"2024-03-17T08:02:28.487769Z","shell.execute_reply.started":"2024-03-17T08:02:26.252498Z","shell.execute_reply":"2024-03-17T08:02:28.486911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Distribution of dog breed is uneven, to stratify the train test split to remove bias-ness","metadata":{"_uuid":"fcc93d55-2f15-452f-b696-ba36e61d9a8c","_cell_guid":"7a73b1e9-b07f-46cf-a16f-c1ca41b7ad3a","trusted":true}},{"cell_type":"code","source":"X=labels['id'].copy()\ny=labels['breed'].copy()\nX='/kaggle/input/dog-breed-identification/train/'+X+'.jpg'","metadata":{"_uuid":"efb1ba4a-644b-47be-a364-af344cd9a7af","_cell_guid":"e4d765ef-1bb5-447f-9739-44456715960b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:28.489317Z","iopub.execute_input":"2024-03-17T08:02:28.489995Z","iopub.status.idle":"2024-03-17T08:02:28.500761Z","shell.execute_reply.started":"2024-03-17T08:02:28.489962Z","shell.execute_reply":"2024-03-17T08:02:28.499564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['id']='/kaggle/input/dog-breed-identification/train/'+labels['id']+'.jpg'","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:28.502203Z","iopub.execute_input":"2024-03-17T08:02:28.502931Z","iopub.status.idle":"2024-03-17T08:02:28.512473Z","shell.execute_reply.started":"2024-03-17T08:02:28.502886Z","shell.execute_reply":"2024-03-17T08:02:28.511283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:28.517039Z","iopub.execute_input":"2024-03-17T08:02:28.518016Z","iopub.status.idle":"2024-03-17T08:02:28.527542Z","shell.execute_reply.started":"2024-03-17T08:02:28.517964Z","shell.execute_reply":"2024-03-17T08:02:28.526094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check a random picture\ni=np.random.randint(len(X))\nfig = plt.figure(figsize=(10, 10))\nimg = plt.imread(X[i])\nplt.axis('off')\nplt.title(y[i])\nplt.imshow(img)","metadata":{"_uuid":"2f9c4f8c-936c-4ba7-a666-a5ee42d5c3f6","_cell_guid":"b66bf887-90db-497d-8d46-9340052f2f2e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:28.528866Z","iopub.execute_input":"2024-03-17T08:02:28.529946Z","iopub.status.idle":"2024-03-17T08:02:29.091303Z","shell.execute_reply.started":"2024-03-17T08:02:28.529901Z","shell.execute_reply":"2024-03-17T08:02:29.090348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_L=pd.concat([X,y],axis=1)","metadata":{"_uuid":"89952cbf-963b-44b2-a147-03e288e56663","_cell_guid":"2d6086d0-64a9-46e0-a393-f744fa7cfdf1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:29.092682Z","iopub.execute_input":"2024-03-17T08:02:29.093279Z","iopub.status.idle":"2024-03-17T08:02:29.098509Z","shell.execute_reply.started":"2024-03-17T08:02:29.093234Z","shell.execute_reply":"2024-03-17T08:02:29.097641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot multiple dogs to see the images\nfig = plt.figure(figsize=(10, 10))\n\nfor i in range(1,10,1):\n    ax = plt.subplot(3, 3, i)\n    g=np.random.randint(len(X))\n    img=plt.imread(labels_L['id'][g])\n    plt.title(labels_L['breed'][g])\n    plt.axis(\"off\")\n    plt.imshow(img)\nplt.show()","metadata":{"_uuid":"acddac7c-9c33-4f37-8357-75e7b86ab0b6","_cell_guid":"9f17b487-2e8a-4721-9cf7-67c42efa2f88","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:29.099624Z","iopub.execute_input":"2024-03-17T08:02:29.100422Z","iopub.status.idle":"2024-03-17T08:02:30.310024Z","shell.execute_reply.started":"2024-03-17T08:02:29.100389Z","shell.execute_reply":"2024-03-17T08:02:30.308803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#train val split for Training set 0.2, stratify=y\nfrom sklearn.model_selection import train_test_split\n\nX_train,X_valid,y_train,y_valid=train_test_split(X,y,test_size=0.2,stratify=y)","metadata":{"_uuid":"f71482ff-a7cb-433a-ad6e-3eeb80a4b363","_cell_guid":"cbd8e4ad-42ac-4146-813f-6cf3cf6f8526","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-02T10:08:26.759909Z","iopub.execute_input":"2024-03-02T10:08:26.760443Z","iopub.status.idle":"2024-03-02T10:08:26.996163Z","shell.execute_reply.started":"2024-03-02T10:08:26.760411Z","shell.execute_reply":"2024-03-02T10:08:26.994768Z"}}},{"cell_type":"markdown","source":"#Verify stratify in train test split\nstatify_check=pd.concat([labels['breed'].value_counts(),pd.concat([X_train,labels],axis=1,\n                                                                  join='inner')['breed'].value_counts()],axis=1)\nstatify_check['reduction']=(statify_check.iloc[:,0]-statify_check.iloc[:,1])/(statify_check.iloc[:,0])\nstatify_check['reduction'].describe()","metadata":{"_uuid":"4adccb5e-35cb-419f-a68f-20956630f3c1","_cell_guid":"92bf3027-9afd-4434-81dc-90f91646b736","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-02T10:08:26.999906Z","iopub.execute_input":"2024-03-02T10:08:27.000694Z","iopub.status.idle":"2024-03-02T10:08:27.029676Z","shell.execute_reply.started":"2024-03-02T10:08:27.000644Z","shell.execute_reply":"2024-03-02T10:08:27.028822Z"}}},{"cell_type":"markdown","source":"#join back X and y for train and test so we can use flow_from_dataframe in datagen\ntrain_strat=pd.concat([X_train,y_train],axis=1)\ntest_strat=pd.concat([X_valid,y_valid],axis=1)","metadata":{"_uuid":"3478d245-98df-487c-9024-93a460c5b32a","_cell_guid":"e8cfcfe8-5934-48fe-96ed-b59ed072315c","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-02T10:12:18.767758Z","iopub.execute_input":"2024-03-02T10:12:18.768152Z","iopub.status.idle":"2024-03-02T10:12:18.776824Z","shell.execute_reply.started":"2024-03-02T10:12:18.768114Z","shell.execute_reply":"2024-03-02T10:12:18.775680Z"}}},{"cell_type":"code","source":"#found a faster way to stratify\nfrom sklearn.model_selection import train_test_split\ntrain_strat, test_strat = train_test_split(labels, test_size=0.2, stratify=labels['breed'])","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:30.311699Z","iopub.execute_input":"2024-03-17T08:02:30.312085Z","iopub.status.idle":"2024-03-17T08:02:30.527746Z","shell.execute_reply.started":"2024-03-17T08:02:30.312050Z","shell.execute_reply":"2024-03-17T08:02:30.526536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n#preprocessing augmentation\ntrain_datagen = ImageDataGenerator(rescale=1/255,\n                                    zoom_range=0.2,\n                                    rotation_range=20,\n                                    width_shift_range=0.2,\n                                    height_shift_range=0.2,\n                                    horizontal_flip=True)\n\nvalidation_datagen = ImageDataGenerator(rescale=1/255)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_strat,\n    x_col='id',\n    y_col='breed',\n    target_size=(512,512), \n    batch_size=20,\n    class_mode='categorical')\n\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    dataframe=test_strat,\n    x_col='id',\n    y_col='breed',\n    target_size=(512,512), \n    batch_size=5,\n    class_mode='categorical')","metadata":{"_uuid":"3928940d-ccc3-475f-97fb-80823830767d","_cell_guid":"e527cdc9-3983-47e1-b0f5-13aab2c4f7a9","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-13T05:44:37.649135Z","iopub.status.idle":"2024-03-13T05:44:37.649548Z","shell.execute_reply.started":"2024-03-13T05:44:37.649362Z","shell.execute_reply":"2024-03-13T05:44:37.649381Z"}}},{"cell_type":"markdown","source":"import tensorflow as tf\n\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(512,512, 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(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(256, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    #tf.keras.layers.BatchNormalization(momentum=0.9),\n    #tf.keras.layers.Conv2D(256, (3,3), activation='relu'),\n    #tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Flatten(),\n\n    tf.keras.layers.Dense(256, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(256, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n\n    tf.keras.layers.Dense(120, activation='softmax')\n    ])\nmodel.summary()\n\nmodel.compile(loss='categorical_crossentropy',\noptimizer='adam',\nmetrics=['accuracy'])","metadata":{"_uuid":"6a9b47d5-ab5f-434b-9098-17cbcc9c2e39","_cell_guid":"c9ff260f-a0c5-463e-96ec-912fc3fcffb3","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-13T05:44:37.650835Z","iopub.status.idle":"2024-03-13T05:44:37.652239Z","shell.execute_reply.started":"2024-03-13T05:44:37.652032Z","shell.execute_reply":"2024-03-13T05:44:37.652055Z"}}},{"cell_type":"markdown","source":"history = model.fit(\ntrain_generator,\nsteps_per_epoch=10,\nepochs=10,\nverbose=1,\nvalidation_data = validation_generator\n#,validation_steps=8\n)\n# #model.save('model.h5')","metadata":{"_uuid":"7b03904a-3db5-4697-9de6-634eca3058d5","_cell_guid":"fabc281f-e044-4c9c-8412-87c554065364","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-10T07:27:13.863147Z","iopub.execute_input":"2024-01-10T07:27:13.863528Z","iopub.status.idle":"2024-01-10T08:03:54.133520Z","shell.execute_reply.started":"2024-01-10T07:27:13.863493Z","shell.execute_reply":"2024-01-10T08:03:54.132604Z"}}},{"cell_type":"markdown","source":"Optimising the parameters of the training model such as, \n1. image size  \n2. adding preprocessing augmentation  \n3. increasing layers  \n\ndoesn't significantly improve results and accuracy usually never crosses 2%  \nThis is likely due to the low number of samples, from the count plot above we can see that the lowest sample count is 66 and after removing 12 for train_test_split (validation) only 54 is left for training which should be insufficient to build an model.  \nquick check in google shows 5000 images is a good number for reliable training  \nTherefore, we have to explore using transfer learning instead","metadata":{"_uuid":"271289c8-c5ec-4f89-b6bf-121713cd84d8","_cell_guid":"9aa071cc-596c-4da9-9370-16995cf8fb4c","trusted":true}},{"cell_type":"markdown","source":"**Transfer Learning**  \nTrain model using transfer learning","metadata":{"_uuid":"5861e911-b071-479c-8a60-a6b367fb14f6","_cell_guid":"ec7f0ab0-d44a-4c09-a31a-01d4402a2c09","trusted":true}},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import MobileNetV2,ResNet50, InceptionV3\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping\n\n# Constants and hyperTune parameters\nIMG_SIZE = 224\nBATCH_SIZE = 32\nepochs=15\nl=0.001\n","metadata":{"_uuid":"cb5a1e37-85dd-44b3-b5a0-0106a6ff8ba1","_cell_guid":"cf2b54c7-2f02-4ae5-83d7-5e7190015a64","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:30.529472Z","iopub.execute_input":"2024-03-17T08:02:30.530227Z","iopub.status.idle":"2024-03-17T08:02:43.786924Z","shell.execute_reply.started":"2024-03-17T08:02:30.530186Z","shell.execute_reply":"2024-03-17T08:02:43.785676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Augmentation and Preprocessing (sample from chatgpt)\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nvalidation_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:43.788479Z","iopub.execute_input":"2024-03-17T08:02:43.789938Z","iopub.status.idle":"2024-03-17T08:02:43.796833Z","shell.execute_reply.started":"2024-03-17T08:02:43.789880Z","shell.execute_reply":"2024-03-17T08:02:43.795129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train val split for Training set 0.2, stratify=y\nfrom sklearn.model_selection import train_test_split\n\ntrain_strat,valid_strat=train_test_split(labels,test_size=0.2,stratify=y)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:43.798362Z","iopub.execute_input":"2024-03-17T08:02:43.799092Z","iopub.status.idle":"2024-03-17T08:02:43.858537Z","shell.execute_reply.started":"2024-03-17T08:02:43.799054Z","shell.execute_reply":"2024-03-17T08:02:43.857568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data preprocessing, augment for train data\ndef train_data_preprocessing(DataFrame,x_column='id',y_column='breed',img_size=224,batch_size=32):\n    train_generator = train_datagen.flow_from_dataframe(\n        dataframe=DataFrame,\n        x_col=x_column,\n        y_col=y_column,\n        target_size=(IMG_SIZE,IMG_SIZE), \n        batch_size=batch_size,\n        class_mode='categorical')\n    return train_generator\n\ndef test_data_preprocessing(DataFrame,x_column='id',y_column='breed',img_size=224,batch_size=32):\n    validation_generator = validation_datagen.flow_from_dataframe(\n        dataframe=DataFrame,\n        x_col=x_column,\n        y_col=y_column,\n        target_size=(IMG_SIZE,IMG_SIZE), \n        batch_size=batch_size,\n        class_mode='categorical')\n    return validation_generator","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:43.859781Z","iopub.execute_input":"2024-03-17T08:02:43.860439Z","iopub.status.idle":"2024-03-17T08:02:43.868379Z","shell.execute_reply.started":"2024-03-17T08:02:43.860405Z","shell.execute_reply":"2024-03-17T08:02:43.867144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator=train_data_preprocessing(train_strat,img_size=IMG_SIZE,batch_size=BATCH_SIZE)\nvalidation_generator=test_data_preprocessing(test_strat,img_size=IMG_SIZE,batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:43.869610Z","iopub.execute_input":"2024-03-17T08:02:43.870008Z","iopub.status.idle":"2024-03-17T08:02:47.545643Z","shell.execute_reply.started":"2024-03-17T08:02:43.869976Z","shell.execute_reply":"2024-03-17T08:02:47.544552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load pre-trained models\nMobileNetV2_base = MobileNetV2(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3))\n#resnet50_base = ResNet50(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3))\ninceptionv3_base = InceptionV3(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3))\n\n# Freeze pre-trained layers\nfor base_model in [MobileNetV2_base, inceptionv3_base]:\n    for layer in base_model.layers:\n        layer.trainable = False","metadata":{"_uuid":"52d3913f-9b71-4dd9-8ba3-5ea291f7956a","_cell_guid":"ddd3625b-b95a-418d-a38f-93ea363856ef","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:47.547014Z","iopub.execute_input":"2024-03-17T08:02:47.547346Z","iopub.status.idle":"2024-03-17T08:02:53.333477Z","shell.execute_reply.started":"2024-03-17T08:02:47.547317Z","shell.execute_reply":"2024-03-17T08:02:53.332286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Custom classifier function\ndef create_model(base_model,learning_rate=0.001):\n    x = base_model.output\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(256, activation='relu')(x)\n    predictions = Dense(len(train_generator.class_indices), activation='softmax')(x)\n    model = Model(inputs=base_model.input, outputs=predictions)\n    model.compile(optimizer=Adam(learning_rate), loss='categorical_crossentropy', metrics=['accuracy'])\n    return model","metadata":{"_uuid":"25b86a78-6645-4e3d-bbd7-05c7c76acbcc","_cell_guid":"77eb2bfd-2517-482e-a430-11429d4bf4d3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:53.334680Z","iopub.execute_input":"2024-03-17T08:02:53.334996Z","iopub.status.idle":"2024-03-17T08:02:53.341740Z","shell.execute_reply.started":"2024-03-17T08:02:53.334968Z","shell.execute_reply":"2024-03-17T08:02:53.340624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Instantiate EarlyStopping callback\nearly_stopping = EarlyStopping(monitor='val_accuracy', patience=3, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:53.343149Z","iopub.execute_input":"2024-03-17T08:02:53.343597Z","iopub.status.idle":"2024-03-17T08:02:53.352561Z","shell.execute_reply.started":"2024-03-17T08:02:53.343559Z","shell.execute_reply":"2024-03-17T08:02:53.351372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(tmodel,train_generator,validation_generator,early_stopping,l=0.001,BATCH_SIZE=32,epochs=10):\n    print(f\"Current model: {tmodel}\\nCurrent Model parameters:\\n \\\n        learning rate = {l}\\n \\\n        epochs={epochs} \\n \\\n        Current Batch Size: {BATCH_SIZE}\")\n    model=create_model(tmodel,learning_rate=l)\n    history = model.fit(\n    train_generator,\n    steps_per_epoch=BATCH_SIZE,\n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=BATCH_SIZE//2,\n    callbacks=[early_stopping]\n    )\n    return history,model","metadata":{"_uuid":"05ac1617-ad58-485a-8ac4-7e0714b72ad4","_cell_guid":"8a316b85-841e-4125-b4a4-4a3fb672f236","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-17T08:02:53.354154Z","iopub.execute_input":"2024-03-17T08:02:53.355225Z","iopub.status.idle":"2024-03-17T08:02:53.368103Z","shell.execute_reply.started":"2024-03-17T08:02:53.355192Z","shell.execute_reply":"2024-03-17T08:02:53.367024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_model_history(history):\n    # Retrieve training and validation loss and accuracy values from the history object\n    training_loss = history.history['loss']\n    validation_loss = history.history['val_loss']\n    training_acc = history.history['accuracy']\n    validation_acc = history.history['val_accuracy']\n    \n    # Get the number of epochs\n    epochs = range(1, len(training_loss) + 1)\n    \n    # Plotting training and validation loss\n    plt.figure(figsize=(12, 5))\n    \n    # Plotting Loss\n    plt.subplot(1, 2, 1)\n    plt.plot(epochs, training_loss, 'b', label='Training Loss')\n    plt.plot(epochs, validation_loss, 'r', label='Validation Loss')\n    plt.title('Training and Validation Loss')\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend()\n    plt.grid(True)\n    \n    # Plotting Accuracy\n    plt.subplot(1, 2, 2)\n    plt.plot(epochs, training_acc, 'b', label='Training Accuracy')\n    plt.plot(epochs, validation_acc, 'r', label='Validation Accuracy')\n    plt.title('Training and Validation Accuracy')\n    plt.xlabel('Epochs')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    plt.grid(True)\n    \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:53.369526Z","iopub.execute_input":"2024-03-17T08:02:53.370322Z","iopub.status.idle":"2024-03-17T08:02:53.381478Z","shell.execute_reply.started":"2024-03-17T08:02:53.370274Z","shell.execute_reply":"2024-03-17T08:02:53.380696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"IMG_SIZE = (224, 224)\nBATCH_SIZE = 32\nepochs=15\nl=0.001\n","metadata":{}},{"cell_type":"code","source":"MobileNetV2_history,MobileNetV2_model=train_model(MobileNetV2_base,train_generator,validation_generator,early_stopping=early_stopping,l=l,BATCH_SIZE=BATCH_SIZE,epochs=epochs)\nplot_model_history(MobileNetV2_history)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:02:53.383102Z","iopub.execute_input":"2024-03-17T08:02:53.383838Z","iopub.status.idle":"2024-03-17T08:14:44.899091Z","shell.execute_reply.started":"2024-03-17T08:02:53.383800Z","shell.execute_reply":"2024-03-17T08:14:44.898205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inceptionv3_history,inceptionv3_model=train_model(inceptionv3_base,train_generator,validation_generator,early_stopping=early_stopping,l=l,BATCH_SIZE=BATCH_SIZE,epochs=epochs)\nplot_model_history(inceptionv3_history)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T08:14:44.900379Z","iopub.execute_input":"2024-03-17T08:14:44.900692Z","iopub.status.idle":"2024-03-17T08:37:52.731597Z","shell.execute_reply.started":"2024-03-17T08:14:44.900664Z","shell.execute_reply":"2024-03-17T08:37:52.730314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"HyperTune parameters","metadata":{}},{"cell_type":"markdown","source":"img_path = os.path.join(test_folder_path, '09e51e17e2b756ff2ace8a87bd1443fa.jpg')\nimg = image.load_img(img_path, target_size=(IMG_SIZE, IMG_SIZE))\nimg_array = image.img_to_array(img)\nimg_array = np.expand_dims(img_array, axis=0)\nimg_array = img_array / 255.0  # Rescale pixel values\npredictions_MobileNetV2.append(MobileNetV2_model.predict(img_array))","metadata":{"execution":{"iopub.status.busy":"2024-03-15T09:18:05.802232Z","iopub.execute_input":"2024-03-15T09:18:05.802607Z","iopub.status.idle":"2024-03-15T09:18:05.885916Z","shell.execute_reply.started":"2024-03-15T09:18:05.802578Z","shell.execute_reply":"2024-03-15T09:18:05.884771Z"}}},{"cell_type":"code","source":"# Make predictions on test images and save results to a CSV file\ntest_images = []\nfile_names = []\npredictions_MobileNetV2=[]\npredictions_inceptionv3=[]\ntest_folder_path='/kaggle/input/dog-breed-identification/test'\nfor filename in os.listdir(test_folder_path):\n    img_path = os.path.join(test_folder_path, filename)\n    img = image.load_img(img_path, target_size=(IMG_SIZE, IMG_SIZE))\n    img_array = image.img_to_array(img)\n    img_array = np.expand_dims(img_array, axis=0)\n    img_array = img_array / 255.0  # Normalize pixel values\n    predictions_MobileNetV2.append(MobileNetV2_model.predict(img_array))\n    predictions_inceptionv3.append(inceptionv3_model.predict(img_array))\n    file_names.append(filename)","metadata":{"_uuid":"4cbbf98b-7f70-4851-b77d-3be2f2273ae2","_cell_guid":"aa214903-56c2-42f1-a954-0afff3d85fa1","execution":{"iopub.status.busy":"2024-03-17T08:37:52.733620Z","iopub.execute_input":"2024-03-17T08:37:52.734827Z","iopub.status.idle":"2024-03-17T09:20:17.523247Z","shell.execute_reply.started":"2024-03-17T08:37:52.734788Z","shell.execute_reply":"2024-03-17T09:20:17.522466Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert images to a numpy array\ntest_images = np.array(test_images)","metadata":{"execution":{"iopub.status.busy":"2024-03-15T02:29:20.425753Z","iopub.execute_input":"2024-03-15T02:29:20.426080Z","iopub.status.idle":"2024-03-15T02:29:21.458201Z","shell.execute_reply.started":"2024-03-15T02:29:20.426053Z","shell.execute_reply":"2024-03-15T02:29:21.456613Z"}}},{"cell_type":"markdown","source":"# Make predictions using each model\npredictions_MobileNetV2 = MobileNetV2_model.predict(test_images)\npredictions_inceptionv3 = inceptionv3_model.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2024-03-15T02:29:21.465899Z","iopub.execute_input":"2024-03-15T02:29:21.466545Z"}}},{"cell_type":"code","source":"predictions_MobileNetV2_array = np.array(predictions_MobileNetV2)\npredictions_MobileNetV2_array=predictions_MobileNetV2_array.reshape(-1, predictions_MobileNetV2_array.shape[-1])\n","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:05:22.351247Z","iopub.execute_input":"2024-03-17T10:05:22.351967Z","iopub.status.idle":"2024-03-17T10:05:22.398507Z","shell.execute_reply.started":"2024-03-17T10:05:22.351924Z","shell.execute_reply":"2024-03-17T10:05:22.397433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_inceptionv3_array = np.array(predictions_inceptionv3)\npredictions_inceptionv3_array=predictions_inceptionv3_array.reshape(-1, predictions_inceptionv3_array.shape[-1])\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensemble_pred =(predictions_MobileNetV2_array + predictions_inceptionv3_array) / 2","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:05:23.472099Z","iopub.execute_input":"2024-03-17T10:05:23.472540Z","iopub.status.idle":"2024-03-17T10:05:23.481537Z","shell.execute_reply.started":"2024-03-17T10:05:23.472505Z","shell.execute_reply":"2024-03-17T10:05:23.480309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensemble_pred_submission=pd.DataFrame(ensemble_pred,index=[x.rstrip(\".jpg\") for x in file_names],columns=train_generator.class_indices)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:14:17.861448Z","iopub.execute_input":"2024-03-17T10:14:17.861856Z","iopub.status.idle":"2024-03-17T10:14:17.868858Z","shell.execute_reply.started":"2024-03-17T10:14:17.861823Z","shell.execute_reply":"2024-03-17T10:14:17.868128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensemble_pred_submission=ensemble_pred_submission.reset_index().rename(columns={'index':'id'})","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:14:18.100793Z","iopub.execute_input":"2024-03-17T10:14:18.101855Z","iopub.status.idle":"2024-03-17T10:14:18.113181Z","shell.execute_reply.started":"2024-03-17T10:14:18.101812Z","shell.execute_reply":"2024-03-17T10:14:18.112233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensemble_pred_submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-17T10:14:20.780372Z","iopub.execute_input":"2024-03-17T10:14:20.780748Z","iopub.status.idle":"2024-03-17T10:14:22.327184Z","shell.execute_reply.started":"2024-03-17T10:14:20.780718Z","shell.execute_reply":"2024-03-17T10:14:22.326301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Decode predictions if needed\ndef decode_predictions(predictions, labels):\n    top_classes = labels[np.argsort(predictions[0])[::-1][:5]]  # Get top 5 predicted classes\n    return top_classes","metadata":{"_uuid":"813c3795-1215-4e08-ae15-fa7a4df18fde","_cell_guid":"4fcd2830-bbc3-4dbb-acd9-777640b02f7e","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-10T09:45:10.660084Z","iopub.status.idle":"2024-01-10T09:45:10.660943Z","shell.execute_reply.started":"2024-01-10T09:45:10.660662Z","shell.execute_reply":"2024-01-10T09:45:10.660689Z"}}},{"cell_type":"code","source":"# Save predictions to a CSV file\n#predictions.to_csv('/kaggle/working/submissions.csv', index=False)","metadata":{"_uuid":"6006ccde-8078-4424-9337-76a535e5483e","_cell_guid":"db0d0f52-6b23-491d-8deb-7f92502769dc","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the top predicted classes from ensemble predictions\n#top_classes_ensemble = decode_predictions(ensemble_predictions, dog_breeds_labels)","metadata":{"_uuid":"cea59834-cc60-4e4d-a927-42ccdbdaa9ab","_cell_guid":"fc1faf59-f7e3-4921-bf37-03caf3049114","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(\"Top 5 predicted classes using Ensemble:\", top_classes_ensemble)","metadata":{"_uuid":"e52e4600-6d26-42e0-af97-2bc08803fb80","_cell_guid":"aa61cd75-6d51-4f58-a335-fd21ece5861d","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}