{"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 *\n\nlabels = pd.read_csv(\"../input/dog-breed-identification/labels.csv\")\nlabels","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-14T10:17:25.20844Z","iopub.execute_input":"2021-06-14T10:17:25.208792Z","iopub.status.idle":"2021-06-14T10:17:27.730608Z","shell.execute_reply.started":"2021-06-14T10:17:25.208759Z","shell.execute_reply":"2021-06-14T10:17:27.729765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\")\n","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:17:27.732321Z","iopub.execute_input":"2021-06-14T10:17:27.73292Z","iopub.status.idle":"2021-06-14T10:17:27.87962Z","shell.execute_reply.started":"2021-06-14T10:17:27.732878Z","shell.execute_reply":"2021-06-14T10:17:27.87869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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),\n                                           Normalize.from_stats(*imagenet_stats)],\n                               bs=32, valid_col=\"is_valid\")\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:17:27.881457Z","iopub.execute_input":"2021-06-14T10:17:27.882044Z","iopub.status.idle":"2021-06-14T10:17:35.659237Z","shell.execute_reply.started":"2021-06-14T10:17:27.881992Z","shell.execute_reply":"2021-06-14T10:17:35.658311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import inception_v3\n\ninception = inception_v3(pretrained=True, aux_logits=False)\ninception = nn.Sequential(*list(inception.children())[:-2],\n                          nn.Flatten()).eval()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:17:35.660501Z","iopub.execute_input":"2021-06-14T10:17:35.660858Z","iopub.status.idle":"2021-06-14T10:17:36.868533Z","shell.execute_reply.started":"2021-06-14T10:17:35.660822Z","shell.execute_reply":"2021-06-14T10:17:36.867623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet = nn.Sequential(*list(resnet50(pretrained=True).children())[:-1], \n                       nn.Flatten()).eval()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:17:36.869819Z","iopub.execute_input":"2021-06-14T10:17:36.870336Z","iopub.status.idle":"2021-06-14T10:17:38.358892Z","shell.execute_reply.started":"2021-06-14T10:17:36.870296Z","shell.execute_reply":"2021-06-14T10:17:38.357952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass NeuralNet(Module):\n    def __init__(self, extractors, hidden_size, vocab_size, device):\n        \n        self.extractors = extractors\n        for conv in self.extractors:\n            conv.to(device)\n        \n        self.classifier = nn.Sequential(\n            nn.Dropout(0.25),\n            nn.Linear(hidden_size, 512),\n            nn.ReLU(),\n            nn.BatchNorm1d(512),\n            nn.Dropout(0.5),\n            nn.Linear(512, vocab_size)\n        )\n        \n    def forward(self, x):\n        \n        features = torch.cat([conv(x) for conv in self.extractors], dim=1)\n        \n        return self.classifier(features)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:17:38.360259Z","iopub.execute_input":"2021-06-14T10:17:38.360754Z","iopub.status.idle":"2021-06-14T10:17:38.367776Z","shell.execute_reply.started":"2021-06-14T10:17:38.360712Z","shell.execute_reply":"2021-06-14T10:17:38.366723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extractors = [inception, resnet]\nhidden_size = 2048 + 2048\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nmodel = NeuralNet(extractors, hidden_size, len(dls.vocab), device)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:17:38.369505Z","iopub.execute_input":"2021-06-14T10:17:38.369876Z","iopub.status.idle":"2021-06-14T10:17:38.479694Z","shell.execute_reply.started":"2021-06-14T10:17:38.369839Z","shell.execute_reply":"2021-06-14T10:17:38.478819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = Learner(dls, model, metrics=accuracy, path=\".\").to_fp16()\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:17:38.481659Z","iopub.execute_input":"2021-06-14T10:17:38.481901Z","iopub.status.idle":"2021-06-14T10:18:43.612947Z","shell.execute_reply.started":"2021-06-14T10:17:38.481876Z","shell.execute_reply":"2021-06-14T10:18:43.612102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(5, 1e-3, wd=0.3)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:18:43.61467Z","iopub.execute_input":"2021-06-14T10:18:43.615105Z","iopub.status.idle":"2021-06-14T10:33:50.356211Z","shell.execute_reply.started":"2021-06-14T10:18:43.615044Z","shell.execute_reply":"2021-06-14T10:33:50.355233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:33:50.357838Z","iopub.execute_input":"2021-06-14T10:33:50.358241Z","iopub.status.idle":"2021-06-14T10:33:50.368196Z","shell.execute_reply.started":"2021-06-14T10:33:50.358196Z","shell.execute_reply":"2021-06-14T10:33:50.367253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = get_image_files(\"../input/dog-breed-identification/test\")\ntest_dl = dls.test_dl(test_files, bs=64)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:33:50.369632Z","iopub.execute_input":"2021-06-14T10:33:50.370172Z","iopub.status.idle":"2021-06-14T10:33:56.472279Z","shell.execute_reply.started":"2021-06-14T10:33:50.370129Z","shell.execute_reply":"2021-06-14T10:33:56.471387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds, targs = learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T10:33:56.473761Z","iopub.execute_input":"2021-06-14T10:33:56.474183Z","iopub.status.idle":"2021-06-14T10:35:45.861781Z","shell.execute_reply.started":"2021-06-14T10:33:56.474142Z","shell.execute_reply":"2021-06-14T10:35:45.860881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = 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-14T10:35:45.863462Z","iopub.execute_input":"2021-06-14T10:35:45.863853Z","iopub.status.idle":"2021-06-14T10:35:49.488833Z","shell.execute_reply.started":"2021-06-14T10:35:45.863812Z","shell.execute_reply":"2021-06-14T10:35:49.487899Z"},"trusted":true},"execution_count":null,"outputs":[]}]}