{"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 *\nimport torchaudio\nfrom sklearn.model_selection import StratifiedKFold\nimport librosa\nimport kornia\nfrom scipy import stats","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-06T07:49:38.905805Z","iopub.execute_input":"2022-04-06T07:49:38.906506Z","iopub.status.idle":"2022-04-06T07:49:40.349206Z","shell.execute_reply.started":"2022-04-06T07:49:38.906419Z","shell.execute_reply":"2022-04-06T07:49:40.348375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)\nwarnings.filterwarnings(\"ignore\", category=UserWarning)","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:49:40.351316Z","iopub.execute_input":"2022-04-06T07:49:40.351554Z","iopub.status.idle":"2022-04-06T07:49:40.357171Z","shell.execute_reply.started":"2022-04-06T07:49:40.351521Z","shell.execute_reply":"2022-04-06T07:49:40.356479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/kaggle-pog-series-s01e02/train.csv')\ndf_test = pd.read_csv('../input/kaggle-pog-series-s01e02/test.csv')\nsubmission = pd.read_csv('../input/kaggle-pog-series-s01e02/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:49:40.358546Z","iopub.execute_input":"2022-04-06T07:49:40.359321Z","iopub.status.idle":"2022-04-06T07:49:40.404973Z","shell.execute_reply.started":"2022-04-06T07:49:40.359275Z","shell.execute_reply":"2022-04-06T07:49:40.404307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = Path('../input/music-genre-spectrogram-pogchamps/spectograms/train')\ntest_path = Path('../input/music-genre-spectrogram-pogchamps/spectograms/test')","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:49:40.406646Z","iopub.execute_input":"2022-04-06T07:49:40.406906Z","iopub.status.idle":"2022-04-06T07:49:40.410671Z","shell.execute_reply.started":"2022-04-06T07:49:40.406856Z","shell.execute_reply":"2022-04-06T07:49:40.409848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_y(filename):\n    resample_name = filename.stem + '.ogg'\n    return df_train[df_train['filename']==resample_name]['genre'].values[0]","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:49:40.412075Z","iopub.execute_input":"2022-04-06T07:49:40.412565Z","iopub.status.idle":"2022-04-06T07:49:40.422047Z","shell.execute_reply.started":"2022-04-06T07:49:40.412530Z","shell.execute_reply":"2022-04-06T07:49:40.421383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Excluded unusual music thanks to this thread: https://www.kaggle.com/c/kaggle-pog-series-s01e02/discussion/312842\ndef get_items(path): \n    excluded_files = [\"010449.png\" , \n                      \"005589.png\" , \n                      \"004921.png\", \n                      \"019511.png\" , \n                      \"013375.png\" , \n                      \"024247.png\", \n                      \"024156.png\"]\n    items = get_image_files(path)\n    items = [item for item in items if item.name not in excluded_files]\n    \n    ## For fast iteration\n#     items = [item for item in items if get_y(item) in ['Punk', 'Rock']]\n    random.shuffle(items)\n#     items.shuffle()\n    return L(items)","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:49:41.814581Z","iopub.execute_input":"2022-04-06T07:49:41.815141Z","iopub.status.idle":"2022-04-06T07:49:41.820163Z","shell.execute_reply.started":"2022-04-06T07:49:41.815100Z","shell.execute_reply":"2022-04-06T07:49:41.819421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_items = get_items(test_path)","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:49:42.467300Z","iopub.execute_input":"2022-04-06T07:49:42.467788Z","iopub.status.idle":"2022-04-06T07:49:43.439425Z","shell.execute_reply.started":"2022-04-06T07:49:42.467752Z","shell.execute_reply":"2022-04-06T07:49:43.438669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ReflectionCrop(RandomCrop):\n    def encodes(self, x:(Image.Image,TensorBBox,TensorPoint)):\n        return x.crop_pad(self.size, self.tl, orig_sz=self.orig_sz, pad_mode=PadMode.Reflection)","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:49:43.442480Z","iopub.execute_input":"2022-04-06T07:49:43.442680Z","iopub.status.idle":"2022-04-06T07:49:43.447741Z","shell.execute_reply.started":"2022-04-06T07:49:43.442656Z","shell.execute_reply":"2022-04-06T07:49:43.446668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDataBlock(DataBlock):\n    def datasets(self:DataBlock, source, verbose=False, splits=None):\n        self.source = source                     ; pv(f\"Collecting items from {source}\", verbose)\n        items = (self.get_items or noop)(source) ; pv(f\"Found {len(items)} items\", verbose)\n        pv(f\"{len(splits)} datasets of sizes {','.join([str(len(s)) for s in splits])}\", verbose)\n        return Datasets(items, tfms=self._combine_type_tfms(), splits=splits, dl_type=self.dl_type, n_inp=self.n_inp, verbose=verbose)\n    def dataloaders(self, source, path='.', verbose=False, splits=None, **kwargs):\n        dsets = self.datasets(source, verbose=verbose, splits=splits)\n        kwargs = {**self.dls_kwargs, **kwargs, 'verbose': verbose}\n        return dsets.dataloaders(path=path, after_item=self.item_tfms, after_batch=self.batch_tfms, **kwargs)","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:49:43.538969Z","iopub.execute_input":"2022-04-06T07:49:43.539246Z","iopub.status.idle":"2022-04-06T07:49:43.547370Z","shell.execute_reply.started":"2022-04-06T07:49:43.539219Z","shell.execute_reply":"2022-04-06T07:49:43.546696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_MP_to_blurMP(model, layer_type_old):\n    conversion_count = 0\n    for name, module in reversed(model._modules.items()):\n        if len(list(module.children())) > 0:\n            # recurse\n            model._modules[name] = convert_MP_to_blurMP(module, layer_type_old)\n\n        if type(module) == layer_type_old:\n            layer_old = module\n            layer_new = kornia.contrib.MaxBlurPool2d(3, True)\n            model._modules[name] = layer_new\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:49:44.096310Z","iopub.execute_input":"2022-04-06T07:49:44.096900Z","iopub.status.idle":"2022-04-06T07:49:44.102507Z","shell.execute_reply.started":"2022-04-06T07:49:44.096851Z","shell.execute_reply":"2022-04-06T07:49:44.101785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learns = [load_learner(learn_pkl, cpu=False) for learn_pkl in Path('../input/inference-music-genre').ls()]","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:52:36.386086Z","iopub.execute_input":"2022-04-06T07:52:36.386345Z","iopub.status.idle":"2022-04-06T07:52:38.483293Z","shell.execute_reply.started":"2022-04-06T07:52:36.386317Z","shell.execute_reply":"2022-04-06T07:52:38.482442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(learns)","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:52:39.252905Z","iopub.execute_input":"2022-04-06T07:52:39.253397Z","iopub.status.idle":"2022-04-06T07:52:39.259723Z","shell.execute_reply.started":"2022-04-06T07:52:39.253364Z","shell.execute_reply":"2022-04-06T07:52:39.259050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_before_epoch = [event.before_fit, event.before_epoch]\n_after_epoch  = [event.after_epoch, event.after_fit]","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:50:28.034924Z","iopub.execute_input":"2022-04-06T07:50:28.035402Z","iopub.status.idle":"2022-04-06T07:50:28.039438Z","shell.execute_reply.started":"2022-04-06T07:50:28.035368Z","shell.execute_reply":"2022-04-06T07:50:28.038240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@patch\ndef ttacustom(self:Learner, ds_idx=1, dl=None, n=4, item_tfms=None, batch_tfms=None, beta=0.25, use_max=False):\n    \"Return predictions on the `ds_idx` dataset or `dl` using Test Time Augmentation\"\n    if dl is None: dl = self.dls[ds_idx].new(shuffled=False, drop_last=False)\n    if item_tfms is not None or batch_tfms is not None: dl = dl.new(after_item=item_tfms, after_batch=batch_tfms)\n    try:\n        self(_before_epoch)\n        with dl.dataset.set_split_idx(0), self.no_mbar():\n            if hasattr(self,'progress'): self.progress.mbar = master_bar(list(range(n)))\n            aug_preds = []\n            for i in self.progress.mbar if hasattr(self,'progress') else range(n):\n                self.epoch = i #To keep track of progress on mbar since the progress callback will use self.epoch\n                preds = self.get_preds(dl=dl, inner=True)[0][None]\n                preds_idx = preds.squeeze().argmax(1)\n                aug_preds.append(preds_idx)\n#         aug_preds = torch.cat(aug_preds)\n#         aug_preds = aug_preds.max(0)[0] if use_max else aug_preds.mean(0)\n#         self.epoch = n\n#         with dl.dataset.set_split_idx(1): preds,targs = self.get_preds(dl=dl, inner=True)\n    finally: self(event.after_fit)\n\n#     if use_max: return torch.stack([preds, aug_preds], 0).max(0)[0],targs\n#     preds = (aug_preds,preds) if beta is None else torch.lerp(aug_preds, preds, beta)\n    return aug_preds","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:50:28.257214Z","iopub.execute_input":"2022-04-06T07:50:28.257439Z","iopub.status.idle":"2022-04-06T07:50:28.266226Z","shell.execute_reply.started":"2022-04-06T07:50:28.257414Z","shell.execute_reply":"2022-04-06T07:50:28.265236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_preds = []\nfor learn in learns:\n    test_dl = learn.dls.test_dl(test_items)\n    learn.dls.bs = 512\n    aug_preds_1fold = learn.ttacustom(dl=test_dl, n=50, beta=None)\n    aug_preds.extend(aug_preds_1fold)","metadata":{"execution":{"iopub.status.busy":"2022-04-06T07:50:36.425163Z","iopub.execute_input":"2022-04-06T07:50:36.425413Z","iopub.status.idle":"2022-04-06T07:52:23.756245Z","shell.execute_reply.started":"2022-04-06T07:50:36.425383Z","shell.execute_reply":"2022-04-06T07:52:23.754693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_votes = stats.mode(torch.vstack(aug_preds))[0][0]","metadata":{"execution":{"iopub.status.busy":"2022-04-05T17:40:01.136508Z","iopub.execute_input":"2022-04-05T17:40:01.136837Z","iopub.status.idle":"2022-04-05T17:40:01.293028Z","shell.execute_reply.started":"2022-04-05T17:40:01.136804Z","shell.execute_reply":"2022-04-05T17:40:01.292342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.vstack(aug_preds).shape","metadata":{"execution":{"iopub.status.busy":"2022-04-05T17:40:02.074786Z","iopub.execute_input":"2022-04-05T17:40:02.075053Z","iopub.status.idle":"2022-04-05T17:40:02.082873Z","shell.execute_reply.started":"2022-04-05T17:40:02.075021Z","shell.execute_reply":"2022-04-05T17:40:02.082103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_votes.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-05T17:40:02.421339Z","iopub.execute_input":"2022-04-05T17:40:02.422013Z","iopub.status.idle":"2022-04-05T17:40:02.429177Z","shell.execute_reply.started":"2022-04-05T17:40:02.421983Z","shell.execute_reply":"2022-04-05T17:40:02.42642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def genreid_from_genre(genre):\n    return int(genre2id[genre2id['genre'] == genre]['genre_id'].values[0])","metadata":{"execution":{"iopub.status.busy":"2022-04-05T17:40:03.085038Z","iopub.execute_input":"2022-04-05T17:40:03.08559Z","iopub.status.idle":"2022-04-05T17:40:03.090746Z","shell.execute_reply.started":"2022-04-05T17:40:03.08554Z","shell.execute_reply":"2022-04-05T17:40:03.089904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preds_idx = final_preds.argmax(axis=1)\ngenre2id = pd.read_csv('../input/kaggle-pog-series-s01e02/genres.csv')\nsongid_preds = {int(file_path.stem):genreid_from_genre(learns[0].dls.vocab[_id]) for file_path, _id in zip(test_items,final_votes)}\nsubmission['genre_id'] = submission['song_id'].map(songid_preds)\nsubmission['genre_id'].fillna(0, inplace=True)\nsubmission.loc[submission['song_id']==22612, 'genre_id'] = 1\nsubmission.loc[submission['song_id']==24013, 'genre_id'] = 0\n\nsubmission.genre_id = submission.genre_id.astype(int)\nsubmission.to_csv(f\"submission_final_{int(time.time())}.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-05T17:40:04.410865Z","iopub.execute_input":"2022-04-05T17:40:04.411566Z","iopub.status.idle":"2022-04-05T17:40:06.990203Z","shell.execute_reply.started":"2022-04-05T17:40:04.411526Z","shell.execute_reply":"2022-04-05T17:40:06.989477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}