{"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":"from fastai.vision.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set_seed(2021)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(\"../input/plant-pathology-2021-fgvc8\")\npath.ls()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(path/\"train.csv\")\ntrain_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"labels\"].value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I use this [resized dataset](https://www.kaggle.com/ankursingh12/resized-plant2021) to save my time.  \nWithout this, it takes too much time to resizing images...😅","metadata":{}},{"cell_type":"code","source":"item_tfms = [RandomResizedCrop(128, min_scale=0.75, ratio=(1., 1.))]\nbatch_tfms = [*aug_transforms(size=128, max_warp=0), Normalize.from_stats(*imagenet_stats)]\n\ndls = ImageDataLoaders.from_df(\n    df = train_df,\n    folder = \"../input/resized-plant2021/img_sz_512\",\n    item_tfms = item_tfms,\n    batch_tfms = batch_tfms,\n    splitter = RandomSplitter(valid_pct=0.1),\n    label_delim = \" \",\n    bs=256\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if you are a big fan of DataBlock API, you can use this code below\n\n# db = DataBlock(\n#     blocks=(ImageBlock, MultiCategoryBlock),\n#     get_x=ColReader(\"image\", pref=\"../input/resized-plant2021/img_sz_640/\"),\n#     get_y=ColReader(\"labels\", label_delim=\" \"),\n#     splitter=RandomSplitter(valid_pct=0.1),\n#     item_tfms=item_tfms,\n#     batch_tfms=batch_tfms\n# )\n# dls = db.dataloaders(train_df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /root/.cache/torch/hub/checkpoints\n# !cp ../input/resnet18/resnet18.pth /root/.cache/torch/hub/checkpoints/resnet18-5c106cde.pth\n!cp ../input/resnet50/resnet50.pth /root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(\n    dls,\n    resnet50,\n    metrics=[accuracy_multi, F1ScoreMulti()]\n).to_fp16()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(\n    7,\n    1e-1,\n    cbs=[\n        SaveModelCallback(),\n        EarlyStoppingCallback(patience=3),\n    ],\n    freeze_epochs=3\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(9)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.read_csv(path/\"sample_submission.csv\")\nsubmission_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_path_series = submission_df[\"image\"].apply(lambda x: f\"../input/plant-pathology-2021-fgvc8/test_images/{x}\")\ntest_image_path_series.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = learn.dls.test_dl(test_image_path_series)\npreds, _ = learn.tta(dl=test_dl)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab = learn.dls.vocab\nvocab","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold = 0.5\n\ndef pred_to_labels(pred):\n    labels = []\n    for i, probability in enumerate(pred):\n        if probability > threshold:\n            labels.append(vocab[i])\n            \n    return \" \".join(labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_list = [pred_to_labels(pred) for pred in preds]\nlabels_list","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df[\"labels\"] = labels_list\nsubmission_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}