{"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 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-18T07:27:00.042370Z","iopub.execute_input":"2021-11-18T07:27:00.043081Z","iopub.status.idle":"2021-11-18T07:27:05.750120Z","shell.execute_reply.started":"2021-11-18T07:27:00.042990Z","shell.execute_reply":"2021-11-18T07:27:05.749398Z"},"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-18T07:27:17.343206Z","iopub.execute_input":"2021-11-18T07:27:17.343576Z","iopub.status.idle":"2021-11-18T07:27:17.350173Z","shell.execute_reply.started":"2021-11-18T07:27:17.343539Z","shell.execute_reply":"2021-11-18T07:27:17.349445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path = \"../input/efficientnetv2model/EfficientNet_V2.h5\"\nhub_url = '../input/efficientnetv2-tfhub-weight-files/tfhub_models/efficientnetv2-l-21k-ft1k/feature_vector'","metadata":{"execution":{"iopub.status.busy":"2021-11-18T07:27:21.698737Z","iopub.execute_input":"2021-11-18T07:27:21.699156Z","iopub.status.idle":"2021-11-18T07:27:21.703412Z","shell.execute_reply.started":"2021-11-18T07:27:21.699118Z","shell.execute_reply":"2021-11-18T07:27:21.702486Z"},"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    model.compile(optimizer ='adam', loss = 'binary_crossentropy', metrics = ['accuracy'])\n    model.summary()\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-11-18T07:27:25.292685Z","iopub.execute_input":"2021-11-18T07:27:25.292951Z","iopub.status.idle":"2021-11-18T07:27:25.299399Z","shell.execute_reply.started":"2021-11-18T07:27:25.292922Z","shell.execute_reply":"2021-11-18T07:27:25.298390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1 = get_model()\nmodel_1.load_weights(weights_path)","metadata":{"execution":{"iopub.status.busy":"2021-11-18T07:27:28.216050Z","iopub.execute_input":"2021-11-18T07:27:28.216778Z","iopub.status.idle":"2021-11-18T07:28:07.262578Z","shell.execute_reply.started":"2021-11-18T07:27:28.216737Z","shell.execute_reply":"2021-11-18T07:28:07.261721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_2 = get_model()\nmodel_2.load_weights(weights_path)","metadata":{"execution":{"iopub.status.busy":"2021-11-18T07:28:26.279962Z","iopub.execute_input":"2021-11-18T07:28:26.280440Z","iopub.status.idle":"2021-11-18T07:28:58.733558Z","shell.execute_reply.started":"2021-11-18T07:28:26.280399Z","shell.execute_reply":"2021-11-18T07:28:58.732710Z"},"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-18T07:29:04.264649Z","iopub.execute_input":"2021-11-18T07:29:04.265269Z","iopub.status.idle":"2021-11-18T07:29:04.292774Z","shell.execute_reply.started":"2021-11-18T07:29:04.265226Z","shell.execute_reply":"2021-11-18T07:29:04.291864Z"},"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-18T07:29:08.403409Z","iopub.execute_input":"2021-11-18T07:29:08.404228Z","iopub.status.idle":"2021-11-18T07:29:08.409188Z","shell.execute_reply.started":"2021-11-18T07:29:08.404176Z","shell.execute_reply":"2021-11-18T07:29:08.408398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process(img):\n    return cv2.resize(img / 255.0, (480, 480)).reshape(-1, 480, 480, 3)\ndef predict(img):\n    img = load_image(img)\n    tta_steps = 5\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)))).numpy()[0]\n        predictions_1.append(pred_1)\n    result_1 = np.median(predictions_1, axis=0)\n    \n    for j in range(tta_steps):\n        pred_2 = model_2.layers[2](model_2.layers[1](model_2.layers[0](process(img)))).numpy()[0]\n        predictions_2.append(pred_1)\n    result_2 = np.median(predictions_2, axis=0)\n    \n    return (result_1 + result_2) / 2","metadata":{"execution":{"iopub.status.busy":"2021-11-18T07:31:34.486142Z","iopub.execute_input":"2021-11-18T07:31:34.486585Z","iopub.status.idle":"2021-11-18T07:31:34.503639Z","shell.execute_reply.started":"2021-11-18T07:31:34.486542Z","shell.execute_reply":"2021-11-18T07:31:34.502833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = submission['image'][1]\npreds = predict(test_images)\nprint(preds[0])","metadata":{"execution":{"iopub.status.busy":"2021-11-18T07:31:37.771103Z","iopub.execute_input":"2021-11-18T07:31:37.771400Z","iopub.status.idle":"2021-11-18T07:31:41.995062Z","shell.execute_reply.started":"2021-11-18T07:31:37.771369Z","shell.execute_reply":"2021-11-18T07:31:41.994268Z"},"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    answer = ' '.join(answer)\n    label.append(answer)\nsubmission['labels'] = label\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-11-18T07:32:08.334405Z","iopub.execute_input":"2021-11-18T07:32:08.334972Z","iopub.status.idle":"2021-11-18T07:32:13.995880Z","shell.execute_reply.started":"2021-11-18T07:32:08.334932Z","shell.execute_reply":"2021-11-18T07:32:13.995175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-18T07:32:24.399155Z","iopub.execute_input":"2021-11-18T07:32:24.399435Z","iopub.status.idle":"2021-11-18T07:32:24.407424Z","shell.execute_reply.started":"2021-11-18T07:32:24.399403Z","shell.execute_reply":"2021-11-18T07:32:24.406691Z"},"trusted":true},"execution_count":null,"outputs":[]}]}