{"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 *\nseed = 42\nset_seed(seed, reproducible=True)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-26T08:04:04.611069Z","iopub.execute_input":"2021-05-26T08:04:04.611452Z","iopub.status.idle":"2021-05-26T08:04:04.616719Z","shell.execute_reply.started":"2021-05-26T08:04:04.611415Z","shell.execute_reply":"2021-05-26T08:04:04.615708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/plant-pathology-2021-fgvc8')\ndata_path = Path('../input/resized-plant2021')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:04:04.621839Z","iopub.execute_input":"2021-05-26T08:04:04.622149Z","iopub.status.idle":"2021-05-26T08:04:04.629805Z","shell.execute_reply.started":"2021-05-26T08:04:04.622120Z","shell.execute_reply":"2021-05-26T08:04:04.628931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(path/'train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:04:04.658208Z","iopub.execute_input":"2021-05-26T08:04:04.658465Z","iopub.status.idle":"2021-05-26T08:04:04.687321Z","shell.execute_reply.started":"2021-05-26T08:04:04.658439Z","shell.execute_reply":"2021-05-26T08:04:04.686402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:04:04.697817Z","iopub.execute_input":"2021-05-26T08:04:04.698056Z","iopub.status.idle":"2021-05-26T08:04:04.703012Z","shell.execute_reply.started":"2021-05-26T08:04:04.698033Z","shell.execute_reply":"2021-05-26T08:04:04.702124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['labels'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:04:04.716134Z","iopub.execute_input":"2021-05-26T08:04:04.716375Z","iopub.status.idle":"2021-05-26T08:04:04.729154Z","shell.execute_reply.started":"2021-05-26T08:04:04.716352Z","shell.execute_reply":"2021-05-26T08:04:04.728329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_tfms = [RandomResizedCrop(224)]\nbatch_tfms = [Dihedral(p=0.5),\n              Rotate(max_deg=180, p=0.5, pad_mode='reflection'),\n              Zoom(min_zoom=1.0, max_zoom=1.2, p=0.5, pad_mode='reflection'),\n              Warp(magnitude=0.2, p=0.5),\n              Brightness(max_lighting=0.15, p=0.75), \n              Contrast(max_lighting=0.15, p=0.75)]\n\ndls = ImageDataLoaders.from_df(\n    df = df,\n    folder = '../input/resized-plant2021/img_sz_256',\n    item_tfms = item_tfms,\n    batch_tfms = batch_tfms,\n    splitter = RandomSplitter(valid_pct=0.1),\n    label_delim = ' ',\n    bs=128)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:04:04.748568Z","iopub.execute_input":"2021-05-26T08:04:04.748803Z","iopub.status.idle":"2021-05-26T08:04:06.032278Z","shell.execute_reply.started":"2021-05-26T08:04:04.748779Z","shell.execute_reply":"2021-05-26T08:04:06.031423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:04:06.033800Z","iopub.execute_input":"2021-05-26T08:04:06.034166Z","iopub.status.idle":"2021-05-26T08:04:07.667960Z","shell.execute_reply.started":"2021-05-26T08:04:06.034127Z","shell.execute_reply":"2021-05-26T08:04:07.667183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /root/.cache/torch/hub/checkpoints/\n!cp ../input/resnet50/resnet50.pth  /root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:04:07.669696Z","iopub.execute_input":"2021-05-26T08:04:07.670184Z","iopub.status.idle":"2021-05-26T08:04:09.310993Z","shell.execute_reply.started":"2021-05-26T08:04:07.670144Z","shell.execute_reply":"2021-05-26T08:04:09.309912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(\n    dls,\n    resnet50, \n    opt_func=Adam, \n    loss_func=BCEWithLogitsLossFlat(), \n    metrics=[accuracy_multi, F1ScoreMulti()]).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:04:09.313164Z","iopub.execute_input":"2021-05-26T08:04:09.313514Z","iopub.status.idle":"2021-05-26T08:04:10.067052Z","shell.execute_reply.started":"2021-05-26T08:04:09.313470Z","shell.execute_reply":"2021-05-26T08:04:10.066209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:04:10.068360Z","iopub.execute_input":"2021-05-26T08:04:10.068696Z","iopub.status.idle":"2021-05-26T08:05:18.847998Z","shell.execute_reply.started":"2021-05-26T08:04:10.068661Z","shell.execute_reply":"2021-05-26T08:05:18.847194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(5, 0.025118863582611083,\n    cbs = [\n        EarlyStoppingCallback(patience=2),\n        SaveModelCallback(),\n    ],\n    freeze_epochs=1\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:05:18.849446Z","iopub.execute_input":"2021-05-26T08:05:18.849796Z","iopub.status.idle":"2021-05-26T08:16:37.837408Z","shell.execute_reply.started":"2021-05-26T08:05:18.849755Z","shell.execute_reply":"2021-05-26T08:16:37.836341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:16:37.838986Z","iopub.execute_input":"2021-05-26T08:16:37.839352Z","iopub.status.idle":"2021-05-26T08:16:39.336545Z","shell.execute_reply.started":"2021-05-26T08:16:37.839309Z","shell.execute_reply":"2021-05-26T08:16:39.335797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:16:39.339073Z","iopub.execute_input":"2021-05-26T08:16:39.339597Z","iopub.status.idle":"2021-05-26T08:16:56.818984Z","shell.execute_reply.started":"2021-05-26T08:16:39.339554Z","shell.execute_reply":"2021-05-26T08:16:56.818048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(9)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:16:56.820811Z","iopub.execute_input":"2021-05-26T08:16:56.821158Z","iopub.status.idle":"2021-05-26T08:16:57.431872Z","shell.execute_reply.started":"2021-05-26T08:16:56.821107Z","shell.execute_reply":"2021-05-26T08:16:57.430975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.read_csv(path/\"sample_submission.csv\")\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:16:57.433153Z","iopub.execute_input":"2021-05-26T08:16:57.433466Z","iopub.status.idle":"2021-05-26T08:16:57.457934Z","shell.execute_reply.started":"2021-05-26T08:16:57.433433Z","shell.execute_reply":"2021-05-26T08:16:57.456990Z"},"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":{"execution":{"iopub.status.busy":"2021-05-26T08:17:59.286817Z","iopub.execute_input":"2021-05-26T08:17:59.287153Z","iopub.status.idle":"2021-05-26T08:17:59.297551Z","shell.execute_reply.started":"2021-05-26T08:17:59.287120Z","shell.execute_reply":"2021-05-26T08:17:59.296705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = learn.dls.test_dl(test_image_path_series)\npreds, _ = learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:17:59.734943Z","iopub.execute_input":"2021-05-26T08:17:59.735273Z","iopub.status.idle":"2021-05-26T08:18:00.790938Z","shell.execute_reply.started":"2021-05-26T08:17:59.735245Z","shell.execute_reply":"2021-05-26T08:18:00.789940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thresh = 0.5\nlabelled_preds = [' '.join([learn.dls.vocab[i] for i,p in enumerate(pred) if p > thresh]) for pred in preds]\nlabelled_preds","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:18:00.792845Z","iopub.execute_input":"2021-05-26T08:18:00.793258Z","iopub.status.idle":"2021-05-26T08:18:00.802393Z","shell.execute_reply.started":"2021-05-26T08:18:00.793210Z","shell.execute_reply":"2021-05-26T08:18:00.801317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df[\"labels\"] = labelled_preds\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:18:00.854297Z","iopub.execute_input":"2021-05-26T08:18:00.854530Z","iopub.status.idle":"2021-05-26T08:18:00.863865Z","shell.execute_reply.started":"2021-05-26T08:18:00.854508Z","shell.execute_reply":"2021-05-26T08:18:00.863033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T08:18:01.705181Z","iopub.execute_input":"2021-05-26T08:18:01.705508Z","iopub.status.idle":"2021-05-26T08:18:01.941928Z","shell.execute_reply.started":"2021-05-26T08:18:01.705480Z","shell.execute_reply":"2021-05-26T08:18:01.941163Z"},"trusted":true},"execution_count":null,"outputs":[]}]}