Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python_style_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ['3.9', '3.10', '3.11', '3.12']
python-version: ['3.10', '3.11', '3.12']
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies = [
"tqdm",
"torch >= 2.0",
"yolov5 >= 7.0.8, < 7.0.12",
"setuptools<82"
]

[project.optional-dependencies]
Expand Down
48 changes: 27 additions & 21 deletions speciesnet/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@
PredictionType = tuple[PredictionLabelType, PredictionScoreType, PredictionSourceType]


def _load_taxonomy_from_file(taxonomy_file):
"""Loads the taxonomy .txt file into a dict."""

def _taxa_from_label(label: str) -> str:
return ";".join(label.split(";")[1:6])

# Create taxonomy map.
with open(taxonomy_file, mode="r", encoding="utf-8") as fp:
labels = [line.strip() for line in fp.readlines()]
taxonomy_map = {_taxa_from_label(label): label for label in labels}

for label in [
Classification.BLANK,
Classification.VEHICLE,
Classification.UNKNOWN,
]:
taxa = _taxa_from_label(label)
if taxa in taxonomy_map:
del taxonomy_map[taxa]

for label in [Classification.HUMAN, Classification.ANIMAL]:
taxa = _taxa_from_label(label)
taxonomy_map[taxa] = label
return taxonomy_map


class SpeciesNetEnsemble:
"""Ensemble component of SpeciesNet."""

Expand Down Expand Up @@ -76,27 +102,7 @@ def __init__(
def load_taxonomy(self):
"""Loads the taxonomy from the model info."""

def _taxa_from_label(label: str) -> str:
return ";".join(label.split(";")[1:6])

# Create taxonomy map.
with open(self.model_info.taxonomy, mode="r", encoding="utf-8") as fp:
labels = [line.strip() for line in fp.readlines()]
taxonomy_map = {_taxa_from_label(label): label for label in labels}

for label in [
Classification.BLANK,
Classification.VEHICLE,
Classification.UNKNOWN,
]:
taxa = _taxa_from_label(label)
if taxa in taxonomy_map:
del taxonomy_map[taxa]

for label in [Classification.HUMAN, Classification.ANIMAL]:
taxa = _taxa_from_label(label)
taxonomy_map[taxa] = label
return taxonomy_map
return _load_taxonomy_from_file(self.model_info.taxonomy)

def load_geofence(self):
"""Loads the geofence map from the model info."""
Expand Down
1 change: 1 addition & 0 deletions speciesnet/scripts/build_geofence_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
may later get blocked

"""

import copy
import json
import os
Expand Down
6 changes: 2 additions & 4 deletions speciesnet/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ def test_successful_loading(self, valid_predictions_json) -> None:
def invalid_predictions_json(self, tmp_path) -> Generator[Path, None, None]:
filepath = tmp_path / "invalid_predictions.json"
with open(filepath, mode="w", encoding="utf-8") as fp:
fp.write(
"""{
fp.write("""{
"predictions": [
{
"filepath": "a.jpg",
Expand All @@ -492,8 +491,7 @@ def invalid_predictions_json(self, tmp_path) -> Generator[Path, None, None]:
}
}
]
}"""
)
}""")
yield filepath
filepath.unlink()

Expand Down