{"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\nimport tensorflow_addons as tfa\n\nfrom PIL import Image\nimport numpy as np\nimport pandas as pd \nimport os ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-13T20:55:29.259854Z","iopub.execute_input":"2021-06-13T20:55:29.260306Z","iopub.status.idle":"2021-06-13T20:55:35.133549Z","shell.execute_reply.started":"2021-06-13T20:55:29.260223Z","shell.execute_reply":"2021-06-13T20:55:35.132588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_IMAGES_DIR = '/kaggle/input/plant-pathology-2021-fgvc8/test_images'\nMODEL_PATH = '../input/plantmodels/efficientnet_b4.h5'\nSUBMISSION_PATH = '/kaggle/working/submission.csv'\n\nIMAGE_SIZE=(512,512)\nLABELS = {0: 'complex', 1: 'frog_eye_leaf_spot', 2: 'healthy', 3: 'powdery_mildew', 4: 'rust', 5: 'scab'}\n\nTHRESHOLD = 0.43","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-13T20:55:35.134965Z","iopub.execute_input":"2021-06-13T20:55:35.135277Z","iopub.status.idle":"2021-06-13T20:55:35.143380Z","shell.execute_reply.started":"2021-06-13T20:55:35.135249Z","shell.execute_reply":"2021-06-13T20:55:35.142082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dependencies = {\n    'Addons>F1Score': tfa.metrics.F1Score\n}\nmodel = tf.keras.models.load_model(MODEL_PATH, custom_objects=dependencies)","metadata":{"execution":{"iopub.status.busy":"2021-06-13T20:55:35.145735Z","iopub.execute_input":"2021-06-13T20:55:35.146475Z","iopub.status.idle":"2021-06-13T20:55:42.159787Z","shell.execute_reply.started":"2021-06-13T20:55:35.146428Z","shell.execute_reply":"2021-06-13T20:55:42.158775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions = []\n\nfor image_name in os.listdir(TEST_IMAGES_DIR):\n    print(f\"Running inference for file: {image_name}\")\n    image_path = os.path.join(TEST_IMAGES_DIR, image_name)\n    image = Image.open(image_path)\n    image = image.resize(IMAGE_SIZE)\n    assert image.mode == 'RGB'\n    \n    np_image = np.expand_dims(np.array(image), axis=0)\n    \n    predctions = model.predict(np_image).ravel()\n    arg_wheres = np.argwhere(predctions > THRESHOLD).ravel()\n    \n    if not arg_wheres.size:\n        arg_wheres = np.argmax(predctions).ravel()\n    \n    classes = [LABELS[arg] for arg in arg_wheres]\n    print(f'Detected classes: {classes}')\n    prediction_string =  ' '.join(classes)\n    submissions.append({'image': image_name, 'labels': prediction_string})\nsubmission_df = pd.DataFrame(submissions)\nsubmission_df.to_csv(SUBMISSION_PATH, index=False)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-13T20:55:42.161041Z","iopub.execute_input":"2021-06-13T20:55:42.161364Z","iopub.status.idle":"2021-06-13T20:55:48.288890Z","shell.execute_reply.started":"2021-06-13T20:55:42.161335Z","shell.execute_reply":"2021-06-13T20:55:48.287829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}