{"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 os\nimport numpy as np\nimport pandas as pd\ntrain = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\n# train = pd.read_csv('/content/drive/MyDrive/01python工研院課程/專題/data/train.csv')\n\ntrain.head()","metadata":{"id":"Q19brw5I4lZJ","outputId":"a6147864-1ee4-42dc-f612-214d442a56bf","execution":{"iopub.status.busy":"2021-07-18T11:04:49.905602Z","iopub.execute_input":"2021-07-18T11:04:49.905985Z","iopub.status.idle":"2021-07-18T11:04:50.063716Z","shell.execute_reply.started":"2021-07-18T11:04:49.905903Z","shell.execute_reply":"2021-07-18T11:04:50.062711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MultiLabelBinarizer\nmb = MultiLabelBinarizer().fit(train.labels.apply(lambda x : x.split()))\nlabels = pd.DataFrame(mb.transform(train.labels.apply(lambda x : x.split())), columns = mb.classes_)\nnew_train = pd.concat([train['image'], labels], axis=1)\nnew_train.head()","metadata":{"id":"jd8rLNbw4lZN","outputId":"ac67f96e-fe24-44be-a94c-23ac5a733902","execution":{"iopub.status.busy":"2021-07-18T11:06:16.867325Z","iopub.execute_input":"2021-07-18T11:06:16.867747Z","iopub.status.idle":"2021-07-18T11:06:18.038298Z","shell.execute_reply.started":"2021-07-18T11:06:16.867711Z","shell.execute_reply":"2021-07-18T11:06:18.037165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\npre = ImageDataGenerator(\n    rescale = 1/255.,\n    rotation_range = 15,\n    width_shift_range=0.2, \n    height_shift_range=0.2,\n    zoom_range = 0.2,\n    shear_range=0.3,\n    featurewise_center=True,\n    featurewise_std_normalization=True,\n    horizontal_flip=True,\n    vertical_flip=True,\n    validation_split= 0.2,)\npos = ImageDataGenerator(rescale = 1./255)","metadata":{"id":"uaBlBmg54lZN","execution":{"iopub.status.busy":"2021-07-18T11:06:20.178169Z","iopub.execute_input":"2021-07-18T11:06:20.178583Z","iopub.status.idle":"2021-07-18T11:06:26.823653Z","shell.execute_reply.started":"2021-07-18T11:06:20.178550Z","shell.execute_reply":"2021-07-18T11:06:26.822520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 88\ntarget_size = (384, 384)\nbatch_size = 8\nresize_img = '../input/resized-plant2021/img_sz_384'\ntest_img = '../input/plant-pathology-2021-fgvc8/test_images'\nsubmission = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsubmission.head()","metadata":{"id":"bEFSk7JO2T_p","outputId":"cff76f4f-ec39-4b35-9f1b-19cf57e329af","execution":{"iopub.status.busy":"2021-07-18T11:06:59.378182Z","iopub.execute_input":"2021-07-18T11:06:59.378671Z","iopub.status.idle":"2021-07-18T11:06:59.401663Z","shell.execute_reply.started":"2021-07-18T11:06:59.378633Z","shell.execute_reply":"2021-07-18T11:06:59.400850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = pre.flow_from_dataframe(\n                  new_train,\n                  directory = resize_img, \n                  x_col = 'image',\n                  y_col = new_train.columns.tolist()[1:],\n                  subset = \"training\",\n                  color_mode = \"rgb\",\n                  target_size = target_size,\n                  class_mode = \"raw\",\n                  batch_size = batch_size,\n                  shuffle = True,\n                  seed = seed,)\nvalid_generator = pre.flow_from_dataframe(\n                  new_train,\n                  directory = resize_img,\n                  x_col = 'image',\n                  y_col = new_train.columns.tolist()[1:],\n                  subset = \"validation\",\n                  color_mode = \"rgb\",\n                  target_size = target_size,\n                  class_mode = \"raw\",\n                  batch_size = batch_size,\n                  shuffle = True,\n                  seed = seed,)\ntest_generator = pos.flow_from_dataframe(\n                  submission,\n                  directory = test_img,\n                  x_col = 'image',\n                  y_col = 'labels',\n                  class_mode = None,\n                  batch_size=4,\n                  target_size = target_size,\n                  color_mode=\"rgb\",\n                  shuffle = False,\n                  seed = seed,)","metadata":{"id":"2qzpa52m4lZO","outputId":"378d7816-c75d-4124-fb70-a238b511cdf8","execution":{"iopub.status.busy":"2021-07-18T11:07:03.970332Z","iopub.execute_input":"2021-07-18T11:07:03.970907Z","iopub.status.idle":"2021-07-18T11:07:48.903828Z","shell.execute_reply.started":"2021-07-18T11:07:03.970867Z","shell.execute_reply":"2021-07-18T11:07:48.902679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nexample = next(train_generator)\nprint(example[0].shape)\nplt.imshow(example[0][0,:,:,:])\nplt.show()","metadata":{"id":"y-9GfAiH4lZP","outputId":"d7428ae1-f755-4bfe-a6d4-75986b5cd4d5","execution":{"iopub.status.busy":"2021-07-18T11:12:06.041670Z","iopub.execute_input":"2021-07-18T11:12:06.042123Z","iopub.status.idle":"2021-07-18T11:12:06.699712Z","shell.execute_reply.started":"2021-07-18T11:12:06.042082Z","shell.execute_reply":"2021-07-18T11:12:06.698681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nexample = next(test_generator)\nprint(example[0].shape)\nplt.imshow(example[0])\nplt.show()","metadata":{"id":"1Ile26Jr4lZP","outputId":"d8d82fb8-c7b3-424d-c599-bd433f9d79e6","execution":{"iopub.status.busy":"2021-07-18T11:12:08.972532Z","iopub.execute_input":"2021-07-18T11:12:08.972985Z","iopub.status.idle":"2021-07-18T11:12:09.952125Z","shell.execute_reply.started":"2021-07-18T11:12:08.972942Z","shell.execute_reply":"2021-07-18T11:12:09.951100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import tensorflow as tf\n# from tensorflow import keras\n# from tensorflow.keras.applications import EfficientNetB5\n# from tensorflow.keras import layers, optimizers\n# from tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D, BatchNormalization\n# from keras import Model\n# base_model = EfficientNetB5(include_top=False, \n#                 weights='imagenet',\n#                 pooling='avg', \n#                 input_shape=(384,384,3))\n# # base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-08T23:43:06.130056Z","iopub.execute_input":"2021-07-08T23:43:06.132817Z","iopub.status.idle":"2021-07-08T23:43:13.363736Z","shell.execute_reply.started":"2021-07-08T23:43:06.132771Z","shell.execute_reply":"2021-07-08T23:43:13.362895Z"},"id":"6f2y78WA4lZQ","outputId":"940eae98-c4e6-4a10-e02e-a442d6be88ca","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_pre = tf.keras.Sequential([\n#         base_model,\n#         layers.BatchNormalization(),\n#         layers.Dense(32,activation='relu'),\n#         layers.Dropout(0.4),\n#         layers.Dense(16,activation='relu'),\n#         layers.Dropout(0.4),\n#         layers.Dense(6,activation='sigmoid')\n# ])","metadata":{"execution":{"iopub.status.busy":"2021-07-08T23:43:13.365900Z","iopub.execute_input":"2021-07-08T23:43:13.366282Z","iopub.status.idle":"2021-07-08T23:43:14.520590Z","shell.execute_reply.started":"2021-07-08T23:43:13.366248Z","shell.execute_reply":"2021-07-08T23:43:14.519751Z"},"id":"YE3HN-V-4lZQ","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras.callbacks import ModelCheckpoint, EarlyStopping\n# model_pre.compile(\n#     optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),\n#     loss='binary_crossentropy',\n#     metrics=[tf.keras.metrics.BinaryAccuracy(name='binary_accuracy'),tf.keras.metrics.AUC(name='auc')])\n# model_checkpoint = ModelCheckpoint(\n#             filepath=\"/content/drive/MyDrive/01python工研院課程/專題/data/0717_384_rlr_B5.h5\", \n#             mode='max',\n#             monitor='val_auc', \n#             save_best_only=True, \n#             save_freq=\"epoch\", \n#             verbose=1)\n# early_stopping = EarlyStopping(\n#             monitor='val_loss',\n#             mode='min', \n#             min_delta=0.003,\n#             patience=10, \n#             verbose=1, \n#             restore_best_weights=True)\n\n# rlr = keras.callbacks.ReduceLROnPlateau(\n#             monitor='val_auc', \n#             mode='auto',\n#             factor=0.2, \n#             patience=2, \n#             verbose=1, \n#             min_delta=0.0001)\n\n# model_pre = model_pre.fit(train_generator,\n#             validation_data=valid_generator,\n#             steps_per_epoch=(train_generator.samples//batch_size),\n#             validation_steps=(valid_generator.samples//batch_size),\n#             epochs=30,\n#             callbacks=[model_checkpoint, early_stopping])\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-08T23:43:14.521971Z","iopub.execute_input":"2021-07-08T23:43:14.522311Z","iopub.status.idle":"2021-07-09T02:14:58.597718Z","shell.execute_reply.started":"2021-07-08T23:43:14.522276Z","shell.execute_reply":"2021-07-09T02:14:58.596703Z"},"id":"j6NDnVs24lZQ","outputId":"0fb54f6f-2c8d-4031-de44-2bee6ff40f4c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n\n# pre_history = model_pre.history\n\n# plt.figure()\n# plt.plot(pre_history['binary_accuracy'])\n# plt.plot(pre_history['val_binary_accuracy'])\n# plt.title('model binary_accuracy')\n# plt.ylabel('accuracy')\n# plt.xlabel('epoch')\n# plt.legend(['train', 'validation'])\n# plt.savefig('accuracy')\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T02:18:04.014927Z","iopub.execute_input":"2021-07-09T02:18:04.015256Z","iopub.status.idle":"2021-07-09T02:18:04.248328Z","shell.execute_reply.started":"2021-07-09T02:18:04.015225Z","shell.execute_reply":"2021-07-09T02:18:04.247313Z"},"id":"KVD0fV_t4lZR","outputId":"ce9ee8f5-e7bd-4b78-a528-274d7aface67","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pre_history = model_pre.history\n\n# plt.figure()\n# plt.plot(pre_history['auc'])\n# plt.plot(pre_history['val_auc'])\n# plt.title('model auc')\n# plt.ylabel('auc')\n# plt.xlabel('epoch')\n# plt.legend(['train', 'validation'])\n# plt.savefig('auc')\n# plt.show()","metadata":{"id":"zedJQTPH3tfd","outputId":"066b9469-d285-4833-8062-9748aee67c73"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\ndef load_model():\n    model_pre = keras.models.load_model(\"../input/0717-384-b5/0717_384_rlr_B5.h5\")\n    return model_pre\nmodel = load_model()","metadata":{"id":"aGqy9lEr4lZS","execution":{"iopub.status.busy":"2021-07-18T11:13:09.173773Z","iopub.execute_input":"2021-07-18T11:13:09.174162Z","iopub.status.idle":"2021-07-18T11:13:22.545894Z","shell.execute_reply.started":"2021-07-18T11:13:09.174127Z","shell.execute_reply":"2021-07-18T11:13:22.544788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_model.trainable = True\n# ### # old test t2\n# from keras.callbacks import ModelCheckpoint, EarlyStopping\n\n# model_checkpoint = ModelCheckpoint(\n#             filepath=\"/content/drive/MyDrive/01python工研院課程/專題/data/0716_384_rlr_B4.h5\", \n#             mode='max',\n#             monitor='val_auc', \n#             save_best_only=True, \n#             save_freq=\"epoch\", \n#             verbose=1)\n# early_stopping = EarlyStopping(\n#             monitor='val_loss',\n#             mode='min', \n#             min_delta=0.003,\n#             patience=10, \n#             verbose=1, \n#             restore_best_weights=True)\n\n# rlr = keras.callbacks.ReduceLROnPlateau(\n#             monitor='val_auc', \n#             mode='auto',\n#             factor=0.2, \n#             patience=2, \n#             verbose=1, \n#             min_delta=0.0001)\n# # # old test t2\n# # model.compile(\n# #     optimizer=keras.optimizers.Adam(1e-5, decay=1e-5/10),\n# #     loss=keras.losses.BinaryCrossentropy(from_logits=True),\n# #     metrics=[tf.keras.metrics.BinaryAccuracy(name='binary_accuracy'),tf.keras.metrics.AUC(name='auc')])\n# ###\n# # new test t1\n# test_generator = pos.flow_from_dataframe(\n#                   submission,\n#                   directory = test_img,\n#                   x_col = 'image',\n#                   y_col = 'labels',\n#                   class_mode = None,\n#                   batch_size=4,\n#                   target_size = target_size,\n#                   color_mode=\"rgb\",\n#                   shuffle = False,\n#                   seed = seed,)\n\n# model = model.fit(\n#     train_generator,\n#     validation_data=valid_generator,\n#     steps_per_epoch=(train_generator.samples//batch_size),\n#     validation_steps=(valid_generator.samples//batch_size),\n#     epochs=30,\n#     callbacks=[model_checkpoint, early_stopping])","metadata":{"execution":{"iopub.status.busy":"2021-07-09T02:18:42.042547Z","iopub.execute_input":"2021-07-09T02:18:42.042867Z","iopub.status.idle":"2021-07-09T05:20:27.617622Z","shell.execute_reply.started":"2021-07-09T02:18:42.042835Z","shell.execute_reply":"2021-07-09T05:20:27.616692Z"},"id":"sKbOgN7k4lZS","outputId":"38d36750-06b8-4eff-b9f2-faddae01015e","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n# model_history = model.history\n\n# plt.figure()\n# plt.plot(model_history['binary_accuracy'])\n# plt.plot(model_history['val_binary_accuracy'])\n# plt.title('model binary_accuracy')\n# plt.ylabel('accuracy')\n# plt.xlabel('epoch')\n# plt.legend(['train', 'validation'])\n# plt.savefig('accuracy')\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T05:20:55.759798Z","iopub.execute_input":"2021-07-09T05:20:55.760170Z","iopub.status.idle":"2021-07-09T05:20:55.950853Z","shell.execute_reply.started":"2021-07-09T05:20:55.760138Z","shell.execute_reply":"2021-07-09T05:20:55.950113Z"},"id":"zGSFR2lc4lZS","outputId":"e96718a6-c9a4-4cbb-f242-e84e48a3cd68","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model()\npred = model.predict(test_generator)","metadata":{"id":"SKnxmDRT4lZT","execution":{"iopub.status.busy":"2021-07-18T11:13:26.300654Z","iopub.execute_input":"2021-07-18T11:13:26.301123Z","iopub.status.idle":"2021-07-18T11:13:46.226977Z","shell.execute_reply.started":"2021-07-18T11:13:26.301087Z","shell.execute_reply":"2021-07-18T11:13:46.226082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (pred>0.23)\nn_label = new_train.columns.tolist()[1:]\nanswer = []\n\nfor i in range(perdict.shape[0]):\n    temp = []\n    for j, k in enumerate(n_label):\n        if perdict[i, j]:\n            temp.append(k)\n    answer.append(temp)\n    \nanswer = [' '.join(n) for n in answer]","metadata":{"id":"BfdudNBq4lZT","execution":{"iopub.status.busy":"2021-07-18T11:13:53.343782Z","iopub.execute_input":"2021-07-18T11:13:53.344336Z","iopub.status.idle":"2021-07-18T11:13:53.351136Z","shell.execute_reply.started":"2021-07-18T11:13:53.344278Z","shell.execute_reply":"2021-07-18T11:13:53.350415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"id":"eNJweZzMHxr5","outputId":"d9c1735b-8d3b-4603-91e8-902db9670ae4"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(pred, decimals=3, out=None)","metadata":{"id":"LdcFf6zrGoqg","outputId":"77b815a0-f6ee-466b-e6c0-221d2c2d0de9","execution":{"iopub.status.busy":"2021-07-18T11:14:06.220564Z","iopub.execute_input":"2021-07-18T11:14:06.221199Z","iopub.status.idle":"2021-07-18T11:14:06.228983Z","shell.execute_reply.started":"2021-07-18T11:14:06.221124Z","shell.execute_reply":"2021-07-18T11:14:06.227893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"id":"Qbv9SGm74lZT","outputId":"757a017a-a0e8-424b-df6c-693c00c396b0","execution":{"iopub.status.busy":"2021-07-18T11:14:21.229746Z","iopub.execute_input":"2021-07-18T11:14:21.230163Z","iopub.status.idle":"2021-07-18T11:14:21.242095Z","shell.execute_reply.started":"2021-07-18T11:14:21.230126Z","shell.execute_reply":"2021-07-18T11:14:21.241041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"id":"H7KCvpCL4lZT","execution":{"iopub.status.busy":"2021-07-18T11:14:38.289038Z","iopub.execute_input":"2021-07-18T11:14:38.289471Z","iopub.status.idle":"2021-07-18T11:14:38.298518Z","shell.execute_reply.started":"2021-07-18T11:14:38.289434Z","shell.execute_reply":"2021-07-18T11:14:38.297266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !mv /content/content/drive/MyDrive/FGVC8/python工研院課程/專題/data/0713test_B4.h5 /content/drive/MyDrive/FGVC8/python工研院課程/專題/data","metadata":{"id":"fKu8VYrlLgL9"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"HgFXo9g6k2NS"},"execution_count":null,"outputs":[]}]}