{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:55:45.997122Z","iopub.execute_input":"2023-10-08T11:55:45.997916Z","iopub.status.idle":"2023-10-08T11:55:52.788001Z","shell.execute_reply.started":"2023-10-08T11:55:45.997885Z","shell.execute_reply":"2023-10-08T11:55:52.786987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ndf.sample(frac=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:55:52.789562Z","iopub.execute_input":"2023-10-08T11:55:52.790438Z","iopub.status.idle":"2023-10-08T11:55:52.850937Z","shell.execute_reply.started":"2023-10-08T11:55:52.790402Z","shell.execute_reply":"2023-10-08T11:55:52.850074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['id'] = df['id']+'.jpg'\ndf.sample(5)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:55:52.852582Z","iopub.execute_input":"2023-10-08T11:55:52.853219Z","iopub.status.idle":"2023-10-08T11:55:52.869701Z","shell.execute_reply.started":"2023-10-08T11:55:52.853186Z","shell.execute_reply":"2023-10-08T11:55:52.868881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.DataFrame([(df['breed'].value_counts().index),(df['breed'].value_counts().values)]).T\ndata= data.rename(columns={0:'breed',1:'count'})\ndata","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:55:52.872101Z","iopub.execute_input":"2023-10-08T11:55:52.873029Z","iopub.status.idle":"2023-10-08T11:55:52.890934Z","shell.execute_reply.started":"2023-10-08T11:55:52.872997Z","shell.execute_reply":"2023-10-08T11:55:52.889787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['weight'] = (1/data['count'])*(10222/120)\ndata","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:55:52.892739Z","iopub.execute_input":"2023-10-08T11:55:52.893377Z","iopub.status.idle":"2023-10-08T11:55:52.906795Z","shell.execute_reply.started":"2023-10-08T11:55:52.893319Z","shell.execute_reply":"2023-10-08T11:55:52.905885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idg = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range= 30,\n                                                      width_shift_range=0.2, height_shift_range=0.2,\n                                                      brightness_range=(0.6,1.5),\n                                                      zoom_range=(0.8,1.2), horizontal_flip=True,\n                                                      validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:55:52.907803Z","iopub.execute_input":"2023-10-08T11:55:52.908373Z","iopub.status.idle":"2023-10-08T11:55:52.913866Z","shell.execute_reply.started":"2023-10-08T11:55:52.908345Z","shell.execute_reply":"2023-10-08T11:55:52.913041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:55:52.915369Z","iopub.execute_input":"2023-10-08T11:55:52.916087Z","iopub.status.idle":"2023-10-08T11:55:52.922072Z","shell.execute_reply.started":"2023-10-08T11:55:52.916059Z","shell.execute_reply":"2023-10-08T11:55:52.921161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_idg = idg.flow_from_dataframe(df, '/kaggle/input/dog-breed-identification/train', 'id', 'breed',\n                                    target_size=(350,500), batch_size = batch_size,\n                                    shuffle=False, subset='training')","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:55:52.923834Z","iopub.execute_input":"2023-10-08T11:55:52.924661Z","iopub.status.idle":"2023-10-08T11:56:16.699600Z","shell.execute_reply.started":"2023-10-08T11:55:52.924633Z","shell.execute_reply":"2023-10-08T11:56:16.698745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_idg = idg.flow_from_dataframe(df, '/kaggle/input/dog-breed-identification/train', 'id', 'breed',\n                                    target_size=(350,500), batch_size = batch_size,\n                                    shuffle=False, subset='validation')","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:56:16.700811Z","iopub.execute_input":"2023-10-08T11:56:16.701622Z","iopub.status.idle":"2023-10-08T11:56:21.608724Z","shell.execute_reply.started":"2023-10-08T11:56:16.701589Z","shell.execute_reply":"2023-10-08T11:56:21.607742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cw = pd.DataFrame.from_dict(train_idg.class_indices,orient='index').reset_index()\ncw = cw.rename(columns={'index':'breed',0:'class_index'})\ncw = data.merge(cw,how='left',left_on='breed',right_on='breed')\ncw.index = cw['class_index']\ncw = cw.drop(columns=['class_index'])\ncw = cw['weight']\ncw = cw.to_dict()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:56:21.612597Z","iopub.execute_input":"2023-10-08T11:56:21.612851Z","iopub.status.idle":"2023-10-08T11:56:21.627705Z","shell.execute_reply.started":"2023-10-08T11:56:21.612829Z","shell.execute_reply":"2023-10-08T11:56:21.626697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input = tf.keras.layers.Input((350,500,3), name='Input_Layer')\n\nvgg_pp =  tf.keras.layers.Lambda(tf.keras.applications.vgg16.preprocess_input, output_shape = (350,500,3), name= \"VGG_Preprocess\") (input)\nvgg_model = tf.keras.applications.vgg16.VGG16(include_top=False, input_shape=(350,500,3))\nvgg_model.trainable = False\nvgg = vgg_model (vgg_pp)\nvgg_gap  = tf.keras.layers.GlobalAveragePooling2D(name='VGG_GAP') (vgg)\n\nresnet_pp = tf.keras.layers.Lambda(tf.keras.applications.resnet_v2.preprocess_input, output_shape = (350,500,3), name = 'Resnet_Prerpocess') (input)\nresnet_model = tf.keras.applications.ResNet50V2(include_top=False, input_shape=(350,500,3))\nresnet_model.trainable = False\nresnet = resnet_model (resnet_pp)\nresnet_gap =  tf.keras.layers.GlobalAveragePooling2D(name='Resnet_GAP') (resnet)\n\nconcat = tf.keras.layers.Concatenate(name='Concat') ([vgg_gap, resnet_gap])\ndropout = tf.keras.layers.Dropout(0.4, name='Dropout') (concat)\n\ndense = tf.keras.layers.Dense(1024, activation = 'relu', name = 'Hidden_Layer') (dropout)\noutput = tf.keras.layers.Dense(120, activation= 'softmax', name='Output') (dense)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:56:21.629330Z","iopub.execute_input":"2023-10-08T11:56:21.629636Z","iopub.status.idle":"2023-10-08T11:56:34.978753Z","shell.execute_reply.started":"2023-10-08T11:56:21.629607Z","shell.execute_reply":"2023-10-08T11:56:34.977873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Model(inputs=input, outputs=output)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:56:34.979813Z","iopub.execute_input":"2023-10-08T11:56:34.980125Z","iopub.status.idle":"2023-10-08T11:56:34.989294Z","shell.execute_reply.started":"2023-10-08T11:56:34.980092Z","shell.execute_reply":"2023-10-08T11:56:34.988295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:56:34.990424Z","iopub.execute_input":"2023-10-08T11:56:34.991394Z","iopub.status.idle":"2023-10-08T11:56:35.035748Z","shell.execute_reply.started":"2023-10-08T11:56:34.991364Z","shell.execute_reply":"2023-10-08T11:56:35.035004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),\n              loss = tf.keras.losses.categorical_crossentropy,\n              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:56:35.036650Z","iopub.execute_input":"2023-10-08T11:56:35.037071Z","iopub.status.idle":"2023-10-08T11:56:35.061277Z","shell.execute_reply.started":"2023-10-08T11:56:35.037020Z","shell.execute_reply":"2023-10-08T11:56:35.060468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf_callbacks = tf.keras.callbacks.TensorBoard(log_dir='logs')","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:56:35.062363Z","iopub.execute_input":"2023-10-08T11:56:35.063042Z","iopub.status.idle":"2023-10-08T11:56:35.067196Z","shell.execute_reply.started":"2023-10-08T11:56:35.063005Z","shell.execute_reply":"2023-10-08T11:56:35.066399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf_mod_call = tf.keras.callbacks.ModelCheckpoint('Dog_Breed_Model',monitor='val_loss', save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:56:35.068422Z","iopub.execute_input":"2023-10-08T11:56:35.069305Z","iopub.status.idle":"2023-10-08T11:56:35.076889Z","shell.execute_reply.started":"2023-10-08T11:56:35.069275Z","shell.execute_reply":"2023-10-08T11:56:35.075918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_idg, batch_size=batch_size, epochs = 20, validation_data=val_idg, callbacks=[tf_mod_call],class_weight= cw)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T11:56:35.078145Z","iopub.execute_input":"2023-10-08T11:56:35.078775Z","iopub.status.idle":"2023-10-08T14:50:28.642354Z","shell.execute_reply.started":"2023-10-08T11:56:35.078740Z","shell.execute_reply":"2023-10-08T14:50:28.641282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,5))\nplt.suptitle('Batch_size=32, (350X500),Adam LR=0.0001, Dropout=0.4')\nplt.subplot(121)\nplt.plot(model.history.history['accuracy'], label='accuracy')\nplt.plot(model.history.history['val_accuracy'], label='val_accuracy')\nplt.legend()\nplt.subplot(122)\nplt.plot(model.history.history['loss'], label='loss')\nplt.plot(model.history.history['val_loss'], label='val_loss')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:50:28.643756Z","iopub.execute_input":"2023-10-08T14:50:28.644850Z","iopub.status.idle":"2023-10-08T14:50:29.118552Z","shell.execute_reply.started":"2023-10-08T14:50:28.644815Z","shell.execute_reply":"2023-10-08T14:50:29.117687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model = tf.keras.models.load_model('/kaggle/working/Dog_Breed_Model')","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:50:29.119530Z","iopub.execute_input":"2023-10-08T14:50:29.119831Z","iopub.status.idle":"2023-10-08T14:50:37.923727Z","shell.execute_reply.started":"2023-10-08T14:50:29.119801Z","shell.execute_reply":"2023-10-08T14:50:37.922778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_val = final_model.predict(val_idg)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:50:37.943906Z","iopub.execute_input":"2023-10-08T14:50:37.944191Z","iopub.status.idle":"2023-10-08T14:51:28.922643Z","shell.execute_reply.started":"2023-10-08T14:50:37.944166Z","shell.execute_reply":"2023-10-08T14:51:28.921639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_val_argmax = np.argmax(result_val, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:28.923986Z","iopub.execute_input":"2023-10-08T14:51:28.924331Z","iopub.status.idle":"2023-10-08T14:51:28.929465Z","shell.execute_reply.started":"2023-10-08T14:51:28.924300Z","shell.execute_reply":"2023-10-08T14:51:28.928123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = val_idg.labels\ny_pred = result_val_argmax","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:28.930817Z","iopub.execute_input":"2023-10-08T14:51:28.931935Z","iopub.status.idle":"2023-10-08T14:51:28.938800Z","shell.execute_reply.started":"2023-10-08T14:51:28.931904Z","shell.execute_reply":"2023-10-08T14:51:28.937787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(y_pred == y_true).mean()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:28.940023Z","iopub.execute_input":"2023-10-08T14:51:28.941038Z","iopub.status.idle":"2023-10-08T14:51:28.949873Z","shell.execute_reply.started":"2023-10-08T14:51:28.940956Z","shell.execute_reply":"2023-10-08T14:51:28.948963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_idg.class_indices","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:28.951415Z","iopub.execute_input":"2023-10-08T14:51:28.952131Z","iopub.status.idle":"2023-10-08T14:51:28.962767Z","shell.execute_reply.started":"2023-10-08T14:51:28.952097Z","shell.execute_reply":"2023-10-08T14:51:28.961782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://cdn.britannica.com/79/79479-050-DEB83D3E/Sealyham-terrier.jpg","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:28.964305Z","iopub.execute_input":"2023-10-08T14:51:28.964998Z","iopub.status.idle":"2023-10-08T14:51:31.300826Z","shell.execute_reply.started":"2023-10-08T14:51:28.964967Z","shell.execute_reply":"2023-10-08T14:51:31.299685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = tf.keras.preprocessing.image.load_img('/kaggle/working/Sealyham-terrier.jpg', target_size=(350, 500))\nimage = tf.keras.preprocessing.image.img_to_array(image)\nplt.imshow(image.astype('int'))\nplt.show()\nimage = np.expand_dims(image, axis=0)\nresult = model.predict(image)\ndata = train_idg.class_indices\nfor i in list(np.argsort(result)[:,:-5:-1][0]):\n  print(list(data.keys())[i],result[:,i]*100)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:31.303272Z","iopub.execute_input":"2023-10-08T14:51:31.303673Z","iopub.status.idle":"2023-10-08T14:51:33.501095Z","shell.execute_reply.started":"2023-10-08T14:51:31.303632Z","shell.execute_reply":"2023-10-08T14:51:33.500043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = pd.DataFrame(result)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:33.502578Z","iopub.execute_input":"2023-10-08T14:51:33.503404Z","iopub.status.idle":"2023-10-08T14:51:33.508866Z","shell.execute_reply.started":"2023-10-08T14:51:33.503372Z","shell.execute_reply":"2023-10-08T14:51:33.507904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report,ConfusionMatrixDisplay,confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:33.514890Z","iopub.execute_input":"2023-10-08T14:51:33.515534Z","iopub.status.idle":"2023-10-08T14:51:33.851037Z","shell.execute_reply.started":"2023-10-08T14:51:33.515500Z","shell.execute_reply":"2023-10-08T14:51:33.850148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_true,y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:33.852287Z","iopub.execute_input":"2023-10-08T14:51:33.852819Z","iopub.status.idle":"2023-10-08T14:51:33.862166Z","shell.execute_reply.started":"2023-10-08T14:51:33.852787Z","shell.execute_reply":"2023-10-08T14:51:33.861210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,12))\nConfusionMatrixDisplay(confusion_matrix(y_true,y_pred)).plot()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:33.863603Z","iopub.execute_input":"2023-10-08T14:51:33.864453Z","iopub.status.idle":"2023-10-08T14:51:57.794265Z","shell.execute_reply.started":"2023-10-08T14:51:33.864419Z","shell.execute_reply":"2023-10-08T14:51:57.793402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_true,y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:57.795581Z","iopub.execute_input":"2023-10-08T14:51:57.796428Z","iopub.status.idle":"2023-10-08T14:51:57.815006Z","shell.execute_reply.started":"2023-10-08T14:51:57.796392Z","shell.execute_reply":"2023-10-08T14:51:57.814100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.save(\"Dog_Breed_Identification.keras\")","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:57.816330Z","iopub.execute_input":"2023-10-08T14:51:57.816836Z","iopub.status.idle":"2023-10-08T14:51:58.334916Z","shell.execute_reply.started":"2023-10-08T14:51:57.816806Z","shell.execute_reply":"2023-10-08T14:51:58.333997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ntest_data = list(os.listdir('/kaggle/input/dog-breed-identification/test'))","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:58.336356Z","iopub.execute_input":"2023-10-08T14:51:58.336686Z","iopub.status.idle":"2023-10-08T14:51:58.527280Z","shell.execute_reply.started":"2023-10-08T14:51:58.336655Z","shell.execute_reply":"2023-10-08T14:51:58.526393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = []\nfor i in test_data:\n  indices.append(i)\n  image = tf.keras.preprocessing.image.load_img(f'/kaggle/input/dog-breed-identification/test/{i}', target_size=(350, 500))\n  image = tf.keras.preprocessing.image.img_to_array(image)\n  image = np.expand_dims(image, axis=0)\n  result = model.predict(image,verbose=None)\n  res.loc[i] = result[0]","metadata":{"execution":{"iopub.status.busy":"2023-10-08T14:51:58.528542Z","iopub.execute_input":"2023-10-08T14:51:58.529054Z","iopub.status.idle":"2023-10-08T15:05:53.140909Z","shell.execute_reply.started":"2023-10-08T14:51:58.529023Z","shell.execute_reply":"2023-10-08T15:05:53.139944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = res.iloc[1:,:]","metadata":{"execution":{"iopub.status.busy":"2023-10-08T15:05:53.142177Z","iopub.execute_input":"2023-10-08T15:05:53.142717Z","iopub.status.idle":"2023-10-08T15:05:53.148020Z","shell.execute_reply.started":"2023-10-08T15:05:53.142685Z","shell.execute_reply":"2023-10-08T15:05:53.147054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.columns = list(train_idg.class_indices.keys())","metadata":{"execution":{"iopub.status.busy":"2023-10-08T15:05:53.149321Z","iopub.execute_input":"2023-10-08T15:05:53.149920Z","iopub.status.idle":"2023-10-08T15:05:53.160129Z","shell.execute_reply.started":"2023-10-08T15:05:53.149889Z","shell.execute_reply":"2023-10-08T15:05:53.159154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res","metadata":{"execution":{"iopub.status.busy":"2023-10-08T15:05:53.161422Z","iopub.execute_input":"2023-10-08T15:05:53.161925Z","iopub.status.idle":"2023-10-08T15:05:53.199808Z","shell.execute_reply.started":"2023-10-08T15:05:53.161895Z","shell.execute_reply":"2023-10-08T15:05:53.198913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.to_csv(\"Final_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-08T15:05:53.200941Z","iopub.execute_input":"2023-10-08T15:05:53.201260Z","iopub.status.idle":"2023-10-08T15:05:54.459391Z","shell.execute_reply.started":"2023-10-08T15:05:53.201210Z","shell.execute_reply":"2023-10-08T15:05:54.458463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}