{"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":{"execution":{"iopub.status.busy":"2021-11-28T05:35:38.708258Z","iopub.execute_input":"2021-11-28T05:35:38.709255Z","iopub.status.idle":"2021-11-28T05:35:38.718585Z","shell.execute_reply.started":"2021-11-28T05:35:38.709202Z","shell.execute_reply":"2021-11-28T05:35:38.717181Z"},"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-28T05:35:38.720793Z","iopub.execute_input":"2021-11-28T05:35:38.721584Z","iopub.status.idle":"2021-11-28T05:35:38.751913Z","shell.execute_reply.started":"2021-11-28T05:35:38.721540Z","shell.execute_reply":"2021-11-28T05:35:38.751198Z"},"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-28T05:35:38.753601Z","iopub.execute_input":"2021-11-28T05:35:38.754912Z","iopub.status.idle":"2021-11-28T05:35:38.797091Z","shell.execute_reply.started":"2021-11-28T05:35:38.754854Z","shell.execute_reply":"2021-11-28T05:35:38.796244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:35:38.799286Z","iopub.execute_input":"2021-11-28T05:35:38.800141Z","iopub.status.idle":"2021-11-28T05:35:38.809411Z","shell.execute_reply.started":"2021-11-28T05:35:38.800084Z","shell.execute_reply":"2021-11-28T05:35:38.808773Z"},"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-28T05:35:38.811534Z","iopub.execute_input":"2021-11-28T05:35:38.812043Z","iopub.status.idle":"2021-11-28T05:35:38.834637Z","shell.execute_reply.started":"2021-11-28T05:35:38.812004Z","shell.execute_reply":"2021-11-28T05:35:38.833892Z"},"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-28T05:35:38.836423Z","iopub.execute_input":"2021-11-28T05:35:38.836953Z","iopub.status.idle":"2021-11-28T05:35:38.853501Z","shell.execute_reply.started":"2021-11-28T05:35:38.836906Z","shell.execute_reply":"2021-11-28T05:35:38.852462Z"},"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-28T05:35:38.855274Z","iopub.execute_input":"2021-11-28T05:35:38.855512Z","iopub.status.idle":"2021-11-28T05:35:38.860623Z","shell.execute_reply.started":"2021-11-28T05:35:38.855486Z","shell.execute_reply":"2021-11-28T05:35:38.859577Z"},"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-28T05:35:38.862003Z","iopub.execute_input":"2021-11-28T05:35:38.862531Z","iopub.status.idle":"2021-11-28T05:35:38.884720Z","shell.execute_reply.started":"2021-11-28T05:35:38.862491Z","shell.execute_reply":"2021-11-28T05:35:38.883509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model(\"../input/resnet50-sub/ResNet50.h5\")\npreds = model.predict(test_generator)\nprint(preds)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:35:38.886589Z","iopub.execute_input":"2021-11-28T05:35:38.887293Z","iopub.status.idle":"2021-11-28T05:35:46.161685Z","shell.execute_reply.started":"2021-11-28T05:35:38.887247Z","shell.execute_reply":"2021-11-28T05:35:46.160298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thresh = {\n    'complex':0.33,\n    'frog_eye_leaf_spot':0.33,\n    'healthy':0.33,\n    'powdery_mildew':0.33,\n    'rust':0.33,\n    'scab':0.33    \n}","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:35:46.163385Z","iopub.status.idle":"2021-11-28T05:35:46.164495Z","shell.execute_reply.started":"2021-11-28T05:35:46.164275Z","shell.execute_reply":"2021-11-28T05:35:46.164300Z"},"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-28T05:35:46.165571Z","iopub.status.idle":"2021-11-28T05:35:46.166195Z","shell.execute_reply.started":"2021-11-28T05:35:46.165972Z","shell.execute_reply":"2021-11-28T05:35:46.165992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:35:46.167584Z","iopub.status.idle":"2021-11-28T05:35:46.168395Z","shell.execute_reply.started":"2021-11-28T05:35:46.168155Z","shell.execute_reply":"2021-11-28T05:35:46.168178Z"},"trusted":true},"execution_count":null,"outputs":[]}]}