{"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":"from fastai.vision.all import *\nimport librosa as librosa","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-26T12:39:33.573878Z","iopub.execute_input":"2022-10-26T12:39:33.574330Z","iopub.status.idle":"2022-10-26T12:39:38.050452Z","shell.execute_reply.started":"2022-10-26T12:39:33.574232Z","shell.execute_reply":"2022-10-26T12:39:38.049286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:39:39.961990Z","iopub.execute_input":"2022-10-26T12:39:39.962751Z","iopub.status.idle":"2022-10-26T12:39:39.967397Z","shell.execute_reply.started":"2022-10-26T12:39:39.962702Z","shell.execute_reply":"2022-10-26T12:39:39.966384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import soundfile as sf","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:39:40.359330Z","iopub.execute_input":"2022-10-26T12:39:40.360053Z","iopub.status.idle":"2022-10-26T12:39:40.364590Z","shell.execute_reply.started":"2022-10-26T12:39:40.359985Z","shell.execute_reply":"2022-10-26T12:39:40.363275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:39:40.696884Z","iopub.execute_input":"2022-10-26T12:39:40.697428Z","iopub.status.idle":"2022-10-26T12:39:40.703051Z","shell.execute_reply.started":"2022-10-26T12:39:40.697384Z","shell.execute_reply":"2022-10-26T12:39:40.701800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /root/.kaggle","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:49:39.881518Z","iopub.execute_input":"2022-10-27T04:49:39.881948Z","iopub.status.idle":"2022-10-27T04:49:40.984137Z","shell.execute_reply.started":"2022-10-27T04:49:39.881909Z","shell.execute_reply":"2022-10-27T04:49:40.982546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile /root/.kaggle/kaggle.json\n{\"username\":\"johnowhitaker\",\"key\":\"6b16da3587f29239146707ce11a23aa3\"}","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:50:00.123791Z","iopub.execute_input":"2022-10-27T04:50:00.124199Z","iopub.status.idle":"2022-10-27T04:50:00.131791Z","shell.execute_reply.started":"2022-10-27T04:50:00.124161Z","shell.execute_reply":"2022-10-27T04:50:00.130701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Specs","metadata":{}},{"cell_type":"code","source":"def chunk_to_spec(chunk, SPEC_HEIGHT=64,SPEC_WIDTH=256, rate=32000, FMIN=200, FMAX=12500):\n    mel_spec = librosa.feature.melspectrogram(y=chunk, \n                                              sr=32000, \n                                              n_fft=1024, \n                                              hop_length=int(32000 * 5 / (SPEC_WIDTH - 1)), \n                                              n_mels=SPEC_HEIGHT, \n                                              fmin=FMIN, \n                                              fmax=FMAX)\n    mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max)\n    return mel_spec_db","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:39:43.905033Z","iopub.execute_input":"2022-10-26T12:39:43.905437Z","iopub.status.idle":"2022-10-26T12:39:43.911392Z","shell.execute_reply.started":"2022-10-26T12:39:43.905400Z","shell.execute_reply":"2022-10-26T12:39:43.910547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/peak-identification/info_df.csv')\nprint(df.shape)\ndf.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:39:49.097183Z","iopub.execute_input":"2022-10-26T12:39:49.097817Z","iopub.status.idle":"2022-10-26T12:39:49.570257Z","shell.execute_reply.started":"2022-10-26T12:39:49.097777Z","shell.execute_reply":"2022-10-26T12:39:49.569343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.groupby('label').count().sort_values('fn', ascending=False).head() # See the most common birds","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:42:00.834265Z","iopub.execute_input":"2022-10-26T12:42:00.834619Z","iopub.status.idle":"2022-10-26T12:42:00.869537Z","shell.execute_reply.started":"2022-10-26T12:42:00.834589Z","shell.execute_reply":"2022-10-26T12:42:00.868426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_twenty = df.groupby('label').count().sort_values('fn', ascending=False).index[:20]\ntop_twenty","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:43:20.837285Z","iopub.execute_input":"2022-10-26T12:43:20.837919Z","iopub.status.idle":"2022-10-26T12:43:20.866775Z","shell.execute_reply.started":"2022-10-26T12:43:20.837871Z","shell.execute_reply":"2022-10-26T12:43:20.865652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" df[df.label.isin(top_twenty)].shape","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:43:24.841150Z","iopub.execute_input":"2022-10-26T12:43:24.841725Z","iopub.status.idle":"2022-10-26T12:43:24.855218Z","shell.execute_reply.started":"2022-10-26T12:43:24.841680Z","shell.execute_reply":"2022-10-26T12:43:24.853918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fn, l, y, peaks = df[df.label.isin(top_twenty)].sample().values[0]\nstart_time = min(l/32000 - 5, max(float(peaks.split('#')[0])-2.5, 2.5))\ny, sr = librosa.load(fn, sr=32000, offset=start_time, duration=5)\nplt.imshow(chunk_to_spec(y, SPEC_HEIGHT=128), cmap='inferno')","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:43:37.629087Z","iopub.execute_input":"2022-10-26T12:43:37.629543Z","iopub.status.idle":"2022-10-26T12:43:37.983102Z","shell.execute_reply.started":"2022-10-26T12:43:37.629505Z","shell.execute_reply":"2022-10-26T12:43:37.981901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf birdcall_clips","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:43:41.265387Z","iopub.execute_input":"2022-10-26T12:43:41.266022Z","iopub.status.idle":"2022-10-26T12:43:42.380782Z","shell.execute_reply.started":"2022-10-26T12:43:41.265977Z","shell.execute_reply":"2022-10-26T12:43:42.379279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p birdcall_clips","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:43:42.383000Z","iopub.execute_input":"2022-10-26T12:43:42.383315Z","iopub.status.idle":"2022-10-26T12:43:43.484580Z","shell.execute_reply.started":"2022-10-26T12:43:42.383284Z","shell.execute_reply":"2022-10-26T12:43:43.483391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"global_count = 0\nfor fn, l, label, peaks in tqdm( df[df.label.isin(top_twenty)].values):\n    peak = peaks.split('#')[0]\n    start_time = min(l/32000 - 5, max(float(peak)-2.5, 2.5))\n    y, sr = librosa.load(fn, sr=32000, offset=start_time, duration=5)\n    sf.write(f'birdcall_clips/call_{label}_{global_count}.wav', y, sr)\n    global_count += 1","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:43:55.135338Z","iopub.execute_input":"2022-10-26T12:43:55.135993Z","iopub.status.idle":"2022-10-26T12:56:08.891161Z","shell.execute_reply.started":"2022-10-26T12:43:55.135953Z","shell.execute_reply":"2022-10-26T12:56:08.889204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l birdcall_clips/ | wc -l","metadata":{"execution":{"iopub.status.busy":"2022-10-26T12:56:08.894982Z","iopub.execute_input":"2022-10-26T12:56:08.895446Z","iopub.status.idle":"2022-10-26T12:56:10.156801Z","shell.execute_reply.started":"2022-10-26T12:56:08.895406Z","shell.execute_reply":"2022-10-26T12:56:10.155670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cp ../input/kagglejson/kaggle.json /root/.kaggle/","metadata":{"execution":{"iopub.status.busy":"2022-10-26T13:10:58.684506Z","iopub.execute_input":"2022-10-26T13:10:58.685046Z","iopub.status.idle":"2022-10-26T13:10:59.807051Z","shell.execute_reply.started":"2022-10-26T13:10:58.684996Z","shell.execute_reply":"2022-10-26T13:10:59.805707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!kaggle datasets init -p 'birdcall_clips/'","metadata":{"execution":{"iopub.status.busy":"2022-10-26T13:11:03.974938Z","iopub.execute_input":"2022-10-26T13:11:03.975610Z","iopub.status.idle":"2022-10-26T13:11:05.556187Z","shell.execute_reply.started":"2022-10-26T13:11:03.975569Z","shell.execute_reply":"2022-10-26T13:11:05.554797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat birdcall_clips/dataset-metadata.json","metadata":{"execution":{"iopub.status.busy":"2022-10-26T13:11:16.984978Z","iopub.execute_input":"2022-10-26T13:11:16.985458Z","iopub.status.idle":"2022-10-26T13:11:18.092523Z","shell.execute_reply.started":"2022-10-26T13:11:16.985420Z","shell.execute_reply":"2022-10-26T13:11:18.091328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile birdcall_clips/dataset-metadata.json\n{\n  \"title\": \"BIRDCALL_CLIPS_5s_top20\",\n  \"id\": \"johnowhitaker/BIRDCALL_CLIPS_5s_top20\",\n  \"licenses\": [\n    {\n      \"name\": \"CC0-1.0\"\n    }\n  ]\n}","metadata":{"execution":{"iopub.status.busy":"2022-10-26T13:12:05.123637Z","iopub.execute_input":"2022-10-26T13:12:05.124151Z","iopub.status.idle":"2022-10-26T13:12:05.137508Z","shell.execute_reply.started":"2022-10-26T13:12:05.124107Z","shell.execute_reply":"2022-10-26T13:12:05.135644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat birdcall_clips/dataset-metadata.json","metadata":{"execution":{"iopub.status.busy":"2022-10-26T13:12:13.673799Z","iopub.execute_input":"2022-10-26T13:12:13.674540Z","iopub.status.idle":"2022-10-26T13:12:14.771881Z","shell.execute_reply.started":"2022-10-26T13:12:13.674498Z","shell.execute_reply":"2022-10-26T13:12:14.770688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!kaggle datasets create -u -p 'birdcall_clips/'","metadata":{"execution":{"iopub.status.busy":"2022-10-26T13:12:28.478674Z","iopub.execute_input":"2022-10-26T13:12:28.479074Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n# Check a few to make sure they vary\nfiles = glob.glob('birdcall_clips/*.wav')\nfn = random.choice(files)\ny, sr = librosa.load(fn, sr=32000)\nprint(fn, len(y))\nplt.imshow(chunk_to_spec(y, SPEC_HEIGHT=128), cmap='inferno')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fn = random.choice(files)\ny, sr = librosa.load(fn, sr=32000)\nprint(fn, len(y))\nplt.imshow(chunk_to_spec(y, SPEC_HEIGHT=128), cmap='inferno')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fn = random.choice(files)\ny, sr = librosa.load(fn, sr=32000)\nprint(fn, len(y))\nplt.imshow(chunk_to_spec(y, SPEC_HEIGHT=128), cmap='inferno')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}