{"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":"markdown","source":"# 0 Imports","metadata":{}},{"cell_type":"code","source":"\"\"\"\n!pip install pretrainedmodels\n!pip install albumentations\n!pip install --upgrade efficientnet-pytorch\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:21.121236Z","iopub.execute_input":"2022-03-09T19:41:21.121760Z","iopub.status.idle":"2022-03-09T19:41:21.148393Z","shell.execute_reply.started":"2022-03-09T19:41:21.121630Z","shell.execute_reply":"2022-03-09T19:41:21.146473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math, random, os\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport librosa\nimport sklearn\nfrom sklearn.preprocessing import OneHotEncoder, OrdinalEncoder\nfrom sklearn.model_selection import train_test_split\n\nimport torch\nfrom torch import nn, optim\nimport torch.nn.functional as F\nfrom torch.utils.data import DataLoader, Dataset, random_split\nfrom torch.nn import init\nimport torchaudio\nfrom torchaudio import transforms\nfrom torchaudio.transforms import MelSpectrogram\nfrom torchvision.models import resnet34, inception_v3, vgg16_bn\nfrom torchvision.transforms import Resize\n\n#from efficientnet_pytorch import EfficientNet\n\nfrom ignite.engine import Events, create_supervised_evaluator, create_supervised_trainer\nfrom ignite.metrics import Accuracy, Loss, RunningAverage, ConfusionMatrix\nfrom ignite.handlers import ModelCheckpoint, EarlyStopping\nfrom ignite.handlers.param_scheduler import LRScheduler\nfrom ignite.contrib.handlers.tqdm_logger import ProgressBar\nfrom ignite.contrib.metrics import ROC_AUC\n\nplt.rcParams['figure.facecolor'] = 'white'\n\nprint(f\"load completed\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:21.152088Z","iopub.execute_input":"2022-03-09T19:41:21.152969Z","iopub.status.idle":"2022-03-09T19:41:24.036160Z","shell.execute_reply.started":"2022-03-09T19:41:21.152924Z","shell.execute_reply":"2022-03-09T19:41:24.035076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p ~/.torch/models\n!cp ../input/resnet34/resnet34.pth ~/.torch/models/resnet34-b627a593.pth\n!mkdir -p /root/.cache/torch/hub/checkpoints\n!cp ../input/resnet34/resnet34.pth /root/.cache/torch/hub/checkpoints/resnet34-b627a593.pth","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:24.038144Z","iopub.execute_input":"2022-03-09T19:41:24.039701Z","iopub.status.idle":"2022-03-09T19:41:27.625485Z","shell.execute_reply.started":"2022-03-09T19:41:24.039652Z","shell.execute_reply":"2022-03-09T19:41:27.624244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#/root/.cache/torch/hub/checkpoints/efficientnet-b7-dcc49843.pth\n\n!mkdir -p ~/.torch/models\n!cp ../input/efficientnet-pytorch/efficientnet-b7-dcc49843.pth ~/.torch/models/efficientnet-b7-dcc49843.pth\n!mkdir -p /root/.cache/torch/hub/checkpoints\n!cp ../input/efficientnet-pytorch/efficientnet-b7-dcc49843.pth /root/.cache/torch/hub/checkpoints/efficientnet-b7-dcc49843.pth","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:27.628354Z","iopub.execute_input":"2022-03-09T19:41:27.628620Z","iopub.status.idle":"2022-03-09T19:41:31.717895Z","shell.execute_reply.started":"2022-03-09T19:41:27.628576Z","shell.execute_reply":"2022-03-09T19:41:31.716402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#/root/.cache/torch/hub/checkpoints/efficientnet-b7-dcc49843.pth\n\n!mkdir -p ~/.torch/models\n!cp ../input/efficientnet-pytorch/efficientnet-b0-08094119.pth ~/.torch/models/efficientnet-b7-dcc49843.pth\n!mkdir -p /root/.cache/torch/hub/checkpoints\n!cp ../input/efficientnet-pytorch/efficientnet-b0-08094119.pth /root/.cache/torch/hub/checkpoints/efficientnet-b7-dcc49843.pth","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:31.723052Z","iopub.execute_input":"2022-03-09T19:41:31.723329Z","iopub.status.idle":"2022-03-09T19:41:35.515528Z","shell.execute_reply.started":"2022-03-09T19:41:31.723271Z","shell.execute_reply":"2022-03-09T19:41:35.514233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_history(history, figsize=(20, 10), metric=\"loss\"):\n    plt.title(metric.capitalize())\n    sns.lineplot(data=history, x=history.index, y=metric, label=metric)\n    sns.lineplot(data=history, x=history.index, y=\"val_\"+metric, label=\"val_\"+metric)\n    plt.xlabel(\"epochs\")\n    plt.tick_params(labelright=True)\n    plt.legend()\n    plt.grid()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.518980Z","iopub.execute_input":"2022-03-09T19:41:35.520100Z","iopub.status.idle":"2022-03-09T19:41:35.529927Z","shell.execute_reply.started":"2022-03-09T19:41:35.520054Z","shell.execute_reply":"2022-03-09T19:41:35.528659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***\n# 1 Data loading","metadata":{}},{"cell_type":"code","source":"KAGGLE_BASE_PATH = \"/kaggle/input/birdclef-2022/\"","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.533109Z","iopub.execute_input":"2022-03-09T19:41:35.534820Z","iopub.status.idle":"2022-03-09T19:41:35.655851Z","shell.execute_reply.started":"2022-03-09T19:41:35.534770Z","shell.execute_reply":"2022-03-09T19:41:35.654682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = KAGGLE_BASE_PATH","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.659560Z","iopub.execute_input":"2022-03-09T19:41:35.661054Z","iopub.status.idle":"2022-03-09T19:41:35.668023Z","shell.execute_reply.started":"2022-03-09T19:41:35.660975Z","shell.execute_reply":"2022-03-09T19:41:35.666860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.1 Train data","metadata":{}},{"cell_type":"code","source":"data_train = pd.read_csv(BASE_PATH + \"train_metadata.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.669918Z","iopub.execute_input":"2022-03-09T19:41:35.671073Z","iopub.status.idle":"2022-03-09T19:41:35.742842Z","shell.execute_reply.started":"2022-03-09T19:41:35.671026Z","shell.execute_reply":"2022-03-09T19:41:35.741962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.747522Z","iopub.execute_input":"2022-03-09T19:41:35.747796Z","iopub.status.idle":"2022-03-09T19:41:35.779476Z","shell.execute_reply.started":"2022-03-09T19:41:35.747766Z","shell.execute_reply":"2022-03-09T19:41:35.778454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.781105Z","iopub.execute_input":"2022-03-09T19:41:35.781684Z","iopub.status.idle":"2022-03-09T19:41:35.809296Z","shell.execute_reply.started":"2022-03-09T19:41:35.781627Z","shell.execute_reply":"2022-03-09T19:41:35.808338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.drop(columns=[\"type\", \"scientific_name\", \"common_name\", \"license\", \"url\"], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.810565Z","iopub.execute_input":"2022-03-09T19:41:35.811165Z","iopub.status.idle":"2022-03-09T19:41:35.819528Z","shell.execute_reply.started":"2022-03-09T19:41:35.811121Z","shell.execute_reply":"2022-03-09T19:41:35.818413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data_train[\"time\"] = pd.to_datetime(data_train[\"time\"])","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.821079Z","iopub.execute_input":"2022-03-09T19:41:35.821491Z","iopub.status.idle":"2022-03-09T19:41:35.829796Z","shell.execute_reply.started":"2022-03-09T19:41:35.821444Z","shell.execute_reply":"2022-03-09T19:41:35.828668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.833150Z","iopub.execute_input":"2022-03-09T19:41:35.833675Z","iopub.status.idle":"2022-03-09T19:41:35.859807Z","shell.execute_reply.started":"2022-03-09T19:41:35.833629Z","shell.execute_reply":"2022-03-09T19:41:35.858518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.861403Z","iopub.execute_input":"2022-03-09T19:41:35.861788Z","iopub.status.idle":"2022-03-09T19:41:35.879482Z","shell.execute_reply.started":"2022-03-09T19:41:35.861708Z","shell.execute_reply":"2022-03-09T19:41:35.878078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***\n## 1.3 Scored birds data","metadata":{}},{"cell_type":"code","source":"scored_birds = pd.read_json(BASE_PATH + \"scored_birds.json\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.881547Z","iopub.execute_input":"2022-03-09T19:41:35.882922Z","iopub.status.idle":"2022-03-09T19:41:35.893254Z","shell.execute_reply.started":"2022-03-09T19:41:35.882873Z","shell.execute_reply":"2022-03-09T19:41:35.892008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scored_birds.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.895344Z","iopub.execute_input":"2022-03-09T19:41:35.895827Z","iopub.status.idle":"2022-03-09T19:41:35.910855Z","shell.execute_reply.started":"2022-03-09T19:41:35.895768Z","shell.execute_reply":"2022-03-09T19:41:35.909265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scored_birds.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.912364Z","iopub.execute_input":"2022-03-09T19:41:35.912931Z","iopub.status.idle":"2022-03-09T19:41:35.923452Z","shell.execute_reply.started":"2022-03-09T19:41:35.912877Z","shell.execute_reply":"2022-03-09T19:41:35.922112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LABELS = scored_birds.iloc[:,0].to_list()\nLABELS","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.925779Z","iopub.execute_input":"2022-03-09T19:41:35.926641Z","iopub.status.idle":"2022-03-09T19:41:35.936913Z","shell.execute_reply.started":"2022-03-09T19:41:35.926506Z","shell.execute_reply":"2022-03-09T19:41:35.935501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***\n# 2 Data exploration","metadata":{}},{"cell_type":"code","source":"data_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.939485Z","iopub.execute_input":"2022-03-09T19:41:35.939838Z","iopub.status.idle":"2022-03-09T19:41:35.963976Z","shell.execute_reply.started":"2022-03-09T19:41:35.939800Z","shell.execute_reply":"2022-03-09T19:41:35.963002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.965264Z","iopub.execute_input":"2022-03-09T19:41:35.966269Z","iopub.status.idle":"2022-03-09T19:41:35.992640Z","shell.execute_reply.started":"2022-03-09T19:41:35.966222Z","shell.execute_reply":"2022-03-09T19:41:35.991539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train[\"rating\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:35.993939Z","iopub.execute_input":"2022-03-09T19:41:35.994494Z","iopub.status.idle":"2022-03-09T19:41:36.004378Z","shell.execute_reply.started":"2022-03-09T19:41:35.994446Z","shell.execute_reply":"2022-03-09T19:41:36.003000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = data_train[(data_train[\"rating\"] >= 1.0) & (data_train[\"rating\"] <= 5.0)]","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.006445Z","iopub.execute_input":"2022-03-09T19:41:36.007252Z","iopub.status.idle":"2022-03-09T19:41:36.017790Z","shell.execute_reply.started":"2022-03-09T19:41:36.007205Z","shell.execute_reply":"2022-03-09T19:41:36.016549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.019998Z","iopub.execute_input":"2022-03-09T19:41:36.020765Z","iopub.status.idle":"2022-03-09T19:41:36.041754Z","shell.execute_reply.started":"2022-03-09T19:41:36.020704Z","shell.execute_reply":"2022-03-09T19:41:36.040638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train[\"rating\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.043330Z","iopub.execute_input":"2022-03-09T19:41:36.043904Z","iopub.status.idle":"2022-03-09T19:41:36.056144Z","shell.execute_reply.started":"2022-03-09T19:41:36.043862Z","shell.execute_reply":"2022-03-09T19:41:36.055040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = data_train[(data_train[\"primary_label\"].isin(scored_birds.iloc[:,0].values)) | (data_train[\"secondary_labels\"].isin(scored_birds.iloc[:,0].values))]","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.057772Z","iopub.execute_input":"2022-03-09T19:41:36.058133Z","iopub.status.idle":"2022-03-09T19:41:36.068859Z","shell.execute_reply.started":"2022-03-09T19:41:36.058090Z","shell.execute_reply":"2022-03-09T19:41:36.067477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.071251Z","iopub.execute_input":"2022-03-09T19:41:36.072236Z","iopub.status.idle":"2022-03-09T19:41:36.088882Z","shell.execute_reply.started":"2022-03-09T19:41:36.072112Z","shell.execute_reply":"2022-03-09T19:41:36.087433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter_geo(\n    data_train,\n    lat=\"latitude\",\n    lon=\"longitude\",\n    color=\"primary_label\",\n    width=1000,\n    height=500,\n    title=\"Bird Distribution\",\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.097920Z","iopub.execute_input":"2022-03-09T19:41:36.098164Z","iopub.status.idle":"2022-03-09T19:41:36.491116Z","shell.execute_reply.started":"2022-03-09T19:41:36.098134Z","shell.execute_reply":"2022-03-09T19:41:36.490201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***\n# 3 Utilities setup","metadata":{}},{"cell_type":"code","source":"class AudioUtils():\n    @staticmethod\n    def open(audio_file):\n        sig, sr = torchaudio.load(audio_file)\n        return (sig, sr)\n\n    @staticmethod\n    def rechannel(audio, new_channel):\n        sig, sr = audio\n        if sig.shape[0] == new_channel:\n            return audio\n        if new_channel == 1:\n            resig = sig[:1, :]\n        else:\n            resig = torch.cat([sig, sig])\n        return resig, sr\n\n    @staticmethod\n    def resample(audio, newsr):\n        sig, sr = audio\n        if sr == newsr:\n            return audio\n        num_channels = sig.shape[0]\n        resig = torchaudio.transforms.Resample(sr, newsr)(sig[:1, :])\n        if num_channels > 1:\n            retwo = torchaudio.transforms.Resample(sr, newsr)(sig[1:, :])\n            resig = torch.cat([resig, retwo])\n        return resig, newsr\n\n    @staticmethod\n    def pad_trunc(audio, max_ms):\n        sig, sr = audio\n        num_rows, sig_len = sig.shape\n        max_len = sr//1000 * max_ms\n        if sig_len > max_len:\n            sig = sig[:, :max_len]\n        elif sig_len < max_len:\n            pad_begin_len = random.randint(0, max_len - sig_len)\n            pad_end_len  =max_len - sig_len - pad_begin_len\n            pad_begin = torch.zeros((num_rows, pad_begin_len))\n            pad_end = torch.zeros((num_rows, pad_end_len))\n            sig = torch.cat((pad_begin, sig, pad_end), 1)\n        return sig, sr\n\n    @staticmethod\n    def time_shift(audio, shift_limit):\n        sig, sr = audio\n        _, sig_len = sig.shape\n        shift_amt = int(random.random() * shift_limit * sig_len)\n        return (sig.roll(shift_amt), sr)\n\n    @staticmethod\n    def spectrogram(audio, n_mels=64, n_fft=1024, hop_len=None):\n        sig, sr = audio\n        top_db = 80\n        spec = transforms.MelSpectrogram(sr, n_fft=n_fft, hop_length=hop_len, n_mels=n_mels)(sig)\n        spec = transforms.AmplitudeToDB(top_db=top_db)(spec)\n        return spec\n\n    @staticmethod\n    def spectrogram_augment(spectrogram, max_mask_pct=0.1, n_freq_maks=1, n_time_masks=1):\n        _, n_mels, n_steps = spectrogram.shape\n        mask_value = spectrogram.mean()\n        aug_spec = spectrogram\n        freq_mask_param = max_mask_pct * n_mels\n        for _ in range(n_freq_maks):\n            aug_spec = transforms.FrequencyMasking(freq_mask_param)(aug_spec, mask_value)\n        time_mask_param = max_mask_pct * n_steps\n        for _ in range(n_time_masks):\n            aug_spec = transforms.TimeMasking(time_mask_param)(aug_spec, mask_value)\n        return aug_spec","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.493212Z","iopub.execute_input":"2022-03-09T19:41:36.493970Z","iopub.status.idle":"2022-03-09T19:41:36.513507Z","shell.execute_reply.started":"2022-03-09T19:41:36.493903Z","shell.execute_reply":"2022-03-09T19:41:36.512422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***\n# 4 Dataset and Dataloader","metadata":{}},{"cell_type":"code","source":"class TrainSoundDataset(Dataset):\n    def __init__(self, df, data_path, label_encoder):\n        self.df = df\n        self.data_path = str(data_path)\n        self.duration = 4000 # ?\n        self.sr = 32000\n        self.channel = 1 # ?\n        self.shift_pct = 0.4\n        self.label_encoder = label_encoder\n        self.df[\"label\"] = self.label_encoder.transform(df[[\"primary_label\"]])\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, index):\n        audio_file = self.data_path + self.df[\"filename\"].iloc[index]\n        y = torch.tensor(self.df[\"label\"].iloc[index], dtype=torch.long)\n\n        audio = AudioUtils.open(audio_file)\n        re_aud = AudioUtils.resample(audio, self.sr)\n        re_chan = AudioUtils.rechannel(re_aud, self.channel)\n        dur_aud = AudioUtils.pad_trunc(re_chan, self.duration)\n        shift_aud = AudioUtils.time_shift(dur_aud, self.shift_pct)\n        sgram = AudioUtils.spectrogram(shift_aud)\n        aug_sgram = AudioUtils.spectrogram_augment(sgram)\n\n        return aug_sgram.cuda(), y.cuda()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.516289Z","iopub.execute_input":"2022-03-09T19:41:36.517199Z","iopub.status.idle":"2022-03-09T19:41:36.530839Z","shell.execute_reply.started":"2022-03-09T19:41:36.517120Z","shell.execute_reply":"2022-03-09T19:41:36.529503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder = OrdinalEncoder()\nlabel_encoder.fit(data_train[[\"primary_label\"]])","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.532274Z","iopub.execute_input":"2022-03-09T19:41:36.532874Z","iopub.status.idle":"2022-03-09T19:41:36.550534Z","shell.execute_reply.started":"2022-03-09T19:41:36.532798Z","shell.execute_reply":"2022-03-09T19:41:36.549199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 16\nLABELS_COUNT = scored_birds.value_counts().count()\nTRAIN_PATH = BASE_PATH + \"train_audio/\"","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.552543Z","iopub.execute_input":"2022-03-09T19:41:36.552982Z","iopub.status.idle":"2022-03-09T19:41:36.562671Z","shell.execute_reply.started":"2022-03-09T19:41:36.552934Z","shell.execute_reply":"2022-03-09T19:41:36.561641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.564378Z","iopub.execute_input":"2022-03-09T19:41:36.564843Z","iopub.status.idle":"2022-03-09T19:41:36.583015Z","shell.execute_reply.started":"2022-03-09T19:41:36.564796Z","shell.execute_reply":"2022-03-09T19:41:36.581910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.584779Z","iopub.execute_input":"2022-03-09T19:41:36.585120Z","iopub.status.idle":"2022-03-09T19:41:36.601771Z","shell.execute_reply.started":"2022-03-09T19:41:36.585077Z","shell.execute_reply":"2022-03-09T19:41:36.600418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data_train = data_train[:200]","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.604252Z","iopub.execute_input":"2022-03-09T19:41:36.604918Z","iopub.status.idle":"2022-03-09T19:41:36.610656Z","shell.execute_reply.started":"2022-03-09T19:41:36.604874Z","shell.execute_reply":"2022-03-09T19:41:36.609272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(data_train.loc[:,\"secondary_labels\":\"filename\"], data_train[\"primary_label\"], test_size=0.2)\ndata_train_split = X_train.join(y_train)\ndata_val_split = X_val.join(y_val)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.613308Z","iopub.execute_input":"2022-03-09T19:41:36.613763Z","iopub.status.idle":"2022-03-09T19:41:36.634118Z","shell.execute_reply.started":"2022-03-09T19:41:36.613699Z","shell.execute_reply":"2022-03-09T19:41:36.633132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = TrainSoundDataset(data_train_split, TRAIN_PATH, label_encoder)\nval_ds = TrainSoundDataset(data_val_split, TRAIN_PATH, label_encoder)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.635898Z","iopub.execute_input":"2022-03-09T19:41:36.636286Z","iopub.status.idle":"2022-03-09T19:41:36.652206Z","shell.execute_reply.started":"2022-03-09T19:41:36.636242Z","shell.execute_reply":"2022-03-09T19:41:36.651180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl = DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True)\nval_dl = DataLoader(val_ds, batch_size=BATCH_SIZE, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.653738Z","iopub.execute_input":"2022-03-09T19:41:36.654738Z","iopub.status.idle":"2022-03-09T19:41:36.661114Z","shell.execute_reply.started":"2022-03-09T19:41:36.654686Z","shell.execute_reply":"2022-03-09T19:41:36.659874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds[0]","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:36.662757Z","iopub.execute_input":"2022-03-09T19:41:36.663666Z","iopub.status.idle":"2022-03-09T19:41:39.060142Z","shell.execute_reply.started":"2022-03-09T19:41:36.663619Z","shell.execute_reply":"2022-03-09T19:41:39.058988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,_ in enumerate(train_ds):\n    print(train_ds[i][0][0].shape)\n    if i > 20:\n        break","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:39.062152Z","iopub.execute_input":"2022-03-09T19:41:39.062881Z","iopub.status.idle":"2022-03-09T19:41:42.120888Z","shell.execute_reply.started":"2022-03-09T19:41:39.062819Z","shell.execute_reply":"2022-03-09T19:41:42.119883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds[0]","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:42.122695Z","iopub.execute_input":"2022-03-09T19:41:42.123103Z","iopub.status.idle":"2022-03-09T19:41:42.260181Z","shell.execute_reply.started":"2022-03-09T19:41:42.123060Z","shell.execute_reply":"2022-03-09T19:41:42.259188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_ds[0]","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:42.261762Z","iopub.execute_input":"2022-03-09T19:41:42.262202Z","iopub.status.idle":"2022-03-09T19:41:42.267167Z","shell.execute_reply.started":"2022-03-09T19:41:42.262155Z","shell.execute_reply":"2022-03-09T19:41:42.265830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***\n# 5 Neural Network","metadata":{}},{"cell_type":"code","source":"\nclass AudioClassifier(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.vgg16 = vgg16_bn()\n        self.vgg16.load_state_dict(torch.load(\"../input/vgg16weight/vgg16_bn-6c64b313.pth\"))\n        print(self.vgg16.classifier[6].out_features)\n        self.vgg16.features[0] = nn.Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n        for param in self.vgg16.features.parameters():\n            param.require_grad = False\n        num_features = self.vgg16.classifier[6].in_features\n        features = list(self.vgg16.classifier.children())[:-1]\n        features.extend([nn.Linear(num_features, len(LABELS))])\n        self.vgg16.classifier = nn.Sequential(*features)\n        print(self.vgg16)\n\n    def forward(self, x):\n        x = self.vgg16(x)\n        return x\n\nmodel = AudioClassifier()\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\nmodel = model.cuda()\nnext(model.parameters()).device\n\n#print(model.effnet)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:42.269419Z","iopub.execute_input":"2022-03-09T19:41:42.270020Z","iopub.status.idle":"2022-03-09T19:41:45.209839Z","shell.execute_reply.started":"2022-03-09T19:41:42.269971Z","shell.execute_reply":"2022-03-09T19:41:45.208855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***\n# 6 Training","metadata":{}},{"cell_type":"code","source":"NUM_EPOCHS = 5","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:45.211563Z","iopub.execute_input":"2022-03-09T19:41:45.212612Z","iopub.status.idle":"2022-03-09T19:41:45.217643Z","shell.execute_reply.started":"2022-03-09T19:41:45.212567Z","shell.execute_reply":"2022-03-09T19:41:45.216513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scored_birds.iloc[:,0].count()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:45.219363Z","iopub.execute_input":"2022-03-09T19:41:45.220525Z","iopub.status.idle":"2022-03-09T19:41:45.233330Z","shell.execute_reply.started":"2022-03-09T19:41:45.220479Z","shell.execute_reply":"2022-03-09T19:41:45.232122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = torch.optim.Adam(model.parameters(), lr=5e-5)\n#optimizer = torch.optim.Adam(model.parameters())\nloss_fn = nn.CrossEntropyLoss()\n\ntrainer = create_supervised_trainer(model, optimizer, loss_fn, device=device)\n\nval_metrics = {\n    \"accuracy\": Accuracy(),\n    \"loss\": Loss(loss_fn),\n    \"conf_matrix\": ConfusionMatrix(num_classes=LABELS_COUNT)\n}\nevaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device)\n\ntraining_history = {\"accuracy\": [], \"loss\": []}\nvalidation_history = {\"accuracy\": [], \"loss\": []}\nlast_epoch = []\n\nRunningAverage(output_transform=lambda x: x).attach(trainer, 'loss')\n\ndef score_function(engine):\n    val_loss = engine.state.metrics[\"loss\"]\n    return -val_loss\n\nearly_stop_handler = EarlyStopping(patience=10, score_function=score_function, trainer=trainer)\nevaluator.add_event_handler(Events.COMPLETED, early_stop_handler)\n\nProgressBar(persist=True).attach(trainer, [\"loss\"])\nProgressBar(persist=False).attach(evaluator, [\"loss\"])\n\n@trainer.on(Events.EPOCH_COMPLETED)\ndef log_training_results(trainer):\n    evaluator.run(train_dl)\n    metrics = evaluator.state.metrics\n    accuracy = metrics['accuracy']*100\n    loss = metrics['loss']\n    last_epoch.append(0)\n    training_history['accuracy'].append(accuracy)\n    training_history['loss'].append(loss)\n    print(\"Training Results - Epoch: {}  Avg accuracy: {:.2f} Avg loss: {:.2f}\"\n          .format(trainer.state.epoch, accuracy, loss))\n\ndef log_validation_results(trainer):\n    evaluator.run(val_dl)\n    metrics = evaluator.state.metrics\n    accuracy = metrics['accuracy']*100\n    loss = metrics['loss']\n    validation_history['accuracy'].append(accuracy)\n    validation_history['loss'].append(loss)\n    print(\"Validation Results - Epoch: {}  Avg accuracy: {:.2f} Avg loss: {:.2f}\"\n          .format(trainer.state.epoch, accuracy, loss))\n\ntrainer.add_event_handler(Events.EPOCH_COMPLETED, log_validation_results)\n\n@trainer.on(Events.COMPLETED)\ndef log_confusion_matrix(trainer):\n    evaluator.run(val_dl)\n    metrics = evaluator.state.metrics\n    cm = metrics['conf_matrix']\n    cm = cm.numpy()\n    cm = cm.astype(int)\n    classes = scored_birds.iloc[:,0].values\n    fig, ax = plt.subplots(figsize=(10,10))  \n    ax= plt.subplot()\n    sns.heatmap(cm, annot=True, ax = ax,fmt=\"d\")\n    ax.set_xlabel('Predicted labels')\n    ax.set_ylabel('True labels') \n    ax.set_title('Confusion Matrix') \n    ax.xaxis.set_ticklabels(classes,rotation=90)\n    ax.yaxis.set_ticklabels(classes,rotation=0)\n\ncheckpoint_handler = ModelCheckpoint(\"saved_models\", \"birdclef\", n_saved=2, create_dir=True, save_as_state_dict=True, require_empty=False)\ntrainer.add_event_handler(Events.EPOCH_COMPLETED, checkpoint_handler, {\"birdclef\": model})","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:45.235474Z","iopub.execute_input":"2022-03-09T19:41:45.235925Z","iopub.status.idle":"2022-03-09T19:41:45.480428Z","shell.execute_reply.started":"2022-03-09T19:41:45.235879Z","shell.execute_reply":"2022-03-09T19:41:45.479270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.run(train_dl, max_epochs=NUM_EPOCHS)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:41:45.482587Z","iopub.execute_input":"2022-03-09T19:41:45.483013Z","iopub.status.idle":"2022-03-09T19:59:12.947820Z","shell.execute_reply.started":"2022-03-09T19:41:45.482969Z","shell.execute_reply":"2022-03-09T19:59:12.946872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(training_history['loss'],label=\"Training Loss\")\nplt.plot(validation_history['loss'],label=\"Validation Loss\")\nplt.xlabel('No. of Epochs')\nplt.ylabel('Loss')\nplt.legend(frameon=False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:12.949522Z","iopub.execute_input":"2022-03-09T19:59:12.950137Z","iopub.status.idle":"2022-03-09T19:59:13.188288Z","shell.execute_reply.started":"2022-03-09T19:59:12.950077Z","shell.execute_reply":"2022-03-09T19:59:13.187265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(training_history['accuracy'],label=\"Training Accuracy\")\nplt.plot(validation_history['accuracy'],label=\"Validation Accuracy\")\nplt.xlabel('No. of Epochs')\nplt.ylabel('Accuracy')\nplt.legend(frameon=False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:13.189696Z","iopub.execute_input":"2022-03-09T19:59:13.191623Z","iopub.status.idle":"2022-03-09T19:59:13.440220Z","shell.execute_reply.started":"2022-03-09T19:59:13.191578Z","shell.execute_reply":"2022-03-09T19:59:13.439196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fetch_last_checkpoint_model_filename(model_save_path):\n    import os\n    from pathlib import Path\n    checkpoint_files = os.listdir(model_save_path)\n    checkpoint_files = [f for f in checkpoint_files if '.pt' in f]\n    checkpoint_iter = [\n        int(x.split('_')[2].split('.')[0])\n        for x in checkpoint_files]\n    last_idx = np.array(checkpoint_iter).argmax()\n    return Path(model_save_path) / checkpoint_files[last_idx]\n\n#model.load_state_dict(torch.load(fetch_last_checkpoint_model_filename('./saved_models')))\n#print(\"Model Loaded\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:13.441597Z","iopub.execute_input":"2022-03-09T19:59:13.442257Z","iopub.status.idle":"2022-03-09T19:59:13.451718Z","shell.execute_reply.started":"2022-03-09T19:59:13.442161Z","shell.execute_reply":"2022-03-09T19:59:13.450355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***\n# 7 Inference","metadata":{}},{"cell_type":"code","source":"class TestDataset(Dataset):\n    def __init__(self, data_dir, meta_df, transform = None):\n        super(TestDataset, self).__init__()\n        self.data_dir = data_dir\n        self.meta_df = meta_df\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.meta_df)\n\n    def __getitem__(self, index):\n        path = self.meta_df.loc[index, \"file_id\"]\n        path = f\"{os.path.join(self.data_dir, path)}.ogg\"\n        time = self.meta_df.loc[index, \"end_time\"]\n        mono_audio = self.load_audio(path, time)\n        mono_audio = mono_audio.unsqueeze(dim=0)\n        return mono_audio\n\n    def load_audio(self, path, time):\n        audio, sample_rate = torchaudio.load(path)\n        audio = audio[:, (time-5)*sample_rate: time*sample_rate]\n        if self.transform != None:\n            for aug in self.transform:\n                audio = aug(audio)\n        return audio[0,:]","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:13.453634Z","iopub.execute_input":"2022-03-09T19:59:13.453978Z","iopub.status.idle":"2022-03-09T19:59:13.467843Z","shell.execute_reply.started":"2022-03-09T19:59:13.453930Z","shell.execute_reply":"2022-03-09T19:59:13.466228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"augm = [\n    MelSpectrogram(n_mels = 128),\n    Resize((128, 128))\n]\naugm","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:13.469880Z","iopub.execute_input":"2022-03-09T19:59:13.470344Z","iopub.status.idle":"2022-03-09T19:59:13.487150Z","shell.execute_reply.started":"2022-03-09T19:59:13.470298Z","shell.execute_reply":"2022-03-09T19:59:13.485507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_PATH = BASE_PATH + \"test_soundscapes/\"\nCSV_TEST_PATH = \"../input/birdclef-2022/test.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:13.488961Z","iopub.execute_input":"2022-03-09T19:59:13.489576Z","iopub.status.idle":"2022-03-09T19:59:13.495164Z","shell.execute_reply.started":"2022-03-09T19:59:13.489531Z","shell.execute_reply":"2022-03-09T19:59:13.493714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(CSV_TEST_PATH)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:13.497220Z","iopub.execute_input":"2022-03-09T19:59:13.497977Z","iopub.status.idle":"2022-03-09T19:59:13.520196Z","shell.execute_reply.started":"2022-03-09T19:59:13.497929Z","shell.execute_reply":"2022-03-09T19:59:13.519219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = TestDataset(TEST_PATH, test_df, transform = augm)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:13.521920Z","iopub.execute_input":"2022-03-09T19:59:13.522279Z","iopub.status.idle":"2022-03-09T19:59:13.528570Z","shell.execute_reply.started":"2022-03-09T19:59:13.522207Z","shell.execute_reply":"2022-03-09T19:59:13.526815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test_df.copy()\ntest[\"target\"] = [False for _ in range(len(test))]\nimp_features = [\"row_id\", \"target\"]\ntest = test[imp_features]\ntest.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:13.530671Z","iopub.execute_input":"2022-03-09T19:59:13.531778Z","iopub.status.idle":"2022-03-09T19:59:13.545698Z","shell.execute_reply.started":"2022-03-09T19:59:13.531731Z","shell.execute_reply":"2022-03-09T19:59:13.544464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 16\ntest_dl = DataLoader(test_dataset, batch_size = BATCH_SIZE, shuffle = False)\nprediction = []\nwith torch.no_grad():\n    for index, patch in enumerate(test_dl):\n        dev_patch = patch.to(device)\n        output = model(dev_patch)\n        output = torch.argmax(output, dim=1).tolist()\n        prediction += output\n\ntest_df[\"target\"] = prediction\ntest_df[\"target\"] = test_df[\"target\"].apply(lambda x : class_labels[str(x)])\ntest_df[\"target\"] = test_df[\"bird\"] == test_df[\"target\"]\nimp_features = [\"row_id\", \"target\"]\ntest_df = test_df[imp_features]\ntest_df.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:13.548130Z","iopub.execute_input":"2022-03-09T19:59:13.548447Z","iopub.status.idle":"2022-03-09T19:59:14.187114Z","shell.execute_reply.started":"2022-03-09T19:59:13.548377Z","shell.execute_reply":"2022-03-09T19:59:14.185428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nfile_list = [f.split('.')[0] for f in sorted(os.listdir(TEST_PATH))]\nprint('Number of test soundscapes:', len(file_list))\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:14.188871Z","iopub.status.idle":"2022-03-09T19:59:14.189747Z","shell.execute_reply.started":"2022-03-09T19:59:14.189431Z","shell.execute_reply":"2022-03-09T19:59:14.189460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\npred = {\n  \"row_id\": [],\n  \"target\": [],\n  \"true_label\": [],\n  \"pred_label\": []\n}\n\nfiles = [f.split('.')[0] for f in sorted(os.listdir(TEST_PATH))]\nSCORED_BIRDS_PATH = BASE_PATH + \"scored_birds.json\"\n\nwith open(SCORED_BIRDS_PATH) as bf:\n    birds = pd.read_json(bf)\n    birds = birds.iloc[:, 0].to_list()\n\nfor file in files:\n    path = TEST_PATH + file +\".ogg\"\n\n    sig, sr = AudioUtils().open(path)\n    duration = len(sig[0]) / sr\n    chunks_nb = math.floor(duration / 5)\n    segments = [[] for i in range(chunks_nb)]\n\n    for i in range(len(segments)):\n        segment_end = (i+1)*5\n        audio_segment = sig[0][i*5*sr:(i+1)*5*sr].cpu().detach().numpy()\n        audio_segment = np.array([audio_segment])\n        audio_segment = torch.from_numpy(audio_segment)\n\n        for bird in birds:\n            re_aud = AudioUtils.resample((audio_segment, sr), sr)\n            re_chan = AudioUtils.rechannel(re_aud, 1)\n            dur_aud = AudioUtils.pad_trunc(re_chan, 4000)\n            spectro = AudioUtils().spectrogram(dur_aud)\n\n            spectro = spectro.cpu().detach().numpy()\n            spectro = np.array([spectro])\n            spectro = torch.from_numpy(spectro)\n\n            bird_label = label_encoder.transform([[bird]])\n            bird_label = torch.tensor(bird_label, dtype=torch.long)\n\n            with torch.no_grad():\n                output = model(spectro.cuda())\n\n            output = torch.argmax(output, dim=1).cpu().tolist()\n\n            pred_label = birds[output[0]]\n            target = bird == pred_label\n\n            row_id = file + '_' + bird + '_' + str(segment_end)\n\n            pred[\"row_id\"].append(row_id)\n            pred[\"target\"].append(target)\n            pred[\"true_label\"].append(bird)\n            pred[\"pred_label\"].append(pred_label)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:14.191426Z","iopub.status.idle":"2022-03-09T19:59:14.192246Z","shell.execute_reply.started":"2022-03-09T19:59:14.191962Z","shell.execute_reply":"2022-03-09T19:59:14.191993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nsubmission_enhanced = pd.DataFrame(data=pred)\nsubmission_enhanced[:40]\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:14.193932Z","iopub.status.idle":"2022-03-09T19:59:14.194765Z","shell.execute_reply.started":"2022-03-09T19:59:14.194459Z","shell.execute_reply":"2022-03-09T19:59:14.194489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nsubmission = submission_enhanced[[\"row_id\", \"target\"]]\nsubmission\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:14.196433Z","iopub.status.idle":"2022-03-09T19:59:14.197246Z","shell.execute_reply.started":"2022-03-09T19:59:14.196967Z","shell.execute_reply":"2022-03-09T19:59:14.196995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:14.198764Z","iopub.status.idle":"2022-03-09T19:59:14.199687Z","shell.execute_reply.started":"2022-03-09T19:59:14.199298Z","shell.execute_reply":"2022-03-09T19:59:14.199365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission.isna().any()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:14.201498Z","iopub.status.idle":"2022-03-09T19:59:14.202461Z","shell.execute_reply.started":"2022-03-09T19:59:14.202125Z","shell.execute_reply":"2022-03-09T19:59:14.202154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nsample_submission = pd.read_csv(BASE_PATH + \"sample_submission.csv\")\nsample_submission.head()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:14.204223Z","iopub.status.idle":"2022-03-09T19:59:14.205092Z","shell.execute_reply.started":"2022-03-09T19:59:14.204797Z","shell.execute_reply":"2022-03-09T19:59:14.204826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:14.206727Z","iopub.status.idle":"2022-03-09T19:59:14.207566Z","shell.execute_reply.started":"2022-03-09T19:59:14.207244Z","shell.execute_reply":"2022-03-09T19:59:14.207287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample_submission.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:59:14.209112Z","iopub.status.idle":"2022-03-09T19:59:14.210002Z","shell.execute_reply.started":"2022-03-09T19:59:14.209686Z","shell.execute_reply":"2022-03-09T19:59:14.209717Z"},"trusted":true},"execution_count":null,"outputs":[]}]}