{"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:31:50.142277Z","iopub.execute_input":"2021-11-28T05:31:50.142804Z","iopub.status.idle":"2021-11-28T05:31:57.31966Z","shell.execute_reply.started":"2021-11-28T05:31:50.142687Z","shell.execute_reply":"2021-11-28T05:31:57.318951Z"},"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:31:57.321044Z","iopub.execute_input":"2021-11-28T05:31:57.321244Z","iopub.status.idle":"2021-11-28T05:31:57.363792Z","shell.execute_reply.started":"2021-11-28T05:31:57.321219Z","shell.execute_reply":"2021-11-28T05:31:57.363176Z"},"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:31:57.365263Z","iopub.execute_input":"2021-11-28T05:31:57.365708Z","iopub.status.idle":"2021-11-28T05:31:57.416733Z","shell.execute_reply.started":"2021-11-28T05:31:57.365675Z","shell.execute_reply":"2021-11-28T05:31:57.415884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:31:57.418556Z","iopub.execute_input":"2021-11-28T05:31:57.418797Z","iopub.status.idle":"2021-11-28T05:31:57.436046Z","shell.execute_reply.started":"2021-11-28T05:31:57.418767Z","shell.execute_reply":"2021-11-28T05:31:57.435275Z"},"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:31:57.437353Z","iopub.execute_input":"2021-11-28T05:31:57.437575Z","iopub.status.idle":"2021-11-28T05:31:57.456452Z","shell.execute_reply.started":"2021-11-28T05:31:57.437549Z","shell.execute_reply":"2021-11-28T05:31:57.455597Z"},"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:31:57.457665Z","iopub.execute_input":"2021-11-28T05:31:57.458201Z","iopub.status.idle":"2021-11-28T05:31:57.474108Z","shell.execute_reply.started":"2021-11-28T05:31:57.458156Z","shell.execute_reply":"2021-11-28T05:31:57.473283Z"},"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:31:57.475275Z","iopub.execute_input":"2021-11-28T05:31:57.475797Z","iopub.status.idle":"2021-11-28T05:31:57.479627Z","shell.execute_reply.started":"2021-11-28T05:31:57.475755Z","shell.execute_reply":"2021-11-28T05:31:57.478841Z"},"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:31:57.480845Z","iopub.execute_input":"2021-11-28T05:31:57.481381Z","iopub.status.idle":"2021-11-28T05:31:57.501435Z","shell.execute_reply.started":"2021-11-28T05:31:57.481347Z","shell.execute_reply":"2021-11-28T05:31:57.500567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model(\"../input/resnet50v2-submit/ResNet50_V2.h5\")\npreds = model.predict(test_generator)\nprint(preds)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:31:57.502888Z","iopub.execute_input":"2021-11-28T05:31:57.503466Z","iopub.status.idle":"2021-11-28T05:32:07.407469Z","shell.execute_reply.started":"2021-11-28T05:31:57.503433Z","shell.execute_reply":"2021-11-28T05:32:07.406794Z"},"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:32:07.409213Z","iopub.execute_input":"2021-11-28T05:32:07.409423Z","iopub.status.idle":"2021-11-28T05:32:07.41393Z","shell.execute_reply.started":"2021-11-28T05:32:07.409398Z","shell.execute_reply":"2021-11-28T05:32:07.413111Z"},"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:32:07.415305Z","iopub.execute_input":"2021-11-28T05:32:07.415863Z","iopub.status.idle":"2021-11-28T05:32:07.448552Z","shell.execute_reply.started":"2021-11-28T05:32:07.415819Z","shell.execute_reply":"2021-11-28T05:32:07.447599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions","metadata":{"execution":{"iopub.status.busy":"2021-11-28T05:32:07.450011Z","iopub.execute_input":"2021-11-28T05:32:07.450508Z","iopub.status.idle":"2021-11-28T05:32:07.464158Z","shell.execute_reply.started":"2021-11-28T05:32:07.450465Z","shell.execute_reply":"2021-11-28T05:32:07.463353Z"},"trusted":true},"execution_count":null,"outputs":[]}]}