{"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":"a0fe39cf-76c0-4577-8ac5-e1ec56960b5a","execution":{"iopub.status.busy":"2021-07-16T15:20:57.272590Z","iopub.execute_input":"2021-07-16T15:20:57.272985Z","iopub.status.idle":"2021-07-16T15:20:57.399631Z","shell.execute_reply.started":"2021-07-16T15:20:57.272905Z","shell.execute_reply":"2021-07-16T15:20:57.398712Z"},"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":"aff1b1f9-9909-4b3b-e771-8aad543aa5df","execution":{"iopub.status.busy":"2021-07-16T15:20:57.401233Z","iopub.execute_input":"2021-07-16T15:20:57.401599Z","iopub.status.idle":"2021-07-16T15:20:58.300406Z","shell.execute_reply.started":"2021-07-16T15:20:57.401562Z","shell.execute_reply":"2021-07-16T15:20:58.299449Z"},"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-16T15:20:58.302156Z","iopub.execute_input":"2021-07-16T15:20:58.302516Z","iopub.status.idle":"2021-07-16T15:21:02.560264Z","shell.execute_reply.started":"2021-07-16T15:20:58.302480Z","shell.execute_reply":"2021-07-16T15:21:02.559379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 88\ntarget_size = (384, 384)\nbatch_size = 16\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":"cf60d79a-948f-43e8-ea75-f0d0cfaaa2c0","execution":{"iopub.status.busy":"2021-07-16T15:23:04.551383Z","iopub.execute_input":"2021-07-16T15:23:04.551741Z","iopub.status.idle":"2021-07-16T15:23:04.567628Z","shell.execute_reply.started":"2021-07-16T15:23:04.551711Z","shell.execute_reply":"2021-07-16T15:23:04.566863Z"},"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=8,\n                  target_size = target_size,\n                  color_mode=\"rgb\",\n                  shuffle = False,\n                  seed = seed,)","metadata":{"id":"2qzpa52m4lZO","outputId":"ab0c0633-be7e-4a33-e0fa-522f1cd26c69","execution":{"iopub.status.busy":"2021-07-16T15:23:07.032254Z","iopub.execute_input":"2021-07-16T15:23:07.032593Z","iopub.status.idle":"2021-07-16T15:23:14.715061Z","shell.execute_reply.started":"2021-07-16T15:23:07.032563Z","shell.execute_reply":"2021-07-16T15:23:14.713453Z"},"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":"302a428f-206d-418e-e808-f8a145f6c3da","execution":{"iopub.status.busy":"2021-07-16T15:23:25.858166Z","iopub.execute_input":"2021-07-16T15:23:25.858589Z","iopub.status.idle":"2021-07-16T15:23:26.788096Z","shell.execute_reply.started":"2021-07-16T15:23:25.858549Z","shell.execute_reply":"2021-07-16T15:23:26.787244Z"},"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":"c0b23c4b-01c5-4c2e-a435-3914511032b1","execution":{"iopub.status.busy":"2021-07-16T15:23:30.055583Z","iopub.execute_input":"2021-07-16T15:23:30.055920Z","iopub.status.idle":"2021-07-16T15:23:30.890162Z","shell.execute_reply.started":"2021-07-16T15:23:30.055892Z","shell.execute_reply":"2021-07-16T15:23:30.886685Z"},"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 EfficientNetB4\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 = EfficientNetB4(include_top=False, \n#                 weights='imagenet',\n#                 pooling='avg', \n#                 input_shape=(384,384,3))","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":"30b7dc8c-52db-4022-8ac2-d031db894b97","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/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\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":"7ed490bf-92b4-4090-f74f-ccdcde78e1c7","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":"c45be899-dcac-4c8d-e000-70742db368fd","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":"f0a7d9ad-a244-4a99-d0a9-e6478980e2cb"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\ndef load_model():\n    model_pre = keras.models.load_model(\"../input/0716-384-rlr-b4/0716_384_rlr_B4.h5\")\n    return model_pre\nmodel = load_model()","metadata":{"id":"aGqy9lEr4lZS","execution":{"iopub.status.busy":"2021-07-16T15:24:06.099081Z","iopub.execute_input":"2021-07-16T15:24:06.099411Z","iopub.status.idle":"2021-07-16T15:24:17.562705Z","shell.execute_reply.started":"2021-07-16T15:24:06.099380Z","shell.execute_reply":"2021-07-16T15:24:17.561805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_model.trainable = True\n\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=8,\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":"6b667508-c88c-4e34-99b8-4f7442de8142","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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":"490eccc8-ca5a-4d98-9640-3751c7c5043e","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-16T15:24:23.526943Z","iopub.execute_input":"2021-07-16T15:24:23.527289Z","iopub.status.idle":"2021-07-16T15:24:41.987814Z","shell.execute_reply.started":"2021-07-16T15:24:23.527258Z","shell.execute_reply":"2021-07-16T15:24:41.986629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (pred>0.3)\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-16T15:34:14.697846Z","iopub.execute_input":"2021-07-16T15:34:14.698168Z","iopub.status.idle":"2021-07-16T15:34:14.703766Z","shell.execute_reply.started":"2021-07-16T15:34:14.698139Z","shell.execute_reply":"2021-07-16T15:34:14.702916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"id":"eNJweZzMHxr5","execution":{"iopub.status.busy":"2021-07-16T15:34:16.934793Z","iopub.execute_input":"2021-07-16T15:34:16.935138Z","iopub.status.idle":"2021-07-16T15:34:16.941289Z","shell.execute_reply.started":"2021-07-16T15:34:16.935107Z","shell.execute_reply":"2021-07-16T15:34:16.940255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(pred, decimals=1, out=None)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T15:34:19.290029Z","iopub.execute_input":"2021-07-16T15:34:19.290505Z","iopub.status.idle":"2021-07-16T15:34:19.297227Z","shell.execute_reply.started":"2021-07-16T15:34:19.290453Z","shell.execute_reply":"2021-07-16T15:34:19.296215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"id":"Qbv9SGm74lZT","outputId":"f98f65c8-6982-4dd7-fbeb-454ac4279a81","execution":{"iopub.status.busy":"2021-07-16T15:34:21.530928Z","iopub.execute_input":"2021-07-16T15:34:21.531243Z","iopub.status.idle":"2021-07-16T15:34:21.541007Z","shell.execute_reply.started":"2021-07-16T15:34:21.531212Z","shell.execute_reply":"2021-07-16T15:34:21.539879Z"},"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-16T15:34:24.176812Z","iopub.execute_input":"2021-07-16T15:34:24.177129Z","iopub.status.idle":"2021-07-16T15:34:24.182803Z","shell.execute_reply.started":"2021-07-16T15:34:24.177100Z","shell.execute_reply":"2021-07-16T15:34:24.181737Z"},"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":[]}]}