{"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 numpy as np\nimport pandas as pd \nimport librosa\nfrom IPython.display import Audio, display\nimport IPython\nimport librosa.display\nfrom pandarallel import pandarallel\npandarallel.initialize(progress_bar=True,nb_workers=8)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-18T05:14:44.446599Z","iopub.execute_input":"2022-08-18T05:14:44.447380Z","iopub.status.idle":"2022-08-18T05:14:58.803006Z","shell.execute_reply.started":"2022-08-18T05:14:44.447342Z","shell.execute_reply":"2022-08-18T05:14:58.801585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\nimport matplotlib.pyplot as plt\nimport IPython.display as ipd\nfrom tqdm import tqdm\nfrom torch.utils.data import Dataset, DataLoader\nfrom scipy.io import wavfile","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:14:58.805253Z","iopub.execute_input":"2022-08-18T05:14:58.806086Z","iopub.status.idle":"2022-08-18T05:15:00.701777Z","shell.execute_reply.started":"2022-08-18T05:14:58.806044Z","shell.execute_reply":"2022-08-18T05:15:00.700420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tqdm.pandas()\nfrom transformers import Wav2Vec2CTCTokenizer","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:00.703560Z","iopub.execute_input":"2022-08-18T05:15:00.704018Z","iopub.status.idle":"2022-08-18T05:15:01.268784Z","shell.execute_reply.started":"2022-08-18T05:15:00.703980Z","shell.execute_reply":"2022-08-18T05:15:01.267458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:01.272680Z","iopub.execute_input":"2022-08-18T05:15:01.273071Z","iopub.status.idle":"2022-08-18T05:15:01.280354Z","shell.execute_reply.started":"2022-08-18T05:15:01.273033Z","shell.execute_reply":"2022-08-18T05:15:01.279024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# loading the mp3 audio file paths","metadata":{}},{"cell_type":"code","source":"filenames = glob('../input/dlsprint/train_files/*.*')\nprint('hi')","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:01.282111Z","iopub.execute_input":"2022-08-18T05:15:01.282853Z","iopub.status.idle":"2022-08-18T05:15:06.246575Z","shell.execute_reply.started":"2022-08-18T05:15:01.282805Z","shell.execute_reply":"2022-08-18T05:15:06.245350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames[0:10]","metadata":{"_kg_hide-output":false,"execution":{"iopub.status.busy":"2022-08-18T05:15:06.248089Z","iopub.execute_input":"2022-08-18T05:15:06.248465Z","iopub.status.idle":"2022-08-18T05:15:06.258606Z","shell.execute_reply.started":"2022-08-18T05:15:06.248430Z","shell.execute_reply":"2022-08-18T05:15:06.257327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **loading the wav audio file paths**","metadata":{}},{"cell_type":"code","source":"wav_train_filepaths = glob('../input/wav-train-dlsprint-16000/wav_train_files/*.*')\nwav_valid_filepaths = glob('../input/wav-valid-dlsprint-16000/wav_validation_files/*.*')","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:06.260329Z","iopub.execute_input":"2022-08-18T05:15:06.260791Z","iopub.status.idle":"2022-08-18T05:15:11.267798Z","shell.execute_reply.started":"2022-08-18T05:15:06.260752Z","shell.execute_reply":"2022-08-18T05:15:11.266438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wav_train_filepaths[0:10]\nwav_valid_filepaths[0:10]","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:11.269302Z","iopub.execute_input":"2022-08-18T05:15:11.269736Z","iopub.status.idle":"2022-08-18T05:15:11.277991Z","shell.execute_reply.started":"2022-08-18T05:15:11.269697Z","shell.execute_reply":"2022-08-18T05:15:11.276850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = [[32392, 194458, 107050,  17993, 205751, 131958,  21297, 152554, 134955, 170380]]","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:11.279203Z","iopub.execute_input":"2022-08-18T05:15:11.280017Z","iopub.status.idle":"2022-08-18T05:15:11.294348Z","shell.execute_reply.started":"2022-08-18T05:15:11.279979Z","shell.execute_reply":"2022-08-18T05:15:11.293321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# function to read wav files","metadata":{}},{"cell_type":"code","source":"def wav_read(filename):\n    #print(filename)\n    sampling_rate, wave = wavfile.read(filename)\n    \n    speech_array = np.float32(wave) / 2**15 - 1\n    return speech_array,sampling_rate","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:11.300977Z","iopub.execute_input":"2022-08-18T05:15:11.301489Z","iopub.status.idle":"2022-08-18T05:15:11.320586Z","shell.execute_reply.started":"2022-08-18T05:15:11.301448Z","shell.execute_reply":"2022-08-18T05:15:11.319590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# playing a samle random wav_file","metadata":{}},{"cell_type":"code","source":"audio1, sr1 = wav_read(wav_valid_filepaths[98])\nprint(audio1,sr1)\nplt.plot(audio1)\nipd.Audio(audio1,rate=16000)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:11.322015Z","iopub.execute_input":"2022-08-18T05:15:11.322556Z","iopub.status.idle":"2022-08-18T05:15:11.633061Z","shell.execute_reply.started":"2022-08-18T05:15:11.322522Z","shell.execute_reply":"2022-08-18T05:15:11.631680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Part 2**","metadata":{}},{"cell_type":"markdown","source":"function to extract mel_spectrogram","metadata":{}},{"cell_type":"markdown","source":"# Reading the train csv file","metadata":{}},{"cell_type":"code","source":"train_df= pd.read_csv('../input/dlsprint/train.csv')\nval_df = pd.read_csv('../input/dlsprint/validation.csv')\ndroplist = ['client_id','up_votes','down_votes','age','gender','accents','locale']\ntrain_df = train_df.drop(droplist,axis=1)\nval_df = val_df.drop(droplist,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:11.634890Z","iopub.execute_input":"2022-08-18T05:15:11.636098Z","iopub.status.idle":"2022-08-18T05:15:14.000909Z","shell.execute_reply.started":"2022-08-18T05:15:11.636046Z","shell.execute_reply":"2022-08-18T05:15:13.999854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:14.002302Z","iopub.execute_input":"2022-08-18T05:15:14.002991Z","iopub.status.idle":"2022-08-18T05:15:14.019480Z","shell.execute_reply.started":"2022-08-18T05:15:14.002951Z","shell.execute_reply":"2022-08-18T05:15:14.018195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install bnunicodenormalizer","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:14.021574Z","iopub.execute_input":"2022-08-18T05:15:14.022188Z","iopub.status.idle":"2022-08-18T05:15:26.835330Z","shell.execute_reply.started":"2022-08-18T05:15:14.022135Z","shell.execute_reply":"2022-08-18T05:15:26.833209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from bnunicodenormalizer import Normalizer","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:26.837372Z","iopub.execute_input":"2022-08-18T05:15:26.837859Z","iopub.status.idle":"2022-08-18T05:15:26.852616Z","shell.execute_reply.started":"2022-08-18T05:15:26.837814Z","shell.execute_reply":"2022-08-18T05:15:26.851199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bnorm=Normalizer()\n\ndef normalize(sen):\n    _words = [bnorm(word)['normalized']  for word in sen.split()]\n    return \" \".join([word for word in _words if word is not None]) \n\ntrain_df[\"sentence\"] = train_df[\"sentence\"].parallel_apply(lambda x:normalize(x))","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:15:26.854742Z","iopub.execute_input":"2022-08-18T05:15:26.855209Z","iopub.status.idle":"2022-08-18T05:21:43.774688Z","shell.execute_reply.started":"2022-08-18T05:15:26.855176Z","shell.execute_reply":"2022-08-18T05:21:43.773533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df[\"sentence\"] = val_df[\"sentence\"].parallel_apply(lambda x:normalize(x))\n","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:43.777436Z","iopub.execute_input":"2022-08-18T05:21:43.778414Z","iopub.status.idle":"2022-08-18T05:21:58.342835Z","shell.execute_reply.started":"2022-08-18T05:21:43.778358Z","shell.execute_reply":"2022-08-18T05:21:58.341166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating a lsit of of all the paths of the train csv","metadata":{}},{"cell_type":"code","source":"def extract_all_chars(batch):\n    all_text = \" \".join(batch[\"sentence\"])\n    vocab = list(set(all_text))\n    return {\"vocab\": [vocab], \"all_text\": [all_text]}","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:58.345411Z","iopub.execute_input":"2022-08-18T05:21:58.345856Z","iopub.status.idle":"2022-08-18T05:21:58.353986Z","shell.execute_reply.started":"2022-08-18T05:21:58.345803Z","shell.execute_reply":"2022-08-18T05:21:58.352647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_train = (extract_all_chars(train_df))\nvocab_val=(extract_all_chars(val_df))","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:58.355654Z","iopub.execute_input":"2022-08-18T05:21:58.356015Z","iopub.status.idle":"2022-08-18T05:21:59.375422Z","shell.execute_reply.started":"2022-08-18T05:21:58.355983Z","shell.execute_reply":"2022-08-18T05:21:59.374037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_list= list(set(vocab_train[\"vocab\"][0]) | set(vocab_val[\"vocab\"][0]))","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.377017Z","iopub.execute_input":"2022-08-18T05:21:59.377402Z","iopub.status.idle":"2022-08-18T05:21:59.383139Z","shell.execute_reply.started":"2022-08-18T05:21:59.377365Z","shell.execute_reply":"2022-08-18T05:21:59.381795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to_remove = ['!','/','॥','।','.',';','‘','…','‚',':','-','\"','—']","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.384737Z","iopub.execute_input":"2022-08-18T05:21:59.385096Z","iopub.status.idle":"2022-08-18T05:21:59.397037Z","shell.execute_reply.started":"2022-08-18T05:21:59.385063Z","shell.execute_reply":"2022-08-18T05:21:59.396113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x in to_remove:\n    \n    if x in vocab_list:\n        vocab_list.remove(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.398196Z","iopub.execute_input":"2022-08-18T05:21:59.399151Z","iopub.status.idle":"2022-08-18T05:21:59.411611Z","shell.execute_reply.started":"2022-08-18T05:21:59.399109Z","shell.execute_reply":"2022-08-18T05:21:59.410697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_dict = {v: k for k, v in enumerate(vocab_list)}","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.412775Z","iopub.execute_input":"2022-08-18T05:21:59.413309Z","iopub.status.idle":"2022-08-18T05:21:59.425619Z","shell.execute_reply.started":"2022-08-18T05:21:59.413276Z","shell.execute_reply":"2022-08-18T05:21:59.424701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_dict[\"[UNK]\"] = len(vocab_dict)\nvocab_dict[\"[PAD]\"] = len(vocab_dict)\nlen(vocab_dict)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.426785Z","iopub.execute_input":"2022-08-18T05:21:59.427357Z","iopub.status.idle":"2022-08-18T05:21:59.440263Z","shell.execute_reply.started":"2022-08-18T05:21:59.427316Z","shell.execute_reply":"2022-08-18T05:21:59.439265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nwith open('vocab.json', 'w') as vocab_file:\n     json.dump(vocab_dict, vocab_file)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.441570Z","iopub.execute_input":"2022-08-18T05:21:59.442792Z","iopub.status.idle":"2022-08-18T05:21:59.453732Z","shell.execute_reply.started":"2022-08-18T05:21:59.442755Z","shell.execute_reply":"2022-08-18T05:21:59.452503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tokenizer","metadata":{}},{"cell_type":"code","source":"tokenizer = Wav2Vec2CTCTokenizer(\"./vocab.json\", unk_token=\"[UNK]\", pad_token=\"[PAD]\", word_delimiter_token=\"|\")","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.455501Z","iopub.execute_input":"2022-08-18T05:21:59.455880Z","iopub.status.idle":"2022-08-18T05:21:59.465610Z","shell.execute_reply.started":"2022-08-18T05:21:59.455845Z","shell.execute_reply":"2022-08-18T05:21:59.464552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_list = list(train_df['path'])\npath_list_val = list(val_df['path'])\npath_list_val[0:10]","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.467395Z","iopub.execute_input":"2022-08-18T05:21:59.468203Z","iopub.status.idle":"2022-08-18T05:21:59.509196Z","shell.execute_reply.started":"2022-08-18T05:21:59.468156Z","shell.execute_reply":"2022-08-18T05:21:59.507747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **# **Part - 2****","metadata":{}},{"cell_type":"markdown","source":"# Creating a function to create the spectrogram from wav files with labels","metadata":{}},{"cell_type":"code","source":"def create_spect_for_wav(audio_path,audio_label,i):\n    \n    audio , sr = wav_read(audio_path) #converting the mp3 to nparray\n    X = librosa.stft(audio)             # short time fourier transform of the audio array\n    Xdb = librosa.amplitude_to_db(np.abs(X)) # decibel e neya\n    plt.figure(figsize=(12, 3)) \n    print(f'{i}th audio : {audio_label}')\n    \n    plt.title('Spectrogram for audio {} th audio'.format(i), size=15)\n    librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')  # plotting the spectrogram\n    plt.colorbar()\n    return Xdb","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.523183Z","iopub.execute_input":"2022-08-18T05:21:59.523626Z","iopub.status.idle":"2022-08-18T05:21:59.531241Z","shell.execute_reply.started":"2022-08-18T05:21:59.523586Z","shell.execute_reply":"2022-08-18T05:21:59.529959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# melspectrogram function","metadata":{}},{"cell_type":"code","source":"def create_mel_spect_for_wav(data,audio_label,i,sr):\n    \n    #audio , sr = wav_read(audio_path) #converting the mp3 to nparray\n    \n    plt.figure(figsize=(12, 3)) \n    print(f'{i}th audio : {audio_label}')\n    #print(sr)\n    X_mel = librosa.feature.melspectrogram(y=data, sr=sr, n_mels=10, fmax=8000)  #fmax = sr/2\n    log_mel = librosa.power_to_db(X_mel)\n    plt.title('Spectrogram for audio {} th audio'.format(i), size=15)\n    librosa.display.specshow(log_mel, sr=sr, x_axis='time', y_axis='hz')  # plotting the spectrogram\n    plt.colorbar()\n    return log_mel","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.532718Z","iopub.execute_input":"2022-08-18T05:21:59.533117Z","iopub.status.idle":"2022-08-18T05:21:59.548839Z","shell.execute_reply.started":"2022-08-18T05:21:59.533081Z","shell.execute_reply":"2022-08-18T05:21:59.547666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **finding the label of the wav_audio** (takes wav_file location and sits out the corresponding sentence)","metadata":{}},{"cell_type":"code","source":"def wav_label(filename):\n    \n    name = filename\n    mp3_filename = name.split('/')[4].split('.')[0] +'.mp3'    \n    #print(mp3_filename)\n    for idx, x in enumerate(path_list):\n        if x==mp3_filename:\n            #index =idx\n            return train_df['sentence'][idx]\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.550881Z","iopub.execute_input":"2022-08-18T05:21:59.551900Z","iopub.status.idle":"2022-08-18T05:21:59.560467Z","shell.execute_reply.started":"2022-08-18T05:21:59.551847Z","shell.execute_reply":"2022-08-18T05:21:59.559413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# wav_label('../input/wav-train-dlsprint-16000/wav_train_files/common_voice_bn_30614357.wav')\n# ipd('../input/wav-train-dlsprint-16000/wav_train_files/common_voice_bn_30614357.wav')","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.562347Z","iopub.execute_input":"2022-08-18T05:21:59.563216Z","iopub.status.idle":"2022-08-18T05:21:59.572099Z","shell.execute_reply.started":"2022-08-18T05:21:59.563167Z","shell.execute_reply":"2022-08-18T05:21:59.570926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wav_read('../input/wav-train-dlsprint-16000/wav_train_files/common_voice_bn_30614352.wav')","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.573300Z","iopub.execute_input":"2022-08-18T05:21:59.574577Z","iopub.status.idle":"2022-08-18T05:21:59.609999Z","shell.execute_reply.started":"2022-08-18T05:21:59.574538Z","shell.execute_reply":"2022-08-18T05:21:59.609044Z"},"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":[]},{"cell_type":"markdown","source":"# # **Part-3 Data Augmentation of the wav files**","metadata":{}},{"cell_type":"code","source":"wav_train_filepaths = glob('../input/wav-train-dlsprint-16000/wav_train_files/*.*')\nwav_valid_filepaths = glob('../input/wav-valid-dlsprint-16000/wav_validation_files/*.*') \n#print(len(wav_read(wav_train_filepaths[10])[0]))  #the default sampling rate of the spicy.io.wavfile is 16000","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:21:59.611243Z","iopub.execute_input":"2022-08-18T05:21:59.612087Z","iopub.status.idle":"2022-08-18T05:22:00.374985Z","shell.execute_reply.started":"2022-08-18T05:21:59.612053Z","shell.execute_reply":"2022-08-18T05:22:00.373704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This functions samples the audio data and then pads accordingly to make the length to exactly 16000**","metadata":{}},{"cell_type":"markdown","source":"# Plotting the waveform function","metadata":{}},{"cell_type":"code","source":"def plot_time_series(data):\n    fig = plt.figure(figsize=(14, 8))\n    plt.title('Raw wave ')\n    plt.ylabel('Amplitude')\n    plt.plot(np.linspace(0, 1, len(data)), data)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.376463Z","iopub.execute_input":"2022-08-18T05:22:00.376862Z","iopub.status.idle":"2022-08-18T05:22:00.383557Z","shell.execute_reply.started":"2022-08-18T05:22:00.376827Z","shell.execute_reply":"2022-08-18T05:22:00.382709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_time_series(load_audio_file('../input/wav-train-dlsprint-16000/wav_train_files/common_voice_bn_30614352.wav'))\n# IPython.display.Audio('../input/wav-train-dlsprint-16000/wav_train_files/common_voice_bn_30614352.wav',autoplay=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.384864Z","iopub.execute_input":"2022-08-18T05:22:00.385406Z","iopub.status.idle":"2022-08-18T05:22:00.398404Z","shell.execute_reply.started":"2022-08-18T05:22:00.385372Z","shell.execute_reply":"2022-08-18T05:22:00.397451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **White noise**","metadata":{}},{"cell_type":"code","source":"# Adding white noise\ndef add_white_noise(data,noise_factor):\n    \n    #data = wav_read(filepath)[0]\n    wn = np.random.randn(len(data))\n    data_wn = data + noise_factor*wn\n    \n    #plot_time_series(data_wn)\n    # We limited the amplitude of the noise so we can still hear the word even with the noise, \n    #which is the objective\n    \n    return data_wn\n\n#ipd.Audio(data_wn, rate=16000)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.399741Z","iopub.execute_input":"2022-08-18T05:22:00.400278Z","iopub.status.idle":"2022-08-18T05:22:00.414375Z","shell.execute_reply.started":"2022-08-18T05:22:00.400245Z","shell.execute_reply":"2022-08-18T05:22:00.413311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Sound shift**","metadata":{}},{"cell_type":"code","source":"# Shifting the sound\ndef shift_sound(data):\n    #data = wav_read(filepath)[0]\n    data_roll = np.roll(data, 1600000)\n    return data_roll\n# plot_time_series(data_roll)\n# ipd.Audio(data_roll, rate=16000)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.415600Z","iopub.execute_input":"2022-08-18T05:22:00.416534Z","iopub.status.idle":"2022-08-18T05:22:00.431287Z","shell.execute_reply.started":"2022-08-18T05:22:00.416495Z","shell.execute_reply":"2022-08-18T05:22:00.430289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Stretching sound**","metadata":{}},{"cell_type":"code","source":"def time_stretch(data, stretch_rate):\n    \n    #data = wav_read(filepath)[0]\n    \n    data = librosa.effects.time_stretch(data, stretch_rate)\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.432669Z","iopub.execute_input":"2022-08-18T05:22:00.433648Z","iopub.status.idle":"2022-08-18T05:22:00.445287Z","shell.execute_reply.started":"2022-08-18T05:22:00.433591Z","shell.execute_reply":"2022-08-18T05:22:00.444043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_file_path = '../input/wav-train-dlsprint-16000/wav_train_files/common_voice_bn_30646492.wav'\nprint('Without any augmentation')\ndata,sr = wav_read(sample_file_path)\nipd.Audio(sample_file_path)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.446882Z","iopub.execute_input":"2022-08-18T05:22:00.447243Z","iopub.status.idle":"2022-08-18T05:22:00.479950Z","shell.execute_reply.started":"2022-08-18T05:22:00.447211Z","shell.execute_reply":"2022-08-18T05:22:00.478694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#after white noise\nwn_data = add_white_noise(data,noise_factor=.05)\n\nipd.Audio(wn_data,rate=16000)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.481330Z","iopub.execute_input":"2022-08-18T05:22:00.482265Z","iopub.status.idle":"2022-08-18T05:22:00.503580Z","shell.execute_reply.started":"2022-08-18T05:22:00.482229Z","shell.execute_reply":"2022-08-18T05:22:00.502378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#after time stretch\ny=time_stretch(data,2)\nipd.Audio(y,rate=16000)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.505155Z","iopub.execute_input":"2022-08-18T05:22:00.505512Z","iopub.status.idle":"2022-08-18T05:22:00.591673Z","shell.execute_reply.started":"2022-08-18T05:22:00.505479Z","shell.execute_reply":"2022-08-18T05:22:00.590615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#after sound shift\nz = shift_sound(data)\nipd.Audio(z,rate=16000)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.593197Z","iopub.execute_input":"2022-08-18T05:22:00.593830Z","iopub.status.idle":"2022-08-18T05:22:00.609493Z","shell.execute_reply.started":"2022-08-18T05:22:00.593792Z","shell.execute_reply":"2022-08-18T05:22:00.608061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **# Dataset Class**","metadata":{}},{"cell_type":"code","source":"len(train_df)==len(wav_train_filepaths)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.611282Z","iopub.execute_input":"2022-08-18T05:22:00.611677Z","iopub.status.idle":"2022-08-18T05:22:00.618925Z","shell.execute_reply.started":"2022-08-18T05:22:00.611618Z","shell.execute_reply":"2022-08-18T05:22:00.617764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**eikhane randomly kisu data ke augment korar jonno random numbers generate kora hoiche **","metadata":{}},{"cell_type":"code","source":"wn_korar_index = np.random.randint(0,len(wav_train_filepaths),(1,10000))\n#wn_korar_index[0][900]\ntime_stretch_korar_index = np.random.randint(0,len(wav_train_filepaths),(1,5000))\nsound_shift_korar_index = np.random.randint(0,len(wav_train_filepaths),(1,3000))\nall_augmentation_korar_index = np.random.randint(0,len(wav_train_filepaths),(1,1000))","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.620352Z","iopub.execute_input":"2022-08-18T05:22:00.620717Z","iopub.status.idle":"2022-08-18T05:22:00.632031Z","shell.execute_reply.started":"2022-08-18T05:22:00.620684Z","shell.execute_reply":"2022-08-18T05:22:00.630547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"for example jodi training loop chalanor shomoy jei **index** er value **wn_korar_index** eitar kono ektar sathe mile jabe oi index er jonno **wn=1** kora hobe and **add_white_noise** augment hoye jabe dataset class e jeye","metadata":{}},{"cell_type":"code","source":"all_augmentation_korar_index[0][90] # remember egula 2d numpy array so access korar shomoy [0][45454] eivabe access kora lagbe","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.633627Z","iopub.execute_input":"2022-08-18T05:22:00.634481Z","iopub.status.idle":"2022-08-18T05:22:00.648225Z","shell.execute_reply.started":"2022-08-18T05:22:00.634432Z","shell.execute_reply":"2022-08-18T05:22:00.646980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class asr_Dataset(Dataset):\n    \n    def __init__(self,df,dir_list):\n        self.df = df\n        self.dir = dir_list\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self,index,wn,time_str,sound_shift,all_aug):\n        \n        #reading the audio\n        data, sr = wav_read(self.dir[index])\n        \n        # applying augmentation to randomly selected samples\n        if wn==1:                                                # wn=1 hole white noise add hobe\n            data = add_white_noise(data,noise_factor=.05)\n            \n        elif time_str==1:                                        # time_str=1 hole time_stretch hobe\n            data = time_stretch(data,2)\n            \n        elif sound_shift == 1:                                   # sound_shift=1 hole shift_sound add hobe\n            data = shift_sound(data)\n            \n        elif all_aug==1:                                        # all_aug=1 hole shob augmentation add hobe\n            data = add_white_noise(data,noise_factor=.05)\n            data = time_stretch(data,stretch_rate=2)\n            data = shift_sound(data)\n            \n        else:\n            pass\n        \n        #determining the mel_spectrogram\n        \n        audio_label = wav_label(self.dir[index])\n        spectrogram = create_mel_spect_for_wav(data,audio_label,index,sr)\n        \n        a = tokenizer.encode(audio_label)\n        b = [0]*(200-len(a))\n        a = a+b\n        token = np.asarray(a)\n        \n        return data,spectrogram,token\n        \n        \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.649814Z","iopub.execute_input":"2022-08-18T05:22:00.650461Z","iopub.status.idle":"2022-08-18T05:22:00.673501Z","shell.execute_reply.started":"2022-08-18T05:22:00.650422Z","shell.execute_reply":"2022-08-18T05:22:00.672471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating a **asr_dataset** class object","metadata":{}},{"cell_type":"code","source":"asr_Dataset_obj = asr_Dataset(train_df,wav_train_filepaths)\nasr_Dataset_obj.__len__()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.674821Z","iopub.execute_input":"2022-08-18T05:22:00.675698Z","iopub.status.idle":"2022-08-18T05:22:00.699053Z","shell.execute_reply.started":"2022-08-18T05:22:00.675652Z","shell.execute_reply":"2022-08-18T05:22:00.697717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# A sample run","metadata":{}},{"cell_type":"code","source":"data , spectrogram_data, token = asr_Dataset_obj.__getitem__(index=28463, wn=0,time_str= 0, sound_shift=0, all_aug=0)\nipd.Audio(data,rate=16000)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:00.700912Z","iopub.execute_input":"2022-08-18T05:22:00.701758Z","iopub.status.idle":"2022-08-18T05:22:01.068053Z","shell.execute_reply.started":"2022-08-18T05:22:00.701707Z","shell.execute_reply":"2022-08-18T05:22:01.066730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectrogram_data.shape ; token","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:01.069818Z","iopub.execute_input":"2022-08-18T05:22:01.071054Z","iopub.status.idle":"2022-08-18T05:22:01.078992Z","shell.execute_reply.started":"2022-08-18T05:22:01.071010Z","shell.execute_reply":"2022-08-18T05:22:01.077806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data , spectrogram_data, token = asr_Dataset_obj.__getitem__(index=28463, wn=0,time_str= 0, sound_shift=0, all_aug=1)\nipd.Audio(data,rate=16000)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:01.080490Z","iopub.execute_input":"2022-08-18T05:22:01.081142Z","iopub.status.idle":"2022-08-18T05:22:01.390358Z","shell.execute_reply.started":"2022-08-18T05:22:01.081104Z","shell.execute_reply":"2022-08-18T05:22:01.389470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Processor**","metadata":{}},{"cell_type":"code","source":"from transformers import Wav2Vec2FeatureExtractor\n\nfeature_extractor = Wav2Vec2FeatureExtractor(feature_size=1, sampling_rate=16000, padding_value=0.0, do_normalize=True, return_attention_mask=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:01.391717Z","iopub.execute_input":"2022-08-18T05:22:01.392072Z","iopub.status.idle":"2022-08-18T05:22:01.403568Z","shell.execute_reply.started":"2022-08-18T05:22:01.392039Z","shell.execute_reply":"2022-08-18T05:22:01.402605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import Wav2Vec2Processor\n\nprocessor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:01.405018Z","iopub.execute_input":"2022-08-18T05:22:01.405854Z","iopub.status.idle":"2022-08-18T05:22:01.422706Z","shell.execute_reply.started":"2022-08-18T05:22:01.405800Z","shell.execute_reply":"2022-08-18T05:22:01.421787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[:3]","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:01.424047Z","iopub.execute_input":"2022-08-18T05:22:01.425258Z","iopub.status.idle":"2022-08-18T05:22:01.437156Z","shell.execute_reply.started":"2022-08-18T05:22:01.425211Z","shell.execute_reply":"2022-08-18T05:22:01.436153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wav_train_filepaths[:4]","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:01.438915Z","iopub.execute_input":"2022-08-18T05:22:01.439570Z","iopub.status.idle":"2022-08-18T05:22:01.449479Z","shell.execute_reply.started":"2022-08-18T05:22:01.439536Z","shell.execute_reply":"2022-08-18T05:22:01.448143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_dataset(mp3_filename):\n    path = '../input/wav-train-dlsprint-16000/wav_train_files/'+ mp3_filename.split('.')[0] + '.wav'\n    \n    if path in wav_train_filepaths:\n        audio, sr = wav_read(path)\n        label = wav_label(path)\n\n    # batched output is \"un-batched\"\n    data = processor(audio, sampling_rate=sr).input_values[0]\n    #data = len(data)\n    \n    with processor.as_target_processor():\n        token = processor(label).input_ids\n    return data,token\n","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:01.451172Z","iopub.execute_input":"2022-08-18T05:22:01.451688Z","iopub.status.idle":"2022-08-18T05:22:01.464071Z","shell.execute_reply.started":"2022-08-18T05:22:01.451617Z","shell.execute_reply":"2022-08-18T05:22:01.463034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data, token = prepare_dataset(train_df['path'][0])","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:01.465327Z","iopub.execute_input":"2022-08-18T05:22:01.466332Z","iopub.status.idle":"2022-08-18T05:22:08.329747Z","shell.execute_reply.started":"2022-08-18T05:22:01.466289Z","shell.execute_reply":"2022-08-18T05:22:08.328502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(data,rate = 16000)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:08.331414Z","iopub.execute_input":"2022-08-18T05:22:08.332364Z","iopub.status.idle":"2022-08-18T05:22:08.345856Z","shell.execute_reply.started":"2022-08-18T05:22:08.332308Z","shell.execute_reply":"2022-08-18T05:22:08.344434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer.decode(token)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:08.347467Z","iopub.execute_input":"2022-08-18T05:22:08.348264Z","iopub.status.idle":"2022-08-18T05:22:08.356779Z","shell.execute_reply.started":"2022-08-18T05:22:08.348213Z","shell.execute_reply":"2022-08-18T05:22:08.355714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:22.012984Z","iopub.execute_input":"2022-08-18T05:22:22.013426Z","iopub.status.idle":"2022-08-18T05:22:22.019526Z","shell.execute_reply.started":"2022-08-18T05:22:22.013390Z","shell.execute_reply":"2022-08-18T05:22:22.018522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['prepared_data'] = train_df['path'][0:10].progress_map(prepare_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:22:39.178175Z","iopub.execute_input":"2022-08-18T05:22:39.178667Z","iopub.status.idle":"2022-08-18T05:22:39.320238Z","shell.execute_reply.started":"2022-08-18T05:22:39.178606Z","shell.execute_reply":"2022-08-18T05:22:39.318192Z"},"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":[]}]}