{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-10T16:07:33.812112Z","iopub.execute_input":"2022-05-10T16:07:33.812625Z","iopub.status.idle":"2022-05-10T16:07:39.347225Z","shell.execute_reply.started":"2022-05-10T16:07:33.812508Z","shell.execute_reply":"2022-05-10T16:07:39.346269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. load data","metadata":{}},{"cell_type":"code","source":"from glob import glob \n\npath = glob(\"/kaggle/input/birdclef-2022/train_audio/\")[0]\n#print(path)\ntrain_data = glob(path +\"*/*.ogg\")\nprint(len(train_data))\nprint(train_data[0])","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:10:19.430959Z","iopub.execute_input":"2022-05-10T16:10:19.431451Z","iopub.status.idle":"2022-05-10T16:10:19.612094Z","shell.execute_reply.started":"2022-05-10T16:10:19.431417Z","shell.execute_reply":"2022-05-10T16:10:19.611065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. label data","metadata":{}},{"cell_type":"code","source":"# 정답데이터 \nimport os \nimport numpy as np\n\nfolder_name = os.listdir(path) \n\ny = np.empty((0, 1, ))\n\nfor index ,folder_name2 in enumerate(folder_name)  :\n    for data  in  train_data :\n        if folder_name2 in data :\n            resp = np.array([index])\n            resp = resp.reshape(1,1,)\n            y = np.vstack((y,resp))\n            \nprint(len(y))","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:11:32.294313Z","iopub.execute_input":"2022-05-10T16:11:32.294653Z","iopub.status.idle":"2022-05-10T16:11:32.764014Z","shell.execute_reply.started":"2022-05-10T16:11:32.294624Z","shell.execute_reply":"2022-05-10T16:11:32.762782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. split data (train / validation ) ","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport tensorflow as tf\n\n# set_seed 가 적용 안되는것을 방지 하기 위해서 2개 설정 (tf 만했을때 정확도 차이있음) \ntf.random.set_seed(777)\nnp.random.seed(777)\n\nx_train, x_val, y_train, y_val = train_test_split(train_data, y, test_size = 0.1, random_state=42)\n\nprint(len(x_train),len(x_val)) \nprint(len(y_train),len(y_val)) ","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:12:08.733505Z","iopub.execute_input":"2022-05-10T16:12:08.733786Z","iopub.status.idle":"2022-05-10T16:12:14.902158Z","shell.execute_reply.started":"2022-05-10T16:12:08.733756Z","shell.execute_reply":"2022-05-10T16:12:14.901063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## label data one hot encoding ","metadata":{}},{"cell_type":"code","source":"# 정답데이터 원핫인코딩 \nfrom tensorflow.keras.utils import to_categorical\n\ntrain_labels = to_categorical(y_train)\nval_labels = to_categorical(y_val)\nprint(val_labels)\nprint(len(train_labels)) # 14109 개의 훈련 정답데이터 \nprint(len(val_labels))  # 743 개의 검증 정답데이터","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:12:50.476276Z","iopub.execute_input":"2022-05-10T16:12:50.476668Z","iopub.status.idle":"2022-05-10T16:12:51.479147Z","shell.execute_reply.started":"2022-05-10T16:12:50.476635Z","shell.execute_reply":"2022-05-10T16:12:51.478083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = np.argmax(train_labels, axis=1)\n\nbird_name = np.array(folder_name)[index]\n\nprint(index)\nprint(len(index))","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:12:59.899983Z","iopub.execute_input":"2022-05-10T16:12:59.900332Z","iopub.status.idle":"2022-05-10T16:12:59.912083Z","shell.execute_reply.started":"2022-05-10T16:12:59.900301Z","shell.execute_reply":"2022-05-10T16:12:59.910626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## visualize ","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport librosa.display\n\ny, sr = librosa.load(x_train[0])\nlibrosa.display.waveshow(y, sr = sr, x_axis = 'time')    ","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:13:22.655535Z","iopub.execute_input":"2022-05-10T16:13:22.655823Z","iopub.status.idle":"2022-05-10T16:13:25.900702Z","shell.execute_reply.started":"2022-05-10T16:13:22.655793Z","shell.execute_reply":"2022-05-10T16:13:25.899495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(18,30))\n\nfor i in range(12) : \n    y, sr = librosa.load(x_train[i])\n    index = np.argmax(train_labels, axis=1)\n    bird_name = np.array(folder_name)[index]\n    ax = fig.add_subplot(6,2,i+1)\n    ax.set_title(bird_name[i])\n    ax = librosa.display.waveshow(y, sr = sr, x_axis = 'time') ","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:13:47.060492Z","iopub.execute_input":"2022-05-10T16:13:47.060836Z","iopub.status.idle":"2022-05-10T16:14:18.106236Z","shell.execute_reply.started":"2022-05-10T16:13:47.060799Z","shell.execute_reply":"2022-05-10T16:14:18.105204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## sample rate 값에 따른 시각화 차이 ","metadata":{}},{"cell_type":"code","source":"y1, sr1 = librosa.load(x_train[1]) # sr 디폴트 값으로 출력하기 \ny2, sr2 = librosa.load(x_train[1], sr=80000) # sr 80000 으로 주어 출력하기(일부러 극단적) \n\nprint(sr1) \nprint(sr2) \nprint(len(y1))\nprint(len(y2))","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:15:56.006941Z","iopub.execute_input":"2022-05-10T16:15:56.007251Z","iopub.status.idle":"2022-05-10T16:16:02.896253Z","shell.execute_reply.started":"2022-05-10T16:15:56.007221Z","shell.execute_reply":"2022-05-10T16:16:02.895055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig = plt.figure(figsize=(15,8))\nax1 = fig.add_subplot(211)\nax2 = fig.add_subplot(212)\nax1.scatter(range(len(y1[90000:100000])), y1[90000:100000], s=1)\nax2.scatter(range(len(y2[300000:400000])), y2[300000:400000], s=1)\nax1.set_xlim(1800,5500)\nax2.set_xlim(32000,45000)","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:16:05.125455Z","iopub.execute_input":"2022-05-10T16:16:05.12577Z","iopub.status.idle":"2022-05-10T16:16:05.591016Z","shell.execute_reply.started":"2022-05-10T16:16:05.125729Z","shell.execute_reply":"2022-05-10T16:16:05.590011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 진폭값 추출 (전체 데이터) ","metadata":{}},{"cell_type":"code","source":"# 1. x_train , x_test 전체정보 진폭값 출력하기 \nimport librosa \n\ndef librosa_read_wav_files(wav_files) :\n    if not isinstance(wav_files, list) : \n        wav_files = [wav_files] \n    return [librosa.load(f)[0] for f in wav_files] # 0 번째요소인 진폭 가져오기 ","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:16:34.877635Z","iopub.execute_input":"2022-05-10T16:16:34.877903Z","iopub.status.idle":"2022-05-10T16:16:34.886086Z","shell.execute_reply.started":"2022-05-10T16:16:34.877874Z","shell.execute_reply":"2022-05-10T16:16:34.884893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 2. wav_rate 하나 생성하기\nwav_rate = librosa.load(x_train[0])[1] # 훈련데이터의 첫번째 동물의 smple rate 값을 wave_rate 에 담기 \nprint(wav_rate) # 22050","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:16:42.526349Z","iopub.execute_input":"2022-05-10T16:16:42.52668Z","iopub.status.idle":"2022-05-10T16:16:43.072271Z","shell.execute_reply.started":"2022-05-10T16:16:42.526649Z","shell.execute_reply":"2022-05-10T16:16:43.071139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nX_train = librosa_read_wav_files(x_train) #  진폭정보를 x_train 에 담음 \nX_val  = librosa_read_wav_files(x_val)  #  진폭정보를 x_test 에 담음 ","metadata":{"execution":{"iopub.status.busy":"2022-05-10T16:16:50.046149Z","iopub.execute_input":"2022-05-10T16:16:50.046453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_features(audio_samples, sample_rate):\n    extracted_features = np.empty((0, 41, )) # 41개의 값을 받을 메모리를 할당하겠다는 뜻 (비어있는 리스트 만듦)\n    \n    #  리스트화 다시한번 확인 코드 \n    if not isinstance(audio_samples, list):\n        audio_samples = [audio_samples]\n        \n    for sample in audio_samples: # 진폭 데이터 리스트\n        zero_cross_feat = librosa.feature.zero_crossing_rate(sample).mean() # 음성신호 파형이 중심축 0을 지나는 횟수를 평균낸것 \n        mfccs = librosa.feature.mfcc(y=sample, sr=sample_rate, n_mfcc=40)\n        mfccsscaled = np.mean(mfccs.T,axis=0)  # 각 주파수별 평균값을 구합니다.\n        mfccsscaled = np.append(mfccsscaled, zero_cross_feat) # 주파수 40개에 대한 평균값 40 개와 zero_cross_feat 값을 가지고\n        mfccsscaled = mfccsscaled.reshape(1, 41, ) # 41개의의 값을 학습데이터로 구성한\n        extracted_features = np.vstack((extracted_features, mfccsscaled)) # 세로로 쌓아서 출력\n    return extracted_features","metadata":{},"execution_count":null,"outputs":[]}]}