{"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 -Uq fastkaggle\n\nfrom fastkaggle import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-09T14:03:16.845934Z","iopub.execute_input":"2022-08-09T14:03:16.847413Z","iopub.status.idle":"2022-08-09T14:03:33.203308Z","shell.execute_reply.started":"2022-08-09T14:03:16.847268Z","shell.execute_reply":"2022-08-09T14:03:33.201806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"comp = 'yum-or-yuck-butterfly-mimics-2022'\npath = setup_comp(comp, install='fastai \"timm>=0.6.2.dev0\"')\nfrom fastai.vision.all import *\nset_seed(42)\nimport gc\n\ntst_files = get_image_files(\"../input/yum-or-yuck-butterfly-mimics-2022/data/butterfly_mimics/image_holdouts\").sorted()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:33.206399Z","iopub.execute_input":"2022-08-09T14:03:33.208201Z","iopub.status.idle":"2022-08-09T14:03:50.778116Z","shell.execute_reply.started":"2022-08-09T14:03:33.208152Z","shell.execute_reply":"2022-08-09T14:03:50.776531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst_files","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:50.780470Z","iopub.execute_input":"2022-08-09T14:03:50.781618Z","iopub.status.idle":"2022-08-09T14:03:50.796783Z","shell.execute_reply.started":"2022-08-09T14:03:50.781564Z","shell.execute_reply":"2022-08-09T14:03:50.795403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"../input/yum-or-yuck-butterfly-mimics-2022/data/butterfly_mimics/image_holdouts.csv\").head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:50.801838Z","iopub.execute_input":"2022-08-09T14:03:50.802806Z","iopub.status.idle":"2022-08-09T14:03:50.832842Z","shell.execute_reply.started":"2022-08-09T14:03:50.802728Z","shell.execute_reply":"2022-08-09T14:03:50.831337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGES_PATH = \"../input/yum-or-yuck-butterfly-mimics-2022/data/butterfly_mimics/images\"","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:50.835216Z","iopub.execute_input":"2022-08-09T14:03:50.836128Z","iopub.status.idle":"2022-08-09T14:03:50.842232Z","shell.execute_reply.started":"2022-08-09T14:03:50.836084Z","shell.execute_reply":"2022-08-09T14:03:50.840829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/yum-or-yuck-butterfly-mimics-2022/data/butterfly_mimics/images.csv')\ndf = df[[\"image\", \"name\"]].copy()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:50.844589Z","iopub.execute_input":"2022-08-09T14:03:50.845574Z","iopub.status.idle":"2022-08-09T14:03:50.869636Z","shell.execute_reply.started":"2022-08-09T14:03:50.845516Z","shell.execute_reply":"2022-08-09T14:03:50.868434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:50.872393Z","iopub.execute_input":"2022-08-09T14:03:50.875973Z","iopub.status.idle":"2022-08-09T14:03:50.889074Z","shell.execute_reply.started":"2022-08-09T14:03:50.875928Z","shell.execute_reply":"2022-08-09T14:03:50.887743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"image\"] = df[\"image\"].apply(lambda x: f\"{IMAGES_PATH}/{x}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:50.892495Z","iopub.execute_input":"2022-08-09T14:03:50.893018Z","iopub.status.idle":"2022-08-09T14:03:50.903529Z","shell.execute_reply.started":"2022-08-09T14:03:50.892941Z","shell.execute_reply":"2022-08-09T14:03:50.902411Z"},"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_df(df, path = \"\", valid_pct=0.0, item_tfms=item,\n        batch_tfms=aug_transforms(size=size, min_scale=0.75, min_zoom=0.96, max_zoom = 1.4), bs=64//accum)\n    cbs = GradientAccumulation(64) if accum else []\n    learn = vision_learner(dls, arch, metrics=error_rate, cbs=cbs).to_fp16()\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-09T14:03:50.904941Z","iopub.execute_input":"2022-08-09T14:03:50.906561Z","iopub.status.idle":"2022-08-09T14:03:50.918196Z","shell.execute_reply.started":"2022-08-09T14:03:50.906509Z","shell.execute_reply":"2022-08-09T14:03:50.916446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = 640,480","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:50.920264Z","iopub.execute_input":"2022-08-09T14:03:50.921291Z","iopub.status.idle":"2022-08-09T14:03:50.929416Z","shell.execute_reply.started":"2022-08-09T14:03:50.921249Z","shell.execute_reply":"2022-08-09T14:03:50.927840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = {\n    'densenet201': {\n        (Resize(res), 224),\n    },\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    \n}","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:50.935304Z","iopub.execute_input":"2022-08-09T14:03:50.935625Z","iopub.status.idle":"2022-08-09T14:03:50.944781Z","shell.execute_reply.started":"2022-08-09T14:03:50.935597Z","shell.execute_reply":"2022-08-09T14:03:50.943003Z"},"trusted":true},"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, epochs = 10)) #, epochs=1))\n        try:\n            gc.collect()\n        except:\n            print(\"GC not working\")\n        torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T14:03:50.946670Z","iopub.execute_input":"2022-08-09T14:03:50.947474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_pickle('tta_res.pkl', tta_res)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tta_prs = first(zip(*tta_res))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"avg_pr = torch.stack(tta_prs).mean(0)\navg_pr.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss = pd.read_csv(\"../input/yum-or-yuck-butterfly-mimics-2022/data/butterfly_mimics/image_holdouts.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(df, path = \"\", valid_pct=0.1, item_tfms=Resize(480, method='squish'),\n    batch_tfms=aug_transforms(size=224, min_scale=0.75))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idxs = avg_pr.argmax(dim=1)\nvocab = np.array(dls.vocab)\n\nss['name'] = vocab[idxs]\nss.to_csv('subm.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}