{"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-23T08:42:40.462141Z","iopub.execute_input":"2021-11-23T08:42:40.462772Z","iopub.status.idle":"2021-11-23T08:42:46.225377Z","shell.execute_reply.started":"2021-11-23T08:42:40.462677Z","shell.execute_reply":"2021-11-23T08:42:46.224629Z"},"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-23T08:42:49.668912Z","iopub.execute_input":"2021-11-23T08:42:49.669180Z","iopub.status.idle":"2021-11-23T08:42:49.673736Z","shell.execute_reply.started":"2021-11-23T08:42:49.669152Z","shell.execute_reply":"2021-11-23T08:42:49.672484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights_path = \"../input/efficientnet-v2-raw-model/EfficientNet_V2_raw.h5\"\nhub_url = '../input/efficientnetv2-tfhub-weight-files/tfhub_models/efficientnetv2-l-21k-ft1k/feature_vector'","metadata":{"execution":{"iopub.status.busy":"2021-11-23T08:43:09.768810Z","iopub.execute_input":"2021-11-23T08:43:09.769092Z","iopub.status.idle":"2021-11-23T08:43:09.772530Z","shell.execute_reply.started":"2021-11-23T08:43:09.769063Z","shell.execute_reply":"2021-11-23T08:43:09.771804Z"},"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-23T08:43:14.411883Z","iopub.execute_input":"2021-11-23T08:43:14.412354Z","iopub.status.idle":"2021-11-23T08:43:14.418111Z","shell.execute_reply.started":"2021-11-23T08:43:14.412314Z","shell.execute_reply":"2021-11-23T08:43:14.417385Z"},"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-23T08:43:18.109729Z","iopub.execute_input":"2021-11-23T08:43:18.110018Z","iopub.status.idle":"2021-11-23T08:43:58.499391Z","shell.execute_reply.started":"2021-11-23T08:43:18.109990Z","shell.execute_reply":"2021-11-23T08:43:58.498590Z"},"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-23T08:44:28.692487Z","iopub.execute_input":"2021-11-23T08:44:28.693132Z","iopub.status.idle":"2021-11-23T08:44:28.722229Z","shell.execute_reply.started":"2021-11-23T08:44:28.693088Z","shell.execute_reply":"2021-11-23T08:44:28.721569Z"},"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-23T08:44:37.600955Z","iopub.execute_input":"2021-11-23T08:44:37.601237Z","iopub.status.idle":"2021-11-23T08:44:37.606353Z","shell.execute_reply.started":"2021-11-23T08:44:37.601207Z","shell.execute_reply":"2021-11-23T08:44:37.605331Z"},"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 = []\n    for i in range(tta_steps):\n        pred = model.layers[2](model.layers[1](model.layers[0](process(img)))).numpy()[0]\n        predictions.append(pred)\n    result = np.median(predictions, axis=0)\n    \n    return result","metadata":{"execution":{"iopub.status.busy":"2021-11-23T08:45:48.234330Z","iopub.execute_input":"2021-11-23T08:45:48.235210Z","iopub.status.idle":"2021-11-23T08:45:48.241924Z","shell.execute_reply.started":"2021-11-23T08:45:48.235164Z","shell.execute_reply":"2021-11-23T08:45:48.241197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = submission['image'][2]\ntest_images = load_image(test_images)\npreds = model.layers[2](model.layers[1](model.layers[0](process(test_images)))).numpy()[0]\nprint(preds[4])","metadata":{"execution":{"iopub.status.busy":"2021-11-23T08:50:15.732622Z","iopub.execute_input":"2021-11-23T08:50:15.733148Z","iopub.status.idle":"2021-11-23T08:50:16.032864Z","shell.execute_reply.started":"2021-11-23T08:50:15.733110Z","shell.execute_reply":"2021-11-23T08:50:16.032114Z"},"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    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-11-23T08:52:38.195511Z","iopub.execute_input":"2021-11-23T08:52:38.195935Z","iopub.status.idle":"2021-11-23T08:52:41.310082Z","shell.execute_reply.started":"2021-11-23T08:52:38.195892Z","shell.execute_reply":"2021-11-23T08:52:41.309338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T08:52:47.248223Z","iopub.execute_input":"2021-11-23T08:52:47.248495Z","iopub.status.idle":"2021-11-23T08:52:47.255716Z","shell.execute_reply.started":"2021-11-23T08:52:47.248465Z","shell.execute_reply":"2021-11-23T08:52:47.254928Z"},"trusted":true},"execution_count":null,"outputs":[]}]}