{"cells":[{"metadata":{"papermill":{"duration":0.015713,"end_time":"2020-08-19T17:17:49.607967","exception":false,"start_time":"2020-08-19T17:17:49.592254","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Training EfficientNet For bird_song","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## Credits\n\n* https://www.kaggle.com/ttahara/training-birdsong-baseline-resnest50-fast\n\n## Inference Notebook \n\n* https://www.kaggle.com/rsinda/ensemble-resnest50-efficient-net","execution_count":null},{"metadata":{"papermill":{"duration":0.011992,"end_time":"2020-08-19T17:17:49.682367","exception":false,"start_time":"2020-08-19T17:17:49.670375","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### import libraries","execution_count":null},{"metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2020-08-19T17:17:49.714988Z","iopub.status.busy":"2020-08-19T17:17:49.714062Z","iopub.status.idle":"2020-08-19T17:18:19.244032Z","shell.execute_reply":"2020-08-19T17:18:19.243105Z"},"papermill":{"duration":29.550079,"end_time":"2020-08-19T17:18:19.244208","exception":false,"start_time":"2020-08-19T17:17:49.694129","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"%%bash\npip install ../input/pytorch-pfn-extras/pytorch-pfn-extras-0.2.1/","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2020-08-19T17:18:19.326572Z","iopub.status.busy":"2020-08-19T17:18:19.325735Z","iopub.status.idle":"2020-08-19T17:18:22.977617Z","shell.execute_reply":"2020-08-19T17:18:22.976407Z"},"papermill":{"duration":3.679817,"end_time":"2020-08-19T17:18:22.977744","exception":false,"start_time":"2020-08-19T17:18:19.297927","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import os\nimport os\nimport sys\nsys.path = [\n    '../input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',\n] + sys.path\nimport gc\nimport time\nimport shutil\nimport random\nimport warnings\nimport typing as tp\nfrom pathlib import Path\nfrom contextlib import contextmanager\n\nimport yaml\nfrom joblib import delayed, Parallel\n\nimport cv2\nimport librosa\nimport audioread\nimport soundfile as sf\n\nimport numpy as np\nimport pandas as pd\n\nfrom sklearn.metrics import f1_score\nfrom sklearn.model_selection import StratifiedKFold\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.utils.data as data\n# import resnest.torch as resnest_torch\n\nimport pytorch_pfn_extras as ppe\nfrom pytorch_pfn_extras.training import extensions as ppe_extensions\n\npd.options.display.max_rows = 500\npd.options.display.max_columns = 500","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:23.007998Z","iopub.status.busy":"2020-08-19T17:18:23.0073Z","iopub.status.idle":"2020-08-19T17:18:23.012121Z","shell.execute_reply":"2020-08-19T17:18:23.011492Z"},"papermill":{"duration":0.021547,"end_time":"2020-08-19T17:18:23.01223","exception":false,"start_time":"2020-08-19T17:18:22.990683","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"Path(\"/root/.cache/torch/checkpoints\").mkdir(parents=True)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:23.043201Z","iopub.status.busy":"2020-08-19T17:18:23.042288Z","iopub.status.idle":"2020-08-19T17:18:23.901013Z","shell.execute_reply":"2020-08-19T17:18:23.900401Z"},"papermill":{"duration":0.876628,"end_time":"2020-08-19T17:18:23.901141","exception":false,"start_time":"2020-08-19T17:18:23.024513","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"!cp ../input/efficientnet-pytorch/efficientnet-b4-e116e8b3.pth /root/.cache/torch/checkpoints/","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"papermill":{"duration":0.012108,"end_time":"2020-08-19T17:18:23.925573","exception":false,"start_time":"2020-08-19T17:18:23.913465","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### define utilities","execution_count":null},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:23.960472Z","iopub.status.busy":"2020-08-19T17:18:23.959541Z","iopub.status.idle":"2020-08-19T17:18:23.962711Z","shell.execute_reply":"2020-08-19T17:18:23.962177Z"},"papermill":{"duration":0.024731,"end_time":"2020-08-19T17:18:23.962819","exception":false,"start_time":"2020-08-19T17:18:23.938088","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def set_seed(seed: int = 42):\n    random.seed(seed)\n    np.random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)  # type: ignore\n#     torch.backends.cudnn.deterministic = True  # type: ignore\n#     torch.backends.cudnn.benchmark = True  # type: ignore\n    \n\n@contextmanager\ndef timer(name: str) -> None:\n    \"\"\"Timer Util\"\"\"\n    t0 = time.time()\n    print(\"[{}] start\".format(name))\n    yield\n    print(\"[{}] done in {:.0f} s\".format(name, time.time() - t0))","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.011751,"end_time":"2020-08-19T17:18:23.98648","exception":false,"start_time":"2020-08-19T17:18:23.974729","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### read data","execution_count":null},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:24.016918Z","iopub.status.busy":"2020-08-19T17:18:24.016106Z","iopub.status.idle":"2020-08-19T17:18:24.018538Z","shell.execute_reply":"2020-08-19T17:18:24.0191Z"},"papermill":{"duration":0.02066,"end_time":"2020-08-19T17:18:24.019211","exception":false,"start_time":"2020-08-19T17:18:23.998551","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"ROOT = Path.cwd().parent\nINPUT_ROOT = ROOT / \"input\"\nRAW_DATA = INPUT_ROOT / \"birdsong-recognition\"\nTRAIN_AUDIO_DIR = RAW_DATA / \"train_audio\"\nTRAIN_RESAMPLED_AUDIO_DIRS = [\n  INPUT_ROOT / \"birdsong-resampled-train-audio-{:0>2}\".format(i)  for i in range(5)\n]\nTEST_AUDIO_DIR = RAW_DATA / \"test_audio\"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:24.049584Z","iopub.status.busy":"2020-08-19T17:18:24.048969Z","iopub.status.idle":"2020-08-19T17:18:24.334905Z","shell.execute_reply":"2020-08-19T17:18:24.33429Z"},"papermill":{"duration":0.303661,"end_time":"2020-08-19T17:18:24.335055","exception":false,"start_time":"2020-08-19T17:18:24.031394","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"\ntrain = pd.read_csv(TRAIN_RESAMPLED_AUDIO_DIRS[0] / \"train_mod.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:24.383396Z","iopub.status.busy":"2020-08-19T17:18:24.370603Z","iopub.status.idle":"2020-08-19T17:18:24.401666Z","shell.execute_reply":"2020-08-19T17:18:24.40218Z"},"papermill":{"duration":0.052304,"end_time":"2020-08-19T17:18:24.402323","exception":false,"start_time":"2020-08-19T17:18:24.350019","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"train.head().T","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:24.434378Z","iopub.status.busy":"2020-08-19T17:18:24.433597Z","iopub.status.idle":"2020-08-19T17:18:24.442343Z","shell.execute_reply":"2020-08-19T17:18:24.441755Z"},"papermill":{"duration":0.026703,"end_time":"2020-08-19T17:18:24.442472","exception":false,"start_time":"2020-08-19T17:18:24.415769","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"if not TEST_AUDIO_DIR.exists():\n    TEST_AUDIO_DIR = INPUT_ROOT / \"birdcall-check\" / \"test_audio\"\n    test = pd.read_csv(INPUT_ROOT / \"birdcall-check\" / \"test.csv\")\nelse:\n    test = pd.read_csv(RAW_DATA / \"test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:24.481445Z","iopub.status.busy":"2020-08-19T17:18:24.480625Z","iopub.status.idle":"2020-08-19T17:18:24.483974Z","shell.execute_reply":"2020-08-19T17:18:24.484485Z"},"papermill":{"duration":0.029204,"end_time":"2020-08-19T17:18:24.484621","exception":false,"start_time":"2020-08-19T17:18:24.455417","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"test.head().T","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.013088,"end_time":"2020-08-19T17:18:24.511118","exception":false,"start_time":"2020-08-19T17:18:24.49803","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### settings","execution_count":null},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:24.542922Z","iopub.status.busy":"2020-08-19T17:18:24.541964Z","iopub.status.idle":"2020-08-19T17:18:24.544908Z","shell.execute_reply":"2020-08-19T17:18:24.544359Z"},"papermill":{"duration":0.020717,"end_time":"2020-08-19T17:18:24.545012","exception":false,"start_time":"2020-08-19T17:18:24.524295","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"settings_str = \"\"\"\nglobals:\n  seed: 1213\n  device: cuda\n  num_epochs: 35\n  output_dir: /kaggle/training_output/\n  use_fold: 0\n  target_sr: 32000\n\ndataset:\n  name: SpectrogramDataset\n  params:\n    img_size: 224\n    melspectrogram_parameters:\n      n_mels: 128\n      fmin: 20\n      fmax: 16000\n    \nsplit:\n  name: StratifiedKFold\n  params:\n    n_splits: 5\n    random_state: 42\n    shuffle: True\n\nloader:\n  train:\n    batch_size: 32\n    shuffle: True\n    num_workers: 2\n    pin_memory: True\n    drop_last: True\n  val:\n    batch_size: 48\n    shuffle: False\n    num_workers: 2\n    pin_memory: True\n    drop_last: False\n\nmodel:\n  name: E_net\n  params:\n    pretrained: True\n    n_classes: 264\n\nloss:\n  name: BCEWithLogitsLoss\n  params: {}\n\noptimizer:\n  name: Adam\n  params:\n    lr: 0.001\n\nscheduler:\n  name: CosineAnnealingLR\n  params:\n    T_max: 10\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:24.585803Z","iopub.status.busy":"2020-08-19T17:18:24.585111Z","iopub.status.idle":"2020-08-19T17:18:24.588519Z","shell.execute_reply":"2020-08-19T17:18:24.588004Z"},"papermill":{"duration":0.029703,"end_time":"2020-08-19T17:18:24.588632","exception":false,"start_time":"2020-08-19T17:18:24.558929","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"settings = yaml.safe_load(settings_str)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:24.619301Z","iopub.status.busy":"2020-08-19T17:18:24.618574Z","iopub.status.idle":"2020-08-19T17:18:24.621622Z","shell.execute_reply":"2020-08-19T17:18:24.621119Z"},"papermill":{"duration":0.01969,"end_time":"2020-08-19T17:18:24.621713","exception":false,"start_time":"2020-08-19T17:18:24.602023","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# if not torch.cuda.is_available():\n#     settings[\"globals\"][\"device\"] = \"cpu\"","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"execution":{"iopub.execute_input":"2020-08-19T17:18:24.654571Z","iopub.status.busy":"2020-08-19T17:18:24.653679Z","iopub.status.idle":"2020-08-19T17:18:24.66294Z","shell.execute_reply":"2020-08-19T17:18:24.663487Z"},"papermill":{"duration":0.029209,"end_time":"2020-08-19T17:18:24.66361","exception":false,"start_time":"2020-08-19T17:18:24.634401","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"for k, v in settings.items():\n    print(\"[{}]\".format(k))\n    print(v)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.013304,"end_time":"2020-08-19T17:18:24.690813","exception":false,"start_time":"2020-08-19T17:18:24.677509","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### preprocess audio data\n\nCode is forked from: https://github.com/koukyo1994/kaggle-birdcall-resnet-baseline-training/blob/master/input/birdsong-recognition/prepare.py\n\nI modified this partially. \n\nHowever, in this notebook, I used uploaded resampled audio because this preprocessing is too heavy for kaggle notebook.","execution_count":null},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:24.726995Z","iopub.status.busy":"2020-08-19T17:18:24.726108Z","iopub.status.idle":"2020-08-19T17:18:24.729179Z","shell.execute_reply":"2020-08-19T17:18:24.728645Z"},"papermill":{"duration":0.024612,"end_time":"2020-08-19T17:18:24.729278","exception":false,"start_time":"2020-08-19T17:18:24.704666","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def resample(ebird_code: str,filename: str, target_sr: int):    \n    audio_dir = TRAIN_AUDIO_DIR\n    resample_dir = TRAIN_RESAMPLED_DIR\n    ebird_dir = resample_dir / ebird_code\n    \n    try:\n        y, _ = librosa.load(\n            audio_dir / ebird_code / filename,\n            sr=target_sr, mono=True, res_type=\"kaiser_fast\")\n\n        filename = filename.replace(\".mp3\", \".wav\")\n        sf.write(ebird_dir / filename, y, samplerate=target_sr)\n    except Exception as e:\n        print(e)\n        with open(\"skipped.txt\", \"a\") as f:\n            file_path = str(audio_dir / ebird_code / filename)\n            f.write(file_path + \"\\n\")","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:24.760692Z","iopub.status.busy":"2020-08-19T17:18:24.759849Z","iopub.status.idle":"2020-08-19T17:18:24.762583Z","shell.execute_reply":"2020-08-19T17:18:24.763235Z"},"papermill":{"duration":0.02077,"end_time":"2020-08-19T17:18:24.763365","exception":false,"start_time":"2020-08-19T17:18:24.742595","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# train_org = train.copy()\n# TRAIN_RESAMPLED_DIR = Path(\"/kaggle/processed_data/train_audio_resampled\")\n# TRAIN_RESAMPLED_DIR.mkdir(parents=True)\n\n# for ebird_code in train.ebird_code.unique():\n#     ebird_dir = TRAIN_RESAMPLED_DIR / ebird_code\n#     ebird_dir.mkdir()\n\n# warnings.simplefilter(\"ignore\")\n# train_audio_infos = train[[\"ebird_code\", \"filename\"]].values.tolist()\n# Parallel(n_jobs=NUM_THREAD, verbose=10)(\n#     delayed(resample)(ebird_code, file_name, TARGET_SR) for ebird_code, file_name in train_audio_infos)\n\n# train[\"resampled_sampling_rate\"] = TARGET_SR\n# train[\"resampled_filename\"] = train[\"filename\"].map(\n#     lambda x: x.replace(\".mp3\", \".wav\"))\n# train[\"resampled_channels\"] = \"1 (mono)\"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.014113,"end_time":"2020-08-19T17:18:24.791645","exception":false,"start_time":"2020-08-19T17:18:24.777532","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Definition","execution_count":null},{"metadata":{"papermill":{"duration":0.013817,"end_time":"2020-08-19T17:18:24.819618","exception":false,"start_time":"2020-08-19T17:18:24.805801","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Dataset\n* forked from: https://github.com/koukyo1994/kaggle-birdcall-resnet-baseline-training/blob/master/src/dataset.py\n* modified partialy\n","execution_count":null},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:24.871013Z","iopub.status.busy":"2020-08-19T17:18:24.860514Z","iopub.status.idle":"2020-08-19T17:18:24.892312Z","shell.execute_reply":"2020-08-19T17:18:24.892806Z"},"papermill":{"duration":0.059533,"end_time":"2020-08-19T17:18:24.892947","exception":false,"start_time":"2020-08-19T17:18:24.833414","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"BIRD_CODE = {\n    'aldfly': 0, 'ameavo': 1, 'amebit': 2, 'amecro': 3, 'amegfi': 4,\n    'amekes': 5, 'amepip': 6, 'amered': 7, 'amerob': 8, 'amewig': 9,\n    'amewoo': 10, 'amtspa': 11, 'annhum': 12, 'astfly': 13, 'baisan': 14,\n    'baleag': 15, 'balori': 16, 'banswa': 17, 'barswa': 18, 'bawwar': 19,\n    'belkin1': 20, 'belspa2': 21, 'bewwre': 22, 'bkbcuc': 23, 'bkbmag1': 24,\n    'bkbwar': 25, 'bkcchi': 26, 'bkchum': 27, 'bkhgro': 28, 'bkpwar': 29,\n    'bktspa': 30, 'blkpho': 31, 'blugrb1': 32, 'blujay': 33, 'bnhcow': 34,\n    'boboli': 35, 'bongul': 36, 'brdowl': 37, 'brebla': 38, 'brespa': 39,\n    'brncre': 40, 'brnthr': 41, 'brthum': 42, 'brwhaw': 43, 'btbwar': 44,\n    'btnwar': 45, 'btywar': 46, 'buffle': 47, 'buggna': 48, 'buhvir': 49,\n    'bulori': 50, 'bushti': 51, 'buwtea': 52, 'buwwar': 53, 'cacwre': 54,\n    'calgul': 55, 'calqua': 56, 'camwar': 57, 'cangoo': 58, 'canwar': 59,\n    'canwre': 60, 'carwre': 61, 'casfin': 62, 'caster1': 63, 'casvir': 64,\n    'cedwax': 65, 'chispa': 66, 'chiswi': 67, 'chswar': 68, 'chukar': 69,\n    'clanut': 70, 'cliswa': 71, 'comgol': 72, 'comgra': 73, 'comloo': 74,\n    'commer': 75, 'comnig': 76, 'comrav': 77, 'comred': 78, 'comter': 79,\n    'comyel': 80, 'coohaw': 81, 'coshum': 82, 'cowscj1': 83, 'daejun': 84,\n    'doccor': 85, 'dowwoo': 86, 'dusfly': 87, 'eargre': 88, 'easblu': 89,\n    'easkin': 90, 'easmea': 91, 'easpho': 92, 'eastow': 93, 'eawpew': 94,\n    'eucdov': 95, 'eursta': 96, 'evegro': 97, 'fiespa': 98, 'fiscro': 99,\n    'foxspa': 100, 'gadwal': 101, 'gcrfin': 102, 'gnttow': 103, 'gnwtea': 104,\n    'gockin': 105, 'gocspa': 106, 'goleag': 107, 'grbher3': 108, 'grcfly': 109,\n    'greegr': 110, 'greroa': 111, 'greyel': 112, 'grhowl': 113, 'grnher': 114,\n    'grtgra': 115, 'grycat': 116, 'gryfly': 117, 'haiwoo': 118, 'hamfly': 119,\n    'hergul': 120, 'herthr': 121, 'hoomer': 122, 'hoowar': 123, 'horgre': 124,\n    'horlar': 125, 'houfin': 126, 'houspa': 127, 'houwre': 128, 'indbun': 129,\n    'juntit1': 130, 'killde': 131, 'labwoo': 132, 'larspa': 133, 'lazbun': 134,\n    'leabit': 135, 'leafly': 136, 'leasan': 137, 'lecthr': 138, 'lesgol': 139,\n    'lesnig': 140, 'lesyel': 141, 'lewwoo': 142, 'linspa': 143, 'lobcur': 144,\n    'lobdow': 145, 'logshr': 146, 'lotduc': 147, 'louwat': 148, 'macwar': 149,\n    'magwar': 150, 'mallar3': 151, 'marwre': 152, 'merlin': 153, 'moublu': 154,\n    'mouchi': 155, 'moudov': 156, 'norcar': 157, 'norfli': 158, 'norhar2': 159,\n    'normoc': 160, 'norpar': 161, 'norpin': 162, 'norsho': 163, 'norwat': 164,\n    'nrwswa': 165, 'nutwoo': 166, 'olsfly': 167, 'orcwar': 168, 'osprey': 169,\n    'ovenbi1': 170, 'palwar': 171, 'pasfly': 172, 'pecsan': 173, 'perfal': 174,\n    'phaino': 175, 'pibgre': 176, 'pilwoo': 177, 'pingro': 178, 'pinjay': 179,\n    'pinsis': 180, 'pinwar': 181, 'plsvir': 182, 'prawar': 183, 'purfin': 184,\n    'pygnut': 185, 'rebmer': 186, 'rebnut': 187, 'rebsap': 188, 'rebwoo': 189,\n    'redcro': 190, 'redhea': 191, 'reevir1': 192, 'renpha': 193, 'reshaw': 194,\n    'rethaw': 195, 'rewbla': 196, 'ribgul': 197, 'rinduc': 198, 'robgro': 199,\n    'rocpig': 200, 'rocwre': 201, 'rthhum': 202, 'ruckin': 203, 'rudduc': 204,\n    'rufgro': 205, 'rufhum': 206, 'rusbla': 207, 'sagspa1': 208, 'sagthr': 209,\n    'savspa': 210, 'saypho': 211, 'scatan': 212, 'scoori': 213, 'semplo': 214,\n    'semsan': 215, 'sheowl': 216, 'shshaw': 217, 'snobun': 218, 'snogoo': 219,\n    'solsan': 220, 'sonspa': 221, 'sora': 222, 'sposan': 223, 'spotow': 224,\n    'stejay': 225, 'swahaw': 226, 'swaspa': 227, 'swathr': 228, 'treswa': 229,\n    'truswa': 230, 'tuftit': 231, 'tunswa': 232, 'veery': 233, 'vesspa': 234,\n    'vigswa': 235, 'warvir': 236, 'wesblu': 237, 'wesgre': 238, 'weskin': 239,\n    'wesmea': 240, 'wessan': 241, 'westan': 242, 'wewpew': 243, 'whbnut': 244,\n    'whcspa': 245, 'whfibi': 246, 'whtspa': 247, 'whtswi': 248, 'wilfly': 249,\n    'wilsni1': 250, 'wiltur': 251, 'winwre3': 252, 'wlswar': 253, 'wooduc': 254,\n    'wooscj2': 255, 'woothr': 256, 'y00475': 257, 'yebfly': 258, 'yebsap': 259,\n    'yehbla': 260, 'yelwar': 261, 'yerwar': 262, 'yetvir': 263\n}\n\nINV_BIRD_CODE = {v: k for k, v in BIRD_CODE.items()}","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:24.948316Z","iopub.status.busy":"2020-08-19T17:18:24.938895Z","iopub.status.idle":"2020-08-19T17:18:24.951005Z","shell.execute_reply":"2020-08-19T17:18:24.950495Z"},"papermill":{"duration":0.044375,"end_time":"2020-08-19T17:18:24.951111","exception":false,"start_time":"2020-08-19T17:18:24.906736","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"PERIOD = 5\n\ndef mono_to_color(\n    X: np.ndarray, mean=None, std=None,\n    norm_max=None, norm_min=None, eps=1e-6\n):\n    # Stack X as [X,X,X]\n    X = np.stack([X, X, X], axis=-1)\n\n    # Standardize\n    mean = mean or X.mean()\n    X = X - mean\n    std = std or X.std()\n    Xstd = X / (std + eps)\n    _min, _max = Xstd.min(), Xstd.max()\n    norm_max = norm_max or _max\n    norm_min = norm_min or _min\n    if (_max - _min) > eps:\n        # Normalize to [0, 255]\n        V = Xstd\n        V[V < norm_min] = norm_min\n        V[V > norm_max] = norm_max\n        V = 255 * (V - norm_min) / (norm_max - norm_min)\n        V = V.astype(np.uint8)\n    else:\n        # Just zero\n        V = np.zeros_like(Xstd, dtype=np.uint8)\n    return V\n\nclass SpectrogramDataset(data.Dataset):\n    def __init__(\n        self,\n        file_list: tp.List[tp.List[str]], img_size=224,\n        waveform_transforms=None, spectrogram_transforms=None, melspectrogram_parameters={}\n    ):\n        self.file_list = file_list  # list of list: [file_path, ebird_code]\n        self.img_size = img_size\n        self.waveform_transforms = waveform_transforms\n        self.spectrogram_transforms = spectrogram_transforms\n        self.melspectrogram_parameters = melspectrogram_parameters\n\n    def __len__(self):\n        return len(self.file_list)\n\n    def __getitem__(self, idx: int):\n        wav_path, ebird_code = self.file_list[idx]\n\n        y, sr = sf.read(wav_path)\n\n        if self.waveform_transforms:\n            y = self.waveform_transforms(y)\n        else:\n            len_y = len(y)\n            effective_length = sr * PERIOD\n            if len_y < effective_length:\n                new_y = np.zeros(effective_length, dtype=y.dtype)\n                start = np.random.randint(effective_length - len_y)\n                new_y[start:start + len_y] = y\n                y = new_y.astype(np.float32)\n            elif len_y > effective_length:\n                start = np.random.randint(len_y - effective_length)\n                y = y[start:start + effective_length].astype(np.float32)\n            else:\n                y = y.astype(np.float32)\n\n        melspec = librosa.feature.melspectrogram(y, sr=sr, **self.melspectrogram_parameters)\n        melspec = librosa.power_to_db(melspec).astype(np.float32)\n\n        if self.spectrogram_transforms:\n            melspec = self.spectrogram_transforms(melspec)\n        else:\n            pass\n\n        image = mono_to_color(melspec)\n        height, width, _ = image.shape\n        image = cv2.resize(image, (int(width * self.img_size / height), self.img_size))\n        image = np.moveaxis(image, 2, 0)\n        image = (image / 255.0).astype(np.float32)\n\n#         labels = np.zeros(len(BIRD_CODE), dtype=\"i\")\n        labels = np.zeros(len(BIRD_CODE), dtype=\"f\")\n        labels[BIRD_CODE[ebird_code]] = 1\n\n        return image, labels","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.013431,"end_time":"2020-08-19T17:18:24.978164","exception":false,"start_time":"2020-08-19T17:18:24.964733","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Training Utility","execution_count":null},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:25.013309Z","iopub.status.busy":"2020-08-19T17:18:25.012444Z","iopub.status.idle":"2020-08-19T17:18:25.015331Z","shell.execute_reply":"2020-08-19T17:18:25.014832Z"},"papermill":{"duration":0.023496,"end_time":"2020-08-19T17:18:25.015427","exception":false,"start_time":"2020-08-19T17:18:24.991931","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def get_loaders_for_training(\n    args_dataset: tp.Dict, args_loader: tp.Dict,\n    train_file_list: tp.List[str], val_file_list: tp.List[str]\n):\n    # # make dataset\n    train_dataset = SpectrogramDataset(train_file_list, **args_dataset)\n    val_dataset = SpectrogramDataset(val_file_list, **args_dataset)\n    # # make dataloader\n    train_loader = data.DataLoader(train_dataset, **args_loader[\"train\"])\n    val_loader = data.DataLoader(val_dataset, **args_loader[\"val\"])\n    \n    return train_loader, val_loader","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:25.048417Z","iopub.status.busy":"2020-08-19T17:18:25.047696Z","iopub.status.idle":"2020-08-19T17:18:25.07405Z","shell.execute_reply":"2020-08-19T17:18:25.073355Z"},"papermill":{"duration":0.044754,"end_time":"2020-08-19T17:18:25.074222","exception":false,"start_time":"2020-08-19T17:18:25.029468","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"from efficientnet_pytorch import model as enet\npretrained_model = {\n    'efficientnet-b2': '../input/efficientnet-pytorch/efficientnet-b2-27687264.pth'\n}\n\nenet_type = 'efficientnet-b2'\n\ndevice = torch.device('cuda')","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:25.113711Z","iopub.status.busy":"2020-08-19T17:18:25.112837Z","iopub.status.idle":"2020-08-19T17:18:25.115908Z","shell.execute_reply":"2020-08-19T17:18:25.115372Z"},"papermill":{"duration":0.027266,"end_time":"2020-08-19T17:18:25.116016","exception":false,"start_time":"2020-08-19T17:18:25.08875","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"class enetv2(nn.Module):\n    def __init__(self, backbone):\n        super(enetv2, self).__init__()\n        self.enet = enet.EfficientNet.from_name(backbone)\n        self.enet.load_state_dict(torch.load(pretrained_model[backbone]))\n\n        self.myfc = nn.Sequential(\n        nn.Linear(self.enet._fc.in_features,1024), nn.ReLU(), nn.Dropout(p=0.2),\n        nn.Linear(1024,512), nn.ReLU(), nn.Dropout(p=0.3),\n        nn.Linear(512,512), nn.ReLU(), nn.Dropout(p=0.3),\n            \n        nn.Linear(512, 264))\n        \n        self.enet._fc = nn.Identity()\n\n    def extract(self, x):\n        return self.enet(x)\n\n    def forward(self, x):\n        x = self.extract(x)\n#         print(x.shape)\n        x = self.myfc(x)\n        return x","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:25.151658Z","iopub.status.busy":"2020-08-19T17:18:25.150723Z","iopub.status.idle":"2020-08-19T17:18:25.152755Z","shell.execute_reply":"2020-08-19T17:18:25.153317Z"},"papermill":{"duration":0.023322,"end_time":"2020-08-19T17:18:25.153454","exception":false,"start_time":"2020-08-19T17:18:25.130132","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def get_model(args: tp.Dict):\n    model =enetv2(enet_type)\n    model = model.to(device)\n        \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#model = get_model(settings[\"model\"])","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:25.195837Z","iopub.status.busy":"2020-08-19T17:18:25.195023Z","iopub.status.idle":"2020-08-19T17:18:25.197501Z","shell.execute_reply":"2020-08-19T17:18:25.198105Z"},"papermill":{"duration":0.030694,"end_time":"2020-08-19T17:18:25.198226","exception":false,"start_time":"2020-08-19T17:18:25.167532","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def train_loop(\n    manager, args, model, device,\n    train_loader, optimizer, scheduler, loss_func\n):\n    \"\"\"Run minibatch training loop\"\"\"\n    while not manager.stop_trigger:\n        model.train()\n        for batch_idx, (data, target) in enumerate(train_loader):\n            with manager.run_iteration():\n                data, target = data.to(device), target.to(device)\n                optimizer.zero_grad()\n                output = model(data)\n                loss = loss_func(output, target)\n                ppe.reporting.report({'train/loss': loss.item()})\n                loss.backward()\n                optimizer.step()\n                scheduler.step()\n\ndef eval_for_batch(\n    args, model, device,\n    data, target, loss_func, eval_func_dict={}\n):\n    \"\"\"\n    Run evaliation for valid\n    \n    This function is applied to each batch of val loader.\n    \"\"\"\n    model.eval()\n    data, target = data.to(device), target.to(device)\n    output = model(data)\n    # Final result will be average of averages of the same size\n    val_loss = loss_func(output, target).item()\n    ppe.reporting.report({'val/loss': val_loss})\n    \n    for eval_name, eval_func in eval_func_dict.items():\n        eval_value = eval_func(output, target).item()\n        ppe.reporting.report({\"val/{}\".format(eval_aame): eval_value})","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:25.242496Z","iopub.status.busy":"2020-08-19T17:18:25.241517Z","iopub.status.idle":"2020-08-19T17:18:25.244209Z","shell.execute_reply":"2020-08-19T17:18:25.244684Z"},"papermill":{"duration":0.031616,"end_time":"2020-08-19T17:18:25.244812","exception":false,"start_time":"2020-08-19T17:18:25.213196","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def set_extensions(\n    manager, args, model, device, test_loader, optimizer,\n    loss_func, eval_func_dict={}\n):\n    \"\"\"set extensions for PPE\"\"\"\n        \n    my_extensions = [\n        # # observe, report\n        ppe_extensions.observe_lr(optimizer=optimizer),\n        # ppe_extensions.ParameterStatistics(model, prefix='model'),\n        # ppe_extensions.VariableStatisticsPlot(model),\n        ppe_extensions.LogReport(),\n        ppe_extensions.PlotReport(['train/loss', 'val/loss'], 'epoch', filename='loss.png'),\n        ppe_extensions.PlotReport(['lr',], 'epoch', filename='lr.png'),\n        ppe_extensions.PrintReport([\n            'epoch', 'iteration', 'lr', 'train/loss', 'val/loss', \"elapsed_time\"]),\n#         ppe_extensions.ProgressBar(update_interval=100),\n\n        # # evaluation\n        (\n            ppe_extensions.Evaluator(\n                test_loader, model,\n                eval_func=lambda data, target:\n                    eval_for_batch(args, model, device, data, target, loss_func, eval_func_dict),\n                progress_bar=True),\n            (1, \"epoch\"),\n        ),\n        # # save model snapshot.\n        (\n            ppe_extensions.snapshot(\n                target=model, filename=\"snapshot_epoch_{.updater.epoch}.pth\"),\n            ppe.training.triggers.MinValueTrigger(key=\"val/loss\", trigger=(1, 'epoch'))\n        ),\n    ]\n           \n    # # set extensions to manager\n    for ext in my_extensions:\n        if isinstance(ext, tuple):\n            manager.extend(ext[0], trigger=ext[1])\n        else:\n            manager.extend(ext)\n        \n    return manager","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.013619,"end_time":"2020-08-19T17:18:25.27275","exception":false,"start_time":"2020-08-19T17:18:25.259131","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Training","execution_count":null},{"metadata":{"papermill":{"duration":0.014161,"end_time":"2020-08-19T17:18:25.301095","exception":false,"start_time":"2020-08-19T17:18:25.286934","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### prepare data","execution_count":null},{"metadata":{"papermill":{"duration":0.015118,"end_time":"2020-08-19T17:18:25.330863","exception":false,"start_time":"2020-08-19T17:18:25.315745","status":"completed"},"tags":[]},"cell_type":"markdown","source":"#### get wav file path","execution_count":null},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:25.367739Z","iopub.status.busy":"2020-08-19T17:18:25.366927Z","iopub.status.idle":"2020-08-19T17:18:26.221557Z","shell.execute_reply":"2020-08-19T17:18:26.219769Z"},"papermill":{"duration":0.876666,"end_time":"2020-08-19T17:18:26.221705","exception":false,"start_time":"2020-08-19T17:18:25.345039","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"tmp_list = []\nfor audio_d in TRAIN_RESAMPLED_AUDIO_DIRS:\n    if not audio_d.exists():\n        continue\n    for ebird_d in audio_d.iterdir():\n        if ebird_d.is_file():\n            continue\n        for wav_f in ebird_d.iterdir():\n            tmp_list.append([ebird_d.name, wav_f.name, wav_f.as_posix()])\n            \ntrain_wav_path_exist = pd.DataFrame(\n    tmp_list, columns=[\"ebird_code\", \"resampled_filename\", \"file_path\"])\n\ndel tmp_list\n\ntrain_all = pd.merge(\n    train, train_wav_path_exist, on=[\"ebird_code\", \"resampled_filename\"], how=\"inner\")\n\nprint(train.shape)\nprint(train_wav_path_exist.shape)\nprint(train_all.shape)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:26.30176Z","iopub.status.busy":"2020-08-19T17:18:26.300561Z","iopub.status.idle":"2020-08-19T17:18:26.309158Z","shell.execute_reply":"2020-08-19T17:18:26.308233Z"},"papermill":{"duration":0.071515,"end_time":"2020-08-19T17:18:26.309269","exception":false,"start_time":"2020-08-19T17:18:26.237754","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"train_all.head()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.015866,"end_time":"2020-08-19T17:18:26.343479","exception":false,"start_time":"2020-08-19T17:18:26.327613","status":"completed"},"tags":[]},"cell_type":"markdown","source":"#### split data","execution_count":null},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:26.378011Z","iopub.status.busy":"2020-08-19T17:18:26.377082Z","iopub.status.idle":"2020-08-19T17:18:26.380025Z","shell.execute_reply":"2020-08-19T17:18:26.379382Z"},"papermill":{"duration":0.021564,"end_time":"2020-08-19T17:18:26.380124","exception":false,"start_time":"2020-08-19T17:18:26.35856","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"# # for test run\n# test_run_idx = sorted(np.random.choice(len(train_all), len(train_all) // 10, replace=False))\n# train_all = train_all.iloc[test_run_idx, :].reset_index(drop=True)\n# settings[\"globals\"][\"num_epochs\"] = 20\n# settings[\"scheduler\"][\"params\"][\"T_max\"] = 4","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"execution":{"iopub.execute_input":"2020-08-19T17:18:26.440288Z","iopub.status.busy":"2020-08-19T17:18:26.419662Z","iopub.status.idle":"2020-08-19T17:18:26.520085Z","shell.execute_reply":"2020-08-19T17:18:26.520847Z"},"papermill":{"duration":0.126123,"end_time":"2020-08-19T17:18:26.521045","exception":false,"start_time":"2020-08-19T17:18:26.394922","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"skf = StratifiedKFold(**settings[\"split\"][\"params\"])\n\ntrain_all[\"fold\"] = -1\nfor fold_id, (train_index, val_index) in enumerate(skf.split(train_all, train_all[\"ebird_code\"])):\n    train_all.iloc[val_index, -1] = fold_id\n    \n# # check the propotion\nfold_proportion = pd.pivot_table(train_all, index=\"ebird_code\", columns=\"fold\", values=\"xc_id\", aggfunc=len)\nprint(fold_proportion.shape)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2020-08-19T17:18:26.585918Z","iopub.status.busy":"2020-08-19T17:18:26.575745Z","iopub.status.idle":"2020-08-19T17:18:26.624221Z","shell.execute_reply":"2020-08-19T17:18:26.624717Z"},"papermill":{"duration":0.086478,"end_time":"2020-08-19T17:18:26.624844","exception":false,"start_time":"2020-08-19T17:18:26.538366","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"fold_proportion","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:26.671824Z","iopub.status.busy":"2020-08-19T17:18:26.670946Z","iopub.status.idle":"2020-08-19T17:18:26.718067Z","shell.execute_reply":"2020-08-19T17:18:26.717216Z"},"papermill":{"duration":0.075415,"end_time":"2020-08-19T17:18:26.718217","exception":false,"start_time":"2020-08-19T17:18:26.642802","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"use_fold = settings[\"globals\"][\"use_fold\"]\ntrain_file_list = train_all.query(\"fold != @use_fold\")[[\"file_path\", \"ebird_code\"]].values.tolist()\nval_file_list = train_all.query(\"fold == @use_fold\")[[\"file_path\", \"ebird_code\"]].values.tolist()\n\nprint(\"[fold {}] train: {}, val: {}\".format(use_fold, len(train_file_list), len(val_file_list)))","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.017279,"end_time":"2020-08-19T17:18:26.753488","exception":false,"start_time":"2020-08-19T17:18:26.736209","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## run training","execution_count":null},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:26.802638Z","iopub.status.busy":"2020-08-19T17:18:26.79752Z","iopub.status.idle":"2020-08-19T17:18:32.329213Z","shell.execute_reply":"2020-08-19T17:18:32.32749Z"},"papermill":{"duration":5.558066,"end_time":"2020-08-19T17:18:32.329351","exception":false,"start_time":"2020-08-19T17:18:26.771285","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"set_seed(settings[\"globals\"][\"seed\"])\ndevice = torch.device(settings[\"globals\"][\"device\"])\noutput_dir = Path(settings[\"globals\"][\"output_dir\"])\n\n# # # get loader\ntrain_loader, val_loader = get_loaders_for_training(\n    settings[\"dataset\"][\"params\"], settings[\"loader\"], train_file_list, val_file_list)\n\n# # # get model\nmodel = get_model(settings[\"model\"])\nmodel = model.to(device)\n\n# # # get optimizer\noptimizer = getattr(\n    torch.optim, settings[\"optimizer\"][\"name\"]\n)(model.parameters(), **settings[\"optimizer\"][\"params\"])\n\n# # # get scheduler\nscheduler = getattr(\n    torch.optim.lr_scheduler, settings[\"scheduler\"][\"name\"]\n)(optimizer, **settings[\"scheduler\"][\"params\"])\n\n# # # get loss\nloss_func = getattr(nn, settings[\"loss\"][\"name\"])(**settings[\"loss\"][\"params\"])\n\n# # # create training manager\ntrigger = None\n\nmanager = ppe.training.ExtensionsManager(\n    model, optimizer, settings[\"globals\"][\"num_epochs\"],\n    iters_per_epoch=len(train_loader),\n    stop_trigger=trigger,\n    out_dir=output_dir\n)\n\n# # # set manager extensions\nmanager = set_extensions(\n    manager, settings, model, device,\n    val_loader, optimizer, loss_func,\n)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T17:18:32.380564Z","iopub.status.busy":"2020-08-19T17:18:32.379753Z","iopub.status.idle":"2020-08-19T23:46:30.820982Z","shell.execute_reply":"2020-08-19T23:46:30.819838Z"},"papermill":{"duration":23278.471382,"end_time":"2020-08-19T23:46:30.821128","exception":false,"start_time":"2020-08-19T17:18:32.349746","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# # runtraining\ntrain_loop(\n    manager, settings, model, device,\n    train_loader, optimizer, scheduler, loss_func)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T23:46:31.598513Z","iopub.status.busy":"2020-08-19T23:46:31.597494Z","iopub.status.idle":"2020-08-19T23:46:31.601928Z","shell.execute_reply":"2020-08-19T23:46:31.601388Z"},"papermill":{"duration":0.557011,"end_time":"2020-08-19T23:46:31.602038","exception":false,"start_time":"2020-08-19T23:46:31.045027","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"del train_loader\ndel val_loader\ndel model\ndel optimizer\ndel scheduler\ndel loss_func\ndel manager\n\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.21916,"end_time":"2020-08-19T23:46:32.041895","exception":false,"start_time":"2020-08-19T23:46:31.822735","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## save results","execution_count":null},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T23:46:32.4911Z","iopub.status.busy":"2020-08-19T23:46:32.490275Z","iopub.status.idle":"2020-08-19T23:46:32.604696Z","shell.execute_reply":"2020-08-19T23:46:32.604199Z"},"papermill":{"duration":0.34147,"end_time":"2020-08-19T23:46:32.604807","exception":false,"start_time":"2020-08-19T23:46:32.263337","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%%bash\nls /kaggle/training_output","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T23:46:33.113386Z","iopub.status.busy":"2020-08-19T23:46:33.112482Z","iopub.status.idle":"2020-08-19T23:46:33.116436Z","shell.execute_reply":"2020-08-19T23:46:33.115913Z"},"papermill":{"duration":0.230439,"end_time":"2020-08-19T23:46:33.116558","exception":false,"start_time":"2020-08-19T23:46:32.886119","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"for f_name in [\"log\",\"loss.png\", \"lr.png\"]:\n    shutil.copy(output_dir / f_name, f_name)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T23:46:33.570288Z","iopub.status.busy":"2020-08-19T23:46:33.569653Z","iopub.status.idle":"2020-08-19T23:46:33.98157Z","shell.execute_reply":"2020-08-19T23:46:33.980613Z"},"papermill":{"duration":0.643415,"end_time":"2020-08-19T23:46:33.981679","exception":false,"start_time":"2020-08-19T23:46:33.338264","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"log = pd.read_json(\"log\")\nbest_epoch = log[\"val/loss\"].idxmin() + 1\nlog.iloc[[best_epoch - 1],]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T23:46:34.432913Z","iopub.status.busy":"2020-08-19T23:46:34.432276Z","iopub.status.idle":"2020-08-19T23:46:35.056088Z","shell.execute_reply":"2020-08-19T23:46:35.056692Z"},"papermill":{"duration":0.851543,"end_time":"2020-08-19T23:46:35.056851","exception":false,"start_time":"2020-08-19T23:46:34.205308","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"shutil.copy(output_dir / \"snapshot_epoch_{}.pth\".format(best_epoch), \"best_model.pth\")","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-08-19T23:46:35.508351Z","iopub.status.busy":"2020-08-19T23:46:35.507432Z","iopub.status.idle":"2020-08-19T23:46:35.912547Z","shell.execute_reply":"2020-08-19T23:46:35.91337Z"},"papermill":{"duration":0.632841,"end_time":"2020-08-19T23:46:35.913603","exception":false,"start_time":"2020-08-19T23:46:35.280762","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"m = get_model({\n    'name': settings[\"model\"][\"name\"],\n    'params': {'pretrained': False, 'n_classes': 264}})\nstate_dict = torch.load('best_model.pth')\nm.load_state_dict(state_dict)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.256839,"end_time":"2020-08-19T23:46:36.515172","exception":false,"start_time":"2020-08-19T23:46:36.258333","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}