Upload inference.py
Browse files12345678910111213141516109 unmodified lines12612712812913013113213313413513613713813914014111 unmodified lines1531541551561571581591605 unmodified lines166167168169170171172"""Inference script for SWUSA-SDZWA-v3 (Southwest USA Species Classifier)This model distinguishes between 27 species native to the Southwest United States.Training data collected by SDZWA and California Mountain Lion Project, with examplesfrom NACTI and CCT datasets. Trained on 91,662 images (70/20/10 split) achieving88% accuracy on test set.Model: Southwest USA v3Input: 299x299 RGB imagesFramework: PyTorch (EfficientNet V2 Medium architecture)Classes: 27 species and categoriesDeveloper: San Diego Zoo Wildlife Alliance (Kyra Swanson)License: MITInfo: https://github.com/conservationtechlab109 unmodified lines# Check CUDA (NVIDIA)return torch.cuda.is_available()def load_model(self, device_str: str = 'cpu') -> None:"""Load PyTorch EfficientNet model and class labels.This SDZWA model uses EfficientNet V2 Medium architecture with ImageNetpretrained weights, fine-tuned on Southwest USA wildlife data.Args:device_str: Device to load model on ('cpu', 'cuda', or 'mps')Raises:RuntimeError: If model loading failsFileNotFoundError: If required files are missing11 unmodified linesraise FileNotFoundError(f"EfficientNet weights not found: {efficientnet_weights}")try:# Set deviceself.device = torch.device(device_str)# Load class labels from CSV# CSV format: id,Code,Species,Common5 unmodified linesself.model = EfficientNetV2M(num_classes=num_classes,pretrained_weights_path=efficientnet_weights,device_str=device_str,tune=False)123456789101112131415109 unmodified lines12512612712812913013113213313413513613711 unmodified lines1491501511521531541551561571581591601611621631641655 unmodified lines171172173174175176177"""Inference script for PAN-SDZWA-v1 (Peruvian Andes Species Classifier)Camera-trap classifier for wildlife of the Peruvian Andes, from the SanDiego Zoo Wildlife Alliance. Shares the EfficientNet V2 Medium pipelinewith the other SDZWA pytorch models (conservationtechlab/animl-py):plain box crop, resize to 299x299, ToTensor, softmax, no normalization.Model: Peruvian Andes v1Input: 299x299 RGB imagesFramework: PyTorch (EfficientNet V2 Medium architecture)Developer: San Diego Zoo Wildlife AllianceLicense: MITInfo: https://github.com/conservationtechlab109 unmodified lines# Check CUDA (NVIDIA)return torch.cuda.is_available()def load_model(self) -> None:"""Load PyTorch EfficientNet model and class labels.This SDZWA model uses EfficientNet V2 Medium architecture with ImageNetpretrained weights, fine-tuned on Southwest USA wildlife data.Raises:RuntimeError: If model loading failsFileNotFoundError: If required files are missing11 unmodified linesraise FileNotFoundError(f"EfficientNet weights not found: {efficientnet_weights}")try:# Detect the device here rather than take it as an argument.# The worker calls load_model() with no argument, so the# previous device_str='cpu' default pinned this model to CPU on# every machine while check_gpu() still reported a GPU. Matches# the legacy sdzwa-pt device order: MPS, then CUDA, then CPU.if torch.backends.mps.is_built() and torch.backends.mps.is_available():self.device = torch.device("mps")elif torch.cuda.is_available():self.device = torch.device("cuda")else:self.device = torch.device("cpu")# Load class labels from CSV# CSV format: id,Code,Species,Common5 unmodified linesself.model = EfficientNetV2M(num_classes=num_classes,pretrained_weights_path=efficientnet_weights,device_str=str(self.device),tune=False)
inference.py+20-15