{"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":"markdown","source":"# 1. At first we will import all libraries","metadata":{}},{"cell_type":"code","source":"! pip install timm","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:11.370056Z","iopub.execute_input":"2022-07-31T08:14:11.370646Z","iopub.status.idle":"2022-07-31T08:14:27.558353Z","shell.execute_reply.started":"2022-07-31T08:14:11.370523Z","shell.execute_reply":"2022-07-31T08:14:27.556929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:27.564298Z","iopub.execute_input":"2022-07-31T08:14:27.566671Z","iopub.status.idle":"2022-07-31T08:14:27.627068Z","shell.execute_reply.started":"2022-07-31T08:14:27.566627Z","shell.execute_reply":"2022-07-31T08:14:27.626145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport torch\nfrom fastai import *\nfrom fastai.basics import *\nfrom fastai.callback.all import *\nfrom fastai.vision import *\nfrom fastai.vision.all import *\nfrom fastai.metrics import error_rate, accuracy\nimport timm\nimport numpy as np\nimport pandas as pd\nimport matplotlib as plt\nimport random\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:27.631580Z","iopub.execute_input":"2022-07-31T08:14:27.633888Z","iopub.status.idle":"2022-07-31T08:14:29.984832Z","shell.execute_reply.started":"2022-07-31T08:14:27.633849Z","shell.execute_reply":"2022-07-31T08:14:29.983536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:29.988530Z","iopub.execute_input":"2022-07-31T08:14:29.991555Z","iopub.status.idle":"2022-07-31T08:14:30.064565Z","shell.execute_reply.started":"2022-07-31T08:14:29.991511Z","shell.execute_reply":"2022-07-31T08:14:30.063541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Defining `path`, `train`, `test` and `df`","metadata":{}},{"cell_type":"code","source":"path = Path('../input/paddy-disease-classification')\npath.ls()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:30.066650Z","iopub.execute_input":"2022-07-31T08:14:30.067390Z","iopub.status.idle":"2022-07-31T08:14:30.141361Z","shell.execute_reply.started":"2022-07-31T08:14:30.067347Z","shell.execute_reply":"2022-07-31T08:14:30.140421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = (path/'train_images')\ntest_dir = (path/'test_images')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:30.142963Z","iopub.execute_input":"2022-07-31T08:14:30.143646Z","iopub.status.idle":"2022-07-31T08:14:30.206502Z","shell.execute_reply.started":"2022-07-31T08:14:30.143609Z","shell.execute_reply":"2022-07-31T08:14:30.205480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir.ls()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:30.208188Z","iopub.execute_input":"2022-07-31T08:14:30.209152Z","iopub.status.idle":"2022-07-31T08:14:30.274125Z","shell.execute_reply.started":"2022-07-31T08:14:30.209113Z","shell.execute_reply":"2022-07-31T08:14:30.273155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir.ls()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:30.275769Z","iopub.execute_input":"2022-07-31T08:14:30.276537Z","iopub.status.idle":"2022-07-31T08:14:30.352020Z","shell.execute_reply.started":"2022-07-31T08:14:30.276498Z","shell.execute_reply":"2022-07-31T08:14:30.351005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(path/'train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:30.353658Z","iopub.execute_input":"2022-07-31T08:14:30.354351Z","iopub.status.idle":"2022-07-31T08:14:30.447883Z","shell.execute_reply.started":"2022-07-31T08:14:30.354282Z","shell.execute_reply":"2022-07-31T08:14:30.446513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Dataloader","metadata":{}},{"cell_type":"code","source":"bs=32","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:30.454295Z","iopub.execute_input":"2022-07-31T08:14:30.454749Z","iopub.status.idle":"2022-07-31T08:14:30.517506Z","shell.execute_reply.started":"2022-07-31T08:14:30.454711Z","shell.execute_reply":"2022-07-31T08:14:30.516472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_folder(path=train_dir, valid_pct=0.2, seed=42, item_tfms=Resize(224), bs=bs)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:30.522631Z","iopub.execute_input":"2022-07-31T08:14:30.523070Z","iopub.status.idle":"2022-07-31T08:14:34.931531Z","shell.execute_reply.started":"2022-07-31T08:14:30.523033Z","shell.execute_reply":"2022-07-31T08:14:34.930444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:34.933062Z","iopub.execute_input":"2022-07-31T08:14:34.933769Z","iopub.status.idle":"2022-07-31T08:14:36.523995Z","shell.execute_reply.started":"2022-07-31T08:14:34.933729Z","shell.execute_reply":"2022-07-31T08:14:36.523123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. The Model Learning Part","metadata":{}},{"cell_type":"code","source":"from timm.models import*\nall_models = timm.list_models('*convnext*')\nall_models","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:36.524977Z","iopub.execute_input":"2022-07-31T08:14:36.525327Z","iopub.status.idle":"2022-07-31T08:14:36.604345Z","shell.execute_reply.started":"2022-07-31T08:14:36.525278Z","shell.execute_reply":"2022-07-31T08:14:36.603279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, 'convnext_large_in22k', model_dir=\"/tmp/models\",metrics=[accuracy, error_rate]).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:36.609191Z","iopub.execute_input":"2022-07-31T08:14:36.609663Z","iopub.status.idle":"2022-07-31T08:14:44.275029Z","shell.execute_reply.started":"2022-07-31T08:14:36.609624Z","shell.execute_reply":"2022-07-31T08:14:44.273943Z"},"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(10, 1e-3)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T08:14:44.276561Z","iopub.execute_input":"2022-07-31T08:14:44.277269Z","iopub.status.idle":"2022-07-31T09:17:58.806918Z","shell.execute_reply.started":"2022-07-31T08:14:44.277230Z","shell.execute_reply":"2022-07-31T09:17:58.805623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T09:20:23.527367Z","iopub.execute_input":"2022-07-31T09:20:23.530008Z","iopub.status.idle":"2022-07-31T09:20:25.766501Z","shell.execute_reply.started":"2022-07-31T09:20:23.529967Z","shell.execute_reply":"2022-07-31T09:20:25.765381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Visualizing the Confusion Metrics","metadata":{}},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T09:19:13.259359Z","iopub.execute_input":"2022-07-31T09:19:13.259690Z","iopub.status.idle":"2022-07-31T09:20:23.522522Z","shell.execute_reply.started":"2022-07-31T09:19:13.259656Z","shell.execute_reply":"2022-07-31T09:20:23.521355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. Submitting the submission.csv file","metadata":{}},{"cell_type":"code","source":"ss = pd.read_csv(path/'sample_submission.csv')\nss","metadata":{"execution":{"iopub.status.busy":"2022-07-31T09:20:34.860790Z","iopub.execute_input":"2022-07-31T09:20:34.861234Z","iopub.status.idle":"2022-07-31T09:20:34.951043Z","shell.execute_reply.started":"2022-07-31T09:20:34.861195Z","shell.execute_reply":"2022-07-31T09:20:34.949778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst_files = get_image_files(path/'test_images').sorted()\ntst_dl = dls.test_dl(tst_files)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T09:20:41.448777Z","iopub.execute_input":"2022-07-31T09:20:41.450033Z","iopub.status.idle":"2022-07-31T09:20:42.767291Z","shell.execute_reply.started":"2022-07-31T09:20:41.449982Z","shell.execute_reply":"2022-07-31T09:20:42.766197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probs,_,idxs = learn.get_preds(dl=tst_dl, with_decoded=True)\nidxs","metadata":{"execution":{"iopub.status.busy":"2022-07-31T09:20:47.460067Z","iopub.execute_input":"2022-07-31T09:20:47.460615Z","iopub.status.idle":"2022-07-31T09:21:47.859120Z","shell.execute_reply.started":"2022-07-31T09:20:47.460565Z","shell.execute_reply":"2022-07-31T09:21:47.858046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.vocab","metadata":{"execution":{"iopub.status.busy":"2022-07-31T09:21:52.010234Z","iopub.execute_input":"2022-07-31T09:21:52.010769Z","iopub.status.idle":"2022-07-31T09:21:52.088007Z","shell.execute_reply.started":"2022-07-31T09:21:52.010724Z","shell.execute_reply":"2022-07-31T09:21:52.086726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mapping = dict(enumerate(dls.vocab))\nresults = pd.Series(idxs.numpy(), name=\"idxs\").map(mapping)\nresults","metadata":{"execution":{"iopub.status.busy":"2022-07-31T09:22:11.760835Z","iopub.execute_input":"2022-07-31T09:22:11.761429Z","iopub.status.idle":"2022-07-31T09:22:11.842667Z","shell.execute_reply.started":"2022-07-31T09:22:11.761383Z","shell.execute_reply":"2022-07-31T09:22:11.841557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss['label'] = results\nss.to_csv('submission.csv', index=False)\n!head submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-07-31T09:22:13.600252Z","iopub.execute_input":"2022-07-31T09:22:13.600749Z","iopub.status.idle":"2022-07-31T09:22:14.590284Z","shell.execute_reply.started":"2022-07-31T09:22:13.600707Z","shell.execute_reply":"2022-07-31T09:22:14.589094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}