{"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":"# Numpy and pandas by default assume a narrow screen - this fixes that\n\nfrom fastai.vision.all import *\n\nfrom ipywidgets import widgets\nfrom pandas.api.types import CategoricalDtype\nimport os\nimport matplotlib as mpl\n# mpl.rcParams['figure.dpi']= 200\nmpl.rcParams['savefig.dpi']= 200\nmpl.rcParams['font.size']=12\n\nset_seed(42)\ntorch.backends.cudnn.deterministic = True\ntorch.backends.cudnn.benchmark = False\npd.set_option('display.max_columns',999)\nnp.set_printoptions(linewidth=200)\ntorch.set_printoptions(linewidth=200)\nimport pandas as pd\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T11:23:39.995338Z","iopub.execute_input":"2021-05-20T11:23:39.995758Z","iopub.status.idle":"2021-05-20T11:23:42.493951Z","shell.execute_reply.started":"2021-05-20T11:23:39.995677Z","shell.execute_reply":"2021-05-20T11:23:42.493068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\nprint(len(df))\nprint(df.columns)\n# print(train['labels'].value_counts())\nprint(df['labels'].value_counts().plot.bar())","metadata":{"execution":{"iopub.status.busy":"2021-05-20T11:23:42.495383Z","iopub.execute_input":"2021-05-20T11:23:42.495708Z","iopub.status.idle":"2021-05-20T11:23:42.728723Z","shell.execute_reply.started":"2021-05-20T11:23:42.495673Z","shell.execute_reply":"2021-05-20T11:23:42.727777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = Image.open('../input/plant-pathology-2021-fgvc8/train_images/800113bb65efe69e.jpg')\nim.thumbnail((256,256))\nim","metadata":{"execution":{"iopub.status.busy":"2021-05-20T11:23:42.730677Z","iopub.execute_input":"2021-05-20T11:23:42.731041Z","iopub.status.idle":"2021-05-20T11:23:42.819682Z","shell.execute_reply.started":"2021-05-20T11:23:42.731003Z","shell.execute_reply":"2021-05-20T11:23:42.818821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\n\n\npath = Path('../input/plant-pathology-2021-fgvc8')\nPath.BASE_PATH=path\npath.ls()\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-20T11:23:42.821044Z","iopub.execute_input":"2021-05-20T11:23:42.821368Z","iopub.status.idle":"2021-05-20T11:23:42.845767Z","shell.execute_reply.started":"2021-05-20T11:23:42.821318Z","shell.execute_reply":"2021-05-20T11:23:42.844866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_dict = df.labels.value_counts()\ncount_dict","metadata":{"execution":{"iopub.status.busy":"2021-05-20T11:23:42.847076Z","iopub.execute_input":"2021-05-20T11:23:42.847476Z","iopub.status.idle":"2021-05-20T11:23:42.857766Z","shell.execute_reply.started":"2021-05-20T11:23:42.847429Z","shell.execute_reply":"2021-05-20T11:23:42.856823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_x(r): return path/'train_images'/r['image']\ndef get_y(r): return r['labels'].split(' ')\ndblock = DataBlock(blocks=(ImageBlock, MultiCategoryBlock),\n                   get_x = get_x, get_y = get_y)\ndsets = dblock.datasets(df)\ndsets.train[2]","metadata":{"execution":{"iopub.status.busy":"2021-05-20T11:23:42.859396Z","iopub.execute_input":"2021-05-20T11:23:42.859856Z","iopub.status.idle":"2021-05-20T11:23:45.755461Z","shell.execute_reply.started":"2021-05-20T11:23:42.859819Z","shell.execute_reply":"2021-05-20T11:23:45.754468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(blocks=(ImageBlock, MultiCategoryBlock),\n               \n                   get_x=get_x, \n                   get_y=get_y,\n                   item_tfms = RandomResizedCrop(244, min_scale=0.55))\ndls = dblock.dataloaders(df)\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T11:23:45.759457Z","iopub.execute_input":"2021-05-20T11:23:45.759824Z","iopub.status.idle":"2021-05-20T11:24:07.375487Z","shell.execute_reply.started":"2021-05-20T11:23:45.759787Z","shell.execute_reply":"2021-05-20T11:24:07.374561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet50, metrics=partial(accuracy_multi, thresh=0.37), model_dir=\"/kaggle/working\")\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T11:24:07.377998Z","iopub.execute_input":"2021-05-20T11:24:07.378514Z","iopub.status.idle":"2021-05-20T11:24:14.052033Z","shell.execute_reply.started":"2021-05-20T11:24:07.378466Z","shell.execute_reply":"2021-05-20T11:24:14.051197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(2, base_lr=3e-3, freeze_epochs=2)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T12:52:59.115091Z","iopub.execute_input":"2021-05-20T12:52:59.115445Z","iopub.status.idle":"2021-05-20T16:19:44.732658Z","shell.execute_reply.started":"2021-05-20T12:52:59.115409Z","shell.execute_reply":"2021-05-20T16:19:44.731790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds,targs = learn.get_preds()\nxs = torch.linspace(0.05,0.95,29)\naccs = [accuracy_multi(preds, targs, thresh=i, sigmoid=False) for i in xs]\nplt.plot(xs,accs);","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:21:22.006419Z","iopub.execute_input":"2021-05-20T16:21:22.006766Z","iopub.status.idle":"2021-05-20T16:31:42.944009Z","shell.execute_reply.started":"2021-05-20T16:21:22.006729Z","shell.execute_reply":"2021-05-20T16:31:42.943173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:31:42.945616Z","iopub.execute_input":"2021-05-20T16:31:42.945962Z","iopub.status.idle":"2021-05-20T16:43:33.925555Z","shell.execute_reply.started":"2021-05-20T16:31:42.945922Z","shell.execute_reply":"2021-05-20T16:43:33.924685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix(figsize=(12,12), dpi=60)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:43:33.927612Z","iopub.execute_input":"2021-05-20T16:43:33.927978Z","iopub.status.idle":"2021-05-20T16:54:00.435407Z","shell.execute_reply.started":"2021-05-20T16:43:33.927936Z","shell.execute_reply":"2021-05-20T16:54:00.434512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save(\"model\")","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:54:00.437346Z","iopub.execute_input":"2021-05-20T16:54:00.437793Z","iopub.status.idle":"2021-05-20T16:54:00.998278Z","shell.execute_reply.started":"2021-05-20T16:54:00.437744Z","shell.execute_reply":"2021-05-20T16:54:00.997275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.most_confused(min_val=5)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:54:00.999781Z","iopub.execute_input":"2021-05-20T16:54:01.000138Z","iopub.status.idle":"2021-05-20T16:54:01.009853Z","shell.execute_reply.started":"2021-05-20T16:54:01.000098Z","shell.execute_reply":"2021-05-20T16:54:01.008832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files=get_image_files(path/'test_images')\nlearn.predict(files[1])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:54:01.011219Z","iopub.execute_input":"2021-05-20T16:54:01.011860Z","iopub.status.idle":"2021-05-20T16:54:01.347056Z","shell.execute_reply.started":"2021-05-20T16:54:01.011804Z","shell.execute_reply":"2021-05-20T16:54:01.346316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = Image.open('../input/plant-pathology-2021-fgvc8/test_images/ad8770db05586b59.jpg')\nim.thumbnail((256,256))\nim","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:54:01.348423Z","iopub.execute_input":"2021-05-20T16:54:01.348771Z","iopub.status.idle":"2021-05-20T16:54:01.446563Z","shell.execute_reply.started":"2021-05-20T16:54:01.348733Z","shell.execute_reply":"2021-05-20T16:54:01.445514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nim = Image.open('../input/plant-pathology-2021-fgvc8/test_images/c7b03e718489f3ca.jpg')\nim.thumbnail((256,256))\nim","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:54:01.462676Z","iopub.execute_input":"2021-05-20T16:54:01.463035Z","iopub.status.idle":"2021-05-20T16:54:01.524045Z","shell.execute_reply.started":"2021-05-20T16:54:01.462999Z","shell.execute_reply":"2021-05-20T16:54:01.523036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files=get_image_files(path/'test_images')\nlearn.predict(files[0])\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:54:01.525427Z","iopub.execute_input":"2021-05-20T16:54:01.525892Z","iopub.status.idle":"2021-05-20T16:54:01.809087Z","shell.execute_reply.started":"2021-05-20T16:54:01.525858Z","shell.execute_reply":"2021-05-20T16:54:01.808114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = Image.open('../input/plant-pathology-2021-fgvc8/test_images/85f8cb619c66b863.jpg')\nim.thumbnail((256,256))\nim","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:54:01.810519Z","iopub.execute_input":"2021-05-20T16:54:01.810875Z","iopub.status.idle":"2021-05-20T16:54:01.913714Z","shell.execute_reply.started":"2021-05-20T16:54:01.810837Z","shell.execute_reply":"2021-05-20T16:54:01.912899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files=get_image_files(path/'test_images')\nlearn.predict(files[2])\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:57:38.351186Z","iopub.execute_input":"2021-05-20T16:57:38.351590Z","iopub.status.idle":"2021-05-20T16:57:38.749926Z","shell.execute_reply.started":"2021-05-20T16:57:38.351538Z","shell.execute_reply":"2021-05-20T16:57:38.748969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl = dls.test_dl(get_image_files(path/'test_images'))\ndl=learn.get_preds(dl=test_dl)\ndl","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:54:02.320056Z","iopub.execute_input":"2021-05-20T16:54:02.320577Z","iopub.status.idle":"2021-05-20T16:54:03.766818Z","shell.execute_reply.started":"2021-05-20T16:54:02.320538Z","shell.execute_reply":"2021-05-20T16:54:03.765787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.DataFrame({\n\"image\":['85f8cb619c66b863.jpg','ad8770db05586b59.jpg','c7b03e718489f3ca.jpg'],\n\"labels\":['complex and frog_eye_leaf_spot','frog_eye_leaf_spot','no label']})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T17:08:55.862017Z","iopub.execute_input":"2021-05-20T17:08:55.862339Z","iopub.status.idle":"2021-05-20T17:08:55.870993Z","shell.execute_reply.started":"2021-05-20T17:08:55.862308Z","shell.execute_reply":"2021-05-20T17:08:55.869929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission=pd.read_csv('./submission.csv')\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2021-05-20T17:11:02.287111Z","iopub.execute_input":"2021-05-20T17:11:02.287513Z","iopub.status.idle":"2021-05-20T17:11:02.298143Z","shell.execute_reply.started":"2021-05-20T17:11:02.287476Z","shell.execute_reply":"2021-05-20T17:11:02.297341Z"},"trusted":true},"execution_count":null,"outputs":[]}]}