{"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.execute_input":"2021-11-28T05:36:07.981405Z","iopub.status.busy":"2021-11-28T05:36:07.980028Z","iopub.status.idle":"2021-11-28T05:36:15.156757Z","shell.execute_reply":"2021-11-28T05:36:15.155892Z","shell.execute_reply.started":"2021-11-28T05:35:38.709202Z"},"papermill":{"duration":7.193084,"end_time":"2021-11-28T05:36:15.156929","exception":false,"start_time":"2021-11-28T05:36:07.963845","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')","metadata":{"execution":{"iopub.execute_input":"2021-11-28T05:36:15.187664Z","iopub.status.busy":"2021-11-28T05:36:15.186882Z","iopub.status.idle":"2021-11-28T05:36:15.226804Z","shell.execute_reply":"2021-11-28T05:36:15.226208Z","shell.execute_reply.started":"2021-11-28T05:35:38.721540Z"},"papermill":{"duration":0.05581,"end_time":"2021-11-28T05:36:15.226943","exception":false,"start_time":"2021-11-28T05:36:15.171133","status":"completed"},"tags":[]},"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.execute_input":"2021-11-28T05:36:15.261154Z","iopub.status.busy":"2021-11-28T05:36:15.260319Z","iopub.status.idle":"2021-11-28T05:36:15.304113Z","shell.execute_reply":"2021-11-28T05:36:15.303448Z","shell.execute_reply.started":"2021-11-28T05:35:38.754854Z"},"papermill":{"duration":0.066452,"end_time":"2021-11-28T05:36:15.304281","exception":false,"start_time":"2021-11-28T05:36:15.237829","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.execute_input":"2021-11-28T05:36:15.329252Z","iopub.status.busy":"2021-11-28T05:36:15.328623Z","iopub.status.idle":"2021-11-28T05:36:15.343651Z","shell.execute_reply":"2021-11-28T05:36:15.344360Z","shell.execute_reply.started":"2021-11-28T05:35:38.800084Z"},"papermill":{"duration":0.029468,"end_time":"2021-11-28T05:36:15.344539","exception":false,"start_time":"2021-11-28T05:36:15.315071","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for label in labels.columns:\n    print(labels[label].value_counts(normalize=True))","metadata":{"execution":{"iopub.execute_input":"2021-11-28T05:36:15.371511Z","iopub.status.busy":"2021-11-28T05:36:15.370546Z","iopub.status.idle":"2021-11-28T05:36:15.386996Z","shell.execute_reply":"2021-11-28T05:36:15.388139Z","shell.execute_reply.started":"2021-11-28T05:35:38.812004Z"},"papermill":{"duration":0.032459,"end_time":"2021-11-28T05:36:15.388487","exception":false,"start_time":"2021-11-28T05:36:15.356028","status":"completed"},"tags":[]},"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.execute_input":"2021-11-28T05:36:15.416655Z","iopub.status.busy":"2021-11-28T05:36:15.415926Z","iopub.status.idle":"2021-11-28T05:36:15.431570Z","shell.execute_reply":"2021-11-28T05:36:15.430926Z","shell.execute_reply.started":"2021-11-28T05:35:38.836906Z"},"papermill":{"duration":0.030073,"end_time":"2021-11-28T05:36:15.431707","exception":false,"start_time":"2021-11-28T05:36:15.401634","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h_target = 480\nw_target = 480\nbatch_size = 16","metadata":{"execution":{"iopub.execute_input":"2021-11-28T05:36:15.461234Z","iopub.status.busy":"2021-11-28T05:36:15.460262Z","iopub.status.idle":"2021-11-28T05:36:15.463018Z","shell.execute_reply":"2021-11-28T05:36:15.462468Z","shell.execute_reply.started":"2021-11-28T05:35:38.855486Z"},"papermill":{"duration":0.019087,"end_time":"2021-11-28T05:36:15.463160","exception":false,"start_time":"2021-11-28T05:36:15.444073","status":"completed"},"tags":[]},"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.execute_input":"2021-11-28T05:36:15.490697Z","iopub.status.busy":"2021-11-28T05:36:15.490037Z","iopub.status.idle":"2021-11-28T05:36:15.505973Z","shell.execute_reply":"2021-11-28T05:36:15.506703Z","shell.execute_reply.started":"2021-11-28T05:35:38.862491Z"},"papermill":{"duration":0.031606,"end_time":"2021-11-28T05:36:15.506959","exception":false,"start_time":"2021-11-28T05:36:15.475353","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model(\"../input/resnet50-480x480-submit/ResNet50_480x480.h5\")\npreds = model.predict(test_generator)\nprint(preds)","metadata":{"execution":{"iopub.execute_input":"2021-11-28T05:36:15.537400Z","iopub.status.busy":"2021-11-28T05:36:15.536410Z","iopub.status.idle":"2021-11-28T05:36:24.805534Z","shell.execute_reply":"2021-11-28T05:36:24.806122Z","shell.execute_reply.started":"2021-11-28T05:35:38.887247Z"},"papermill":{"duration":9.286149,"end_time":"2021-11-28T05:36:24.806307","exception":false,"start_time":"2021-11-28T05:36:15.520158","status":"completed"},"tags":[]},"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.execute_input":"2021-11-28T05:36:24.836742Z","iopub.status.busy":"2021-11-28T05:36:24.836129Z","iopub.status.idle":"2021-11-28T05:36:24.840620Z","shell.execute_reply":"2021-11-28T05:36:24.839982Z","shell.execute_reply.started":"2021-11-28T05:35:46.164275Z"},"papermill":{"duration":0.02101,"end_time":"2021-11-28T05:36:24.840759","exception":false,"start_time":"2021-11-28T05:36:24.819749","status":"completed"},"tags":[]},"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.execute_input":"2021-11-28T05:36:24.872703Z","iopub.status.busy":"2021-11-28T05:36:24.871772Z","iopub.status.idle":"2021-11-28T05:36:24.883072Z","shell.execute_reply":"2021-11-28T05:36:24.883676Z","shell.execute_reply.started":"2021-11-28T05:35:46.165972Z"},"papermill":{"duration":0.029594,"end_time":"2021-11-28T05:36:24.883841","exception":false,"start_time":"2021-11-28T05:36:24.854247","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions","metadata":{"execution":{"iopub.execute_input":"2021-11-28T05:36:24.914663Z","iopub.status.busy":"2021-11-28T05:36:24.913729Z","iopub.status.idle":"2021-11-28T05:36:24.922267Z","shell.execute_reply":"2021-11-28T05:36:24.922715Z","shell.execute_reply.started":"2021-11-28T05:35:46.168155Z"},"papermill":{"duration":0.025657,"end_time":"2021-11-28T05:36:24.922880","exception":false,"start_time":"2021-11-28T05:36:24.897223","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}