{"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":"import os\nimport pandas as pd\nimport torch\nimport random\nimport numpy as np\nimport torch.nn as nn\nfrom sklearn import preprocessing\nimport torchvision\nimport torchaudio  # importing torchaudio library for audio processing\nfrom tqdm import tqdm\nimport torch.nn.functional as F\nimport torch.nn.init as init\nimport torch.optim as optim\nimport matplotlib.pyplot as plt\nfrom torchvision import transforms\nfrom torch.optim.lr_scheduler import ReduceLROnPlateau\nfrom torch.utils.data import Dataset, DataLoader, random_split\nfrom types import SimpleNamespace\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-15T07:01:45.035210Z","iopub.execute_input":"2023-05-15T07:01:45.036106Z","iopub.status.idle":"2023-05-15T07:01:47.597817Z","shell.execute_reply.started":"2023-05-15T07:01:45.036072Z","shell.execute_reply":"2023-05-15T07:01:47.596562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = SimpleNamespace()\n\n# paths\ncfg.data_folder = ''\ncfg.name = \"ftt1\"\ncfg.data_dir = \"../input/birdclef-2023/\"\ncfg.train_data_folder = cfg.data_dir + \"train_audio/\"\ncfg.val_data_folder = cfg.data_dir + \"train_audio/\"\ncfg.test_data_folder = cfg.data_dir + \"test_soundscapes/soundscape_29201.ogg\"\n\ncfg.birds = np.array(['abethr1', 'abhori1', 'abythr1', 'afbfly1', 'afdfly1', 'afecuc1',\n       'affeag1', 'afgfly1', 'afghor1', 'afmdov1', 'afpfly1', 'afpkin1',\n       'afpwag1', 'afrgos1', 'afrgrp1', 'afrjac1', 'afrthr1', 'amesun2',\n       'augbuz1', 'bagwea1', 'barswa', 'bawhor2', 'bawman1', 'bcbeat1',\n       'beasun2', 'bkctch1', 'bkfruw1', 'blacra1', 'blacuc1', 'blakit1',\n       'blaplo1', 'blbpuf2', 'blcapa2', 'blfbus1', 'blhgon1', 'blhher1',\n       'blksaw1', 'blnmou1', 'blnwea1', 'bltapa1', 'bltbar1', 'bltori1',\n       'blwlap1', 'brcale1', 'brcsta1', 'brctch1', 'brcwea1', 'brican1',\n       'brobab1', 'broman1', 'brosun1', 'brrwhe3', 'brtcha1', 'brubru1',\n       'brwwar1', 'bswdov1', 'btweye2', 'bubwar2', 'butapa1', 'cabgre1',\n       'carcha1', 'carwoo1', 'categr', 'ccbeat1', 'chespa1', 'chewea1',\n       'chibat1', 'chtapa3', 'chucis1', 'cibwar1', 'cohmar1', 'colsun2',\n       'combul2', 'combuz1', 'comsan', 'crefra2', 'crheag1', 'crohor1',\n       'darbar1', 'darter3', 'didcuc1', 'dotbar1', 'dutdov1', 'easmog1',\n       'eaywag1', 'edcsun3', 'egygoo', 'equaka1', 'eswdov1', 'eubeat1',\n       'fatrav1', 'fatwid1', 'fislov1', 'fotdro5', 'gabgos2', 'gargan',\n       'gbesta1', 'gnbcam2', 'gnhsun1', 'gobbun1', 'gobsta5', 'gobwea1',\n       'golher1', 'grbcam1', 'grccra1', 'grecor', 'greegr', 'grewoo2',\n       'grwpyt1', 'gryapa1', 'grywrw1', 'gybfis1', 'gycwar3', 'gyhbus1',\n       'gyhkin1', 'gyhneg1', 'gyhspa1', 'gytbar1', 'hadibi1', 'hamerk1',\n       'hartur1', 'helgui', 'hipbab1', 'hoopoe', 'huncis1', 'hunsun2',\n       'joygre1', 'kerspa2', 'klacuc1', 'kvbsun1', 'laudov1', 'lawgol',\n       'lesmaw1', 'lessts1', 'libeat1', 'litegr', 'litswi1', 'litwea1',\n       'loceag1', 'lotcor1', 'lotlap1', 'luebus1', 'mabeat1', 'macshr1',\n       'malkin1', 'marsto1', 'marsun2', 'mcptit1', 'meypar1', 'moccha1',\n       'mouwag1', 'ndcsun2', 'nobfly1', 'norbro1', 'norcro1', 'norfis1',\n       'norpuf1', 'nubwoo1', 'pabspa1', 'palfly2', 'palpri1', 'piecro1',\n       'piekin1', 'pitwhy', 'purgre2', 'pygbat1', 'quailf1', 'ratcis1',\n       'raybar1', 'rbsrob1', 'rebfir2', 'rebhor1', 'reboxp1', 'reccor',\n       'reccuc1', 'reedov1', 'refbar2', 'refcro1', 'reftin1', 'refwar2',\n       'rehblu1', 'rehwea1', 'reisee2', 'rerswa1', 'rewsta1', 'rindov',\n       'rocmar2', 'rostur1', 'ruegls1', 'rufcha2', 'sacibi2', 'sccsun2',\n       'scrcha1', 'scthon1', 'shesta1', 'sichor1', 'sincis1', 'slbgre1',\n       'slcbou1', 'sltnig1', 'sobfly1', 'somgre1', 'somtit4', 'soucit1',\n       'soufis1', 'spemou2', 'spepig1', 'spewea1', 'spfbar1', 'spfwea1',\n       'spmthr1', 'spwlap1', 'squher1', 'strher', 'strsee1', 'stusta1',\n       'subbus1', 'supsta1', 'tacsun1', 'tafpri1', 'tamdov1', 'thrnig1',\n       'trobou1', 'varsun2', 'vibsta2', 'vilwea1', 'vimwea1', 'walsta1',\n       'wbgbir1', 'wbrcha2', 'wbswea1', 'wfbeat1', 'whbcan1', 'whbcou1',\n       'whbcro2', 'whbtit5', 'whbwea1', 'whbwhe3', 'whcpri2', 'whctur2',\n       'wheslf1', 'whhsaw1', 'whihel1', 'whrshr1', 'witswa1', 'wlwwar',\n       'wookin1', 'woosan', 'wtbeat1', 'yebapa1', 'yebbar1', 'yebduc1',\n       'yebere1', 'yebgre1', 'yebsto1', 'yeccan1', 'yefcan', 'yelbis1',\n       'yenspu1', 'yertin1', 'yesbar1', 'yespet1', 'yetgre1', 'yewgre1'])\n\n\ncfg.n_classes = len(cfg.birds)\n\ncfg.train_df = \"../input/birdclef-2023/train_metadata.csv\"\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-15T07:01:47.599391Z","iopub.execute_input":"2023-05-15T07:01:47.599994Z","iopub.status.idle":"2023-05-15T07:01:47.622445Z","shell.execute_reply.started":"2023-05-15T07:01:47.599958Z","shell.execute_reply":"2023-05-15T07:01:47.620809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AudioUtil():\n    @staticmethod\n    def open(audio_file):\n        sig, sr = torchaudio.load(audio_file)\n        return (sig, sr)\n\n    @staticmethod\n    def rechannel(aud, new_channel):\n        sig, sr = aud\n\n        if (sig.shape[0] == new_channel):\n          # Nothing to do\n          return aud\n\n        if (new_channel == 1):\n          # Convert from stereo to mono by selecting only the first channel\n          resig = sig[:1, :]\n        else:\n          # Convert from mono to stereo by duplicating the first channel\n          resig = torch.cat([sig, sig, sig])\n\n        return ((resig, sr))\n\n    @staticmethod\n    def resample(aud, newsr):\n        sig, sr = aud\n\n        if (sr == newsr):\n        # Nothing to do\n            return aud\n\n        num_channels = sig.shape[0]\n        # Resample first channel\n        resig = torchaudio.transforms.Resample(sr, newsr)(sig[:1,:])\n        if (num_channels > 1):\n        # Resample the second channel and merge both channels\n            retwo = torchaudio.transforms.Resample(sr, newsr)(sig[1:,:])\n            resig = torch.cat([resig, retwo])\n\n        return ((resig, newsr))\n\n    @staticmethod\n    def pad_trunc(aud, max_ms):\n        sig, sr = aud\n        num_rows, sig_len = sig.shape\n        max_len = sr//1000 * max_ms\n\n        if (sig_len > max_len):\n        # Truncate the signal to the given length\n            sig = sig[:,:max_len]\n\n        elif (sig_len < max_len):\n            # Length of padding to add at the beginning and end of the signal\n            pad_begin_len = random.randint(0, max_len - sig_len)\n            pad_end_len = max_len - sig_len - pad_begin_len\n\n            # Pad with 0s\n            pad_begin = torch.zeros((num_rows, pad_begin_len))\n            pad_end = torch.zeros((num_rows, pad_end_len))\n\n            sig = torch.cat((pad_begin, sig, pad_end), 1)\n\n        return (sig, sr)\n\n    @staticmethod\n    def time_shift(aud, shift_limit):\n        sig,sr = aud\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 spectro_gram(aud, n_mels=64, n_fft=1024, hop_len=None):\n        sig,sr = aud\n        top_db = 80\n\n        # spec has shape [channel, n_mels, time], where channel is mono, stereo etc\n        spec = torchaudio.transforms.MelSpectrogram(sr, n_fft=n_fft, hop_length=hop_len, n_mels=n_mels)(sig)\n\n        # Convert to decibels\n        spec = torchaudio.transforms.AmplitudeToDB(top_db=top_db)(spec)\n        return (spec)\n\n    @staticmethod\n    def spectro_augment(spec, max_mask_pct=0.1, n_freq_masks=1, n_time_masks=1):\n        _, n_mels, n_steps = spec.shape\n        mask_value = spec.mean()\n        aug_spec = spec\n\n        freq_mask_param = max_mask_pct * n_mels\n        for _ in range(n_freq_masks):\n            aug_spec = torchaudio.transforms.FrequencyMasking(freq_mask_param)(aug_spec, mask_value)\n\n        time_mask_param = max_mask_pct * n_steps\n        for _ in range(n_time_masks):\n            aug_spec = torchaudio.transforms.TimeMasking(time_mask_param)(aug_spec, mask_value)\n\n        return aug_spec","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:01:47.624624Z","iopub.execute_input":"2023-05-15T07:01:47.625674Z","iopub.status.idle":"2023-05-15T07:01:47.647596Z","shell.execute_reply.started":"2023-05-15T07:01:47.625636Z","shell.execute_reply":"2023-05-15T07:01:47.646179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocessData(aud):\n    duration = 8000\n    sr = 32000\n    channel = 3\n    shift_pct = 0.4\n    reaud = AudioUtil.resample(aud, sr)\n    rechan = AudioUtil.rechannel(reaud, channel)\n    dur_aud = AudioUtil.pad_trunc(rechan, duration)\n    shift_aud = AudioUtil.time_shift(dur_aud, shift_pct)\n    sgram = AudioUtil.spectro_gram(shift_aud, n_mels=64, n_fft=1024, hop_len=None)\n    aug_sgram = AudioUtil.spectro_augment(sgram, max_mask_pct=0.1, n_freq_masks=2, n_time_masks=2)\n    aug_sgram_m, aug_sgram_s = aug_sgram.mean(), aug_sgram.std()\n    aug_sgram = (aug_sgram - aug_sgram_m) / aug_sgram_s\n    return aug_sgram","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:01:47.652020Z","iopub.execute_input":"2023-05-15T07:01:47.652521Z","iopub.status.idle":"2023-05-15T07:01:47.665277Z","shell.execute_reply.started":"2023-05-15T07:01:47.652485Z","shell.execute_reply":"2023-05-15T07:01:47.664309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 定义数据类处理文件\nclass RawData:\n\n    __train_data_path = cfg.train_data_folder\n    \n    __train_df = pd.read_csv(cfg.train_df)\n    \n    __labels_t = None\n    __audio_names = None\n    __le=preprocessing.LabelEncoder()\n\n    @staticmethod\n    def labels_t():\n        if RawData.__labels_t is None:\n            labels = RawData.__train_df['primary_label']\n            #RawData.__le = preprocessing.LabelEncoder()\n            RawData.__labels_t = RawData.__le.fit_transform(labels)\n            \n            #RawData.__labels_t = torch.as_tensor(targets)\n        return RawData.__labels_t\n\n    @staticmethod\n    def audio_names():\n        if RawData.__audio_names is None:\n            RawData.__audio_names = RawData.__train_df['filename']\n        return RawData.__audio_names\n\n    @staticmethod\n    def re_transf(data):\n        return RawData.__le.inverse_transform(data)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-15T07:01:47.666374Z","iopub.execute_input":"2023-05-15T07:01:47.667296Z","iopub.status.idle":"2023-05-15T07:01:47.776055Z","shell.execute_reply.started":"2023-05-15T07:01:47.667264Z","shell.execute_reply":"2023-05-15T07:01:47.775031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def MakeFrame(audio, duration=5, sr=32000):\n    frame_length = int(duration * sr)\n    frame_step = int(duration * sr)\n    if frame_length>audio.shape[1]:\n        return audio.unsqueeze(0)\n        #audio = torch.tensor(audio)\n    else:\n        chunks = audio.unfold(1, frame_length, frame_step).transpose(0, 1)\n        return chunks","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:01:47.777555Z","iopub.execute_input":"2023-05-15T07:01:47.777879Z","iopub.status.idle":"2023-05-15T07:01:47.784359Z","shell.execute_reply.started":"2023-05-15T07:01:47.777851Z","shell.execute_reply":"2023-05-15T07:01:47.782869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n#data_path = '/kaggle/working/'\nsave_data_path='/kaggle/working/data/'\nif os.path.exists(save_data_path):\n    shutil.rmtree(save_data_path)\nt_data_path=save_data_path+'data/'\nos.mkdir(save_data_path) \nos.mkdir(t_data_path) \nfor tit in cfg.birds[:132]:\n    os.mkdir(t_data_path+tit) ","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:01:47.785742Z","iopub.execute_input":"2023-05-15T07:01:47.786155Z","iopub.status.idle":"2023-05-15T07:01:48.165296Z","shell.execute_reply.started":"2023-05-15T07:01:47.786095Z","shell.execute_reply":"2023-05-15T07:01:48.164096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.birds[132]","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:01:48.166900Z","iopub.execute_input":"2023-05-15T07:01:48.167326Z","iopub.status.idle":"2023-05-15T07:01:48.176012Z","shell.execute_reply.started":"2023-05-15T07:01:48.167292Z","shell.execute_reply":"2023-05-15T07:01:48.174827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(cfg.train_df)\nn=len(train_df)\n#k=int(n/2)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:01:48.177716Z","iopub.execute_input":"2023-05-15T07:01:48.178430Z","iopub.status.idle":"2023-05-15T07:01:48.282648Z","shell.execute_reply.started":"2023-05-15T07:01:48.178397Z","shell.execute_reply":"2023-05-15T07:01:48.281722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lists = []\nfor i in range(n):\n    print(i,end='\\r')\n    data = train_df.iloc[i]\n    if data['primary_label']=='lesmaw1':\n        print(i)\n        break\n    #print(data['primary_label'])\n    loadpath = cfg.train_data_folder+ data['filename']\n    aud,sr = torchaudio.load(loadpath)  \n    #print(aud.shape)\n    signal=preprocessData((aud,sr))\n    savepath = t_data_path+ data['filename'][:-4]+'.pth'\n    torch.save(signal,savepath)\n    lists.append([data['primary_label'],data['filename']])\n\n    #break","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:01:48.284237Z","iopub.execute_input":"2023-05-15T07:01:48.284910Z","iopub.status.idle":"2023-05-15T07:15:11.971617Z","shell.execute_reply.started":"2023-05-15T07:01:48.284876Z","shell.execute_reply":"2023-05-15T07:15:11.968958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''train_df = pd.read_csv(cfg.train_df)\nlists = []\nfor i in range(len(train_df)):\n    data = train_df.iloc[i]\n    #print(data['primary_label'])\n    loadpath = cfg.train_data_folder+ data['filename']\n    aud,sr = torchaudio.load(loadpath)  \n    #print(aud.shape)\n    frames=MakeFrame(aud)\n    #print(frames.shape)\n    i=5\n    for frame in frames:\n        signal=preprocessData((frame,sr))\n        savepath = save_data_path+ data['filename'][:-4]+'_%d'%i+'.pth'\n        torch.save(signal,savepath)\n        lists.append([data['primary_label'],savepath])\n        i+=5\n    #break'''","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:15:11.975235Z","iopub.execute_input":"2023-05-15T07:15:11.975837Z","iopub.status.idle":"2023-05-15T07:15:11.988077Z","shell.execute_reply.started":"2023-05-15T07:15:11.975773Z","shell.execute_reply":"2023-05-15T07:15:11.986649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd_lists=pd.DataFrame(lists)\npd_lists.to_csv(save_data_path+'lists.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:15:11.990029Z","iopub.execute_input":"2023-05-15T07:15:11.990499Z","iopub.status.idle":"2023-05-15T07:15:12.053345Z","shell.execute_reply.started":"2023-05-15T07:15:11.990457Z","shell.execute_reply":"2023-05-15T07:15:12.051936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport zipfile\nimport datetime\n\ndef file2zip(packagePath, zipPath):\n    '''\n  :param packagePath: 文件夹路径\n  :param zipPath: 压缩包路径\n  :return:\n  '''\n    zip = zipfile.ZipFile(zipPath, 'w', zipfile.ZIP_DEFLATED)\n    for path, dirNames, fileNames in os.walk(packagePath):\n        fpath = path.replace(packagePath, '')\n        for name in fileNames:\n            fullName = os.path.join(path, name)\n            name = fpath + '\\\\' + name\n            zip.write(fullName, name)\n    zip.close()\n\n\n\n# 文件夹路径\n#packagePath = '/kaggle/working/'\nzipPath = save_data_path+'output.zip'\nif os.path.exists(zipPath):\n    os.remove(zipPath)\nfile2zip(t_data_path, zipPath)\nprint(\"打包完成\")\nprint(datetime.datetime.utcnow())\n","metadata":{"execution":{"iopub.status.busy":"2023-05-15T07:15:12.058521Z","iopub.execute_input":"2023-05-15T07:15:12.058903Z","iopub.status.idle":"2023-05-15T07:18:26.843822Z","shell.execute_reply.started":"2023-05-15T07:15:12.058872Z","shell.execute_reply":"2023-05-15T07:18:26.842856Z"},"trusted":true},"execution_count":null,"outputs":[]}]}