{"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 usefull library","metadata":{}},{"cell_type":"code","source":"import cv2\nimport random\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-02T03:50:27.520452Z","iopub.execute_input":"2021-12-02T03:50:27.520719Z","iopub.status.idle":"2021-12-02T03:50:33.531848Z","shell.execute_reply.started":"2021-12-02T03:50:27.520637Z","shell.execute_reply":"2021-12-02T03:50:33.531121Z"},"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","metadata":{"execution":{"iopub.status.busy":"2021-12-02T03:51:02.543271Z","iopub.execute_input":"2021-12-02T03:51:02.544064Z","iopub.status.idle":"2021-12-02T03:51:02.549498Z","shell.execute_reply.started":"2021-12-02T03:51:02.544015Z","shell.execute_reply":"2021-12-02T03:51:02.548327Z"},"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-12-02T03:51:05.863884Z","iopub.execute_input":"2021-12-02T03:51:05.864727Z","iopub.status.idle":"2021-12-02T03:51:59.240285Z","shell.execute_reply.started":"2021-12-02T03:51:05.864689Z","shell.execute_reply":"2021-12-02T03:51:59.239394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path = \"../input/effnet-b5-model/EfficientNet_B5_Fullaug.h5\"","metadata":{"execution":{"iopub.status.busy":"2021-12-02T03:51:59.245451Z","iopub.execute_input":"2021-12-02T03:51:59.24579Z","iopub.status.idle":"2021-12-02T03:51:59.25372Z","shell.execute_reply.started":"2021-12-02T03:51:59.245752Z","shell.execute_reply":"2021-12-02T03:51:59.252748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model():\n    base_model = efn.EfficientNetB5(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    model = Model(base_model.input, outputs) \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:01:02.657268Z","iopub.execute_input":"2021-12-02T04:01:02.657565Z","iopub.status.idle":"2021-12-02T04:01:02.666736Z","shell.execute_reply.started":"2021-12-02T04:01:02.657533Z","shell.execute_reply":"2021-12-02T04:01:02.665874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model()\nmodel.load_weights(weights_path)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:01:04.681768Z","iopub.execute_input":"2021-12-02T04:01:04.682028Z","iopub.status.idle":"2021-12-02T04:01:10.149822Z","shell.execute_reply.started":"2021-12-02T04:01:04.682Z","shell.execute_reply":"2021-12-02T04:01:10.148942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing 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-12-02T04:01:10.151458Z","iopub.execute_input":"2021-12-02T04:01:10.151719Z","iopub.status.idle":"2021-12-02T04:01:10.18051Z","shell.execute_reply.started":"2021-12-02T04:01:10.151684Z","shell.execute_reply":"2021-12-02T04:01:10.179714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-02T04:01:10.181842Z","iopub.execute_input":"2021-12-02T04:01:10.182173Z","iopub.status.idle":"2021-12-02T04:01:10.186951Z","shell.execute_reply.started":"2021-12-02T04:01:10.182123Z","shell.execute_reply":"2021-12-02T04:01:10.186199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def insect_augmentation(image, n_insects = 2, dark_insect=False, p = 0.5, insects_folder='../input/insect/insect'):\n    aug_prob = random.random()\n    flag = False\n    if aug_prob < p:\n        flag = True\n        height, width, _ = image.shape  # target image width and height\n        insects_images = os.listdir(insects_folder)\n        img_shape = image.shape\n\n        for _ in range(n_insects):\n            insect = cv2.cvtColor(cv2.imread(os.path.join(insects_folder, random.choice(insects_images))), cv2.COLOR_RGB2BGR)\n            insect = cv2.flip(insect, random.choice([-1, 0, 1]))\n            insect = cv2.rotate(insect, random.choice([0, 1, 2]))\n            insect = cv2.resize(insect, (width, height))\n\n            h_height, h_width, _ = insect.shape  # insect image width and height\n            roi_ho = random.randint(0, image.shape[0] - insect.shape[0])\n            roi_wo = random.randint(0, image.shape[1] - insect.shape[1])\n            roi = image[roi_ho:roi_ho + h_height, roi_wo:roi_wo + h_width]\n\n            # Creating a mask and inverse mask \n            img2gray = cv2.cvtColor(insect, cv2.COLOR_BGR2GRAY)\n            ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)\n            #mask_inv = cv2.cvtColor(cv2.bitwise_not(mask),cv2.COLOR_BGR2GRAY)\n            mask_inv = cv2.bitwise_not(mask)\n\n            # Now black-out the area of insect in ROI\n            img_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)\n\n            # Take only region of insect from insect image.\n            if dark_insect:\n                img_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)\n                insect_fg = cv2.bitwise_and(img_bg, img_bg, mask=mask)\n            else:\n                insect_fg = cv2.bitwise_and(insect, insect, mask=mask)\n\n            # Put insect in ROI and modify the target image\n            dst = cv2.add(img_bg, insect_fg, dtype = cv2.CV_64F)\n\n            image[roi_ho:roi_ho + h_height, roi_wo:roi_wo + h_width] = dst\n\n\n    return image","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:01:11.564047Z","iopub.execute_input":"2021-12-02T04:01:11.564598Z","iopub.status.idle":"2021-12-02T04:01:11.576856Z","shell.execute_reply.started":"2021-12-02T04:01:11.56456Z","shell.execute_reply":"2021-12-02T04:01:11.575879Z"},"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    # Insect\n    n = random.randint(1, 2)\n    image = insect_augmentation(image, n_insects = n, dark_insect=False, p = 0.05)\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-12-02T04:01:14.372438Z","iopub.execute_input":"2021-12-02T04:01:14.3732Z","iopub.status.idle":"2021-12-02T04:01:14.388012Z","shell.execute_reply.started":"2021-12-02T04:01:14.373124Z","shell.execute_reply":"2021-12-02T04:01:14.387186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"def process(img):\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)\ndef predict(img):\n    img = load_image(img)\n    tta_steps = 5\n    predictions = []\n    for i in range(tta_steps):\n        pred = np.array(model.predict(process(img)))[0]\n        predictions.append(pred)\n    result = np.median(predictions, axis=0)\n    \n    return result","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:01:17.227229Z","iopub.execute_input":"2021-12-02T04:01:17.227809Z","iopub.status.idle":"2021-12-02T04:01:17.23426Z","shell.execute_reply.started":"2021-12-02T04:01:17.22777Z","shell.execute_reply":"2021-12-02T04:01:17.233197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_label = ['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']\nlabel = []\nfor i in range(len(submission['image'])):\n    test_images = submission['image'][i]\n    preds = predict(test_images)\n    answer = []\n    for j in range(len(preds)):\n        if preds[j] > 0.09:\n            answer.append(n_label[j])\n    if (\"healthy\" in answer) and (len(answer) > 1):\n        answer.remove('healthy')\n    if len(answer) == 0:\n        answer.append('healthy')\n    answer = ' '.join(answer)\n    label.append(answer)\nsubmission['labels'] = label\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:03:33.944663Z","iopub.execute_input":"2021-12-02T04:03:33.944924Z","iopub.status.idle":"2021-12-02T04:03:39.018697Z","shell.execute_reply.started":"2021-12-02T04:03:33.944895Z","shell.execute_reply":"2021-12-02T04:03:39.018005Z"},"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-12-02T04:03:47.744995Z","iopub.execute_input":"2021-12-02T04:03:47.745305Z","iopub.status.idle":"2021-12-02T04:03:47.753421Z","shell.execute_reply.started":"2021-12-02T04:03:47.745263Z","shell.execute_reply":"2021-12-02T04:03:47.752625Z"},"trusted":true},"execution_count":null,"outputs":[]}]}