{"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":"Result 1: score 0.08579 using densenet121 and TTA32. TTA32 is very long - 1 hour, training is almost the same time. I don't like the curve shape of training loss - too zigzagy, so trying another model.\n\nResult 2: Score: 0.17189 using resnet50 training 8 epochs (less 2 epochs than above) and TTA4, also removed ShiftScaleRotate, so I don't know the exact reason for the lower score.\n\nResult 3: Score 0.04953 for softmax() and 0.05880 for just dog clip. Should use softmax(). For this try I am back to densent121, reduced training set to 10% from initial 20%, returned back ShiftScaleRotate() augmentation, trained simple learn.fine_tune(12), used TTA32.","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *\nimport pandas as pd\n%matplotlib inline\nset_seed(3865)","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:18:33.03191Z","iopub.execute_input":"2022-06-16T12:18:33.032705Z","iopub.status.idle":"2022-06-16T12:18:33.042668Z","shell.execute_reply.started":"2022-06-16T12:18:33.032653Z","shell.execute_reply":"2022-06-16T12:18:33.041519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('.')\nwith zipfile.ZipFile('../input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as zip_ref:\n    zip_ref.extractall('.')","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:18:33.048732Z","iopub.execute_input":"2022-06-16T12:18:33.0491Z","iopub.status.idle":"2022-06-16T12:18:53.784265Z","shell.execute_reply.started":"2022-06-16T12:18:33.049069Z","shell.execute_reply":"2022-06-16T12:18:53.783539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n# some bad images should be deleted from the train set\nos.remove('./train/dog.10797.jpg')\nos.remove('./train/dog.10747.jpg')\nos.remove('./train/dog.10237.jpg')\nos.remove('./train/dog.9517.jpg')\nos.remove('./train/dog.8736.jpg')\nos.remove('./train/dog.5604.jpg')\nos.remove('./train/dog.1043.jpg')\nos.remove('./train/cat.4338.jpg')\nos.remove('./train/dog.10161.jpg')\nos.remove('./train/dog.10190.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:18:53.786114Z","iopub.execute_input":"2022-06-16T12:18:53.786528Z","iopub.status.idle":"2022-06-16T12:18:53.799283Z","shell.execute_reply.started":"2022-06-16T12:18:53.786486Z","shell.execute_reply":"2022-06-16T12:18:53.797809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tpath = \"./train\"\nftrain = get_image_files(tpath)\nprint('Train set size:', len(ftrain))","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:18:53.800697Z","iopub.execute_input":"2022-06-16T12:18:53.80117Z","iopub.status.idle":"2022-06-16T12:18:54.337438Z","shell.execute_reply.started":"2022-06-16T12:18:53.801122Z","shell.execute_reply":"2022-06-16T12:18:54.336448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for debug shorter version\n# ftrain = ftrain[:100]","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:18:54.339902Z","iopub.execute_input":"2022-06-16T12:18:54.346341Z","iopub.status.idle":"2022-06-16T12:18:54.368226Z","shell.execute_reply.started":"2022-06-16T12:18:54.346277Z","shell.execute_reply":"2022-06-16T12:18:54.366861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import cv2\n# test an image\n#timg = cv2.imread('./train/dog.10190.jpg')\n#plt.imshow(timg)","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:18:54.37493Z","iopub.execute_input":"2022-06-16T12:18:54.380456Z","iopub.status.idle":"2022-06-16T12:18:54.389745Z","shell.execute_reply.started":"2022-06-16T12:18:54.380389Z","shell.execute_reply":"2022-06-16T12:18:54.388694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as Alb\nclass AlbTransform(Transform):\n    def __init__(self, aug): self.aug = aug\n    def encodes(self, img: PILImage):\n        aug_img = self.aug(image=np.array(img))['image']\n        return PILImage.create(aug_img)\n    \ndef get_augs(): return Alb.Compose([\n    Alb.ShiftScaleRotate(rotate_limit=20, border_mode=0, value=(0,0,0) ),\n    Alb.Transpose(),\n    Alb.Flip(),\n    Alb.RandomRotate90(),\n    Alb.RandomBrightnessContrast(),\n    Alb.HueSaturationValue(\n      hue_shift_limit=5, \n      sat_shift_limit=5, \n      val_shift_limit=5 ),    \n])\n\nitem_tfms = [Resize(224), AlbTransform(get_augs())] \nbatch_tfms = Normalize.from_stats(*imagenet_stats) ","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:18:54.395251Z","iopub.execute_input":"2022-06-16T12:18:54.397925Z","iopub.status.idle":"2022-06-16T12:18:55.902976Z","shell.execute_reply.started":"2022-06-16T12:18:54.39787Z","shell.execute_reply":"2022-06-16T12:18:55.901978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_name_re( path=tpath, \n    fnames=ftrain, pat=r'(.+)\\.\\d+.jpg$', valid_pct=0.1, \n    item_tfms=item_tfms, batch_tfms=batch_tfms, bs=64, shuffle=True )","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:18:55.904473Z","iopub.execute_input":"2022-06-16T12:18:55.904886Z","iopub.status.idle":"2022-06-16T12:18:56.002389Z","shell.execute_reply.started":"2022-06-16T12:18:55.904853Z","shell.execute_reply":"2022-06-16T12:18:56.001628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# uncomment to test data loaders\n# dls.train.show_batch(max_n=12)\nprint('train items:', len(dls.train.items), 'validation items:', len(dls.valid.items))\n# dls.valid.show_batch(max_n=12)\n# dls.vocab","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:19:47.463671Z","iopub.execute_input":"2022-06-16T12:19:47.464222Z","iopub.status.idle":"2022-06-16T12:19:48.83793Z","shell.execute_reply.started":"2022-06-16T12:19:47.464163Z","shell.execute_reply":"2022-06-16T12:19:48.837036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, densenet121, path='.', \n    loss_func=FocalLoss(),  \n    metrics=[accuracy]  )","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:18:57.303612Z","iopub.execute_input":"2022-06-16T12:18:57.303967Z","iopub.status.idle":"2022-06-16T12:19:00.408144Z","shell.execute_reply.started":"2022-06-16T12:18:57.303936Z","shell.execute_reply":"2022-06-16T12:19:00.407184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(12, freeze_epochs=1, cbs=[ShowGraphCallback()])\nprint('fine_tune done')","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:20:17.28802Z","iopub.execute_input":"2022-06-16T12:20:17.289085Z","iopub.status.idle":"2022-06-16T12:21:43.360898Z","shell.execute_reply.started":"2022-06-16T12:20:17.289032Z","shell.execute_reply":"2022-06-16T12:21:43.358988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import time\n# startTime = time.time()\n\n# lrs = learn.lr_find(suggest_funcs=(minimum, steep, valley, slide))","metadata":{"execution":{"iopub.status.busy":"2022-06-16T07:17:28.410493Z","iopub.execute_input":"2022-06-16T07:17:28.411087Z","iopub.status.idle":"2022-06-16T07:32:23.468954Z","shell.execute_reply.started":"2022-06-16T07:17:28.411031Z","shell.execute_reply":"2022-06-16T07:32:23.467708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print('lr_find in:', time.time()-startTime, 'secs')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# learn.fit_one_cycle(8, lr_max=lrs.valley, wd=0.2, cbs=[ShowGraphCallback()])\n# print('training done')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare test data\nftest = get_image_files('test')\n# ftest = ftest[:50] # scale down for debug\nprint('Testing', len(ftest), 'items')","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:22:29.246816Z","iopub.execute_input":"2022-06-16T12:22:29.2473Z","iopub.status.idle":"2022-06-16T12:22:29.369608Z","shell.execute_reply.started":"2022-06-16T12:22:29.247245Z","shell.execute_reply":"2022-06-16T12:22:29.368557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# make dataloader for test data\ntst_dl = dls.test_dl(ftest, with_labels=False, shuffle=False)\n# uncomment to see if dataloader is working\ntst_dl.show_batch(max_n=12)","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:22:33.505828Z","iopub.execute_input":"2022-06-16T12:22:33.506261Z","iopub.status.idle":"2022-06-16T12:22:34.644735Z","shell.execute_reply.started":"2022-06-16T12:22:33.506225Z","shell.execute_reply":"2022-06-16T12:22:34.643795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"startTime = time.time()\npreds = learn.tta(dl=tst_dl, n=32, use_max=False)\nprint('TTA in:', time.time()-startTime, 'secs')","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:22:49.140257Z","iopub.execute_input":"2022-06-16T12:22:49.140748Z","iopub.status.idle":"2022-06-16T12:23:20.026537Z","shell.execute_reply.started":"2022-06-16T12:22:49.140711Z","shell.execute_reply":"2022-06-16T12:23:20.025123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for this competition should not submit 0/1, should submit probability of \"dog\"\nsubm_df = pd.DataFrame()\nsubm_df['id'] = [item.stem for item in tst_dl.items]\nsubm_df['label'] = preds[0][:,1].clip(0.005, 0.995)\nsubm_df.to_csv('submission.csv', header=True, index=False)\nsubm_df","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:31:29.336054Z","iopub.execute_input":"2022-06-16T12:31:29.337695Z","iopub.status.idle":"2022-06-16T12:31:29.364336Z","shell.execute_reply.started":"2022-06-16T12:31:29.337649Z","shell.execute_reply":"2022-06-16T12:31:29.363642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm_df['label'] = torch.softmax(preds[0], dim=1)[:, 1]\nsubm_df.to_csv('submission-softmax.csv', header=True, index=False)\nsubm_df","metadata":{"execution":{"iopub.status.busy":"2022-06-16T12:31:33.930134Z","iopub.execute_input":"2022-06-16T12:31:33.931122Z","iopub.status.idle":"2022-06-16T12:31:33.951432Z","shell.execute_reply.started":"2022-06-16T12:31:33.931082Z","shell.execute_reply":"2022-06-16T12:31:33.950472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cleanup data\nfrom shutil import rmtree\nrmtree('./train', ignore_errors=True)\nrmtree('./test', ignore_errors=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Have a nice day!","metadata":{}}]}