{"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-12-05T08:43:31.907536Z","iopub.execute_input":"2021-12-05T08:43:31.907865Z","iopub.status.idle":"2021-12-05T08:43:37.526034Z","shell.execute_reply.started":"2021-12-05T08:43:31.907786Z","shell.execute_reply":"2021-12-05T08:43:37.525269Z"},"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-12-05T08:43:37.527695Z","iopub.execute_input":"2021-12-05T08:43:37.527955Z","iopub.status.idle":"2021-12-05T08:43:37.532356Z","shell.execute_reply.started":"2021-12-05T08:43:37.52792Z","shell.execute_reply":"2021-12-05T08:43:37.531716Z"},"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-05T08:43:37.536542Z","iopub.execute_input":"2021-12-05T08:43:37.537004Z","iopub.status.idle":"2021-12-05T08:44:29.916146Z","shell.execute_reply.started":"2021-12-05T08:43:37.536964Z","shell.execute_reply":"2021-12-05T08:44:29.915371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path_v2 = \"../input/efficientnetv2model/EfficientNet_V2.h5\"\nweights_path_16 = \"../input/fgvc8vgg16/FGVC8-VGG16.h5\"\nhub_url = '../input/efficientnetv2-tfhub-weight-files/tfhub_models/efficientnetv2-l-21k-ft1k/feature_vector'","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:44:29.917741Z","iopub.execute_input":"2021-12-05T08:44:29.917977Z","iopub.status.idle":"2021-12-05T08:44:29.923553Z","shell.execute_reply.started":"2021-12-05T08:44:29.917944Z","shell.execute_reply":"2021-12-05T08:44:29.92285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_v2():\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-12-05T08:44:29.924774Z","iopub.execute_input":"2021-12-05T08:44:29.925237Z","iopub.status.idle":"2021-12-05T08:44:29.933177Z","shell.execute_reply.started":"2021-12-05T08:44:29.9252Z","shell.execute_reply":"2021-12-05T08:44:29.932383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_16():\n    VGG16_MODEL = tf.keras.applications.VGG16(weights=None ,include_top=False, input_shape=(HEIGHT, WIDTH, 3))\n    \n    x=VGG16_MODEL.output\n    x=GlobalAveragePooling2D()(x)\n    x=Dense(256,activation='relu')(x)\n    x=Dropout(0.2)(x)\n    x=Dense(128,activation='relu')(x)\n    prediction=Dense(6,activation='sigmoid')(x)\n\n    model=Model(inputs=VGG16_MODEL.input, outputs=prediction)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:44:29.935508Z","iopub.execute_input":"2021-12-05T08:44:29.935779Z","iopub.status.idle":"2021-12-05T08:44:29.943221Z","shell.execute_reply.started":"2021-12-05T08:44:29.935744Z","shell.execute_reply":"2021-12-05T08:44:29.942507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_v2 = get_model_v2()\nmodel_v2.load_weights(weights_path_v2)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:46:43.167626Z","iopub.execute_input":"2021-12-05T08:46:43.168317Z","iopub.status.idle":"2021-12-05T08:47:22.696626Z","shell.execute_reply.started":"2021-12-05T08:46:43.168285Z","shell.execute_reply":"2021-12-05T08:47:22.695821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_16 = get_model_16()\nmodel_16.load_weights(weights_path_16)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:47:22.699082Z","iopub.execute_input":"2021-12-05T08:47:22.699377Z","iopub.status.idle":"2021-12-05T08:47:31.377249Z","shell.execute_reply.started":"2021-12-05T08:47:22.699339Z","shell.execute_reply":"2021-12-05T08:47:31.376493Z"},"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-12-05T08:47:31.378669Z","iopub.execute_input":"2021-12-05T08:47:31.378958Z","iopub.status.idle":"2021-12-05T08:47:31.408028Z","shell.execute_reply.started":"2021-12-05T08:47:31.378924Z","shell.execute_reply":"2021-12-05T08:47:31.407338Z"},"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-05T08:47:31.409997Z","iopub.execute_input":"2021-12-05T08:47:31.410252Z","iopub.status.idle":"2021-12-05T08:47:31.415408Z","shell.execute_reply.started":"2021-12-05T08:47:31.410203Z","shell.execute_reply":"2021-12-05T08:47:31.414058Z"},"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-12-05T08:47:31.416937Z","iopub.execute_input":"2021-12-05T08:47:31.417484Z","iopub.status.idle":"2021-12-05T08:47:31.430881Z","shell.execute_reply.started":"2021-12-05T08:47:31.417427Z","shell.execute_reply":"2021-12-05T08:47:31.430147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 = 4\n    predictions_v2 = []\n    predictions_16 = []\n    for i in range(tta_steps):\n        pred_v2 = model_v2.layers[2](model_v2.layers[1](model_v2.layers[0](process(img)))).numpy()[0]\n        pred_16 = model_16.predict(process(img))[0]\n        predictions_v2.append(pred_v2)\n        predictions_16.append(pred_16)\n    result_v2 = np.median(predictions_v2, axis=0)\n    result_16 = np.median(predictions_16, axis=0)\n    \n    return (result_v2 + result_16) / 2","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:48:58.558235Z","iopub.execute_input":"2021-12-05T08:48:58.558837Z","iopub.status.idle":"2021-12-05T08:48:58.567302Z","shell.execute_reply.started":"2021-12-05T08:48:58.558787Z","shell.execute_reply":"2021-12-05T08:48:58.566625Z"},"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.3:\n            answer.append(n_label[j])\n    answer = ' '.join(answer)\n    label.append(answer)\nsubmission['labels'] = label\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:49:02.219219Z","iopub.execute_input":"2021-12-05T08:49:02.219496Z","iopub.status.idle":"2021-12-05T08:49:22.583017Z","shell.execute_reply.started":"2021-12-05T08:49:02.219447Z","shell.execute_reply":"2021-12-05T08:49:22.582293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T08:49:30.16458Z","iopub.execute_input":"2021-12-05T08:49:30.165304Z","iopub.status.idle":"2021-12-05T08:49:30.173322Z","shell.execute_reply.started":"2021-12-05T08:49:30.165266Z","shell.execute_reply":"2021-12-05T08:49:30.172521Z"},"trusted":true},"execution_count":null,"outputs":[]}]}