{"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 tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_addons as tfa\nimport numpy as np\nimport pandas as pd\nfrom kaggle_datasets import KaggleDatasets\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-24T14:59:54.06572Z","iopub.execute_input":"2021-05-24T14:59:54.066289Z","iopub.status.idle":"2021-05-24T14:59:59.845981Z","shell.execute_reply.started":"2021-05-24T14:59:54.066197Z","shell.execute_reply":"2021-05-24T14:59:59.845117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels2id = {\n    'scab': 0,\n    'healthy': 1,\n    'frog_eye_leaf_spot': 2,\n    'rust': 3,\n    'complex': 4,\n    'powdery_mildew': 5\n}\n\nid2labels = {v:k for k,v in labels2id.items()}\n\nlabel_classes = labels2id.keys()","metadata":{"execution":{"iopub.status.busy":"2021-05-24T15:00:36.037172Z","iopub.execute_input":"2021-05-24T15:00:36.03751Z","iopub.status.idle":"2021-05-24T15:00:36.043633Z","shell.execute_reply.started":"2021-05-24T15:00:36.037484Z","shell.execute_reply":"2021-05-24T15:00:36.04257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = [600,600]\nchannels = 3\nnum_classes = len(label_classes)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T15:00:37.382052Z","iopub.execute_input":"2021-05-24T15:00:37.382386Z","iopub.status.idle":"2021-05-24T15:00:37.386504Z","shell.execute_reply.started":"2021-05-24T15:00:37.382359Z","shell.execute_reply":"2021-05-24T15:00:37.385677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model('../input/plant-pathology-2021/ResNet152V2-600.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-24T15:00:46.424777Z","iopub.execute_input":"2021-05-24T15:00:46.425137Z","iopub.status.idle":"2021-05-24T15:01:07.202383Z","shell.execute_reply.started":"2021-05-24T15:00:46.425109Z","shell.execute_reply":"2021-05-24T15:01:07.20145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_predict_image(image_path):\n    image = tf.io.read_file(image_path)\n    image = tf.io.decode_jpeg(image, channels=channels)\n    image = tf.image.convert_image_dtype(image, tf.float32)\n    image = tf.image.resize(image, image_size)\n    image = tf.expand_dims(image,axis=0)\n    return image","metadata":{"execution":{"iopub.status.busy":"2021-05-23T15:24:25.946293Z","iopub.execute_input":"2021-05-23T15:24:25.946914Z","iopub.status.idle":"2021-05-23T15:24:25.954573Z","shell.execute_reply.started":"2021-05-23T15:24:25.946856Z","shell.execute_reply":"2021-05-23T15:24:25.953157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = '../input/plant-pathology-2021-fgvc8/test_images/'\n\nsubmission = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-23T15:24:28.101237Z","iopub.execute_input":"2021-05-23T15:24:28.101854Z","iopub.status.idle":"2021-05-23T15:24:28.119745Z","shell.execute_reply.started":"2021-05-23T15:24:28.101799Z","shell.execute_reply":"2021-05-23T15:24:28.118738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for row in submission.index:\n    \n    image = load_predict_image(test_path+submission.loc[row,'image'])\n    predict = model.predict(image)[0]\n    predict = [1 if i>0.5 else 0 for i in predict]\n    result = []\n    for i,j in enumerate(predict):\n        if j:\n            result.append(id2labels.get(i))\n    result = ' '.join(result)\n    submission.loc[row,'labels'] = result","metadata":{"execution":{"iopub.status.busy":"2021-05-23T15:24:30.685394Z","iopub.execute_input":"2021-05-23T15:24:30.685803Z","iopub.status.idle":"2021-05-23T15:24:38.910708Z","shell.execute_reply.started":"2021-05-23T15:24:30.685766Z","shell.execute_reply":"2021-05-23T15:24:38.909649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-23T15:24:52.578396Z","iopub.execute_input":"2021-05-23T15:24:52.578765Z","iopub.status.idle":"2021-05-23T15:24:52.586349Z","shell.execute_reply.started":"2021-05-23T15:24:52.578733Z","shell.execute_reply":"2021-05-23T15:24:52.585249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}