Download scripts/2b_predict_loca2023grid3.py from sammlapp/pa_forest_bird_localization_2023: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/datasets/sammlapp/pa_forest_bird_localization_2023/resolve/main/scripts/2b_predict_loca2023grid3.py
- Command line
-
hf download hf://datasets/sammlapp/pa_forest_bird_localization_2023/scripts/2b_predict_loca2023grid3.py
-
curl -L -o 2b_predict_loca2023grid3.py https://huggingface.co/datasets/sammlapp/pa_forest_bird_localization_2023/resolve/main/scripts/2b_predict_loca2023grid3.py
1.49 kB
| from opensoundscape.ml import bioacoustics_model_zoo as bmz | |
| import numpy as np | |
| import pandas as pd | |
| from glob import glob | |
| from pathlib import Path | |
| from tqdm import tqdm | |
| import concurrent | |
| try: | |
| from redacted_paths import REDACTED_RESAMPLED_ROOT2, REDACTED_OUTPUT_DIR | |
| except ImportError: | |
| print("redacted paths not available in this compute environment") | |
| out_dir = f"{REDACTED_OUTPUT_DIR}/birdnet_preds/loca2023_grid3" | |
| audio_root = REDACTED_RESAMPLED_ROOT2 | |
| species_list = pd.read_csv("../ABA12_birdnet_species_table.csv") | |
| # load model | |
| birdnet = bmz.load("BirdNET") | |
| # find audio files for prediction | |
| files = glob(audio_root + "*/*.WAV") | |
| print(f"found {len(files)} files for prediction") | |
| # helper function for paralellizing prediction on CPU | |
| def go(f): | |
| parent = Path(f).parent.stem | |
| out_path = f"{out_dir}/{parent}/{Path(f).stem}_scores.csv" | |
| if Path(out_path).exists(): | |
| # already completed this file | |
| return | |
| Path(out_path).parent.mkdir(exist_ok=True, parents=True) | |
| # predict with BirdNET | |
| scores = birdnet.predict([f], batch_size=1) | |
| # trim columns to only scientific name | |
| scores.columns = [s.split("_")[0] for s in scores.columns] | |
| # subset to North American ABA code 1-2 species (701 species) | |
| scores[species_list.scientific].to_csv(out_path) | |
| # predict on each file | |
| with concurrent.futures.ProcessPoolExecutor(max_workers=64) as executor: | |
| list(tqdm(executor.map(go, files), total=len(files))) | |
| print("finished prediction") | |