|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +import zipfile |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from ordered_set import OrderedSet |
| 9 | + |
| 10 | +from birdnet.core.backends import ( |
| 11 | + PBBackend, |
| 12 | + VersionedGeoBackendProtocol, |
| 13 | +) |
| 14 | +from birdnet.geo.models.v3_0.model import GeoDownloaderBaseV3_0 |
| 15 | +from birdnet.globals import ( |
| 16 | + MODEL_PRECISION_FP32, |
| 17 | + MODEL_PRECISIONS, |
| 18 | +) |
| 19 | +from birdnet.utils.helper import ( |
| 20 | + check_protobuf_model_files_exist, |
| 21 | + download_file_tqdm, |
| 22 | + get_species_from_file, |
| 23 | +) |
| 24 | +from birdnet.utils.local_data import get_lang_dir, get_model_path |
| 25 | + |
| 26 | + |
| 27 | +class GeoPBDownloaderV3_0(GeoDownloaderBaseV3_0): |
| 28 | + @classmethod |
| 29 | + def _get_paths(cls) -> tuple[Path, Path]: |
| 30 | + model_path = get_model_path("geo", "3.0", "pb", MODEL_PRECISION_FP32) |
| 31 | + lang_dir = get_lang_dir("geo", "3.0", "pb") |
| 32 | + return model_path, lang_dir |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def _check_geo_model_available(cls) -> bool: |
| 36 | + model_path, lang_dir = cls._get_paths() |
| 37 | + |
| 38 | + model_is_downloaded = True |
| 39 | + model_is_downloaded &= model_path.is_dir() |
| 40 | + model_is_downloaded &= check_protobuf_model_files_exist(model_path) |
| 41 | + |
| 42 | + model_is_downloaded &= lang_dir.is_dir() |
| 43 | + for lang in cls.AVAILABLE_LANGUAGES: |
| 44 | + model_is_downloaded &= (lang_dir / f"{lang}.txt").is_file() |
| 45 | + |
| 46 | + return model_is_downloaded |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def _download_model(cls) -> None: |
| 50 | + dl_url = "TODO" # TODO: update with real zenodo URL once published |
| 51 | + dl_size = 0 # TODO: update with real download size once published |
| 52 | + |
| 53 | + with tempfile.TemporaryDirectory(prefix="birdnet_download") as temp_dir: |
| 54 | + zip_download_path = Path(temp_dir) / "download.zip" |
| 55 | + download_file_tqdm( |
| 56 | + dl_url, |
| 57 | + zip_download_path, |
| 58 | + download_size=dl_size, |
| 59 | + description="Downloading geo model v3.0 (pb)", |
| 60 | + ) |
| 61 | + |
| 62 | + print("Extracting...") |
| 63 | + extract_dir = Path(temp_dir) / "extracted" |
| 64 | + |
| 65 | + with zipfile.ZipFile(zip_download_path, "r") as zip_ref: |
| 66 | + zip_ref.extractall(extract_dir) |
| 67 | + |
| 68 | + geo_model_dl_dir = extract_dir / "meta-model" # TODO: update folder name if different |
| 69 | + species_dl_dir = extract_dir / "labels" |
| 70 | + |
| 71 | + geo_model_dir, geo_lang_dir = cls._get_paths() |
| 72 | + geo_model_dir.parent.mkdir(parents=True, exist_ok=True) |
| 73 | + shutil.rmtree(geo_model_dir, ignore_errors=True) |
| 74 | + shutil.move(geo_model_dl_dir, geo_model_dir) |
| 75 | + |
| 76 | + geo_lang_dir.parent.mkdir(parents=True, exist_ok=True) |
| 77 | + shutil.rmtree(geo_lang_dir, ignore_errors=True) |
| 78 | + shutil.move(species_dl_dir, geo_lang_dir) |
| 79 | + print("Extracted.") |
| 80 | + |
| 81 | + @classmethod |
| 82 | + def get_model_path_and_labels( |
| 83 | + cls, |
| 84 | + lang: str, |
| 85 | + ) -> tuple[Path, OrderedSet[str]]: |
| 86 | + if not cls._check_geo_model_available(): |
| 87 | + cls._download_model() |
| 88 | + assert cls._check_geo_model_available() |
| 89 | + |
| 90 | + model_dir, langs_path = cls._get_paths() |
| 91 | + |
| 92 | + lang_file = langs_path / f"{lang}.txt" |
| 93 | + if not lang_file.is_file(): |
| 94 | + raise ValueError(f"Language does not exist: {lang}") |
| 95 | + |
| 96 | + labels = get_species_from_file(lang_file, encoding="utf8") |
| 97 | + return model_dir, labels |
| 98 | + |
| 99 | + |
| 100 | +class GeoPBBackendFP32V3_0(PBBackend, VersionedGeoBackendProtocol): |
| 101 | + def __init__( |
| 102 | + self, |
| 103 | + model_path: Path, |
| 104 | + device_name: str, |
| 105 | + half_precision: bool, |
| 106 | + ) -> None: |
| 107 | + super().__init__(model_path, device_name, half_precision) |
| 108 | + |
| 109 | + @classmethod |
| 110 | + def input_key(cls) -> str: |
| 111 | + return "MNET_INPUT" # TODO: update if key name differs in v3.0 |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def prediction_signature_name(cls) -> str: |
| 115 | + return "serving_default" |
| 116 | + |
| 117 | + @classmethod |
| 118 | + def prediction_key(cls) -> str: |
| 119 | + return "MNET_CLASS_ACTIVATION" # TODO: update if key name differs in v3.0 |
| 120 | + |
| 121 | + @classmethod |
| 122 | + def supports_encoding(cls) -> bool: |
| 123 | + return False |
| 124 | + |
| 125 | + @classmethod |
| 126 | + def encoding_signature_name(cls) -> str | None: |
| 127 | + return None |
| 128 | + |
| 129 | + @classmethod |
| 130 | + def encoding_key(cls) -> str | None: |
| 131 | + return None |
| 132 | + |
| 133 | + @classmethod |
| 134 | + def precision(cls) -> MODEL_PRECISIONS: |
| 135 | + return MODEL_PRECISION_FP32 |
0 commit comments