{"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":"!pip install torchlibrosa audiomentations torchaudio","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-28T07:09:25.990981Z","iopub.execute_input":"2023-05-28T07:09:25.991941Z","iopub.status.idle":"2023-05-28T07:09:43.306105Z","shell.execute_reply.started":"2023-05-28T07:09:25.991895Z","shell.execute_reply":"2023-05-28T07:09:43.304208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os,sys,re,glob,random\nimport pandas as pd\nfrom numpy.lib.stride_tricks import sliding_window_view\nimport librosa as lb\nimport IPython.display as ipd\nimport soundfile as sf\nimport numpy as np\nimport ast, joblib, pickle\nfrom pathlib import Path\n\nimport librosa.display\nfrom sklearn import preprocessing\n\n#Deep learning from pytorch\nimport torch, torchaudio\nimport torchvision\nfrom torch.utils.data import DataLoader, Dataset\nimport torch.optim as optim\nfrom torchvision import transforms\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom timm.scheduler import CosineLRScheduler\n\nfrom albumentations.core.transforms_interface import ImageOnlyTransform\nfrom torchlibrosa.stft import LogmelFilterBank, Spectrogram\nfrom torchlibrosa.augmentation import SpecAugmentation\nfrom tqdm import tqdm\nfrom torch.nn.parameter import Parameter\nimport copy, codecs\nimport sklearn.metrics\n\nimport audiomentations as AA\n\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:09:43.309234Z","iopub.execute_input":"2023-05-28T07:09:43.309685Z","iopub.status.idle":"2023-05-28T07:09:56.173497Z","shell.execute_reply.started":"2023-05-28T07:09:43.309635Z","shell.execute_reply":"2023-05-28T07:09:56.172254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_aug = AA.Compose(\n    [\n        AA.AddBackgroundNoise(\n            sounds_path=\"/kaggle/input/birdclef2021-background-noise/ff1010bird_nocall/nocall\", min_snr_in_db=5, max_snr_in_db=10, p=0.5\n        ),\n        AA.AddBackgroundNoise(\n            sounds_path=\"/kaggle/input/birdclef2021-background-noise/train_soundscapes/nocall\", min_snr_in_db=5, max_snr_in_db=10, p=0.5\n        ),\n        AA.AddBackgroundNoise(\n            sounds_path=\"/kaggle/input/birdclef2021-background-noise/aicrowd2020_noise_30sec/noise_30sec\",\n            min_snr_in_db=5,\n            max_snr_in_db=10,\n            p=0.75,\n        ),\n        AA.AddBackgroundNoise(\n            sounds_path=\"/kaggle/input/birdclef2023esc50-sample/useesc50\",\n            min_snr_in_db=5,\n            max_snr_in_db=10,\n            p=0.75,\n        ),\n        AA.AddGaussianSNR(\n            min_snr_in_db=5,max_snr_in_db=10.0,p=0.25\n        )\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:09:56.175266Z","iopub.execute_input":"2023-05-28T07:09:56.175712Z","iopub.status.idle":"2023-05-28T07:10:21.357021Z","shell.execute_reply.started":"2023-05-28T07:09:56.175665Z","shell.execute_reply":"2023-05-28T07:10:21.355899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class WaveformDataset(Dataset):\n    def __init__(self,\n                 CFG,\n                 df: pd.DataFrame,\n                 period = 5,\n                 prilabelp=1.0,\n                 seclabelp=0.5,\n                 mixup_prob = 0.15,\n                 smooth=0.005,\n                 train = True\n                 ):\n      \n        self.df = df.reset_index(drop=True)\n        self.df[\"mixup_weight\"] = self.df[\"sample_weight\"]/self.df[\"sample_weight\"].sum()\n        #self.label_weight = self.df.set_index(\"label_id\").[\"sample_weight\"]\n        self.CFG = CFG\n        self.aug = train_aug\n        self.sr = CFG.sr\n        self.period = period\n        self.df[\"sort_index\"] = self.df.index\n        self.smooth = smooth\n        self.prilabelp = prilabelp - smooth\n        self.seclabelp = seclabelp - smooth\n        self.train = train\n\n        #Matrix Factorization (サブラベル同士は相関なしとして扱う)\n        self.mfdf = self.df[(self.df.sec_num > 0)][[\"label_id\",\"labels_id\"]].explode(\"labels_id\").reset_index(drop=True)\n        \n        #mixupするlabel_idリストを作成する\n        self.mixup_idlist = self.mfdf.groupby(\"label_id\").labels_id.apply(list).to_dict()\n        \n        #mixupする先はシングルラベルにする\n        sdf = self.df[(self.df.sec_num==0)|(self.df.primary_label==\"lotcor1\")]\n        \n        #label_idリストからレコード番号を取得し、レコード番号からランダムサンプリングする\n        self.id2record = sdf.groupby(\"label_id\").sort_index.apply(list)\n\n        boxdf = pd.read_csv(\"/kaggle/input/sodaugresult/box.csv\",index_col=0).reset_index().rename(columns={\"index\":\"unique_id\"})\n        boxdf[\"filename_id\"] = boxdf[\"unique_id\"].apply(lambda x: x.split(\"_\")[0])\n        boxdf[\"time_id\"] = boxdf[\"unique_id\"].apply(lambda x: x.split(\"_\")[1])\n        boxdf[\"box_id\"] = boxdf[\"unique_id\"].apply(lambda x: x.split(\"_\")[2])\n        boxdf[\"start_x\"] = boxdf[\"time_id\"].astype(int)*500\n        boxdf[\"x1\"] = boxdf[\"x1\"] + boxdf[\"start_x\"]\n        boxdf[\"y1\"] = boxdf[\"y1\"]\n        boxdf[\"x2\"] = boxdf[\"x2\"] + boxdf[\"start_x\"]\n        boxdf[\"y2\"] = boxdf[\"y2\"]\n\n        # Group the DataFrame by 'filename_id'\n        grouped = boxdf.groupby('filename_id')\n\n        # Create a dictionary where the keys are the filename_ids and the values are the corresponding sub-dataframes (gdf)\n        self.gdf_dict = {name: group for name, group in grouped}\n        \n        self.mask_size = 8\n        \n    def crop_or_pad(self, y, length, is_train=False, start=None):\n        if len(y) < length//2:\n            if is_train:\n                wid = length//2 - len(y)\n                start = np.random.randint(length//2, length//2 + wid)\n                y_cp = np.zeros(length,dtype=np.float32)\n                y_cp[start : start + len(y)] = y\n                y = y_cp\n            else:\n                y = np.concatenate([y, np.zeros(length - len(y))])\n        elif len(y) < length:\n            y = np.concatenate([y, np.zeros(length - len(y))])\n\n        elif len(y) > length:\n            if not is_train:\n                start = start or 0\n            else:\n                start = start or np.random.randint(len(y) - length)\n\n            y = y[start:start + length]\n\n        return y\n\n    def get_offset(self, row):\n        #準備\n        if row.filename_id in self.gdf_dict:\n            gdf = self.gdf_dict[row.filename_id]\n            mask = np.zeros((128, gdf.start_x.max() + 500),dtype=np.float64)\n            for jdx, brow in gdf.iterrows():\n                mask[int(brow.y1):int(brow.y2),int(brow.x1):int(brow.x2)] += brow.conf\n            if mask.shape[1] > self.period*100:\n                sampling_weights = sliding_window_view(mask.max(axis=0), self.period*100).sum(axis=1)[::100]\n                sample_prob = sampling_weights/sampling_weights.sum()\n            else:\n                sample_prob = None\n            mask_prob = sliding_window_view(mask.sum(axis=1), self.mask_size).sum(axis=1)\n            mask_prob = 1/(mask_prob + 1e-5)\n            mask_prob = mask_prob/mask_prob.sum()\n        else:\n            sample_prob = None\n            mask_prob = None\n\n        #periodより長いか判定する\n        duration_seconds = librosa.get_duration(filename=row.audio_paths,sr=None)\n        if duration_seconds < self.period:\n            #offsetを変えても情報が全て入っているので、0として扱う\n            offset = 0\n        else:\n            if sample_prob is not None:\n                offset = np.random.choice(len(sample_prob),p=sample_prob)\n            else:\n                offset = random.uniform(0, duration_seconds - self.period)\n        \n        mask_freq_array = np.ones((self.CFG.n_mel, self.period*100),dtype=bool)\n            \n        #for mask_offset in mask_offsets:\n        if mask_prob is not None:\n            mask_offsets = np.random.choice(len(mask_prob), size=2, p=mask_prob)\n            for mask_offset in mask_offsets:\n                mask_freq_array[mask_offset:mask_offset + self.mask_size] = 0\n        else:\n            pass\n        print(offset)\n        print(mask_offset)\n        return offset, mask_freq_array\n\n    def load_audio(self, row, offset):\n        data, sr = librosa.load(row.audio_paths, sr=self.sr, offset=offset, duration=self.period, mono=True)\n        return data\n\n    def preprocess_audio(self, data, row):\n        #augemnt1\n        if (self.train)&(random.uniform(0,1) < row.weight):\n             data = self.aug(samples=data, sample_rate=self.sr)\n\n        #test datasetの最大長\n        max_sec = len(data)//self.sr\n\n        #0秒の場合は１秒として取り扱う\n        max_sec = 1 if max_sec==0 else max_sec\n        \n        data = self.crop_or_pad(data , length=self.sr*self.period, is_train=self.train)\n\n        return data\n\n    def __len__(self):\n        return len(self.df)\n\n    def get_audio(self, row):\n        offset, freqmask = self.get_offset(row)\n        data = self.load_audio(row, offset)\n        data = self.preprocess_audio(data, row)\n\n        if self.train:\n            #add train data\n#             if row.sec_num==0:\n#                 pair_idx = np.random.choice(self.id2record[row.label_id])\n#                 row_pair = self.df.iloc[pair_idx]\n#                 offset, freqmask_pair = self.get_offset(row_pair)\n#                 data_pair = self.load_audio(row_pair, offset)\n#                 data_pair = self.preprocess_audio(data_pair, row_pair)\n\n#             else:\n            offset, freqmask_pair = self.get_offset(row)\n            data_pair = self.load_audio(row, offset)\n            data_pair = self.preprocess_audio(data_pair, row)\n\n            data = np.stack([data, data_pair])\n            freqmask = np.stack([freqmask, freqmask_pair])\n        \n        labels = torch.zeros(self.CFG.CLASS_NUM, dtype=torch.float32) + self.smooth\n        if row.sec_num != 0:\n            labels[row.labels_id] = self.seclabelp\n        if row.label_id != -1:\n            labels[row.label_id] = self.prilabelp\n        \n\n        return data, labels, freqmask\n    \n    def __getitem__(self, idx):\n        row = self.df.iloc[idx]\n        audio1, label1, freqmask1 = self.get_audio(row)\n        if self.train:\n            pair_idx = np.random.choice(len(self.df), p=self.df[\"mixup_weight\"].values)\n            row2 = self.df.iloc[pair_idx]\n            audio2, label2, freqmask2 = self.get_audio(row2)\n            audio = np.stack([audio1,audio2])\n            label = np.stack([label1,label2])\n            freqmask = np.stack([freqmask1,freqmask2])\n        else:\n            audio = audio1\n            label = label1\n        weight = torch.tensor(row.weight, dtype=torch.float32)\n        audio = torch.tensor(audio, dtype=torch.float32)\n        return audio, label, weight, freqmask","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:10:21.361344Z","iopub.execute_input":"2023-05-28T07:10:21.362365Z","iopub.status.idle":"2023-05-28T07:10:21.406678Z","shell.execute_reply.started":"2023-05-28T07:10:21.362320Z","shell.execute_reply":"2023-05-28T07:10:21.405183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Collate:\n    def __init__(self,CFG):\n            self.CFG = CFG\n            self.unit = self.CFG.period*self.CFG.sr\n            self.cutoff = self.CFG.factors*self.CFG.frame\n            self.ptodb = torchaudio.transforms.AmplitudeToDB(top_db=CFG.top_db)\n            self.random_state = np.random.RandomState(42)\n            self.mixupalpha_out = CFG.mixup_alpha_out\n            self.mixupalpha_in = CFG.mixup_alpha_in\n            \n    def get_lambda(self, batch_size, mixup_alpha):\n        lams = []\n        inv_lams = []\n        for _ in range(batch_size):\n            lam = self.random_state.beta(mixup_alpha, mixup_alpha, 1)[0]\n            lams.append(lam)\n            inv_lams.append(1.0-lam)\n        return torch.tensor(lams, dtype=torch.float32), torch.tensor(inv_lams, dtype=torch.float32)\n            \n    def crop_or_pad(self, y, length, start=None,is_train=False):\n        unit_len = min(length, self.unit*3)\n        if len(y) < unit_len:\n            start = np.random.randint(0, unit_len - len(y))\n            y_unit = np.zeros(unit_len,dtype=np.float32)\n            y_unit[start : start + len(y)] = y\n            \n            n_repeats = length // unit_len\n            epsilon = length % unit_len\n            y = np.concatenate([y_unit]*n_repeats + [y_unit[:epsilon]])\n            \n        elif len(y) < length:\n            y = np.concatenate([y, np.zeros(length - len(y))])\n            \n        elif len(y) > length:\n            if not is_train:\n                start = start or 0\n            else:\n                start = start or np.random.randint(len(y) - length)\n\n            y = y[start:start + length]\n\n        return torch.tensor(y,dtype=torch.float32)\n    \n    def wavtoimg(self, wavs, time_max):\n        wavs = torch.stack([self.crop_or_pad(wav, length = time_max) for wav in wavs])\n        power = random.uniform(self.CFG.augpower_min,self.CFG.augpower_min)\n        melimg= torchaudio.transforms.MelSpectrogram(\n            n_mels = self.CFG.n_mel, \n            sample_rate= self.CFG.sr, \n            f_min = self.CFG.fmin, \n            f_max = self.CFG.fmax, \n            n_fft = self.CFG.n_fft, \n            hop_length= self.CFG.hop_len,\n            norm = None,\n            power = power,\n            mel_scale = 'htk')(wavs)\n        dbimg = self.ptodb(melimg)\n        img = (dbimg.to(torch.float32) + 80)/80\n        return img[:,:,:-1]\n    \n    def inner_mixup(self, x, x_mix, batch_size, factor):\n        perms = torch.randperm(factor).to(x.device)\n        for i, perm in enumerate(perms):\n            x_mix[:,:,i*self.CFG.frame:(i+1)*self.CFG.frame] = x[:,:,perm*self.CFG.frame:(perm+1)*self.CFG.frame]\n\n        lam1, lam2 = self.get_lambda(batch_size, self.mixupalpha_in)\n        lam1, lam2 = lam1.to(x.device), lam2.to(x.device)\n        x = lam1[:,None,None]*x + lam2[:,None,None]*x_mix\n        return x\n            \n        \n    def __call__(self, batch):\n        audios1, audios2, labels, weights = list(zip(*batch))\n        labels = torch.stack(labels)\n        weights = torch.stack(weights)\n\n        # calculate max time length of this batch\n#         time_array = np.append(\n#             np.array([len(ad) for ad in audios1]),\n#             np.array([len(ad) for ad in audios2]),\n#         axis=0)\n        \n#         print(time_array)\n        \n#         #frame Nomalization\n#         time_max = max(self.unit, time_array.max()//self.unit * self.unit)\n#         time_max = min(self.unit*self.CFG.factors, time_max)\n        \n        img1 = self.wavtoimg(audios1, self.cutoff)\n        img2 = self.wavtoimg(audios2, self.cutoff)\n        if (random.uniform(0,1) < self.CFG.mixup_in_prob1):\n            img1 = self.inner_mixup(img1, img1, len(batch), time_max//self.unit)\n            \n        if (random.uniform(0,1) < self.CFG.mixup_in_prob2):\n            img2 = self.inner_mixup(img2, img2, len(batch), time_max//self.unit)\n            \n        if (random.uniform(0,1) < self.CFG.mixup_out_prob):\n            lam1, lam2 = self.get_lambda(len(batch), self.mixupalpha_out)\n            lam1, lam2 = lam1.to(img1.device), lam2.to(img1.device)\n            imgs = lam1[:,None,None]*img1 + lam2[:,None,None]*img2\n            labels = lam1[:,None]*labels[:,0,:] + lam2[:,None]*labels[:,1,:]\n        else:\n            imgs = img1\n            labels = labels[:,0,:]\n        \n        #setting cutoff\n        imgs = imgs[:,:,:self.cutoff]\n\n        return audios1, audios2, img1, img2, imgs, labels, weights","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:10:21.409075Z","iopub.execute_input":"2023-05-28T07:10:21.409621Z","iopub.status.idle":"2023-05-28T07:10:21.443204Z","shell.execute_reply.started":"2023-05-28T07:10:21.409581Z","shell.execute_reply":"2023-05-28T07:10:21.441753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/birdclef2023-split-creating-dataframe/train.csv\")\ntrain[\"labels_id\"] = train.labels_id.apply(eval)\ntrain[\"filename\"] = train.filename.apply(lambda x: x.split(\"/\")[-1])\npdf = pd.DataFrame(glob.glob(\"/kaggle/input/birdclef-2023/train_audio/**/*.ogg\"),columns=[\"audio_paths\"])\npdf[\"filename\"] = pdf.apply(lambda x: x[\"audio_paths\"].split(\"/\")[-1],axis=1)\ntrain = pd.merge(train,pdf,on=[\"filename\"])\ntrain[\"weight\"] = 1\n\nprimary_label_counts_map = train[\"label_id\"].value_counts().to_dict()\nsecondary_label_counts_map = train[\"labels_id\"].explode().value_counts().to_dict()\ntrain[\"primary_count\"] = train[\"label_id\"].map(primary_label_counts_map)\ntrain[\"secondary_count\"] = train[\"label_id\"].map(secondary_label_counts_map).fillna(0)\ntrain[\"label_count\"] = train[\"primary_count\"] + train[\"secondary_count\"]\ntrain[\"sample_weight\"] = train[\"label_count\"]**(1/3)/train[\"label_count\"]","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:10:21.445619Z","iopub.execute_input":"2023-05-28T07:10:21.446273Z","iopub.status.idle":"2023-05-28T07:11:13.789185Z","shell.execute_reply.started":"2023-05-28T07:10:21.446226Z","shell.execute_reply":"2023-05-28T07:11:13.787553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open('/kaggle/input/birdclef2023-soundobjectdetectionanalysis/mask_array.pkl', 'rb') as f:\n#     mask_array = pickle.load(f)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:11:13.791266Z","iopub.execute_input":"2023-05-28T07:11:13.791691Z","iopub.status.idle":"2023-05-28T07:11:13.797583Z","shell.execute_reply.started":"2023-05-28T07:11:13.791640Z","shell.execute_reply":"2023-05-28T07:11:13.796163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    CLASS_NUM = 264\n    #image parameter\n    sr = 32000\n    period = 5\n    n_mel = 128\n    fmin = 50\n    fmax = 14000\n    power = 2\n    top_db = None\n    prilabelp = 1.0\n    seclabelp = 0.5\n    frame = 500\n    augpower_min = 1.9\n    augpower_max = 2.1\n    mixup_in_prob1 = 1.0\n    mixup_in_prob2 = 1.0\n    mixup_out_prob = 0.15\n    mixup_in_prob = 1.0\n    backbone_dropout = 0.2\n    backbone_droppath = 0.2\n    head_dropout = 0.2\n\n    mixup_alpha_in = 5.0\n    mixup_alpha_out = 5.0\n    sample_size = 300\n    # time_len = sr[1/s] * time[s] /hop_len = sr[1/s] * time[s] 4/n_fft \n    n_fft = 1024\n\n    hop_len = 320\n    \n    #バッチサイズ\n    batch_size = 20\n\n    #前処理CPUコア数\n    workers = 20\n\n    #学習率 (best range 5e-9~2e-4)\n    lr = 5e-3\n\n    #スケジューラーの最小学習率\n    min_lr = 1e-5\n\n    #ウォームアップステップ\n    warmupstep = 0\n\n    #エポック数\n    epochs = 40\n\n    #factor update\n    #factors = list([15,14,13,12,11,10,10,9,9,9,8,8,8,7,7,7,6,6,6]) + list([max(1, 6 - i//3) for i in range(30)])\n    factors = 6# + list([max(1, 6 - i//3) for i in range(epochs)])\n\n    batch_factor = {\n        9:1,8:1,7:1,6:1,5:2,4:2,3:3,2:3,1:3\n    }\n\n    #lr ratio (best fit 3)\n    lr_ratio = 5\n\n    #label smoothing rate\n    smooth = 0.005","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:11:13.799869Z","iopub.execute_input":"2023-05-28T07:11:13.800382Z","iopub.status.idle":"2023-05-28T07:11:13.813351Z","shell.execute_reply.started":"2023-05-28T07:11:13.800329Z","shell.execute_reply":"2023-05-28T07:11:13.812426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:26:05.759330Z","iopub.execute_input":"2023-05-28T07:26:05.760517Z","iopub.status.idle":"2023-05-28T07:26:05.770128Z","shell.execute_reply.started":"2023-05-28T07:26:05.760458Z","shell.execute_reply":"2023-05-28T07:26:05.768502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = (sum(imgs)/len(imgs))[:,:,:3000]\nplt.figure(figsize=(20,5),dpi=300)\nplt.imshow(img[0])\nplt.tight_layout()\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:26:12.286912Z","iopub.execute_input":"2023-05-28T07:26:12.287430Z","iopub.status.idle":"2023-05-28T07:26:13.907207Z","shell.execute_reply.started":"2023-05-28T07:26:12.287392Z","shell.execute_reply":"2023-05-28T07:26:13.905156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"period = 15*5\nds = WaveformDataset(CFG,train.sort_values(\"sec\",ascending=False),period=period)\ncollate_fn = Collate(CFG)\n\nfor idx, (audio, label, weight, freqmask) in enumerate(ds):\n    imgs = []\n    for i in range(2):\n        for j in range(2):\n            img = collate_fn.wavtoimg(audio[i,j][None,:],32000*period)\n            imgs.append(img)\n    img = (sum(imgs)/len(imgs))\n    c, f, t = img.shape\n    img = img.permute(0, 2, 1)\n    img = img.reshape(15, t//15, f, c)\n    img = img.permute(0, 3, 2, 1)\n    for im in img:\n        plt.figure(figsize=(5,2),dpi=100)\n        plt.imshow(im[0])\n        plt.tight_layout()\n        plt.axis('off')\n        plt.show()\n\n    \n    if idx==0:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:21:16.205209Z","iopub.execute_input":"2023-05-28T07:21:16.205861Z","iopub.status.idle":"2023-05-28T07:21:28.479766Z","shell.execute_reply.started":"2023-05-28T07:21:16.205823Z","shell.execute_reply":"2023-05-28T07:21:28.478203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#collate_fn.wavtoimg(a1,time_max=32000*5)","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:11:34.521072Z","iopub.execute_input":"2023-05-28T07:11:34.521535Z","iopub.status.idle":"2023-05-28T07:11:34.527228Z","shell.execute_reply.started":"2023-05-28T07:11:34.521499Z","shell.execute_reply":"2023-05-28T07:11:34.525750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import concurrent.futures\n# from tqdm import tqdm\n# import librosa\n\n# datas = {}\n\n# def load_data(row):\n#     data, sr = librosa.load(row.audio_paths, duration=15*5)\n#     return row.filename_id, data\n\n# with concurrent.futures.ThreadPoolExecutor() as executor:\n#     results = list(tqdm(executor.map(load_data, train.itertuples(index=False)), total=len(train)))\n\n# for filename_id, data in results:\n#     datas[filename_id] = data","metadata":{"execution":{"iopub.status.busy":"2023-05-28T07:11:34.529437Z","iopub.execute_input":"2023-05-28T07:11:34.529893Z","iopub.status.idle":"2023-05-28T07:11:34.546637Z","shell.execute_reply.started":"2023-05-28T07:11:34.529857Z","shell.execute_reply":"2023-05-28T07:11:34.545534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}