{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":66696,"databundleVersionId":7411250,"sourceType":"competition"}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"copying https://www.kaggle.com/code/jhoward/first-steps-road-to-the-top-part-1","metadata":{}},{"cell_type":"code","source":"try: import fastkaggle\nexcept ModuleNotFoundError:\n    !pip install -Uq fastkaggle\n\nfrom fastkaggle import *\n\ncomp = 'dhu-ailab-image-classification'\n\npath = setup_comp(comp, install='fastai \"timm>=0.6.2.dev0\"')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-08T19:09:35.360955Z","iopub.execute_input":"2024-03-08T19:09:35.361868Z","iopub.status.idle":"2024-03-08T19:09:47.292277Z","shell.execute_reply.started":"2024-03-08T19:09:35.361827Z","shell.execute_reply":"2024-03-08T19:09:47.291293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:09:47.294203Z","iopub.execute_input":"2024-03-08T19:09:47.294521Z","iopub.status.idle":"2024-03-08T19:09:47.302217Z","shell.execute_reply.started":"2024-03-08T19:09:47.294496Z","shell.execute_reply":"2024-03-08T19:09:47.301044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\n#set_seed(42)\n\npath.ls()","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:09:47.303774Z","iopub.execute_input":"2024-03-08T19:09:47.304125Z","iopub.status.idle":"2024-03-08T19:09:47.318761Z","shell.execute_reply.started":"2024-03-08T19:09:47.304095Z","shell.execute_reply":"2024-03-08T19:09:47.317837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_path = path/'train'\nfiles = get_image_files(trn_path)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:16:43.670065Z","iopub.execute_input":"2024-03-08T19:16:43.670689Z","iopub.status.idle":"2024-03-08T19:16:45.114730Z","shell.execute_reply.started":"2024-03-08T19:16:43.670658Z","shell.execute_reply":"2024-03-08T19:16:45.113942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = PILImage.create(files[0])\nprint(img.size)\nimg.to_thumb(128)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:17:15.596297Z","iopub.execute_input":"2024-03-08T19:17:15.596649Z","iopub.status.idle":"2024-03-08T19:17:15.627221Z","shell.execute_reply.started":"2024-03-08T19:17:15.596621Z","shell.execute_reply":"2024-03-08T19:17:15.626299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastcore.parallel import *\n\ndef f(o): return PILImage.create(o).size\nsizes = parallel(f, files, n_workers=8)\npd.Series(sizes).value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:17:33.236647Z","iopub.execute_input":"2024-03-08T19:17:33.236982Z","iopub.status.idle":"2024-03-08T19:17:57.374452Z","shell.execute_reply.started":"2024-03-08T19:17:33.236955Z","shell.execute_reply":"2024-03-08T19:17:57.373445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_folder(trn_path, valid_pct=0.2,\n    item_tfms=Resize(115, method='squish'),\n    batch_tfms=aug_transforms(size=115, min_scale=0.75))\n\ndls.show_batch(max_n=6)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:19:25.344257Z","iopub.execute_input":"2024-03-08T19:19:25.345095Z","iopub.status.idle":"2024-03-08T19:19:28.601052Z","shell.execute_reply.started":"2024-03-08T19:19:25.345053Z","shell.execute_reply":"2024-03-08T19:19:28.599933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, 'resnet26d', metrics=error_rate, path='.').to_fp16()","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:19:58.672626Z","iopub.execute_input":"2024-03-08T19:19:58.672979Z","iopub.status.idle":"2024-03-08T19:20:00.794878Z","shell.execute_reply.started":"2024-03-08T19:19:58.672951Z","shell.execute_reply":"2024-03-08T19:20:00.794055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find(suggest_funcs=(valley, slide))","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:20:06.108563Z","iopub.execute_input":"2024-03-08T19:20:06.109307Z","iopub.status.idle":"2024-03-08T19:20:54.495629Z","shell.execute_reply.started":"2024-03-08T19:20:06.109265Z","shell.execute_reply":"2024-03-08T19:20:54.494479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(3, 0.02)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:21:45.845152Z","iopub.execute_input":"2024-03-08T19:21:45.845519Z","iopub.status.idle":"2024-03-08T19:23:52.833957Z","shell.execute_reply.started":"2024-03-08T19:21:45.845489Z","shell.execute_reply":"2024-03-08T19:23:52.833049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss = pd.read_csv(path/'sample_submission.csv')\nss","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:28:42.440466Z","iopub.execute_input":"2024-03-08T19:28:42.441377Z","iopub.status.idle":"2024-03-08T19:28:42.462923Z","shell.execute_reply.started":"2024-03-08T19:28:42.441338Z","shell.execute_reply":"2024-03-08T19:28:42.461951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst_files = get_image_files(path/'test').sorted()\ntst_dl = dls.test_dl(tst_files)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:24:40.244580Z","iopub.execute_input":"2024-03-08T19:24:40.245259Z","iopub.status.idle":"2024-03-08T19:24:40.311147Z","shell.execute_reply.started":"2024-03-08T19:24:40.245229Z","shell.execute_reply":"2024-03-08T19:24:40.310375Z"},"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":"2024-03-08T19:25:06.379892Z","iopub.execute_input":"2024-03-08T19:25:06.380257Z","iopub.status.idle":"2024-03-08T19:25:08.494744Z","shell.execute_reply.started":"2024-03-08T19:25:06.380231Z","shell.execute_reply":"2024-03-08T19:25:08.493795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.vocab","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:25:32.212378Z","iopub.execute_input":"2024-03-08T19:25:32.213278Z","iopub.status.idle":"2024-03-08T19:25:32.219416Z","shell.execute_reply.started":"2024-03-08T19:25:32.213242Z","shell.execute_reply":"2024-03-08T19:25:32.218573Z"},"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":"2024-03-08T19:25:51.360038Z","iopub.execute_input":"2024-03-08T19:25:51.361192Z","iopub.status.idle":"2024-03-08T19:25:51.376741Z","shell.execute_reply.started":"2024-03-08T19:25:51.361148Z","shell.execute_reply":"2024-03-08T19:25:51.375379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss['species'] = results\nss.to_csv('submission.csv', index=False)\n!head submission.csv","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:29:49.296433Z","iopub.execute_input":"2024-03-08T19:29:49.296834Z","iopub.status.idle":"2024-03-08T19:29:50.278332Z","shell.execute_reply.started":"2024-03-08T19:29:49.296803Z","shell.execute_reply":"2024-03-08T19:29:50.277141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(ss)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:29:00.024515Z","iopub.execute_input":"2024-03-08T19:29:00.025428Z","iopub.status.idle":"2024-03-08T19:29:00.033850Z","shell.execute_reply.started":"2024-03-08T19:29:00.025386Z","shell.execute_reply":"2024-03-08T19:29:00.032752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = setup_comp(comp, install='\"fastcore>=1.4.5\" \"fastai>=2.7.1\" \"timm>=0.6.2.dev0\"')","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:32:45.380755Z","iopub.execute_input":"2024-03-08T19:32:45.381198Z","iopub.status.idle":"2024-03-08T19:32:57.143668Z","shell.execute_reply.started":"2024-03-08T19:32:45.381163Z","shell.execute_reply":"2024-03-08T19:32:57.142582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(arch, item, batch, epochs=5):\n    dls = ImageDataLoaders.from_folder(trn_path, valid_pct=0.2, item_tfms=item, batch_tfms=batch)\n    learn = vision_learner(dls, arch, metrics=error_rate).to_fp16()\n    learn.fine_tune(epochs, 0.02)\n    return learn","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:33:37.907574Z","iopub.execute_input":"2024-03-08T19:33:37.907954Z","iopub.status.idle":"2024-03-08T19:33:37.913511Z","shell.execute_reply.started":"2024-03-08T19:33:37.907923Z","shell.execute_reply":"2024-03-08T19:33:37.912506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = train('resnet26d', item=Resize(115),\n              batch=aug_transforms(size=100, min_scale=0.75))","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:34:20.360875Z","iopub.execute_input":"2024-03-08T19:34:20.361758Z","iopub.status.idle":"2024-03-08T19:37:49.030872Z","shell.execute_reply.started":"2024-03-08T19:34:20.361717Z","shell.execute_reply":"2024-03-08T19:37:49.029721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arch = 'convnext_small_in22k'","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:37:49.033269Z","iopub.execute_input":"2024-03-08T19:37:49.033650Z","iopub.status.idle":"2024-03-08T19:37:49.038615Z","shell.execute_reply.started":"2024-03-08T19:37:49.033614Z","shell.execute_reply":"2024-03-08T19:37:49.037680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = train(arch, item=Resize(115, method='squish'),\n              batch=aug_transforms(size=100, min_scale=0.75))","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:37:49.039929Z","iopub.execute_input":"2024-03-08T19:37:49.040530Z","iopub.status.idle":"2024-03-08T19:41:29.558250Z","shell.execute_reply.started":"2024-03-08T19:37:49.040498Z","shell.execute_reply":"2024-03-08T19:41:29.557074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = train(arch, item=Resize(115),\n              batch=aug_transforms(size=100, min_scale=0.75))","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:42:15.252700Z","iopub.execute_input":"2024-03-08T19:42:15.253644Z","iopub.status.idle":"2024-03-08T19:45:57.773602Z","shell.execute_reply.started":"2024-03-08T19:42:15.253603Z","shell.execute_reply":"2024-03-08T19:45:57.772483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid = learn.dls.valid\npreds,targs = learn.get_preds(dl=valid)\nerror_rate(preds, targs)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:45:57.775854Z","iopub.execute_input":"2024-03-08T19:45:57.776146Z","iopub.status.idle":"2024-03-08T19:46:04.811093Z","shell.execute_reply.started":"2024-03-08T19:45:57.776119Z","shell.execute_reply":"2024-03-08T19:46:04.809921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.dls.train.show_batch(max_n=6, unique=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:46:04.812726Z","iopub.execute_input":"2024-03-08T19:46:04.813111Z","iopub.status.idle":"2024-03-08T19:46:06.397775Z","shell.execute_reply.started":"2024-03-08T19:46:04.813074Z","shell.execute_reply":"2024-03-08T19:46:06.396577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tta_preds,_ = learn.tta(dl=valid)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:46:06.400900Z","iopub.execute_input":"2024-03-08T19:46:06.401275Z","iopub.status.idle":"2024-03-08T19:46:42.188760Z","shell.execute_reply.started":"2024-03-08T19:46:06.401241Z","shell.execute_reply":"2024-03-08T19:46:42.187411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"error_rate(tta_preds, targs)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:46:42.190404Z","iopub.execute_input":"2024-03-08T19:46:42.191072Z","iopub.status.idle":"2024-03-08T19:46:42.199023Z","shell.execute_reply.started":"2024-03-08T19:46:42.191034Z","shell.execute_reply":"2024-03-08T19:46:42.198202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_path = path/'train'","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:51:42.048207Z","iopub.execute_input":"2024-03-08T19:51:42.048618Z","iopub.status.idle":"2024-03-08T19:51:42.054869Z","shell.execute_reply.started":"2024-03-08T19:51:42.048588Z","shell.execute_reply":"2024-03-08T19:51:42.053472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arch = 'convnext_base_in22k'","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:54:20.100283Z","iopub.execute_input":"2024-03-08T19:54:20.101224Z","iopub.status.idle":"2024-03-08T19:54:20.108267Z","shell.execute_reply.started":"2024-03-08T19:54:20.101177Z","shell.execute_reply":"2024-03-08T19:54:20.107037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = train(arch, epochs=12,\n              item=Resize(480, method='squish'),\n              batch=aug_transforms(size=256, min_scale=0.75))","metadata":{"execution":{"iopub.status.busy":"2024-03-08T19:54:24.035709Z","iopub.execute_input":"2024-03-08T19:54:24.036479Z","iopub.status.idle":"2024-03-08T20:08:33.075132Z","shell.execute_reply.started":"2024-03-08T19:54:24.036439Z","shell.execute_reply":"2024-03-08T20:08:33.073964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tta_preds,targs = learn.tta(dl=learn.dls.valid)\nerror_rate(tta_preds, targs)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:08:33.078874Z","iopub.execute_input":"2024-03-08T20:08:33.079681Z","iopub.status.idle":"2024-03-08T20:09:22.096256Z","shell.execute_reply.started":"2024-03-08T20:08:33.079647Z","shell.execute_reply":"2024-03-08T20:09:22.095199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst_files = get_image_files(path/'test').sorted()\ntst_dl = learn.dls.test_dl(tst_files)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:09:22.097869Z","iopub.execute_input":"2024-03-08T20:09:22.098594Z","iopub.status.idle":"2024-03-08T20:09:22.110085Z","shell.execute_reply.started":"2024-03-08T20:09:22.098555Z","shell.execute_reply":"2024-03-08T20:09:22.109103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds,_ = learn.tta(dl=tst_dl)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:09:22.112763Z","iopub.execute_input":"2024-03-08T20:09:22.113191Z","iopub.status.idle":"2024-03-08T20:09:37.885451Z","shell.execute_reply.started":"2024-03-08T20:09:22.113158Z","shell.execute_reply":"2024-03-08T20:09:37.884261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idxs = preds.argmax(dim=1)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:09:37.887132Z","iopub.execute_input":"2024-03-08T20:09:37.888027Z","iopub.status.idle":"2024-03-08T20:09:37.892782Z","shell.execute_reply.started":"2024-03-08T20:09:37.887964Z","shell.execute_reply":"2024-03-08T20:09:37.891845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab = np.array(learn.dls.vocab)\nresults = pd.Series(vocab[idxs], name=\"idxs\")","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:09:37.894156Z","iopub.execute_input":"2024-03-08T20:09:37.894430Z","iopub.status.idle":"2024-03-08T20:09:37.914227Z","shell.execute_reply.started":"2024-03-08T20:09:37.894408Z","shell.execute_reply":"2024-03-08T20:09:37.913191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss = pd.read_csv(path/'sample_submission.csv')\nss['species'] = results\nss.to_csv('submission.csv', index=False)\n!head subm.csv","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:09:37.915417Z","iopub.execute_input":"2024-03-08T20:09:37.915672Z","iopub.status.idle":"2024-03-08T20:09:38.964420Z","shell.execute_reply.started":"2024-03-08T20:09:37.915650Z","shell.execute_reply":"2024-03-08T20:09:38.963264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arch = 'convnext_base'\nlearn = train(arch, epochs=12,\n              item=Resize(480, method='squish'),\n              batch=aug_transforms(size=256, min_scale=0.75))","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:09:38.966272Z","iopub.execute_input":"2024-03-08T20:09:38.967265Z","iopub.status.idle":"2024-03-08T20:23:41.765026Z","shell.execute_reply.started":"2024-03-08T20:09:38.967222Z","shell.execute_reply":"2024-03-08T20:23:41.764014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tta_preds,targs = learn.tta(dl=learn.dls.valid)\nerror_rate(tta_preds, targs)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:26:20.284230Z","iopub.execute_input":"2024-03-08T20:26:20.285106Z","iopub.status.idle":"2024-03-08T20:27:15.631490Z","shell.execute_reply.started":"2024-03-08T20:26:20.285062Z","shell.execute_reply":"2024-03-08T20:27:15.630377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tst_files = get_image_files(path/'test').sorted()\ntst_dl = learn.dls.test_dl(tst_files)\npreds,_ = learn.tta(dl=tst_dl)\nidxs = preds.argmax(dim=1)","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:27:20.407335Z","iopub.execute_input":"2024-03-08T20:27:20.408401Z","iopub.status.idle":"2024-03-08T20:27:36.147618Z","shell.execute_reply.started":"2024-03-08T20:27:20.408353Z","shell.execute_reply":"2024-03-08T20:27:36.146464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab = np.array(learn.dls.vocab)\nresults = pd.Series(vocab[idxs], name=\"idxs\")\nss = pd.read_csv(path/'sample_submission.csv')\nss['species'] = results\nss.to_csv('submission.csv', index=False)\n!head subm.csv\n","metadata":{"execution":{"iopub.status.busy":"2024-03-08T20:27:48.888968Z","iopub.execute_input":"2024-03-08T20:27:48.889907Z","iopub.status.idle":"2024-03-08T20:27:49.926137Z","shell.execute_reply.started":"2024-03-08T20:27:48.889861Z","shell.execute_reply":"2024-03-08T20:27:49.924918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}