{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2020-09-05T03:32:32.429924Z","iopub.status.busy":"2020-09-05T03:32:32.428909Z","iopub.status.idle":"2020-09-05T03:32:33.381596Z","shell.execute_reply":"2020-09-05T03:32:33.380863Z"},"papermill":{"duration":0.972288,"end_time":"2020-09-05T03:32:33.381778","exception":false,"start_time":"2020-09-05T03:32:32.409490","status":"completed"},"tags":[],"trusted":false},"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\nimport math\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n#         print(os.path.join(dirname, filename))\n        pass\n\n# You can write up to 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.execute_input":"2020-09-05T03:32:33.414029Z","iopub.status.busy":"2020-09-05T03:32:33.413246Z","iopub.status.idle":"2020-09-05T03:32:43.789663Z","shell.execute_reply":"2020-09-05T03:32:43.788927Z"},"papermill":{"duration":10.397587,"end_time":"2020-09-05T03:32:43.789817","exception":false,"start_time":"2020-09-05T03:32:33.392230","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"import cv2\nimport pathlib\nimport librosa\nimport librosa.display\nimport skimage\nimport skimage.io\nfrom IPython.display import Audio\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nimport warnings\nimport tensorflow as tf\n\nfrom scipy.ndimage.measurements import center_of_mass\n\nfrom keras import Sequential\nfrom keras import layers\nfrom keras.models import Sequential\nfrom keras.layers import Dense, LSTM, Dropout, GRU, Bidirectional, Conv2D, MaxPooling2D,  Activation, Flatten, experimental, BatchNormalization, MaxPool2D\nfrom keras.optimizers import SGD\nfrom keras.wrappers.scikit_learn import KerasClassifier\nfrom sklearn.metrics import mean_squared_error, f1_score\nfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import shuffle\n\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-09-05T03:32:43.816804Z","iopub.status.busy":"2020-09-05T03:32:43.815870Z","iopub.status.idle":"2020-09-05T03:32:43.819240Z","shell.execute_reply":"2020-09-05T03:32:43.818465Z"},"papermill":{"duration":0.019261,"end_time":"2020-09-05T03:32:43.819369","exception":false,"start_time":"2020-09-05T03:32:43.800108","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"## Convert to mono types and Sampling Rate: 44100 (Hz) \nconfig = {\n    \"sample_rate\": 44100 ## \n}","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-09-05T03:32:43.856136Z","iopub.status.busy":"2020-09-05T03:32:43.855285Z","iopub.status.idle":"2020-09-05T03:32:43.858502Z","shell.execute_reply":"2020-09-05T03:32:43.859153Z"},"papermill":{"duration":0.029803,"end_time":"2020-09-05T03:32:43.859317","exception":false,"start_time":"2020-09-05T03:32:43.829514","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def spectrogram_image(y, sr,):       \n    \n    \"\"\"\n    y: audio samples: numpy array (2, n)\n    sr: sample rate: number\n    \"\"\"\n    \n    HOP_SIZE = 1024       \n    N_MELS = 128              \n    WINDOW_TYPE = 'hann' \n    FEATURE = 'mel'      \n    FMIN = 1400 \n    \n    y_chunks= librosa.effects.split(y) \n    \n    mfccs_final = []\n    \n    for chunk in y_chunks:\n        \n        mels = librosa.feature.melspectrogram(y=y,sr=sr,\n                                        hop_length=HOP_SIZE, \n                                        n_mels=N_MELS, \n                                        htk=True, \n                                        fmin=FMIN, \n                                        fmax=sr/2) \n\n        mels = librosa.power_to_db(mels**2,ref=np.max)\n        mfccs = librosa.feature.mfcc(S=mels, n_mfcc=40) \n\n        mfcss_img = np.reshape(mfccs, (*mfccs.shape, 1))\n        \n        ## resize and rescale image\n        resize_and_rescale = tf.keras.Sequential([\n            layers.experimental.preprocessing.Resizing(40, 40),\n            layers.experimental.preprocessing.Rescaling(1./255)\n        ])\n        \n        mfcss_img = resize_and_rescale(mfcss_img)\n\n        mfcss_image = np.reshape(mfcss_img, (mfcss_img.shape[0], mfcss_img.shape[1]))\n        mfccs_final.append(mfcss_image)\n    \n    return np.array(mfccs_final)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-09-05T03:32:43.896410Z","iopub.status.busy":"2020-09-05T03:32:43.895214Z","iopub.status.idle":"2020-09-05T03:32:43.898020Z","shell.execute_reply":"2020-09-05T03:32:43.898948Z"},"papermill":{"duration":0.025655,"end_time":"2020-09-05T03:32:43.899179","exception":false,"start_time":"2020-09-05T03:32:43.873524","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def gen_label_encoder():\n    return LabelEncoder()\n\ndef save_image(y, out):\n    skimage.io.imsave(out, y)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-09-05T03:32:43.935012Z","iopub.status.busy":"2020-09-05T03:32:43.934167Z","iopub.status.idle":"2020-09-05T03:32:44.350135Z","shell.execute_reply":"2020-09-05T03:32:44.350805Z"},"papermill":{"duration":0.43954,"end_time":"2020-09-05T03:32:44.350974","exception":false,"start_time":"2020-09-05T03:32:43.911434","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"raw_datasets = pd.read_csv(\"/kaggle/input/birdsong-recognition/train.csv\")\n\ndatasets = raw_datasets.loc[:, \n            ['location', 'rating', 'ebird_code', 'duration', 'filename', 'time', 'primary_label', 'sampling_rate',\n             'length', 'channels', 'pitch', 'bird_seen', 'background', 'bitrate_of_mp3', 'volume', 'file_type']]\n\ndatasets = datasets[datasets.rating >= 4.]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-09-05T03:32:44.385895Z","iopub.status.busy":"2020-09-05T03:32:44.378046Z","iopub.status.idle":"2020-09-05T03:32:44.389820Z","shell.execute_reply":"2020-09-05T03:32:44.388995Z"},"papermill":{"duration":0.028668,"end_time":"2020-09-05T03:32:44.389957","exception":false,"start_time":"2020-09-05T03:32:44.361289","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"## Loading data\nlabel_encoder = gen_label_encoder()\n\ndatasets['duration'] = datasets.duration.astype(float)\ndatasets['label'] = label_encoder.fit_transform(datasets.ebird_code.to_numpy())","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-09-05T03:32:44.421285Z","iopub.status.busy":"2020-09-05T03:32:44.420385Z","iopub.status.idle":"2020-09-05T03:32:44.428444Z","shell.execute_reply":"2020-09-05T03:32:44.427553Z"},"papermill":{"duration":0.028335,"end_time":"2020-09-05T03:32:44.428585","exception":false,"start_time":"2020-09-05T03:32:44.400250","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def data_generator(datasets):\n    while True:\n        for index, row in datasets.iterrows():\n            audio_p = f'/kaggle/input/birdsong-recognition/train_audio/{row.ebird_code}/{row.filename}'\n            if os.path.isfile(audio_p):   \n                try:\n                    audio_numpy, _ = librosa.load(audio_p, mono=True, sr=None)\n                    audio_numpy, _ = librosa.effects.trim(audio_numpy, top_db=20)\n                    audio_name = row.filename\n                    \n                    audio_mfccs = spectrogram_image(\n                        audio_numpy, \n                        config['sample_rate']\n                    )\n                    \n                    yield (\n                        audio_mfccs, \n                        tf.keras.utils.to_categorical(\n                            row.label, \n                            num_classes=len(datasets.ebird_code.unique()), \n                        ),\n                        row.ebird_code, \n                        row.filename)\n                    \n                except Exception as e:\n                    print(f\"ignore error data {audio_name}\")\n                    raise e\n                    pass\n        else:\n            break\n","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-09-05T03:32:44.458926Z","iopub.status.busy":"2020-09-05T03:32:44.458062Z","iopub.status.idle":"2020-09-05T11:42:39.905890Z","shell.execute_reply":"2020-09-05T11:42:39.907240Z"},"papermill":{"duration":29395.468977,"end_time":"2020-09-05T11:42:39.908135","exception":false,"start_time":"2020-09-05T03:32:44.439158","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"for mfccs, encoded_y, ebird_code, filename in data_generator(datasets):\n           \n    HOP_SIZE = 1024       \n    N_MELS = 128            \n    \n    path = pathlib.Path(f'/kaggle/working/{ebird_code}')\n    \n    if not path.exists():\n        path.mkdir(parents=True, exist_ok=True)\n          \n    index = 0\n    [file_path, _] = os.path.splitext(os.path.join(*path.parts, filename))\n    for mfcc in mfccs:  \n        save_image(mfcc, out=f\"{file_path}.{index}.png\")\n        index += 1","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}