{"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 sklearn.preprocessing import MultiLabelBinarizer\nfrom pathlib import Path\nimport os.path\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport os\nimport cv2\nimport pandas as pd\nimport random\nimport os\nimport PIL\nimport tensorflow as tf\nfrom keras.optimizers import Adam\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:23.910253Z","iopub.execute_input":"2021-05-26T10:12:23.910606Z","iopub.status.idle":"2021-05-26T10:12:23.920932Z","shell.execute_reply.started":"2021-05-26T10:12:23.910571Z","shell.execute_reply":"2021-05-26T10:12:23.919833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/plant-pathology-2021-fgvc8'","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:23.922488Z","iopub.execute_input":"2021-05-26T10:12:23.922989Z","iopub.status.idle":"2021-05-26T10:12:23.931660Z","shell.execute_reply.started":"2021-05-26T10:12:23.922951Z","shell.execute_reply":"2021-05-26T10:12:23.930934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(r'../input/plant-pathology-2021-fgvc8/train.csv', index_col='image')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:23.933575Z","iopub.execute_input":"2021-05-26T10:12:23.933919Z","iopub.status.idle":"2021-05-26T10:12:23.963886Z","shell.execute_reply.started":"2021-05-26T10:12:23.933885Z","shell.execute_reply":"2021-05-26T10:12:23.963107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.labels.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:23.965968Z","iopub.execute_input":"2021-05-26T10:12:23.966214Z","iopub.status.idle":"2021-05-26T10:12:23.980643Z","shell.execute_reply.started":"2021-05-26T10:12:23.966191Z","shell.execute_reply":"2021-05-26T10:12:23.979722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,12))\nlabels = sns.barplot(train_df.labels.value_counts().index,train_df.labels.value_counts())\nfor item in labels.get_xticklabels():\n    item.set_rotation(45)\nplt.title('Label Distribution', weight='bold')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:23.983290Z","iopub.execute_input":"2021-05-26T10:12:23.983569Z","iopub.status.idle":"2021-05-26T10:12:24.398055Z","shell.execute_reply.started":"2021-05-26T10:12:23.983546Z","shell.execute_reply":"2021-05-26T10:12:24.397217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_Path = '../input/plant-pathology-2021-fgvc8/train_images'\ntest_img_Path = '../input/plant-pathology-2021-fgvc8/test_images'\nsample_submission = pd.read_csv(r'../input/plant-pathology-2021-fgvc8/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:24.399256Z","iopub.execute_input":"2021-05-26T10:12:24.399735Z","iopub.status.idle":"2021-05-26T10:12:24.410737Z","shell.execute_reply.started":"2021-05-26T10:12:24.399696Z","shell.execute_reply":"2021-05-26T10:12:24.409908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:24.411952Z","iopub.execute_input":"2021-05-26T10:12:24.412442Z","iopub.status.idle":"2021-05-26T10:12:24.424899Z","shell.execute_reply.started":"2021-05-26T10:12:24.412404Z","shell.execute_reply":"2021-05-26T10:12:24.423802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['labels'] = train_df['labels'].apply(lambda s: s.split(' '))\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:24.427801Z","iopub.execute_input":"2021-05-26T10:12:24.428085Z","iopub.status.idle":"2021-05-26T10:12:24.447115Z","shell.execute_reply.started":"2021-05-26T10:12:24.428055Z","shell.execute_reply":"2021-05-26T10:12:24.446101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfig1 = plt.figure(figsize=(20,10))\n\nfor i in range(1, 10):\n    \n    rand =  random.randrange(1, 18000)\n    sample = os.path.join('../input/plant-pathology-2021-fgvc8/train_images', train_df.index[rand])\n    \n    img = PIL.Image.open(sample)\n    \n    ax = fig1.add_subplot(4,3,i)\n    ax.imshow(img)\n    \n    title = f\"{train_df['labels'][rand]}{img.size}\"\n    plt.title(title)\n    \n    fig1.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:24.449562Z","iopub.execute_input":"2021-05-26T10:12:24.450008Z","iopub.status.idle":"2021-05-26T10:12:33.423972Z","shell.execute_reply.started":"2021-05-26T10:12:24.449970Z","shell.execute_reply":"2021-05-26T10:12:33.417530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = '../input/resized-plant2021/img_sz_512/'\nTEST_PATH = '../input/plant-pathology-2021-fgvc8/test_images/'\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:33.425210Z","iopub.execute_input":"2021-05-26T10:12:33.425565Z","iopub.status.idle":"2021-05-26T10:12:33.429241Z","shell.execute_reply.started":"2021-05-26T10:12:33.425531Z","shell.execute_reply":"2021-05-26T10:12:33.428549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_train = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:33.430482Z","iopub.execute_input":"2021-05-26T10:12:33.430996Z","iopub.status.idle":"2021-05-26T10:12:33.456984Z","shell.execute_reply.started":"2021-05-26T10:12:33.430963Z","shell.execute_reply":"2021-05-26T10:12:33.456229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:33.459106Z","iopub.execute_input":"2021-05-26T10:12:33.459353Z","iopub.status.idle":"2021-05-26T10:12:33.464702Z","shell.execute_reply.started":"2021-05-26T10:12:33.459328Z","shell.execute_reply":"2021-05-26T10:12:33.463764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_train['labels'] = resized_train['labels'].apply(lambda s: s.split(' '))\nresized_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:33.466210Z","iopub.execute_input":"2021-05-26T10:12:33.466869Z","iopub.status.idle":"2021-05-26T10:12:33.489167Z","shell.execute_reply.started":"2021-05-26T10:12:33.466829Z","shell.execute_reply":"2021-05-26T10:12:33.488427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans_label = MultiLabelBinarizer().fit(resized_train['labels'])\nlabels = pd.DataFrame(trans_label.transform(resized_train['labels']), columns=trans_label.classes_)\ntrain_df = pd.concat([resized_train['image'], labels], axis=1)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:33.490292Z","iopub.execute_input":"2021-05-26T10:12:33.490647Z","iopub.status.idle":"2021-05-26T10:12:33.529270Z","shell.execute_reply.started":"2021-05-26T10:12:33.490612Z","shell.execute_reply":"2021-05-26T10:12:33.528451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resized_train","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:33.530524Z","iopub.execute_input":"2021-05-26T10:12:33.531003Z","iopub.status.idle":"2021-05-26T10:12:33.546932Z","shell.execute_reply.started":"2021-05-26T10:12:33.530964Z","shell.execute_reply":"2021-05-26T10:12:33.546201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\ndatagen = keras.preprocessing.image.ImageDataGenerator(rescale=1/255.0,\n                                                        preprocessing_function=None,\n                                                        data_format=None,\n                                                        validation_split= 0.2\n                                                    )","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:33.549488Z","iopub.execute_input":"2021-05-26T10:12:33.549747Z","iopub.status.idle":"2021-05-26T10:12:33.555240Z","shell.execute_reply.started":"2021-05-26T10:12:33.549723Z","shell.execute_reply":"2021-05-26T10:12:33.554406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = datagen.flow_from_dataframe(\n    resized_train,\n    directory = '../input/resized-plant2021/img_sz_512',\n    x_col = 'image',\n    y_col = 'labels',\n    subset=\"training\",\n    color_mode=\"rgb\",\n    target_size = (224,224),\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=True,\n    seed=40,\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:12:33.556459Z","iopub.execute_input":"2021-05-26T10:12:33.557017Z","iopub.status.idle":"2021-05-26T10:13:24.236469Z","shell.execute_reply.started":"2021-05-26T10:12:33.556980Z","shell.execute_reply":"2021-05-26T10:13:24.235597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_data = datagen.flow_from_dataframe(\n    resized_train,\n    directory = '../input/resized-plant2021/img_sz_512',\n    x_col = 'image',\n    y_col = 'labels',\n    subset=\"validation\",\n    color_mode=\"rgb\",\n    target_size = (224,224),\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=False,\n    seed=40,\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:13:24.237771Z","iopub.execute_input":"2021-05-26T10:13:24.238290Z","iopub.status.idle":"2021-05-26T10:13:51.910780Z","shell.execute_reply.started":"2021-05-26T10:13:24.238252Z","shell.execute_reply":"2021-05-26T10:13:51.909917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MultiLabelBinarizer\nmlb = MultiLabelBinarizer()\nhot_labels = mlb.fit_transform(resized_train['labels'])\nprint(mlb.classes_)\nprint(hot_labels)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:13:51.911957Z","iopub.execute_input":"2021-05-26T10:13:51.912439Z","iopub.status.idle":"2021-05-26T10:13:51.939636Z","shell.execute_reply.started":"2021-05-26T10:13:51.912400Z","shell.execute_reply":"2021-05-26T10:13:51.938628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels = pd.DataFrame(hot_labels,columns=mlb.classes_,index=resized_train.index)\ndf_labels","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:13:51.941002Z","iopub.execute_input":"2021-05-26T10:13:51.941513Z","iopub.status.idle":"2021-05-26T10:13:51.963845Z","shell.execute_reply.started":"2021-05-26T10:13:51.941454Z","shell.execute_reply":"2021-05-26T10:13:51.963036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nsns.barplot(x=df_labels.columns,y=df_labels.sum().values)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:13:51.966952Z","iopub.execute_input":"2021-05-26T10:13:51.967243Z","iopub.status.idle":"2021-05-26T10:13:52.113870Z","shell.execute_reply.started":"2021-05-26T10:13:51.967218Z","shell.execute_reply":"2021-05-26T10:13:52.113077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **DenseNet 121**","metadata":{}},{"cell_type":"code","source":"from keras.applications import InceptionResNetV2\nfrom keras.applications import MobileNetV2\nfrom keras.applications import DenseNet121\nfrom keras.applications import DenseNet169\n\nimport keras\nfrom keras.layers import Dense,Dropout,Flatten\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\nfrom keras.models import Model\nfrom tensorflow.keras.callbacks import EarlyStopping\nimport tensorflow_addons as tfa\n\nweight_path='../input/tf-keras-pretrained-model-weights/No Top/densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5'\nbase_model=DenseNet121(weights=weight_path,include_top=False, input_shape=(224,224,3))\nx=base_model.output\nx=GlobalAveragePooling2D()(x)\nx=Dense(128,activation='relu')(x)\nx=Dropout(0.5)(x)\nx=Dense(64,activation='relu')(x)\npredictions=Dense(6,activation='sigmoid')(x)\n\nmodel=Model(inputs=base_model.input,outputs=predictions)\n\nfor layer in base_model.layers:\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:13:52.115222Z","iopub.execute_input":"2021-05-26T10:13:52.115599Z","iopub.status.idle":"2021-05-26T10:13:58.141070Z","shell.execute_reply.started":"2021-05-26T10:13:52.115560Z","shell.execute_reply":"2021-05-26T10:13:58.140222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1 = tfa.metrics.F1Score(num_classes=6,average='macro')\n\nmodel.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy',f1])\nes=EarlyStopping(patience=4,monitor=f1,mode='max',restore_best_weights=True)\nhist = model.fit_generator(generator=train_data,\n                    validation_data=valid_data,\n                    epochs=20,\n                    steps_per_epoch=train_data.samples//128,\n                    validation_steps=valid_data.samples//128,\n                    callbacks=[es])","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:25:06.291943Z","iopub.execute_input":"2021-05-26T10:25:06.292271Z","iopub.status.idle":"2021-05-26T10:31:56.821914Z","shell.execute_reply.started":"2021-05-26T10:25:06.292240Z","shell.execute_reply":"2021-05-26T10:31:56.821076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Accuracy","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nepoch_list = list(range(1, len(hist.history['accuracy']) + 1))\nplt.plot(epoch_list, hist.history['accuracy'],label='accuracy')\nplt.plot(epoch_list, hist.history['val_accuracy'],label='val_accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:34:45.223680Z","iopub.execute_input":"2021-05-26T10:34:45.224099Z","iopub.status.idle":"2021-05-26T10:34:45.503613Z","shell.execute_reply.started":"2021-05-26T10:34:45.224057Z","shell.execute_reply":"2021-05-26T10:34:45.502740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# F1","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nepoch_list = list(range(1, len(hist.history['f1_score']) + 1))\nplt.plot(epoch_list, hist.history['f1_score'],label='f1_score')\nplt.plot(epoch_list, hist.history['val_f1_score'],label='val_f1_score')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:34:50.700079Z","iopub.execute_input":"2021-05-26T10:34:50.700396Z","iopub.status.idle":"2021-05-26T10:34:50.872534Z","shell.execute_reply.started":"2021-05-26T10:34:50.700366Z","shell.execute_reply":"2021-05-26T10:34:50.871573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loss","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nepoch_list = list(range(1, len(hist.history['accuracy']) + 1))\nplt.plot(epoch_list, hist.history['loss'],label='loss')\nplt.plot(epoch_list, hist.history['val_loss'],label='val_loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:34:56.565653Z","iopub.execute_input":"2021-05-26T10:34:56.566030Z","iopub.status.idle":"2021-05-26T10:34:56.732342Z","shell.execute_reply.started":"2021-05-26T10:34:56.565984Z","shell.execute_reply":"2021-05-26T10:34:56.731409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path=\"../input/plant-pathology-2021-fgvc8/sample_submission.csv\"\ntest = pd.read_csv(test_path)\ntest","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:35:00.245334Z","iopub.execute_input":"2021-05-26T10:35:00.245787Z","iopub.status.idle":"2021-05-26T10:35:00.261845Z","shell.execute_reply.started":"2021-05-26T10:35:00.245749Z","shell.execute_reply":"2021-05-26T10:35:00.260815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = datagen.flow_from_dataframe(\n    test,\n    directory='../input/plant-pathology-2021-fgvc8/test_images',\n    x_col='image',\n    y_col=None,\n    color_mode='rgb',\n    target_size=(224,224),\n    class_mode=None,\n    shuffle=False\n)\npredictions = model.predict(test_data)\nprint(predictions)\n\nclass_idx=[]\nfor pred in predictions:\n    pred=list(pred)\n    temp=[]\n    for i in pred:\n        if (i>0.4):\n            temp.append(pred.index(i))\n    if (temp!=[]):\n        class_idx.append(temp)\n    else:\n        temp.append(np.argmax(pred))\n        class_idx.append(temp)\nprint(class_idx)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:35:02.079920Z","iopub.execute_input":"2021-05-26T10:35:02.080239Z","iopub.status.idle":"2021-05-26T10:35:05.015640Z","shell.execute_reply.started":"2021-05-26T10:35:02.080210Z","shell.execute_reply":"2021-05-26T10:35:05.014697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_dict = train_data.class_indices\ndef get_key(val):\n    for key,value in class_dict.items():\n        if (val==value):\n            return key\nprint(class_dict)\n\nsub_pred=[]\nfor img_ in class_idx:\n    img_pred=[]\n    for i in img_:\n        img_pred.append(get_key(i))\n    sub_pred.append( ' '.join(img_pred))\nprint(sub_pred)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:35:07.276937Z","iopub.execute_input":"2021-05-26T10:35:07.277261Z","iopub.status.idle":"2021-05-26T10:35:07.285431Z","shell.execute_reply.started":"2021-05-26T10:35:07.277230Z","shell.execute_reply":"2021-05-26T10:35:07.284423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = test[['image']]\nsub['labels']=sub_pred\nsub","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:35:09.950621Z","iopub.execute_input":"2021-05-26T10:35:09.950948Z","iopub.status.idle":"2021-05-26T10:35:09.964097Z","shell.execute_reply.started":"2021-05-26T10:35:09.950917Z","shell.execute_reply":"2021-05-26T10:35:09.963279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T10:35:11.934281Z","iopub.execute_input":"2021-05-26T10:35:11.934632Z","iopub.status.idle":"2021-05-26T10:35:12.196150Z","shell.execute_reply.started":"2021-05-26T10:35:11.934597Z","shell.execute_reply":"2021-05-26T10:35:12.195292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}