Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /autonomi-ai_nos /nos /models /faster_rcnn.py
| from dataclasses import dataclass | |
| from typing import Dict, List, Union | |
| import numpy as np | |
| import torch | |
| import torchvision.transforms.functional as F | |
| from PIL import Image | |
| from nos import hub | |
| from nos.common import ImageSpec, TaskType, TensorSpec | |
| from nos.common.io import prepare_images | |
| from nos.common.types import Batch, ImageT, TensorT | |
| from nos.hub import TorchHubConfig | |
| class FasterRCNNConfig(TorchHubConfig): | |
| pass | |
| class FasterRCNN: | |
| """FasterRCNN model from torchvision""" | |
| configs = { | |
| "torchvision/fasterrcnn-mobilenet-v3-large-320-fpn": FasterRCNNConfig( | |
| model_name="torchvision/fasterrcnn_mobilenet_v3_large_320_fpn", | |
| repo="pytorch/vision", | |
| ), | |
| } | |
| def __init__(self, model_name: str = "torchvision/fasterrcnn-mobilenet-v3-large-320-fpn"): | |
| from torchvision.models.detection import fasterrcnn_mobilenet_v3_large_320_fpn | |
| try: | |
| self.cfg = FasterRCNN.configs[model_name] | |
| except KeyError: | |
| raise ValueError(f"Invalid model_name: {model_name}, available models: {FasterRCNN.configs.keys()}") | |
| model_name = self.cfg.model_name | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| self.model = fasterrcnn_mobilenet_v3_large_320_fpn(weights="DEFAULT").to(self.device) | |
| self.model.eval() | |
| def __call__( | |
| self, images: Union[Image.Image, np.ndarray, List[Image.Image], List[np.ndarray]] | |
| ) -> Dict[str, np.ndarray]: | |
| """Predict bounding boxes for images.""" | |
| images = prepare_images(images) | |
| with torch.inference_mode(): | |
| images = torch.stack([F.to_tensor(image) for image in images]) | |
| images = images.to(self.device) | |
| predictions = self.model(images) | |
| return { | |
| "bboxes": [pred["boxes"].cpu().numpy() for pred in predictions], | |
| "scores": [pred["scores"].cpu().numpy() for pred in predictions], | |
| "labels": [pred["labels"].cpu().numpy().astype(np.int32) for pred in predictions], | |
| } | |
| hub.register( | |
| "torchvision/fasterrcnn-mobilenet-v3-large-320-fpn", | |
| TaskType.OBJECT_DETECTION_2D, | |
| FasterRCNN, | |
| init_args=("torchvision/fasterrcnn-mobilenet-v3-large-320-fpn",), | |
| method="__call__", | |
| inputs={ | |
| "images": Union[ | |
| Batch[ImageT[Image.Image, ImageSpec(shape=(480, 640, 3), dtype="uint8")], 16], | |
| Batch[ImageT[Image.Image, ImageSpec(shape=(960, 1280, 3), dtype="uint8")], 4], | |
| ] | |
| }, | |
| outputs={ | |
| "bboxes": Batch[TensorT[np.ndarray, TensorSpec(shape=(None, 4), dtype="float32")]], | |
| "scores": Batch[TensorT[np.ndarray, TensorSpec(shape=(None), dtype="float32")]], | |
| "labels": Batch[TensorT[np.ndarray, TensorSpec(shape=(None), dtype="int32")]], | |
| }, | |
| ) | |
Xet Storage Details
- Size:
- 2.83 kB
- Xet hash:
- 114bac5dd8b5f4de86fad59ce0e77f12aa1c2d0be9f2d757bfad091bc0efc0f6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.