{"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 pandas as pd\nimport numpy as np\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-25T06:18:13.745579Z","iopub.execute_input":"2021-05-25T06:18:13.745940Z","iopub.status.idle":"2021-05-25T06:18:18.734134Z","shell.execute_reply.started":"2021-05-25T06:18:13.745865Z","shell.execute_reply":"2021-05-25T06:18:18.733149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading dataset\ntrain = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T06:18:51.835176Z","iopub.execute_input":"2021-05-25T06:18:51.835487Z","iopub.status.idle":"2021-05-25T06:18:51.884615Z","shell.execute_reply.started":"2021-05-25T06:18:51.835457Z","shell.execute_reply":"2021-05-25T06:18:51.883647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MultiLabelBinarizer\n\nmlb = MultiLabelBinarizer().fit(train.labels.apply(lambda x : x.split()))\nlabels = pd.DataFrame(mlb.transform(train.labels.apply(lambda x : x.split())), columns = mlb.classes_)\n\nlabels = pd.concat([train['image'], labels], axis=1)\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T06:18:54.089154Z","iopub.execute_input":"2021-05-25T06:18:54.089466Z","iopub.status.idle":"2021-05-25T06:18:54.753589Z","shell.execute_reply.started":"2021-05-25T06:18:54.089436Z","shell.execute_reply":"2021-05-25T06:18:54.752714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nimg_gen = ImageDataGenerator(\n    rescale=1/255,\n    validation_split=0.2\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T06:18:57.502614Z","iopub.execute_input":"2021-05-25T06:18:57.502971Z","iopub.status.idle":"2021-05-25T06:18:57.508357Z","shell.execute_reply.started":"2021-05-25T06:18:57.502937Z","shell.execute_reply":"2021-05-25T06:18:57.507481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16\nIMG_W, IMG_H = 380, 380\ntarget_size = (IMG_W, IMG_H)\n# image_path = '/kaggle/input/plant-pathology-2021-fgvc8/train_images'\nimage_path = '/kaggle/input/resized-plant2021/img_sz_384'\n\nimg_iter = img_gen.flow_from_dataframe(\n    labels,\n    directory=image_path,\n    shuffle=True,\n    class_mode=\"raw\",\n    color_mode=\"rgb\",\n    x_col='image',\n    y_col=labels.columns.tolist()[1:],\n    target_size=target_size,\n    batch_size=batch_size,\n    subset='training'\n)\n\nimg_iter_val = img_gen.flow_from_dataframe(\n    labels,\n    directory=image_path,\n    shuffle=True,\n    class_mode=\"raw\",\n    color_mode=\"rgb\",\n    x_col='image',\n    y_col=labels.columns.tolist()[1:],\n    target_size=target_size,\n    batch_size=batch_size,\n    subset='validation'\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T06:24:00.227280Z","iopub.execute_input":"2021-05-25T06:24:00.227609Z","iopub.status.idle":"2021-05-25T06:24:05.974117Z","shell.execute_reply.started":"2021-05-25T06:24:00.227580Z","shell.execute_reply":"2021-05-25T06:24:05.973195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB4\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D, BatchNormalization\nfrom keras import Model\n\nbase_model = EfficientNetB4(\n    include_top=False,\n    weights='../input/keras-pretrained-models/EfficientNetB4_NoTop_ImageNet.h5',\n    input_shape=(IMG_W, IMG_H, 3)\n)\n\nx=base_model.output\nx=GlobalAveragePooling2D()(x)\nx=BatchNormalization()(x)\nx=Dense(64,activation='relu')(x)\nx=Dropout(0.3)(x)\nx=Dense(32,activation='relu')(x)\nx=Dropout(0.3)(x)\n\npredictions = Dense(len(labels.columns.tolist()[1:]), activation='sigmoid')(x)\nmodel = Model(inputs=base_model.input, outputs=predictions)\nmodel.compile(loss=\"binary_crossentropy\", optimizer='adam', metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T06:25:55.543436Z","iopub.execute_input":"2021-05-25T06:25:55.543799Z","iopub.status.idle":"2021-05-25T06:26:00.560347Z","shell.execute_reply.started":"2021-05-25T06:25:55.543750Z","shell.execute_reply":"2021-05-25T06:26:00.559508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_checkpoint = ModelCheckpoint(\n    filepath=\"./pp2021.h5\", \n    monitor='val_loss', \n    save_best_only=True, \n    verbose=1,\n    mode='min')\n\nearly_stopping = EarlyStopping(\n    monitor='val_loss', \n    min_delta=0,\n    patience=10, \n    verbose=1, \n    restore_best_weights=True)\n\nhistory = model.fit(\n    img_iter,\n    validation_data=img_iter_val,\n    epochs=20,\n    callbacks=[model_checkpoint, early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T06:26:24.560104Z","iopub.execute_input":"2021-05-25T06:26:24.560425Z","iopub.status.idle":"2021-05-25T06:41:31.144646Z","shell.execute_reply.started":"2021-05-25T06:26:24.560395Z","shell.execute_reply":"2021-05-25T06:41:31.141943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\ndef load_best_model():\n    m = keras.models.load_model(\n        \"./pp2021.h5\"\n    )\n    return m","metadata":{"execution":{"iopub.status.busy":"2021-05-24T10:30:10.567949Z","iopub.execute_input":"2021-05-24T10:30:10.5683Z","iopub.status.idle":"2021-05-24T10:30:10.573011Z","shell.execute_reply.started":"2021-05-24T10:30:10.568264Z","shell.execute_reply":"2021-05-24T10:30:10.571929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_best_model()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T10:30:13.106142Z","iopub.execute_input":"2021-05-24T10:30:13.106474Z","iopub.status.idle":"2021-05-24T10:30:15.701612Z","shell.execute_reply.started":"2021-05-24T10:30:13.106446Z","shell.execute_reply":"2021-05-24T10:30:15.70072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef plotHistory(result):\n    plt.plot(result.history['accuracy'])\n    plt.plot(result.history['val_accuracy'])\n    plt.title('Model accuracy')\n    plt.ylabel('accuracy')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'test'], loc='upper left')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:36:53.13382Z","iopub.execute_input":"2021-05-23T16:36:53.134142Z","iopub.status.idle":"2021-05-23T16:36:53.287907Z","shell.execute_reply.started":"2021-05-23T16:36:53.134113Z","shell.execute_reply":"2021-05-23T16:36:53.287135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotHistory(history)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.trainable = True\n\nmodel.compile(\n    optimizer=keras.optimizers.Adam(1e-5),  # Low learning rate\n    loss=keras.losses.BinaryCrossentropy(from_logits=True),\n    metrics=[\"accuracy\"],\n)\n\nepochs = 10\nhistory = model.fit(\n    img_iter,\n    validation_data=img_iter_val,\n    epochs=epochs,\n    callbacks=[model_checkpoint, early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T16:39:52.17939Z","iopub.execute_input":"2021-05-23T16:39:52.179737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotHistory(history)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_best_model()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\ntest_path = \"../input/plant-pathology-2021-fgvc8/test_images\"","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:36:43.947124Z","iopub.execute_input":"2021-05-23T17:36:43.947517Z","iopub.status.idle":"2021-05-23T17:36:43.963934Z","shell.execute_reply.started":"2021-05-23T17:36:43.947484Z","shell.execute_reply":"2021-05-23T17:36:43.963069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_test_datagen = ImageDataGenerator(\n    rescale=1./255,\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:34:49.872777Z","iopub.execute_input":"2021-05-23T17:34:49.873182Z","iopub.status.idle":"2021-05-23T17:34:49.878986Z","shell.execute_reply.started":"2021-05-23T17:34:49.873147Z","shell.execute_reply":"2021-05-23T17:34:49.877723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = image_test_datagen.flow_from_dataframe(\n    test_df,\n    directory = test_path,\n    x_col = \"image\",\n    y_col = \"labels\",\n    target_size = target_size,\n    color_mode=\"rgb\",\n    batch_size=1,\n    shuffle=False,\n    seed=42,\n    subset=None\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:37:05.63622Z","iopub.execute_input":"2021-05-23T17:37:05.636544Z","iopub.status.idle":"2021-05-23T17:37:05.667627Z","shell.execute_reply.started":"2021-05-23T17:37:05.636514Z","shell.execute_reply":"2021-05-23T17:37:05.666681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicts = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:37:22.859834Z","iopub.execute_input":"2021-05-23T17:37:22.860254Z","iopub.status.idle":"2021-05-23T17:37:25.555327Z","shell.execute_reply.started":"2021-05-23T17:37:22.860214Z","shell.execute_reply":"2021-05-23T17:37:25.554561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"verdict = (predicts>0.50)\nlabel = labels.columns.tolist()[1:]\nanswer = []\nfor i in range(verdict.shape[0]):\n    tmp = []\n    for j, c in enumerate(label):\n        if verdict[i, j]:\n            tmp.append(c)\n    answer.append(tmp)\n    \nanswer = [' '.join(t) for t in answer]","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:38:40.596771Z","iopub.execute_input":"2021-05-23T17:38:40.597119Z","iopub.status.idle":"2021-05-23T17:38:40.602243Z","shell.execute_reply.started":"2021-05-23T17:38:40.597088Z","shell.execute_reply":"2021-05-23T17:38:40.601405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['labels'] = np.array(answer)\ntest_df","metadata":{"execution":{"iopub.status.busy":"2021-05-23T17:39:11.926417Z","iopub.execute_input":"2021-05-23T17:39:11.926765Z","iopub.status.idle":"2021-05-23T17:39:11.937408Z","shell.execute_reply.started":"2021-05-23T17:39:11.926733Z","shell.execute_reply":"2021-05-23T17:39:11.936274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}