{"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":"from fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:36:08.812939Z","iopub.execute_input":"2021-06-06T15:36:08.813460Z","iopub.status.idle":"2021-06-06T15:36:09.699405Z","shell.execute_reply.started":"2021-06-06T15:36:08.813374Z","shell.execute_reply":"2021-06-06T15:36:09.698437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nlabels","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:36:09.701329Z","iopub.execute_input":"2021-06-06T15:36:09.701656Z","iopub.status.idle":"2021-06-06T15:36:09.733876Z","shell.execute_reply.started":"2021-06-06T15:36:09.701630Z","shell.execute_reply":"2021-06-06T15:36:09.732959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[\"breed\"].value_counts().plot(kind=\"hist\");","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:36:09.735589Z","iopub.execute_input":"2021-06-06T15:36:09.735962Z","iopub.status.idle":"2021-06-06T15:36:09.886403Z","shell.execute_reply.started":"2021-06-06T15:36:09.735925Z","shell.execute_reply":"2021-06-06T15:36:09.885454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"markdown","source":"split data set into training and validation set in a stratified manner","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedShuffleSplit\n\nsplit = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)\ntrain_ids, valid_ids = next(split.split(labels, labels[\"breed\"]))\nlabels[\"is_valid\"] = [i in valid_ids for i in range(len(labels))]\n\nlabels[\"id\"] = labels[\"id\"].apply(lambda x: x + \".jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:36:09.887907Z","iopub.execute_input":"2021-06-06T15:36:09.888266Z","iopub.status.idle":"2021-06-06T15:36:09.976953Z","shell.execute_reply.started":"2021-06-06T15:36:09.888225Z","shell.execute_reply":"2021-06-06T15:36:09.976175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"preparing dataloaders","metadata":{}},{"cell_type":"code","source":"path = \"../input/dog-breed-identification/train\"\n\ndls = ImageDataLoaders.from_df(labels, path,\n                               item_tfms=Resize(460, method=\"squeeze\"),\n                               batch_tfms=[*aug_transforms(size=300, mult=1.5),\n                                           Contrast(),\n                                           Saturation(),\n                                           Normalize.from_stats(*imagenet_stats)],\n                               bs=64, valid_col=\"is_valid\")","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:36:09.978181Z","iopub.execute_input":"2021-06-06T15:36:09.978550Z","iopub.status.idle":"2021-06-06T15:36:13.502384Z","shell.execute_reply.started":"2021-06-06T15:36:09.978513Z","shell.execute_reply":"2021-06-06T15:36:13.501474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:36:13.506583Z","iopub.execute_input":"2021-06-06T15:36:13.508663Z","iopub.status.idle":"2021-06-06T15:36:14.972731Z","shell.execute_reply.started":"2021-06-06T15:36:13.508621Z","shell.execute_reply":"2021-06-06T15:36:14.971893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training our model","metadata":{}},{"cell_type":"markdown","source":"compute weights for each class to deal with classes imabalance","metadata":{}},{"cell_type":"code","source":"label_count = labels[\"breed\"].value_counts()\nn_samples = labels.shape[0]\nn_classes = len(dls.vocab)\nweights = [n_samples / (n_classes * label_count[label]) for label in dls.vocab]\nweights = tensor(weights, device=\"cuda\")","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:36:14.973894Z","iopub.execute_input":"2021-06-06T15:36:14.974212Z","iopub.status.idle":"2021-06-06T15:36:14.985675Z","shell.execute_reply.started":"2021-06-06T15:36:14.974180Z","shell.execute_reply":"2021-06-06T15:36:14.984805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train the model","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet152, loss_func=nn.CrossEntropyLoss(),\n                    metrics=[accuracy, F.cross_entropy], path=\".\").to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:36:14.987972Z","iopub.execute_input":"2021-06-06T15:36:14.988595Z","iopub.status.idle":"2021-06-06T15:36:16.836812Z","shell.execute_reply.started":"2021-06-06T15:36:14.988557Z","shell.execute_reply":"2021-06-06T15:36:16.835968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"freeze the convnet part","metadata":{}},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:36:16.838161Z","iopub.execute_input":"2021-06-06T15:36:16.838522Z","iopub.status.idle":"2021-06-06T15:38:04.787027Z","shell.execute_reply.started":"2021-06-06T15:36:16.838481Z","shell.execute_reply":"2021-06-06T15:38:04.786228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3, 1e-3)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:39:00.834534Z","iopub.execute_input":"2021-06-06T15:39:00.834875Z","iopub.status.idle":"2021-06-06T15:47:10.311802Z","shell.execute_reply.started":"2021-06-06T15:39:00.834844Z","shell.execute_reply":"2021-06-06T15:47:10.310889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save(\"freezed\")\n#learn = learn.load(\"freezed\").to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:47:14.328645Z","iopub.execute_input":"2021-06-06T15:47:14.328990Z","iopub.status.idle":"2021-06-06T15:47:15.060948Z","shell.execute_reply.started":"2021-06-06T15:47:14.328955Z","shell.execute_reply":"2021-06-06T15:47:15.060008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:47:16.244405Z","iopub.execute_input":"2021-06-06T15:47:16.244726Z","iopub.status.idle":"2021-06-06T15:48:58.101433Z","shell.execute_reply.started":"2021-06-06T15:47:16.244697Z","shell.execute_reply":"2021-06-06T15:48:58.100481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5, slice(1e-6, 1e-4))","metadata":{"execution":{"iopub.status.busy":"2021-06-06T15:49:37.024916Z","iopub.execute_input":"2021-06-06T15:49:37.025279Z","iopub.status.idle":"2021-06-06T16:07:47.920629Z","shell.execute_reply.started":"2021-06-06T15:49:37.025244Z","shell.execute_reply":"2021-06-06T16:07:47.919606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get predictions of test data","metadata":{}},{"cell_type":"code","source":"test_files = get_image_files(\"../input/dog-breed-identification/test\")\ntest_dl = dls.test_dl(test_files, bs=8)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T16:07:57.854096Z","iopub.execute_input":"2021-06-06T16:07:57.854474Z","iopub.status.idle":"2021-06-06T16:08:09.743149Z","shell.execute_reply.started":"2021-06-06T16:07:57.854434Z","shell.execute_reply":"2021-06-06T16:08:09.742257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds, targs = learn.tta(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T16:08:09.744569Z","iopub.execute_input":"2021-06-06T16:08:09.744888Z","iopub.status.idle":"2021-06-06T16:19:23.175280Z","shell.execute_reply.started":"2021-06-06T16:08:09.744853Z","shell.execute_reply":"2021-06-06T16:19:23.174281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = torch.softmax(preds, dim=1)\nsub = pd.DataFrame({\"id\":test_files.map(lambda x:x.stem)})\nsub[list(dls.vocab)] = preds\nsub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T16:20:20.593810Z","iopub.execute_input":"2021-06-06T16:20:20.594139Z","iopub.status.idle":"2021-06-06T16:20:24.103994Z","shell.execute_reply.started":"2021-06-06T16:20:20.594108Z","shell.execute_reply":"2021-06-06T16:20:24.103135Z"},"trusted":true},"execution_count":null,"outputs":[]}]}