{"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":"markdown","source":"## Import thư viện","metadata":{}},{"cell_type":"code","source":"import cv2\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.optimizers import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.preprocessing import MultiLabelBinarizer\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-30T03:36:22.644589Z","iopub.execute_input":"2021-11-30T03:36:22.644883Z","iopub.status.idle":"2021-11-30T03:36:28.662539Z","shell.execute_reply.started":"2021-11-30T03:36:22.644805Z","shell.execute_reply":"2021-11-30T03:36:28.661623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.system('pip install /kaggle/input/kerasapplications -q')\nos.system('pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps')\n\nimport efficientnet.tfkeras as efn","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:36:28.665603Z","iopub.execute_input":"2021-11-30T03:36:28.665879Z","iopub.status.idle":"2021-11-30T03:37:21.161992Z","shell.execute_reply.started":"2021-11-30T03:36:28.665846Z","shell.execute_reply":"2021-11-30T03:37:21.161270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show kết quả quá trình training","metadata":{}},{"cell_type":"code","source":"def plot_hist(path):\n  history = pd.read_csv(path)\n\n  acc = history['accuracy']\n  val_acc = history['val_accuracy']\n\n  loss = history['loss']\n  val_loss = history['val_loss']\n  plt.style.use('fivethirtyeight')\n  plt.figure(figsize=(20, 10))\n\n  plt.subplot(1, 2, 1)\n  plt.plot(acc, label='Training Accuracy')\n  plt.plot(val_acc, label='Validation Accuracy')\n  plt.legend(loc='lower right')\n  plt.ylabel('Accuracy')\n  plt.ylim([min(plt.ylim()), 1])\n  plt.title('Training and Validation Accuracy')\n  plt.xlabel('epoch')\n\n  plt.subplot(1, 2, 2)\n  plt.plot(loss, label='Training Loss')\n  plt.plot(val_loss, label='Validation Loss')\n  plt.legend(loc='upper right')\n  plt.ylabel('Categorical Crossentropy')\n  plt.ylim([min(plt.ylim()), max(plt.ylim())])\n  plt.title('Training and Validation Loss')\n\n  plt.xlabel('epoch')\n  plt.savefig('evaluation.jpg')\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:21.163405Z","iopub.execute_input":"2021-11-30T03:37:21.163644Z","iopub.status.idle":"2021-11-30T03:37:21.172610Z","shell.execute_reply.started":"2021-11-30T03:37:21.163612Z","shell.execute_reply":"2021-11-30T03:37:21.171856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(\"../input/efficientnetb0/FGVC8-efn-b0.log\")","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:21.175326Z","iopub.execute_input":"2021-11-30T03:37:21.175736Z","iopub.status.idle":"2021-11-30T03:37:21.765489Z","shell.execute_reply.started":"2021-11-30T03:37:21.175699Z","shell.execute_reply":"2021-11-30T03:37:21.764761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load model","metadata":{}},{"cell_type":"code","source":"HEIGHT = 480\nWIDTH = 480\nCHANNELS = 3\nCLASSES = 6\ntop_dropout_rate = 0.2\nAUTO = tf.data.experimental.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:21.766439Z","iopub.execute_input":"2021-11-30T03:37:21.766662Z","iopub.status.idle":"2021-11-30T03:37:21.771702Z","shell.execute_reply.started":"2021-11-30T03:37:21.766629Z","shell.execute_reply":"2021-11-30T03:37:21.771003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path = \"../input/efficientnetb0/FGVC8-efn-b0.h5\"","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:21.772886Z","iopub.execute_input":"2021-11-30T03:37:21.773650Z","iopub.status.idle":"2021-11-30T03:37:21.781900Z","shell.execute_reply.started":"2021-11-30T03:37:21.773614Z","shell.execute_reply":"2021-11-30T03:37:21.780940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model():\n    base_model = efn.EfficientNetB0(include_top=False, weights=None, input_shape=(HEIGHT, WIDTH, 3))\n\n    x = base_model.output\n    x = GlobalAveragePooling2D()(x)\n    x = Dropout(top_dropout_rate)(x)\n    outputs = Dense(CLASSES, activation='sigmoid')(x)\n    \n    return Model(base_model.input, outputs)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:21.783522Z","iopub.execute_input":"2021-11-30T03:37:21.783886Z","iopub.status.idle":"2021-11-30T03:37:21.791834Z","shell.execute_reply.started":"2021-11-30T03:37:21.783849Z","shell.execute_reply":"2021-11-30T03:37:21.791119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model()\nmodel.load_weights(weights_path)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:21.794576Z","iopub.execute_input":"2021-11-30T03:37:21.794760Z","iopub.status.idle":"2021-11-30T03:37:26.236527Z","shell.execute_reply.started":"2021-11-30T03:37:21.794738Z","shell.execute_reply":"2021-11-30T03:37:26.235742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load data test","metadata":{}},{"cell_type":"code","source":"test_img = '../input/plant-pathology-2021-fgvc8/test_images'\nsubmission = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:26.237763Z","iopub.execute_input":"2021-11-30T03:37:26.238026Z","iopub.status.idle":"2021-11-30T03:37:26.258633Z","shell.execute_reply.started":"2021-11-30T03:37:26.237991Z","shell.execute_reply":"2021-11-30T03:37:26.257988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing test data","metadata":{}},{"cell_type":"code","source":"def load_image(image_id):\n    file_path = str(image_id)\n    img = cv2.imread(test_img+'/'+file_path)\n    return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:26.261200Z","iopub.execute_input":"2021-11-30T03:37:26.261466Z","iopub.status.idle":"2021-11-30T03:37:26.266153Z","shell.execute_reply.started":"2021-11-30T03:37:26.261434Z","shell.execute_reply":"2021-11-30T03:37:26.265283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_full_augment(image):\n    \n    p_spatial = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_rotate = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_crop = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_pixel_1 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_pixel_2 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_pixel_3 = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    \n    flag = False\n\n    # Pixel-level transforms\n    if p_pixel_1 >= .4:\n        image = tf.image.random_saturation(image, lower=.7, upper=1.3)\n        flag = True\n    if p_pixel_2 >= .4:\n        image = tf.image.random_contrast(image, lower=.8, upper=1.2)\n        flag = True\n    if p_pixel_3 >= .4:\n        image = tf.image.random_brightness(image, max_delta=.1)\n        flag = True\n\n\n    # Flips\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    if p_spatial > .75:\n        image = tf.image.transpose(image)\n        flag = True\n        \n    # Rotates\n    if p_rotate > .75:\n        image = tf.image.rot90(image, k=3) # rotate 270º\n        flag = True\n    elif p_rotate > .5:\n        image = tf.image.rot90(image, k=2) # rotate 180º\n        flag = True\n    elif p_rotate > .25:\n        image = tf.image.rot90(image, k=1) # rotate 90\n        flag = True\n        \n    # Crops\n    if p_crop > .7:\n        if p_crop > .9:\n            image = tf.image.central_crop(image, central_fraction=.7)\n        elif p_crop > .8:\n            image = tf.image.central_crop(image, central_fraction=.8)\n        else:\n            image = tf.image.central_crop(image, central_fraction=.9)\n        flag = True\n    elif p_crop > .4:\n        HEIGHT1 = image.shape[0]\n        WIDTH1 = image.shape[1]\n        crop_size_h = tf.random.uniform([], int(HEIGHT1*.8), HEIGHT1, dtype=tf.float32)\n        crop_size_w = tf.random.uniform([], int(WIDTH1*.8), WIDTH1, dtype=tf.float32)\n        image = tf.image.random_crop(image, size=[crop_size_h, crop_size_w, 3])\n        flag = True\n    \n    return image","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:26.267793Z","iopub.execute_input":"2021-11-30T03:37:26.268089Z","iopub.status.idle":"2021-11-30T03:37:26.284033Z","shell.execute_reply.started":"2021-11-30T03:37:26.268053Z","shell.execute_reply":"2021-11-30T03:37:26.283125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process(path):\n    img = load_image(path)\n    img = data_full_augment(img)\n    img = tf.cast(img, tf.float32) / 255.0\n    img = np.array(img)\n    return cv2.resize(img , (480, 480)).reshape(-1, 480, 480, 3)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:26.285096Z","iopub.execute_input":"2021-11-30T03:37:26.285923Z","iopub.status.idle":"2021-11-30T03:37:26.294929Z","shell.execute_reply.started":"2021-11-30T03:37:26.285882Z","shell.execute_reply":"2021-11-30T03:37:26.294090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict","metadata":{}},{"cell_type":"code","source":"preds = []\nfor i in range(len(submission['image'])):\n    test_images = submission['image'][i]\n    pred = model.predict(process(test_images))[0]\n    preds.append(pred)\n    \npreds = np.array(preds)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:26.297694Z","iopub.execute_input":"2021-11-30T03:37:26.297895Z","iopub.status.idle":"2021-11-30T03:37:34.858628Z","shell.execute_reply.started":"2021-11-30T03:37:26.297870Z","shell.execute_reply":"2021-11-30T03:37:34.857762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"perdict = (preds>0.35)\nn_label = ['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']\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    if len(temp) > 1 and \"healthy\" in temp:\n        temp.remove(\"healthy\")\n    elif len(temp) == 0:\n        temp.append('healthy')\n    answer.append(temp)\n    \nanswer = [' '.join(n) for n in answer]","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:34.859918Z","iopub.execute_input":"2021-11-30T03:37:34.860196Z","iopub.status.idle":"2021-11-30T03:37:34.868889Z","shell.execute_reply.started":"2021-11-30T03:37:34.860159Z","shell.execute_reply":"2021-11-30T03:37:34.868129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['labels'] = np.array(answer)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:34.870327Z","iopub.execute_input":"2021-11-30T03:37:34.871207Z","iopub.status.idle":"2021-11-30T03:37:34.884082Z","shell.execute_reply.started":"2021-11-30T03:37:34.871165Z","shell.execute_reply":"2021-11-30T03:37:34.883181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T03:37:34.885650Z","iopub.execute_input":"2021-11-30T03:37:34.885986Z","iopub.status.idle":"2021-11-30T03:37:34.894930Z","shell.execute_reply.started":"2021-11-30T03:37:34.885946Z","shell.execute_reply":"2021-11-30T03:37:34.893987Z"},"trusted":true},"execution_count":null,"outputs":[]}]}