{"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":"# install fastkaggle if not available\ntry: import fastkaggle\nexcept ModuleNotFoundError:\n    !pip install -q fastkaggle\n\nfrom fastkaggle import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-02T12:03:12.644515Z","iopub.execute_input":"2022-08-02T12:03:12.644930Z","iopub.status.idle":"2022-08-02T12:03:26.882889Z","shell.execute_reply.started":"2022-08-02T12:03:12.644841Z","shell.execute_reply":"2022-08-02T12:03:26.881622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"comp = 'plant-seedlings-classification'\npath = setup_comp(comp, install='\"fastcore>=1.4.5\" \"fastai>=2.7.1\" \"timm>=0.6.2.dev0\"')\nfrom fastai.vision.all import *\nset_seed(42)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T12:05:19.406379Z","iopub.execute_input":"2022-08-02T12:05:19.406824Z","iopub.status.idle":"2022-08-02T12:05:33.861364Z","shell.execute_reply.started":"2022-08-02T12:05:19.406786Z","shell.execute_reply":"2022-08-02T12:05:33.860247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/plant-seedlings-classification/')","metadata":{"execution":{"iopub.status.busy":"2022-08-02T12:06:06.610492Z","iopub.execute_input":"2022-08-02T12:06:06.610945Z","iopub.status.idle":"2022-08-02T12:06:06.616494Z","shell.execute_reply.started":"2022-08-02T12:06:06.610896Z","shell.execute_reply":"2022-08-02T12:06:06.615434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path.ls()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T12:08:23.321506Z","iopub.execute_input":"2022-08-02T12:08:23.321965Z","iopub.status.idle":"2022-08-02T12:08:23.331861Z","shell.execute_reply.started":"2022-08-02T12:08:23.321926Z","shell.execute_reply":"2022-08-02T12:08:23.330719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(arch, size, item=Resize(480, method='squish'), accum=1, finetune=True, epochs=12):\n    dls = ImageDataLoaders.from_folder(trn_path, valid_pct=0.2, item_tfms=item,\n                                      batch_tfms=aug_transforms(size=size, min_scale=0.75), bs=64//accum)\n    cbs = GradientAccumulation(64) if accum else []\n    learn = vision_learner(dls, arch, metrics=error_rate, cbs=cbs).to_fp16()\n    \n    if finetune:\n        learn.fine_tune(epochs, 0.01)\n        return learn.tta(dl=dls.test_dl(tst_files))\n    else:\n        learn.unfreeze()\n        learn.fit_one_cycle(epochs, 0.01)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T12:07:29.267152Z","iopub.execute_input":"2022-08-02T12:07:29.267624Z","iopub.status.idle":"2022-08-02T12:07:29.280588Z","shell.execute_reply.started":"2022-08-02T12:07:29.267584Z","shell.execute_reply":"2022-08-02T12:07:29.279539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\n\ndef report_gpu():\n    print(torch.cuda.list_gpu_processes())\n    gc.collect()\n    torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T12:07:38.617214Z","iopub.execute_input":"2022-08-02T12:07:38.617676Z","iopub.status.idle":"2022-08-02T12:07:38.628400Z","shell.execute_reply.started":"2022-08-02T12:07:38.617637Z","shell.execute_reply":"2022-08-02T12:07:38.627210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = (640,480)\n\nmodels = {\n    'convnext_large_in22k': {\n        (Resize(res), (320,224)),\n    }, 'vit_large_patch16_224': {\n        (Resize(480, method='squish'), 224),\n        (Resize(res), 224),\n    }, 'swinv2_large_window12_192_22k': {\n        (Resize(480, method='squish'), 192),\n        (Resize(res), 192),\n    }, 'swin_large_patch4_window7_224': {\n        (Resize(res), 224),\n    }\n}","metadata":{"execution":{"iopub.status.busy":"2022-08-02T12:08:09.950041Z","iopub.execute_input":"2022-08-02T12:08:09.950538Z","iopub.status.idle":"2022-08-02T12:08:09.960288Z","shell.execute_reply.started":"2022-08-02T12:08:09.950498Z","shell.execute_reply":"2022-08-02T12:08:09.959270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_path = '../input/plant-seedlings-classification/train'","metadata":{"execution":{"iopub.status.busy":"2022-08-02T12:08:52.656684Z","iopub.execute_input":"2022-08-02T12:08:52.657162Z","iopub.status.idle":"2022-08-02T12:08:52.662649Z","shell.execute_reply.started":"2022-08-02T12:08:52.657119Z","shell.execute_reply":"2022-08-02T12:08:52.661333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst_files = get_image_files('../input/plant-seedlings-classification/test').sorted()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tta_res = []\n\nfor arch, details in models.items():\n    for item, size in details:\n        print('-----', arch)\n        print(size)\n        print(item.name)\n        tta_res.append(train(arch, size, item=item, accum=2))\n        gc.collect()\n        torch.cuda.empty_cache()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_pickle('tta_res.pkl', tta_res)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tta_prs = first(zip(*tta_res))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tta_prs += tta_prs[1:3]\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"avg_pr = torch.stack(tta_prs).mean(0)\navg_pr.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_folder(trn_path, valid_pct=0.2, item_tfms=Resize(480, method='squish'),\n                                  batch_tfms=aug_transforms(size=224, min_scale=0.75))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idxs = avg_pr.argmax(dim=1)\nvocab = np.array(dls.vocab)\nss = pd.read_csv('../input/plant-seedlings-classification/sample_submission.csv')\nss['species'] = vocab[idxs]\nss.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}