-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom, custom handler that outputs confidence and bbox co-ords.
- Loading branch information
Showing
2 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# based on pytorch's yolov8n example. | ||
|
||
from collections import defaultdict | ||
import os | ||
|
||
import torch | ||
from torchvision import transforms | ||
from ultralytics import YOLO | ||
|
||
from ts.torch_handler.object_detector import ObjectDetector | ||
|
||
IMG_SIZE = 640 | ||
|
||
try: | ||
import torch_xla.core.xla_model as xm | ||
|
||
XLA_AVAILABLE = True | ||
except ImportError as error: | ||
XLA_AVAILABLE = False | ||
|
||
|
||
class Yolov8Handler(ObjectDetector): | ||
image_processing = transforms.Compose( | ||
[ | ||
transforms.Resize(IMG_SIZE), | ||
transforms.CenterCrop(IMG_SIZE), | ||
transforms.ToTensor(), | ||
] | ||
) | ||
|
||
def initialize(self, context): | ||
if torch.cuda.is_available(): | ||
self.device = torch.device("cuda") | ||
elif XLA_AVAILABLE: | ||
self.device = xm.xla_device() | ||
else: | ||
self.device = torch.device("cpu") | ||
|
||
properties = context.system_properties | ||
self.manifest = context.manifest | ||
model_dir = properties.get("model_dir") | ||
self.model_pt_path = None | ||
if "serializedFile" in self.manifest["model"]: | ||
serialized_file = self.manifest["model"]["serializedFile"] | ||
self.model_pt_path = os.path.join(model_dir, serialized_file) | ||
self.model = self._load_torchscript_model(self.model_pt_path) | ||
self.initialized = True | ||
|
||
def _load_torchscript_model(self, model_pt_path): | ||
"""Loads the PyTorch model and returns the NN model object. | ||
Args: | ||
model_pt_path (str): denotes the path of the model file. | ||
Returns: | ||
(NN Model Object) : Loads the model object. | ||
""" | ||
# TODO: remove this method if https://github.com/pytorch/text/issues/1793 gets resolved | ||
|
||
model = YOLO(model_pt_path) | ||
model.to(self.device) | ||
return model | ||
|
||
def postprocess(self, res): | ||
output = [] | ||
for data in res: | ||
result_dict = defaultdict(list) | ||
for cls, conf, xywh in zip( | ||
data.boxes.cls.tolist(), data.boxes.conf, data.boxes.xywh | ||
): | ||
name = data.names[int(cls)] | ||
result_dict[name].append({"conf": conf.item(), "xywh": xywh.tolist()}) | ||
output.append(result_dict) | ||
return output |