{"cells":[{"metadata":{"_uuid":"d40180f7d62493d3b2a4fbd78e10202f608e049d"},"cell_type":"markdown","source":"I don't know how to use any advanced fastai techniques, so I just went as simple as possible in this notebook."},{"metadata":{"trusted":true,"_uuid":"2992805e16e326ed87c0647c1108d228ca592905"},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90f1c51577908087a3425112ae0c26f262f5ac6c"},"cell_type":"code","source":"from fastai.vision import *\nfrom fastai.metrics import error_rate","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59a5442fad8da690faff101b9714bf5f272e2a84"},"cell_type":"code","source":"bs = 16","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nfrom pathlib import Path\nprint(os.listdir(\"../input\"))\nwork_p = Path(\"./\")\np = Path(\"../input\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5527f17bf2836b880511343d983f25e40705eb6"},"cell_type":"code","source":"len(os.listdir(p/\"test\"))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"28f251c60a09adef9a9aa46a27c2449d633618fa"},"cell_type":"markdown","source":"This one won't work since we need write permissions. To fix, images should be moved to {work_p}"},{"metadata":{"trusted":true,"_uuid":"b36cea1fff5626f6abccc0505959be9c01b02743"},"cell_type":"code","source":"# folders = [\"train\", \"test\"]\n# for c in classes:\n#     print(c)\n#     verify_images(p/c, delete=False, max_size=500)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"906c47635bb2e2d54d7b6c4fd1337303cde8b262"},"cell_type":"markdown","source":"## Data pipeline"},{"metadata":{"trusted":true,"_uuid":"ee45294e0071999f76210e59ed54621d39fd5eff"},"cell_type":"code","source":"train_df = pd.read_csv(p/\"train.csv\")#.sample(frac=0.3, random_state=2)\nprint(train_df.shape); train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"25fdfd2419aa7efd6670f062c746cec3b134d166"},"cell_type":"code","source":"labels_count = train_df.Id.value_counts()\ntrain_names = train_df.index.values","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c500388e50c59e2544e247be009cddcafe990fbb"},"cell_type":"markdown","source":"I have no idea how to properly create a validation set, so I just duplicate every single occurence"},{"metadata":{"trusted":true,"_uuid":"194245a2a1b586db2c887b8dbc210bb5c355ac06"},"cell_type":"code","source":"for idx,row in train_df.iterrows():\n    if labels_count[row['Id']] < 2:\n        for i in row*math.ceil((2 - labels_count[row['Id']])/labels_count[row['Id']]):\n            train_df = train_df.append(row,ignore_index=True)\n\nprint(train_df.shape)\n# plt.hist(train_df.Id.value_counts()[1:],bins=100,range=[0,100]);\n# plt.hist(train_df.Id.value_counts()[1:],bins=100,range=[0,100]);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9ed3c3ec9bfdfd5c7765dd31305eedba7b933894"},"cell_type":"markdown","source":"# Training"},{"metadata":{"trusted":true,"_uuid":"b247f0e1755c180ebc160813410449599b8f4f89"},"cell_type":"code","source":"name = f'res50-full-train'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94d7ed48bf0d2d9336123c0a04f1c83dbf3e607a"},"cell_type":"code","source":"np.random.seed(2)\ndata = (ImageDataBunch.from_df(work_p, train_df, folder=p/\"train\", test=p/\"test\", valid_pct=0.20, ds_tfms=get_transforms(), size=224, bs=bs)\n        .normalize(imagenet_stats))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"403bc41b30080cd1a5c6c844a7247dfbd9affa53"},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(7,6))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4b02d484ff2cda6ac1ac6ddf7ddbec9b458fc8e4"},"cell_type":"markdown","source":"Looks like default transformation is not alright. Let's give it a go anyway."},{"metadata":{"trusted":true,"_uuid":"5a41d52988389a02434f3f449d61fe37fab0a112"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet50, metrics=error_rate)\nlearn.fit_one_cycle(2, max_lr=slice(6.31e-07, 3e-07))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5fe51961eb6944d04a9a255bd766681bc0dda06d"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet50, metrics=error_rate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"42d94d77dd2b820dd087132dbc4454d746ad7355"},"cell_type":"code","source":"learn.fit_one_cycle(2, max_lr=3e-03)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"389b2a4130d14ab64647b930983045be6bd12284"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet50, metrics=error_rate)\nlearn.fit_one_cycle(2, max_lr=0.5e-02)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db00a87a63b8ec1b8a60c7d284cf72e49b01b3c2"},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66751c2571ce4d26bd80ac201302d8f9e522ee41"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet50, metrics=error_rate)\nlearn.fit_one_cycle(2, max_lr=slice(5e-02, 2.5e-02))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85de1447dddc81d09b8b917c6c1bcefaa2571599"},"cell_type":"code","source":"# learn.save(\"stage-1\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8db3c52e63f8e696e433b62911ef7e8b948410a8"},"cell_type":"code","source":"# learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b80d3609d146ddbdd6652d698829f50b213d4e14"},"cell_type":"code","source":"# learn.fit_one_cycle(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1095b21384daeb53ad388e0fc17876458fe29d18"},"cell_type":"code","source":"# learn.load('stage-1');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b956149b545f2281ff5f5d11cbab0edfc2a4000"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"993b405a8164041fa877e7c1da6dc922ff02e527"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8132d89a0955f3c2d3f807545a13ead4bd1ef202"},"cell_type":"markdown","source":"Bigger size doesn't fit the kernel"},{"metadata":{"trusted":true,"_uuid":"1289fe26f112936b1454ac087545f137c8cd2e06"},"cell_type":"code","source":"# data_bigger = ImageDataBunch.from_df(work_p, train_df, folder=p/\"train\", valid_pct=0.20, ds_tfms=get_transforms(), size=448, bs=bs).normalize(imagenet_stats)\n# learn_bigger = create_cnn(data_bigger, models.resnet34, metrics=error_rate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc0e7728feb76b368f4090154c958867407e6ae0"},"cell_type":"code","source":"# learn_bigger.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b679ede24662438fb88f48384a81d12d0436a2a"},"cell_type":"code","source":"# data_bigger.show_batch(rows=3, figsize=(7,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5c5b318c2e0129387831e91cbab3a2da8b06716"},"cell_type":"markdown","source":"# Interpretation"},{"metadata":{"trusted":true,"_uuid":"c39d5526e562b6f92150a08329cb91e12701c24d"},"cell_type":"code","source":"# interp = ClassificationInterpretation.from_learner(learn)\n# losses,idxs = interp.top_losses()\n# len(data.valid_ds)==len(losses)==len(idxs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"745589117658b6aec940dabe6365ce77dc245a53"},"cell_type":"code","source":"# interp.plot_top_losses(9, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"93240d90864563223feb88871068f1b1cf475a9e"},"cell_type":"markdown","source":"# Unfreezing, fine-tuning, and learning rates"},{"metadata":{"trusted":true,"_uuid":"a030d77c4e9ae94e999aef526ca4604c2ceb2967"},"cell_type":"code","source":"# learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd4297b4cdde83bf012a1daf66662b26142461af"},"cell_type":"code","source":"# learn.fit_one_cycle(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99d3ba452cc48cf9798df99ca252025d408f434c"},"cell_type":"code","source":"# learn.load('stage-1');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe27e7c682b3dffe45b9953be0d9036640ec9cc5"},"cell_type":"code","source":"# learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"05ab51917e48f79e2277f89d58292fee71795ec2"},"cell_type":"code","source":"# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33dec452ea7df1727954175e80bd8b5382aea579"},"cell_type":"code","source":"# learn.unfreeze()\n# learn.fit_one_cycle(2, max_lr=slice(1e-5,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e1c5385816d7c5553364b743d0d5880a1c457cfa"},"cell_type":"markdown","source":"# Submission"},{"metadata":{"trusted":true,"_uuid":"a73cf44e129718236901754f2930274f14b187ff"},"cell_type":"code","source":"preds, _ = learn.get_preds(DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9f2a78d3a5354e46a2868677d5129596b7d6f56d"},"cell_type":"code","source":"preds = torch.cat((preds, torch.ones_like(preds[:, :1])), 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eb05a59fe3a8b63d15224b5e1b7def1c14557d93"},"cell_type":"code","source":"def top_5_pred_labels(preds, classes):\n    top_5 = np.argsort(preds.numpy())[:, ::-1][:, :5]\n    labels = []\n    for i in range(top_5.shape[0]):\n        labels.append(' '.join([classes[idx] for idx in top_5[i]]))\n    return labels\n\ndef create_submission(preds, data, name, classes=None):\n    if not classes: classes = data.classes\n    sub = pd.DataFrame({'Image': [path.name for path in data.test_ds.x.items]})\n    sub['Id'] = top_5_pred_labels(preds, classes)\n    sub.to_csv(f'{name}.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39b5908571333158a0af723bda21129f8610993a"},"cell_type":"code","source":"create_submission(preds, learn.data, name, learn.data.classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2a03c5ebaaea35f63e8855fb2b8371052f94a14d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}