{"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\nmodel = tf.keras.models.load_model('../input/dlcv-projekt/model-best.h5')","metadata":{"execution":{"iopub.status.busy":"2021-06-07T22:18:51.730131Z","iopub.execute_input":"2021-06-07T22:18:51.730470Z","iopub.status.idle":"2021-06-07T22:18:57.264426Z","shell.execute_reply.started":"2021-06-07T22:18:51.730438Z","shell.execute_reply":"2021-06-07T22:18:57.263503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /kaggle/tmp/test_dataset/test\n!cp -r ../input/plant-pathology-2021-fgvc8/test_images /kaggle/tmp/test_dataset/test","metadata":{"execution":{"iopub.status.busy":"2021-06-07T22:18:57.266779Z","iopub.execute_input":"2021-06-07T22:18:57.267427Z","iopub.status.idle":"2021-06-07T22:18:58.578740Z","shell.execute_reply.started":"2021-06-07T22:18:57.267387Z","shell.execute_reply":"2021-06-07T22:18:58.577655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np\nmaxsize = (224, 224)\nimage = Image.open('../input/plant-pathology-2021-fgvc8/test_images/85f8cb619c66b863.jpg')\nimage.thumbnail(maxsize, Image.ANTIALIAS)\nx = np.asarray(image)","metadata":{"execution":{"iopub.status.busy":"2021-06-07T22:18:58.581103Z","iopub.execute_input":"2021-06-07T22:18:58.581479Z","iopub.status.idle":"2021-06-07T22:18:58.618239Z","shell.execute_reply.started":"2021-06-07T22:18:58.581426Z","shell.execute_reply":"2021-06-07T22:18:58.617498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntest_datagen = ImageDataGenerator()  # rescale=1. / 255)\ntest_generator = test_datagen.flow_from_directory('/kaggle/tmp/test_dataset', class_mode=None, target_size=(380, 380), shuffle=False, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2021-06-07T22:18:58.619892Z","iopub.execute_input":"2021-06-07T22:18:58.620256Z","iopub.status.idle":"2021-06-07T22:18:58.732117Z","shell.execute_reply.started":"2021-06-07T22:18:58.620220Z","shell.execute_reply":"2021-06-07T22:18:58.731380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = model.predict(test_generator)\nx","metadata":{"execution":{"iopub.status.busy":"2021-06-07T22:18:58.735832Z","iopub.execute_input":"2021-06-07T22:18:58.736082Z","iopub.status.idle":"2021-06-07T22:19:02.191177Z","shell.execute_reply.started":"2021-06-07T22:18:58.736057Z","shell.execute_reply":"2021-06-07T22:19:02.190337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#labels = ['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']\nlabels = ['complex', 'frog_eye_leaf_spot', 'powdery_mildew', 'rust', 'scab']\nthreshold = 0.7\nf = lambda x: 1 if x > threshold else 0\ng = lambda x: [labels[i] for i in x if i is not 0]","metadata":{"execution":{"iopub.status.busy":"2021-06-07T22:19:02.192539Z","iopub.execute_input":"2021-06-07T22:19:02.192981Z","iopub.status.idle":"2021-06-07T22:19:02.198944Z","shell.execute_reply.started":"2021-06-07T22:19:02.192937Z","shell.execute_reply":"2021-06-07T22:19:02.197829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nz = np.vectorize(f)(x)\n#z = z[:,:-1] # TODO: remove this once new model is trained\nz","metadata":{"execution":{"iopub.status.busy":"2021-06-07T22:19:02.200494Z","iopub.execute_input":"2021-06-07T22:19:02.200956Z","iopub.status.idle":"2021-06-07T22:19:02.211823Z","shell.execute_reply.started":"2021-06-07T22:19:02.200906Z","shell.execute_reply":"2021-06-07T22:19:02.210709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nimport pandas as pd\npredictions = [[labels[i] for i, j in enumerate(y) if j != 0] for y in z]\npredictions_str = [' '.join(i) if i != [] else 'healthy' for i in predictions]\nfilenames = [Path(i).name for i in test_generator.filenames]\ndf = pd.DataFrame({\n    'image': filenames,\n    'labels': predictions_str\n})\ndf","metadata":{"execution":{"iopub.status.busy":"2021-06-07T22:19:02.214711Z","iopub.execute_input":"2021-06-07T22:19:02.215153Z","iopub.status.idle":"2021-06-07T22:19:02.233506Z","shell.execute_reply.started":"2021-06-07T22:19:02.215115Z","shell.execute_reply":"2021-06-07T22:19:02.232482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-07T22:19:02.234998Z","iopub.execute_input":"2021-06-07T22:19:02.235400Z","iopub.status.idle":"2021-06-07T22:19:02.244135Z","shell.execute_reply.started":"2021-06-07T22:19:02.235361Z","shell.execute_reply":"2021-06-07T22:19:02.243038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}