{"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 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-11-27T06:46:07.676271Z","iopub.execute_input":"2021-11-27T06:46:07.676685Z","iopub.status.idle":"2021-11-27T06:46:13.582077Z","shell.execute_reply.started":"2021-11-27T06:46:07.676652Z","shell.execute_reply":"2021-11-27T06:46:13.581212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT = 480\nWIDTH = 480\nCHANNELS = 3\nCLASSES = 6\ntop_dropout_rate = 0.2","metadata":{"execution":{"iopub.status.busy":"2021-11-27T06:46:13.585676Z","iopub.execute_input":"2021-11-27T06:46:13.585890Z","iopub.status.idle":"2021-11-27T06:46:13.593599Z","shell.execute_reply.started":"2021-11-27T06:46:13.585864Z","shell.execute_reply":"2021-11-27T06:46:13.592836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path_1 = \"../input/efnet-v2-geoaug-model/EfficientNet_V2_GeoAug.h5\"\nweights_path_2 = \"../input/efnet-v2-insectaug-model/EfficientNet_V2_InsectAug.h5\"\nhub_url = '../input/efficientnetv2-tfhub-weight-files/tfhub_models/efficientnetv2-l-21k-ft1k/feature_vector'","metadata":{"execution":{"iopub.status.busy":"2021-11-27T06:46:13.594977Z","iopub.execute_input":"2021-11-27T06:46:13.595397Z","iopub.status.idle":"2021-11-27T06:46:13.602581Z","shell.execute_reply.started":"2021-11-27T06:46:13.595356Z","shell.execute_reply":"2021-11-27T06:46:13.601619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model():\n    model = tf.keras.Sequential([\n            tf.keras.layers.InputLayer(input_shape = [HEIGHT, WIDTH, CHANNELS]),\n            hub.KerasLayer(hub_url, trainable = True),\n            Dropout(top_dropout_rate, name = \"top_dropout\"),\n            Dense(CLASSES, activation = 'sigmoid')\n        ])\n    model.build((HEIGHT, WIDTH, CHANNELS))\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-11-27T06:46:13.607301Z","iopub.execute_input":"2021-11-27T06:46:13.607561Z","iopub.status.idle":"2021-11-27T06:46:13.615841Z","shell.execute_reply.started":"2021-11-27T06:46:13.607535Z","shell.execute_reply":"2021-11-27T06:46:13.614604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1 = get_model()\nmodel_1.load_weights(weights_path_1)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T06:46:13.617398Z","iopub.execute_input":"2021-11-27T06:46:13.617868Z","iopub.status.idle":"2021-11-27T06:46:53.839861Z","shell.execute_reply.started":"2021-11-27T06:46:13.617832Z","shell.execute_reply":"2021-11-27T06:46:53.839045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_2 = get_model()\nmodel_2.load_weights(weights_path_2)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T06:46:53.842812Z","iopub.execute_input":"2021-11-27T06:46:53.843413Z","iopub.status.idle":"2021-11-27T06:47:27.718657Z","shell.execute_reply.started":"2021-11-27T06:46:53.843372Z","shell.execute_reply":"2021-11-27T06:47:27.717774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-27T06:47:27.720694Z","iopub.execute_input":"2021-11-27T06:47:27.720998Z","iopub.status.idle":"2021-11-27T06:47:27.749572Z","shell.execute_reply.started":"2021-11-27T06:47:27.720937Z","shell.execute_reply":"2021-11-27T06:47:27.748718Z"},"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    \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\n    # Rotates\n    if p_rotate > .75:\n        image = tf.image.rot90(image, k=3) # rotate 270º\n    elif p_rotate > .5:\n        image = tf.image.rot90(image, k=2) # rotate 180º\n    elif p_rotate > .25:\n        image = tf.image.rot90(image, k=1) # rotate 90\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    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    \n    return image","metadata":{"execution":{"iopub.status.busy":"2021-11-27T06:47:27.750953Z","iopub.execute_input":"2021-11-27T06:47:27.751382Z","iopub.status.idle":"2021-11-27T06:47:27.762011Z","shell.execute_reply.started":"2021-11-27T06:47:27.751343Z","shell.execute_reply":"2021-11-27T06:47:27.761295Z"},"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-11-27T06:47:27.763375Z","iopub.execute_input":"2021-11-27T06:47:27.763828Z","iopub.status.idle":"2021-11-27T06:47:27.774491Z","shell.execute_reply.started":"2021-11-27T06:47:27.763792Z","shell.execute_reply":"2021-11-27T06:47:27.773517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process(img, aug):\n    if aug == 1:\n        img = data_full_augment(img)\n    if aug == 2:\n        return cv2.resize(img / 255.0,(480, 480)).reshape(-1, 480, 480, 3)\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 = 4\n    predictions_1 = []\n    predictions_2 = []\n    for i in range(tta_steps):\n        pred_1 = model_1.layers[2](model_1.layers[1](model_1.layers[0](process(img, 1)))).numpy()[0]\n        pred_2 = model_2.layers[2](model_2.layers[1](model_2.layers[0](process(img, 2)))).numpy()[0]\n        predictions_1.append(pred_1)\n        predictions_2.append(pred_1)\n    result_1 = np.median(predictions_1, axis=0)\n    result_2 = np.median(predictions_2, axis=0)\n    \n    return (result_1 + result_2) / 2","metadata":{"execution":{"iopub.status.busy":"2021-11-27T06:47:27.777175Z","iopub.execute_input":"2021-11-27T06:47:27.777592Z","iopub.status.idle":"2021-11-27T06:47:27.786923Z","shell.execute_reply.started":"2021-11-27T06:47:27.777489Z","shell.execute_reply":"2021-11-27T06:47:27.786011Z"},"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.31:\n            answer.append(n_label[j])\n    answer = ' '.join(answer)\n    label.append(answer)\nsubmission['labels'] = label\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-11-27T06:47:27.788430Z","iopub.execute_input":"2021-11-27T06:47:27.788812Z","iopub.status.idle":"2021-11-27T06:47:46.054549Z","shell.execute_reply.started":"2021-11-27T06:47:27.788778Z","shell.execute_reply":"2021-11-27T06:47:46.053833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T06:47:52.986744Z","iopub.execute_input":"2021-11-27T06:47:52.987019Z","iopub.status.idle":"2021-11-27T06:47:52.994156Z","shell.execute_reply.started":"2021-11-27T06:47:52.986987Z","shell.execute_reply":"2021-11-27T06:47:52.993338Z"},"trusted":true},"execution_count":null,"outputs":[]}]}