{"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":"## Plant Pathology 2021 - FGVC8\n#### Identify the category of foliar diseases in apple trees","metadata":{}},{"cell_type":"markdown","source":"Apples are one of the most important temperate fruit crops in the world. Foliar (leaf) diseases pose a major threat to the overall productivity and quality of apple orchards. The current process for disease diagnosis in apple orchards is based on manual scouting by humans, which is time-consuming and expensive.\n\nAlthough computer vision-based models have shown promise for plant disease identification, there are some limitations that need to be addressed. Large variations in visual symptoms of a single disease across different apple cultivars, or new varieties that originated under cultivation, are major challenges for computer vision-based disease identification. These variations arise from differences in natural and image capturing environments, for example, leaf color and leaf morphology, the age of infected tissues, non-uniform image background, and different light illumination during imaging etc.","metadata":{}},{"cell_type":"markdown","source":"#### Importing libraries","metadata":{}},{"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","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:18:49.000382Z","iopub.execute_input":"2021-07-02T05:18:49.000761Z","iopub.status.idle":"2021-07-02T05:18:49.839806Z","shell.execute_reply.started":"2021-07-02T05:18:49.000666Z","shell.execute_reply":"2021-07-02T05:18:49.838938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from cycler import cycler\nimport matplotlib as mpl\n\nraw_light_palette = [\n    (0, 122, 255), # Blue\n    (255, 149, 0), # Orange\n    (52, 199, 89), # Green\n    (255, 59, 48), # Red\n    (175, 82, 222),# Purple\n    (255, 45, 85), # Pink\n    (88, 86, 214), # Indigo\n    (90, 200, 250),# Teal\n    (255, 204, 0)  # Yellow\n]\nraw_dark_palette = [\n    (10, 132, 255), # Blue\n    (255, 159, 10), # Orange\n    (48, 209, 88),  # Green\n    (255, 69, 58),  # Red\n    (191, 90, 242), # Purple\n    (94, 92, 230),  # Indigo\n    (255, 55, 95),  # Pink\n    (100, 210, 255),# Teal\n    (255, 214, 10)  # Yellow\n]\nraw_gray_light_palette = [\n    (142, 142, 147),# Gray\n    (174, 174, 178),# Gray (2)\n    (199, 199, 204),# Gray (3)\n    (209, 209, 214),# Gray (4)\n    (229, 229, 234),# Gray (5)\n    (242, 242, 247),# Gray (6)\n]\nraw_gray_dark_palette = [\n    (142, 142, 147),# Gray\n    (99, 99, 102),  # Gray (2)\n    (72, 72, 74),   # Gray (3)\n    (58, 58, 60),   # Gray (4)\n    (44, 44, 46),   # Gray (5)\n    (28, 28, 39),   # Gray (6)\n]\n\nlight_palette = np.array(raw_light_palette)/255\ndark_palette = np.array(raw_dark_palette)/255\ngray_light_palette = np.array(raw_gray_light_palette)/255\ngray_dark_palette = np.array(raw_gray_dark_palette)/255\n\nmpl.rcParams['axes.prop_cycle'] = cycler('color',dark_palette)\nmpl.rcParams['figure.facecolor']  = gray_dark_palette[-2]\nmpl.rcParams['figure.edgecolor']  = gray_dark_palette[-2]\nmpl.rcParams['axes.facecolor'] =  gray_dark_palette[-2]\n\nwhite_color = gray_light_palette[-2]\nmpl.rcParams['text.color'] = white_color\nmpl.rcParams['axes.labelcolor'] = white_color\nmpl.rcParams['axes.edgecolor'] = white_color\nmpl.rcParams['xtick.color'] = white_color\nmpl.rcParams['ytick.color'] = white_color\n\nmpl.rcParams['figure.dpi'] = 200\nmpl.rcParams['axes.spines.top'] = False\nmpl.rcParams['axes.spines.right'] = False","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-02T05:18:49.841258Z","iopub.execute_input":"2021-07-02T05:18:49.841572Z","iopub.status.idle":"2021-07-02T05:18:49.856268Z","shell.execute_reply.started":"2021-07-02T05:18:49.841538Z","shell.execute_reply":"2021-07-02T05:18:49.855298Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:18:49.858115Z","iopub.execute_input":"2021-07-02T05:18:49.858628Z","iopub.status.idle":"2021-07-02T05:18:49.910714Z","shell.execute_reply.started":"2021-07-02T05:18:49.85859Z","shell.execute_reply":"2021-07-02T05:18:49.909842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:18:49.912197Z","iopub.execute_input":"2021-07-02T05:18:49.912541Z","iopub.status.idle":"2021-07-02T05:18:49.939329Z","shell.execute_reply.started":"2021-07-02T05:18:49.912503Z","shell.execute_reply":"2021-07-02T05:18:49.938498Z"},"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":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-02T05:18:49.940644Z","iopub.execute_input":"2021-07-02T05:18:49.941005Z","iopub.status.idle":"2021-07-02T05:18:49.952318Z","shell.execute_reply.started":"2021-07-02T05:18:49.940969Z","shell.execute_reply":"2021-07-02T05:18:49.951313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['labels'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:18:49.953746Z","iopub.execute_input":"2021-07-02T05:18:49.954195Z","iopub.status.idle":"2021-07-02T05:18:49.967471Z","shell.execute_reply.started":"2021-07-02T05:18:49.954149Z","shell.execute_reply":"2021-07-02T05:18:49.966382Z"},"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":"2021-07-02T05:18:49.968976Z","iopub.execute_input":"2021-07-02T05:18:49.969359Z","iopub.status.idle":"2021-07-02T05:18:50.43354Z","shell.execute_reply.started":"2021-07-02T05:18:49.969323Z","shell.execute_reply":"2021-07-02T05:18:50.432713Z"},"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":"2021-07-02T05:18:50.436057Z","iopub.execute_input":"2021-07-02T05:18:50.436365Z","iopub.status.idle":"2021-07-02T05:18:53.238Z","shell.execute_reply.started":"2021-07-02T05:18:50.436332Z","shell.execute_reply":"2021-07-02T05:18:53.237126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASSES = train['labels'].unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:18:53.239891Z","iopub.execute_input":"2021-07-02T05:18:53.240383Z","iopub.status.idle":"2021-07-02T05:18:53.246234Z","shell.execute_reply.started":"2021-07-02T05:18:53.240348Z","shell.execute_reply":"2021-07-02T05:18:53.24553Z"},"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":"2021-07-02T05:18:53.247386Z","iopub.execute_input":"2021-07-02T05:18:53.247896Z","iopub.status.idle":"2021-07-02T05:20:04.74645Z","shell.execute_reply.started":"2021-07-02T05:18:53.247848Z","shell.execute_reply":"2021-07-02T05:20:04.745535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_classes = train_data.class_indices\ndict_classes","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:20:04.747739Z","iopub.execute_input":"2021-07-02T05:20:04.748236Z","iopub.status.idle":"2021-07-02T05:20:04.754641Z","shell.execute_reply.started":"2021-07-02T05:20:04.748198Z","shell.execute_reply":"2021-07-02T05:20:04.753631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom keras import Sequential\nfrom keras.applications import InceptionResNetV2, DenseNet169, ResNet152V2\nfrom tensorflow.keras.layers import Dense","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:20:04.756009Z","iopub.execute_input":"2021-07-02T05:20:04.756442Z","iopub.status.idle":"2021-07-02T05:20:04.815396Z","shell.execute_reply.started":"2021-07-02T05:20:04.756402Z","shell.execute_reply":"2021-07-02T05:20:04.814532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* I am using the ResNet152V2, InceptionResNetV2, DenseNet169.","metadata":{}},{"cell_type":"markdown","source":"#### Defining the ResNet152V2 Convolutional Neural Net:","metadata":{}},{"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":"2021-07-02T05:20:04.816602Z","iopub.execute_input":"2021-07-02T05:20:04.816979Z","iopub.status.idle":"2021-07-02T05:20:15.058755Z","shell.execute_reply.started":"2021-07-02T05:20:04.816936Z","shell.execute_reply":"2021-07-02T05:20:15.057823Z"},"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 = ['accuracy'])\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, batch_size=128)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-07-02T05:20:15.060647Z","iopub.execute_input":"2021-07-02T05:20:15.061013Z","iopub.status.idle":"2021-07-02T05:47:52.193546Z","shell.execute_reply.started":"2021-07-02T05:20:15.060977Z","shell.execute_reply":"2021-07-02T05:47:52.192549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Defining the InceptionResNetV2 Convolutional Neural Net:","metadata":{}},{"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":"2021-07-02T05:47:52.196824Z","iopub.execute_input":"2021-07-02T05:47:52.197121Z","iopub.status.idle":"2021-07-02T05:48:01.154466Z","shell.execute_reply.started":"2021-07-02T05:47:52.197088Z","shell.execute_reply":"2021-07-02T05:48:01.153539Z"},"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, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:48:01.156215Z","iopub.execute_input":"2021-07-02T05:48:01.156536Z","iopub.status.idle":"2021-07-02T05:48:28.152887Z","shell.execute_reply.started":"2021-07-02T05:48:01.1565Z","shell.execute_reply":"2021-07-02T05:48:28.148877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Defining the DenseNet169 Convolutional Neural Net:","metadata":{}},{"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":"2021-07-02T05:48:28.154578Z","iopub.status.idle":"2021-07-02T05:48:28.155258Z"},"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, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:48:28.160617Z","iopub.status.idle":"2021-07-02T05:48:28.161386Z"},"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":"2021-07-02T05:48:28.164205Z","iopub.status.idle":"2021-07-02T05:48:28.164658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Making predictions on test data:","metadata":{}},{"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":"2021-07-02T05:48:28.166692Z","iopub.status.idle":"2021-07-02T05:48:28.167082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = (pred_net+pred_iresnet2+pred_dense).tolist()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:48:28.172429Z","iopub.status.idle":"2021-07-02T05:48:28.17281Z"},"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":"2021-07-02T05:48:28.177028Z","iopub.status.idle":"2021-07-02T05:48:28.177905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:48:28.179169Z","iopub.status.idle":"2021-07-02T05:48:28.179657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['labels'] = pred\ntest_df","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:48:28.180992Z","iopub.status.idle":"2021-07-02T05:48:28.181433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T05:48:28.183502Z","iopub.status.idle":"2021-07-02T05:48:28.183938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### If you liked the Notebook Please Upvote It !\n#### Thank You","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}