{"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\nfrom pathlib import Path\nimport os.path\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport os\nimport cv2\n\nfrom keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:21:17.743828Z","iopub.execute_input":"2022-10-10T09:21:17.744214Z","iopub.status.idle":"2022-10-10T09:21:17.793651Z","shell.execute_reply.started":"2022-10-10T09:21:17.744180Z","shell.execute_reply":"2022-10-10T09:21:17.792730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a list with the filepaths for training and testing\ntrain_img_Path = '../input/plant-pathology-2021-fgvc8/train_images'\n\ntest_img_Path = '../input/plant-pathology-2021-fgvc8/test_images'\n\nimg_Path = '../input/resized-plant2021/img_sz_256'\n\ntrain = pd.read_csv(r'../input/plant-pathology-2021-fgvc8/train.csv')\n\nsample_submission = pd.read_csv(r'../input/plant-pathology-2021-fgvc8/sample_submission.csv')\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:21:49.379056Z","iopub.execute_input":"2022-10-10T09:21:49.379738Z","iopub.status.idle":"2022-10-10T09:21:49.409900Z","shell.execute_reply.started":"2022-10-10T09:21:49.379702Z","shell.execute_reply":"2022-10-10T09:21:49.409025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Number of pictures in the training dataset: {train.shape[0]}\\n')\nprint(f'Number of different labels: {len(train.labels.unique())}\\n')\nprint(f'Labels: {train.labels.unique()}')","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:21:59.033130Z","iopub.execute_input":"2022-10-10T09:21:59.034108Z","iopub.status.idle":"2022-10-10T09:21:59.043714Z","shell.execute_reply.started":"2022-10-10T09:21:59.034062Z","shell.execute_reply":"2022-10-10T09:21:59.042636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['labels'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:22:08.768389Z","iopub.execute_input":"2022-10-10T09:22:08.768751Z","iopub.status.idle":"2022-10-10T09:22:08.784715Z","shell.execute_reply.started":"2022-10-10T09:22:08.768721Z","shell.execute_reply":"2022-10-10T09:22:08.783685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14,7))\nb = sns.countplot(x='labels', data=train, order=sorted(train['labels'].unique()))\nfor item in b.get_xticklabels():\n    item.set_rotation(90)\nplt.title('Label Distribution', weight='bold')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:22:17.794897Z","iopub.execute_input":"2022-10-10T09:22:17.795387Z","iopub.status.idle":"2022-10-10T09:22:18.262272Z","shell.execute_reply.started":"2022-10-10T09:22:17.795339Z","shell.execute_reply":"2022-10-10T09:22:18.261229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,40))\ni=1\nfor idx,s in train.head(9).iterrows():\n    img_path = os.path.join(img_Path,s['image'])\n    img=cv2.imread(img_path)\n    img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n    fig=plt.subplot(9,3,i)\n    fig.imshow(img)\n    fig.set_title(s['labels'])\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:22:28.448504Z","iopub.execute_input":"2022-10-10T09:22:28.448877Z","iopub.status.idle":"2022-10-10T09:22:30.397741Z","shell.execute_reply.started":"2022-10-10T09:22:28.448846Z","shell.execute_reply":"2022-10-10T09:22:30.396482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASSES = train['labels'].unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:22:42.079046Z","iopub.execute_input":"2022-10-10T09:22:42.079412Z","iopub.status.idle":"2022-10-10T09:22:42.085955Z","shell.execute_reply.started":"2022-10-10T09:22:42.079381Z","shell.execute_reply":"2022-10-10T09:22:42.084740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\n# Preprocessing the Training set\ntrain_datagen = ImageDataGenerator(rescale=1./255,\n                                   shear_range = 0.1,\n                                   zoom_range = 0.1,\n                                   horizontal_flip = True,\n                                   validation_split=0.25)\n\ntrain_data = train_datagen.flow_from_dataframe(train,\n                                              directory=img_Path,\n                                              classes=CLASSES,\n                                              x_col=\"image\",\n                                              y_col=\"labels\",\n                                              target_size=(150, 150),\n                                              subset='training')\n\nval_data = train_datagen.flow_from_dataframe(train,\n                                            directory=img_Path,\n                                            classes=CLASSES,\n                                            x_col=\"image\",\n                                            y_col=\"labels\",\n                                            target_size=(150, 150),\n                                            subset='validation')","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:22:54.359656Z","iopub.execute_input":"2022-10-10T09:22:54.360777Z","iopub.status.idle":"2022-10-10T09:23:58.145018Z","shell.execute_reply.started":"2022-10-10T09:22:54.360732Z","shell.execute_reply":"2022-10-10T09:23:58.143993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_classes = train_data.class_indices\ndict_classes","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:24:18.416380Z","iopub.execute_input":"2022-10-10T09:24:18.416746Z","iopub.status.idle":"2022-10-10T09:24:18.423995Z","shell.execute_reply.started":"2022-10-10T09:24:18.416714Z","shell.execute_reply":"2022-10-10T09:24:18.423047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom keras.preprocessing.image import load_img\nfrom tensorflow.keras.utils import to_categorical\nfrom keras import Sequential\nfrom tensorflow.keras.applications import InceptionResNetV2, DenseNet169, ResNet152V2\nfrom tensorflow.keras.layers import Dense","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:27:13.698946Z","iopub.execute_input":"2022-10-10T09:27:13.699649Z","iopub.status.idle":"2022-10-10T09:27:13.705411Z","shell.execute_reply.started":"2022-10-10T09:27:13.699611Z","shell.execute_reply":"2022-10-10T09:27:13.704413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_Net = ResNet152V2(include_top = False, \n                         weights = '../input/keras-pretrained-models/ResNet152V2_NoTop_ImageNet.h5', \n                         input_shape = train_data.image_shape, \n                         pooling='avg',\n                         classes = CLASSES)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:27:25.088834Z","iopub.execute_input":"2022-10-10T09:27:25.089524Z","iopub.status.idle":"2022-10-10T09:27:34.923686Z","shell.execute_reply.started":"2022-10-10T09:27:25.089486Z","shell.execute_reply":"2022-10-10T09:27:34.922681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Callbacks\nEarlyStop_callback = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)\nmy_callback=[EarlyStop_callback]","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:29:21.689408Z","iopub.execute_input":"2022-10-10T09:29:21.689787Z","iopub.status.idle":"2022-10-10T09:29:21.695418Z","shell.execute_reply.started":"2022-10-10T09:29:21.689755Z","shell.execute_reply":"2022-10-10T09:29:21.694270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adding the final layers to the above base models where the actual classification is done in the dense layers\nmodel_Net = Sequential()\nmodel_Net.add(base_Net)\nmodel_Net.add(Dense(12, activation=('softmax')))\n\nmodel_Net.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['AUC'])\nmodel_Net.summary()\n\n# Training the CNN on the Train data and evaluating it on the val data\nb = model_Net.fit(train_data, validation_data = val_data, epochs = 10,callbacks=my_callback, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:29:41.273993Z","iopub.execute_input":"2022-10-10T09:29:41.274679Z","iopub.status.idle":"2022-10-10T09:59:28.831120Z","shell.execute_reply.started":"2022-10-10T09:29:41.274643Z","shell.execute_reply":"2022-10-10T09:59:28.829825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_InceptionResNetV2 = InceptionResNetV2(include_top = False, \n                         weights = '../input/keras-pretrained-models/InceptionResNetV2_NoTop_ImageNet.h5', \n                         input_shape = train_data.image_shape, \n                         pooling='avg',\n                         classes = CLASSES)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:00:02.709609Z","iopub.execute_input":"2022-10-10T10:00:02.710286Z","iopub.status.idle":"2022-10-10T10:00:10.421870Z","shell.execute_reply.started":"2022-10-10T10:00:02.710230Z","shell.execute_reply":"2022-10-10T10:00:10.420832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adding the final layers to the above base models where the actual classification is done in the dense layers\nmodel_IResNet2 = Sequential()\nmodel_IResNet2.add(base_InceptionResNetV2)\nmodel_IResNet2.add(Dense(12, activation=('softmax')))\n\nmodel_IResNet2.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])\nmodel_IResNet2.summary()\n\n# Training the CNN on the Train data and evaluating it on the val data\nc = model_IResNet2.fit(train_data, validation_data = val_data, epochs = 10,callbacks=my_callback, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:00:24.494134Z","iopub.execute_input":"2022-10-10T10:00:24.494523Z","iopub.status.idle":"2022-10-10T10:26:59.581935Z","shell.execute_reply.started":"2022-10-10T10:00:24.494491Z","shell.execute_reply":"2022-10-10T10:26:59.580882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_DenseNet169 = DenseNet169(include_top = False, \n                         weights = '../input/keras-pretrained-models/DenseNet169_NoTop_ImageNet.h5', \n                         input_shape = train_data.image_shape, \n                         pooling='avg',\n                         classes = CLASSES)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:27:26.418509Z","iopub.execute_input":"2022-10-10T10:27:26.418873Z","iopub.status.idle":"2022-10-10T10:27:31.229768Z","shell.execute_reply.started":"2022-10-10T10:27:26.418843Z","shell.execute_reply":"2022-10-10T10:27:31.228796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adding the final layers to the above base models where the actual classification is done in the dense layers\nmodel_dense = Sequential()\nmodel_dense.add(base_DenseNet169)\nmodel_dense.add(Dense(12, activation=('softmax')))\n\nmodel_dense.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])\nmodel_dense.summary()\n\n# Training the CNN on the Train data and evaluating it on the val data\nd = model_dense.fit(train_data, validation_data = val_data, epochs = 10,callbacks=my_callback, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:27:42.314599Z","iopub.execute_input":"2022-10-10T10:27:42.314951Z","iopub.status.idle":"2022-10-10T10:51:17.403035Z","shell.execute_reply.started":"2022-10-10T10:27:42.314921Z","shell.execute_reply":"2022-10-10T10:51:17.401924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = '/kaggle/input/plant-pathology-2021-fgvc8/test_images/'\ntest_df = pd.DataFrame()\ntest_df['image'] = os.listdir(test_dir)\n\ntest_data = train_datagen.flow_from_dataframe(dataframe=test_df,\n                                    directory=test_dir,\n                                    x_col=\"image\",\n                                    y_col=None,\n                                    batch_size=32,\n                                    seed=42,\n                                    shuffle=False,\n                                    class_mode=None,\n                                    target_size=(150, 150))","metadata":{"execution":{"iopub.status.busy":"2022-10-10T11:18:11.233520Z","iopub.execute_input":"2022-10-10T11:18:11.234671Z","iopub.status.idle":"2022-10-10T11:18:11.251724Z","shell.execute_reply.started":"2022-10-10T11:18:11.234616Z","shell.execute_reply":"2022-10-10T11:18:11.250670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_net = model_Net.predict(test_data)\npred_iresnet2 = model_IResNet2.predict(test_data)\npred_dense = model_dense.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T11:18:20.143299Z","iopub.execute_input":"2022-10-10T11:18:20.143664Z","iopub.status.idle":"2022-10-10T11:18:31.300645Z","shell.execute_reply.started":"2022-10-10T11:18:20.143628Z","shell.execute_reply":"2022-10-10T11:18:31.299640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = ((pred_net+pred_iresnet2+pred_dense)/3).tolist()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T11:18:32.872837Z","iopub.execute_input":"2022-10-10T11:18:32.873554Z","iopub.status.idle":"2022-10-10T11:18:32.878590Z","shell.execute_reply.started":"2022-10-10T11:18:32.873519Z","shell.execute_reply":"2022-10-10T11:18:32.877544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(pred)):\n    pred[i] = np.argmax(pred[i])\n\n    \ndef get_key(val):\n    for key, value in dict_classes.items():\n        if val == value:\n            return key\n        \n\nfor i in range(len(pred)):\n    pred[i] = get_key(pred[i])","metadata":{"execution":{"iopub.status.busy":"2022-10-10T11:18:41.133524Z","iopub.execute_input":"2022-10-10T11:18:41.133899Z","iopub.status.idle":"2022-10-10T11:18:41.139958Z","shell.execute_reply.started":"2022-10-10T11:18:41.133866Z","shell.execute_reply":"2022-10-10T11:18:41.138988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['labels'] = pred\ntest_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T11:18:50.393535Z","iopub.execute_input":"2022-10-10T11:18:50.393930Z","iopub.status.idle":"2022-10-10T11:18:50.402806Z","shell.execute_reply.started":"2022-10-10T11:18:50.393895Z","shell.execute_reply":"2022-10-10T11:18:50.401678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}