{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\nsys.path.append(\"../input/fastai2-offline/wwf-0.0.3-py3-none-any.whl\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import timm\nfrom wwf.vision.timm import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path(\"../input/cassava-leaf-disease-classification\")\npath.ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"set_seed(2021)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(path/\"train.csv\")\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"item_tfms = RandomResizedCrop(224, min_scale=0.75, ratio=(1.,1.))\nbatch_tfms = [*aug_transforms(size=224, max_warp=0), Normalize.from_stats(*imagenet_stats)]\n\ndls = ImageDataLoaders.from_df(\n    df=train_df,\n    folder=path/\"train_images\",\n    item_tfms=item_tfms,\n    batch_tfms=batch_tfms,\n    splitter=RandomSplitter(valid_pct=0.1),\n)\ndls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"In this competition, we are not allowed to use Internet.  \nHowever, cnn_learner() automatically tries to download resnet model via Internet.  \nTo avoid this, add data(resnet model) from here(https://www.kaggle.com/pytorch/resnet18 or https://www.kaggle.com/pytorch/resnet34),  \nand copy it to /root/.cache/torch/hub/checkpoints/resnet34-333f7ec4.pth  "},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir -p /root/.cache/torch/hub/checkpoints\n# !cp ../input/pytorch-pretrained-models/xception-43020ad28.pth /root/.cache/torch/hub/checkpoints/xception-43020ad28.pth\n# !cp ../input/pytorch-pretrained-models/inceptionresnetv2-520b38e4.pth /root/.cache/torch/hub/checkpoints/inception_resnet_v2-940b1cd6.pth\n# !cp ../input/pytorch-pretrained-models/densenet121-fbdb23505.pth /root/.cache/torch/hub/checkpoints/densenet121_ra-50efcf5c.pth\n# !cp ../input/pytorch-pretrained-models/inceptionv4-8e4777a0.pth /root/.cache/torch/hub/checkpoints/inceptionv4-8e4777a0.pth\n!cp ../input/timm-pretrained-efficientnet/efficientnet/efficientnet_b3_ra-a5e2fbc7.pth /root/.cache/torch/hub/checkpoints/efficientnet_b3_ra2-cf984f9c.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[\"label\"].value_counts()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.utils import class_weight\n\nclass_weights = [train_df[\"label\"].value_counts()[i] for i in range(5)]\nclass_weights\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"weights = len(train_df) / torch.tensor(class_weights).cuda()\nweights","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learn = cnn_learner(\n#     dls, \n#     resnet34, \n#     metrics=accuracy,\n#     loss_func=CrossEntropyLossFlat(weight=weights),\n#     opt_func=ranger,    \n#     ps=0.8,\n# ).to_fp16()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"timm.list_models(\"efficientnet*\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = timm_learner(\n    dls,\n    \"efficientnet_b3\",\n     metrics=accuracy,\n    loss_func=CrossEntropyLossFlat(weight=weights),\n#     loss_func=LabelSmoothingCrossEntropy(),\n#     opt_func=ranger,    \n).to_fp16()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import gc \ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fine_tune(\n    25,\n    1e-1,\n    cbs=[\n        SaveModelCallback(),\n        EarlyStoppingCallback(patience=10),\n        GradientAccumulation(),\n#         MixUp(),\n    ],\n    freeze_epochs=5,\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def fit_with(wd):\n#     learn = cnn_learner(\n#         dls, \n#         resnet50, \n#         metrics=accuracy,\n#         opt_func=ranger,    \n#         wd=wd,\n#         loss_func=CrossEntropyLossFlat(weight=weights)\n#     ).to_fp16()\n            \n#     learn.fit_one_cycle(\n#         10, \n#         slice(1e-6, 1e-4), \n#     )\n        \n#     acc = learn.validate()[1]\n        \n#     return acc\n\n# pbounds = {\n#     \"wd\": (0.01, 10)\n# }\n\n# optimizer = BayesianOptimization(\n#     f=fit_with,\n#     pbounds=pbounds,\n#     verbose=1,\n# )\n\n# optimizer.maximize()\n\n# optimizer.max","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save(\"fine_tune_5+25\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.load(\"fine_tune_5+25\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.recorder.plot_loss()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.show_results()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_top_losses(9)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_confusion_matrix()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv(path/\"sample_submission.csv\")\nsubmission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_image_path_series = submission_df[\"image_id\"].apply(lambda x: path/'test_images'/x)\ntest_image_path_series.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dl = learn.dls.test_dl(test_image_path_series)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds, _ = learn.get_preds(dl=test_dl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = np.argmax(preds, axis=1)\nsubmission_df[\"label\"] = result\nsubmission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}