{"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":"# from google.colab import drive\n# drive.mount('/content/drive')","metadata":{"id":"Va7r0Imikx0t","outputId":"59410859-3bc3-4e94-e54f-c16ae8aab657"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ! mkdir -p plant-pathology-2021-fgvc8\n# ! unzip -q ./drive/MyDrive/01python工研院課程/專題/plant-pathology-2021-fgvc8.zip -d plant-pathology-2021-fgvc8","metadata":{"id":"jCPhEy7tlc3s"},"execution_count":null,"outputs":[]},{"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/FGVC8/python工研院課程/專題/data/train.csv')\ntrain.head()","metadata":{"id":"5PKEE4HSgvQU","outputId":"7ae8a759-ea46-4753-ec7b-af2869376ae5","execution":{"iopub.status.busy":"2021-07-15T04:59:18.293730Z","iopub.execute_input":"2021-07-15T04:59:18.294108Z","iopub.status.idle":"2021-07-15T04:59:18.321793Z","shell.execute_reply.started":"2021-07-15T04:59:18.294063Z","shell.execute_reply":"2021-07-15T04:59:18.321018Z"},"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":"7bvMUPi5gvQZ","outputId":"7867486c-ac91-41e6-e313-a1be400ec02a","execution":{"iopub.status.busy":"2021-07-15T04:59:20.850506Z","iopub.execute_input":"2021-07-15T04:59:20.850896Z","iopub.status.idle":"2021-07-15T04:59:21.694532Z","shell.execute_reply.started":"2021-07-15T04:59:20.850829Z","shell.execute_reply":"2021-07-15T04:59:21.693776Z"},"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":"04yCGWStgvQa","execution":{"iopub.status.busy":"2021-07-15T04:59:24.796406Z","iopub.execute_input":"2021-07-15T04:59:24.796738Z","iopub.status.idle":"2021-07-15T04:59:28.820126Z","shell.execute_reply.started":"2021-07-15T04:59:24.796706Z","shell.execute_reply":"2021-07-15T04:59:28.819068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 88\ntarget_size = (512, 512)\nbatch_size = 8\nresize_img = '../input/resized-plant2021/img_sz_512'\ntest_img = '../input/plant-pathology-2021-fgvc8/test_images'\nsubmission = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\n\n# colab\n# resize_img = '/content/drive/MyDrive/FGVC8/python工研院課程/專題/data/resize/img_sz_512'\n# test_img = '/content/drive/MyDrive/FGVC8/python工研院課程/專題/data/test_images'\n# submission = pd.read_csv('/content/drive/MyDrive/FGVC8/python工研院課程/專題/data/sample_submission.csv')\n\nsubmission.head()","metadata":{"id":"BYTMk30LgvQb","outputId":"095ee4ec-5e16-42d2-9cbc-bc73b4ae6c8f","execution":{"iopub.status.busy":"2021-07-15T05:00:14.904730Z","iopub.execute_input":"2021-07-15T05:00:14.905110Z","iopub.status.idle":"2021-07-15T05:00:14.922639Z","shell.execute_reply.started":"2021-07-15T05:00:14.905067Z","shell.execute_reply":"2021-07-15T05:00:14.921930Z"},"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":"4l7KnWMkgvQb","outputId":"045c1433-250c-490e-82b6-e4ff6364157c","execution":{"iopub.status.busy":"2021-07-15T05:00:17.765357Z","iopub.execute_input":"2021-07-15T05:00:17.765707Z","iopub.status.idle":"2021-07-15T05:00:39.719029Z","shell.execute_reply.started":"2021-07-15T05:00:17.765677Z","shell.execute_reply":"2021-07-15T05:00:39.718203Z"},"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":"L6AngUNigvQc","outputId":"93e4ebbb-8d84-48f1-a376-93aa50c6f36d","execution":{"iopub.status.busy":"2021-07-15T05:00:50.075122Z","iopub.execute_input":"2021-07-15T05:00:50.075467Z","iopub.status.idle":"2021-07-15T05:00:50.732588Z","shell.execute_reply.started":"2021-07-15T05:00:50.075438Z","shell.execute_reply":"2021-07-15T05:00:50.731747Z"},"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":"ROeCkp8egvQc","outputId":"4396c505-6258-4c08-e9b8-50d573fff17a","execution":{"iopub.status.busy":"2021-07-15T05:00:54.724440Z","iopub.execute_input":"2021-07-15T05:00:54.724770Z","iopub.status.idle":"2021-07-15T05:00:55.563615Z","shell.execute_reply.started":"2021-07-15T05:00:54.724741Z","shell.execute_reply":"2021-07-15T05:00:55.562875Z"},"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=(512,512,3))","metadata":{"execution":{"iopub.status.busy":"2021-07-10T06:59:36.799201Z","iopub.execute_input":"2021-07-10T06:59:36.799568Z","iopub.status.idle":"2021-07-10T06:59:45.884222Z","shell.execute_reply.started":"2021-07-10T06:59:36.799535Z","shell.execute_reply":"2021-07-10T06:59:45.882945Z"},"id":"C6hdBE7FgvQd","outputId":"60b3d89c-ce3c-466e-adaf-acd04cfc4a36","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-10T07:02:47.885327Z","iopub.execute_input":"2021-07-10T07:02:47.885697Z","iopub.status.idle":"2021-07-10T07:02:49.426387Z","shell.execute_reply.started":"2021-07-10T07:02:47.885638Z","shell.execute_reply":"2021-07-10T07:02:49.425234Z"},"id":"6UdGtZu0gvQd","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=0.001, decay=0.001/20),\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/FGVC8/python工研院課程/專題/data/0714_512_512_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# model_pre = model_pre.fit(train_generator,\n#             validation_data=valid_generator,\n#             # steps_per_epoch=1000,\n#             epochs=30,\n#             callbacks=[model_checkpoint, early_stopping])","metadata":{"execution":{"iopub.status.busy":"2021-07-10T07:03:02.576350Z","iopub.execute_input":"2021-07-10T07:03:02.576798Z"},"id":"Td-9DK7zgvQe","outputId":"28a87347-65b4-4eeb-c1c9-a8b9a58caeb4","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":{"id":"0VXsYb6GgvQe","outputId":"ba790d10-b424-416a-ac8e-666f07c54804","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":"ToWqNS_SgvQf","outputId":"09ced1dd-8ee3-411f-de0e-4b1667fb3e15","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#\nimport keras\ndef load_model():\n    model_pre = keras.models.load_model(\"../input/0714-512-512-b4/0714_512_512_B4.h5\")\n    return model_pre\nmodel = load_model()","metadata":{"id":"vmLrqBzKgvQf","execution":{"iopub.status.busy":"2021-07-15T05:01:26.989759Z","iopub.execute_input":"2021-07-15T05:01:26.990131Z","iopub.status.idle":"2021-07-15T05:01:38.346694Z","shell.execute_reply.started":"2021-07-15T05:01:26.990102Z","shell.execute_reply":"2021-07-15T05:01:38.345839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_model.trainable = True\n\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# model = model.fit(\n#     train_generator,\n#     validation_data=valid_generator,\n#     epochs=10,\n#     callbacks=[model_checkpoint, early_stopping])","metadata":{"id":"YYdWiDRhgvQg","outputId":"579bb23d-162b-4f40-83d2-3a2495d6c669","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":{"id":"kd18U63FgvQg","outputId":"c3e466de-39e9-44a1-cff7-196f9d9db6ff","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#\nmodel = load_model()\npred = model.predict(test_generator)","metadata":{"id":"J9fcnq_hgvQh","execution":{"iopub.status.busy":"2021-07-15T05:01:42.190458Z","iopub.execute_input":"2021-07-15T05:01:42.190796Z","iopub.status.idle":"2021-07-15T05:02:01.032706Z","shell.execute_reply.started":"2021-07-15T05:01:42.190765Z","shell.execute_reply":"2021-07-15T05:02:01.031562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (pred>0.40)\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":"HVArKwglgvQh","execution":{"iopub.status.busy":"2021-07-15T05:02:01.034694Z","iopub.execute_input":"2021-07-15T05:02:01.035013Z","iopub.status.idle":"2021-07-15T05:02:01.045133Z","shell.execute_reply.started":"2021-07-15T05:02:01.034982Z","shell.execute_reply":"2021-07-15T05:02:01.044205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.around(pred, decimals=1, out=None)","metadata":{"id":"SrRRaU59m6Lz","outputId":"9a3a2684-db54-4a75-f763-5900912afff7","execution":{"iopub.status.busy":"2021-07-15T05:02:09.822508Z","iopub.execute_input":"2021-07-15T05:02:09.822840Z","iopub.status.idle":"2021-07-15T05:02:09.830181Z","shell.execute_reply.started":"2021-07-15T05:02:09.822811Z","shell.execute_reply":"2021-07-15T05:02:09.829247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"id":"FMGTCJvbgvQh","outputId":"9e7714a7-212c-41c0-caa7-b99c5cf2721c","execution":{"iopub.status.busy":"2021-07-15T05:02:17.279001Z","iopub.execute_input":"2021-07-15T05:02:17.279329Z","iopub.status.idle":"2021-07-15T05:02:17.289406Z","shell.execute_reply.started":"2021-07-15T05:02:17.279299Z","shell.execute_reply":"2021-07-15T05:02:17.288613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"id":"PLJciqXLgvQh","execution":{"iopub.status.busy":"2021-07-15T05:02:24.061993Z","iopub.execute_input":"2021-07-15T05:02:24.062330Z","iopub.status.idle":"2021-07-15T05:02:24.070401Z","shell.execute_reply.started":"2021-07-15T05:02:24.062301Z","shell.execute_reply":"2021-07-15T05:02:24.069553Z"},"trusted":true},"execution_count":null,"outputs":[]}]}