{"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 pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport tensorflow.keras as keras\nimport PIL\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport random\nfrom tqdm import tqdm\nimport tensorflow_addons as tfa\nimport random\nfrom sklearn.preprocessing import MultiLabelBinarizer","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-28T10:30:51.173518Z","iopub.execute_input":"2021-11-28T10:30:51.173909Z","iopub.status.idle":"2021-11-28T10:30:58.000713Z","shell.execute_reply.started":"2021-11-28T10:30:51.173875Z","shell.execute_reply":"2021-11-28T10:30:57.999802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:30:58.002522Z","iopub.execute_input":"2021-11-28T10:30:58.002925Z","iopub.status.idle":"2021-11-28T10:30:58.052423Z","shell.execute_reply.started":"2021-11-28T10:30:58.002879Z","shell.execute_reply":"2021-11-28T10:30:58.051474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MultiLabelBinarizer\nlabel_split = train.labels.apply(lambda x: x.split()) #chia 1 chuỗi các nhãn thành nhiều nhãn nếu có dấu cách\ntrans_label = MultiLabelBinarizer().fit(label_split)\nlabels = pd.DataFrame(trans_label.transform(label_split), columns=trans_label.classes_)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:30:58.054782Z","iopub.execute_input":"2021-11-28T10:30:58.055213Z","iopub.status.idle":"2021-11-28T10:30:58.275501Z","shell.execute_reply.started":"2021-11-28T10:30:58.055168Z","shell.execute_reply":"2021-11-28T10:30:58.274376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:30:58.278747Z","iopub.execute_input":"2021-11-28T10:30:58.279168Z","iopub.status.idle":"2021-11-28T10:30:58.300595Z","shell.execute_reply.started":"2021-11-28T10:30:58.279134Z","shell.execute_reply":"2021-11-28T10:30:58.299577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for label in labels.columns:\n    print(labels[label].value_counts(normalize=True))","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:30:58.301707Z","iopub.execute_input":"2021-11-28T10:30:58.301991Z","iopub.status.idle":"2021-11-28T10:30:58.332645Z","shell.execute_reply.started":"2021-11-28T10:30:58.301965Z","shell.execute_reply":"2021-11-28T10:30:58.331946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsubmissions.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:30:58.333589Z","iopub.execute_input":"2021-11-28T10:30:58.333965Z","iopub.status.idle":"2021-11-28T10:30:58.352590Z","shell.execute_reply.started":"2021-11-28T10:30:58.333938Z","shell.execute_reply":"2021-11-28T10:30:58.351557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h_target = 224\nw_target = 224\nbatch_size = 16","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:30:58.353775Z","iopub.execute_input":"2021-11-28T10:30:58.354059Z","iopub.status.idle":"2021-11-28T10:30:58.357681Z","shell.execute_reply.started":"2021-11-28T10:30:58.354031Z","shell.execute_reply":"2021-11-28T10:30:58.356773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_data_generator.flow_from_dataframe(\n    submissions,\n    directory = '../input/plant-pathology-2021-fgvc8/test_images',\n    x_col=\"image\",\n    y_col=None,\n    target_size=(h_target, w_target),\n    color_mode=\"rgb\",\n    classes=None,\n    class_mode=None,\n    shuffle=False,\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:30:58.359599Z","iopub.execute_input":"2021-11-28T10:30:58.360113Z","iopub.status.idle":"2021-11-28T10:30:58.383916Z","shell.execute_reply.started":"2021-11-28T10:30:58.360074Z","shell.execute_reply":"2021-11-28T10:30:58.382975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model(\"../input/resnet50-train/ResNet 50 v2.h5\")\npreds = model.predict(test_generator)\nprint(preds)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:31:08.502303Z","iopub.execute_input":"2021-11-28T10:31:08.502673Z","iopub.status.idle":"2021-11-28T10:31:16.369150Z","shell.execute_reply.started":"2021-11-28T10:31:08.502640Z","shell.execute_reply":"2021-11-28T10:31:16.368050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thresh = {\n    'complex':0.23,\n    'frog_eye_leaf_spot':0.23,\n    'healthy':0.26,\n    'powdery_mildew':0.23,\n    'rust':0.23,\n    'scab':0.23\n}","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:31:28.910738Z","iopub.execute_input":"2021-11-28T10:31:28.911121Z","iopub.status.idle":"2021-11-28T10:31:28.915857Z","shell.execute_reply.started":"2021-11-28T10:31:28.911089Z","shell.execute_reply":"2021-11-28T10:31:28.914970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(submissions['image'])):\n    if preds[i][2] == np.max(preds[i]):\n        submissions['labels'][i] == 'healthy'\n    else:\n        label_comb = []\n        for j, label in enumerate(thresh.keys()):\n            if preds[i][j] > thresh[label]:\n                label_comb.append(label)\n        submissions['labels'][i] = ' '.join([str(elem) for elem in label_comb]\n  )\n        if submissions['labels'][i] == ''or 'healthy' in submissions['labels'][i]:\n            submissions['labels'][i] = ' '.join(labels.columns[:][preds[i] >= np.max(preds[i])])\nsubmissions.to_csv('submission.csv', index=False)   ","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:31:34.934076Z","iopub.execute_input":"2021-11-28T10:31:34.934748Z","iopub.status.idle":"2021-11-28T10:31:34.950430Z","shell.execute_reply.started":"2021-11-28T10:31:34.934696Z","shell.execute_reply":"2021-11-28T10:31:34.949394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions","metadata":{"execution":{"iopub.status.busy":"2021-11-28T10:31:37.077362Z","iopub.execute_input":"2021-11-28T10:31:37.077732Z","iopub.status.idle":"2021-11-28T10:31:37.087198Z","shell.execute_reply.started":"2021-11-28T10:31:37.077702Z","shell.execute_reply":"2021-11-28T10:31:37.086335Z"},"trusted":true},"execution_count":null,"outputs":[]}]}