-
Notifications
You must be signed in to change notification settings - Fork 871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature/add torchserve detectron2 #3355
Open
Mudassar-MLE
wants to merge
9
commits into
pytorch:master
Choose a base branch
from
Mudassar-MLE:feature/torchserve-detectron2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f45e5ea
added requirements.txt.
Mudassar-MLE f81c035
added detectron2 handler file.
Mudassar-MLE 3dd5ce5
added readme.md file.
Mudassar-MLE c48e52b
Merge branch 'master' into feature/torchserve-detectron2
Mudassar-MLE 65c6715
updated readme file.
Mudassar-MLE 7f85f0a
Merge branch 'master' into feature/torchserve-detectron2
Mudassar-MLE 715d1be
updated readme file and used time from utility.
Mudassar-MLE c441bdf
Merge branch 'feature/torchserve-detectron2' of https://github.com/Mu…
Mudassar-MLE 7c3c87e
Merge branch 'master' into feature/torchserve-detectron2
Mudassar-MLE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Object Detection using torchvision's pretrained fast-rcnn model | ||
|
||
* Download the pre-trained fast-rcnn object detection model's state_dict from the following URL : | ||
|
||
https://download.pytorch.org/models/fasterrcnn_resnet50_fpn_coco-258fb6c6.pth | ||
|
||
```bash | ||
wget https://download.pytorch.org/models/fasterrcnn_resnet50_fpn_coco-258fb6c6.pth | ||
``` | ||
|
||
* Create a model archive file and serve the fastrcnn model in TorchServe using below commands | ||
|
||
```bash | ||
torch-model-archiver --model-name fastrcnn --version 1.0 --model-file examples/object_detector/fast-rcnn/model.py --serialized-file fasterrcnn_resnet50_fpn_coco-258fb6c6.pth --handler object_detector --extra-files examples/object_detector/index_to_name.json | ||
mkdir model_store | ||
mv fastrcnn.mar model_store/ | ||
torchserve --start --model-store model_store --models fastrcnn=fastrcnn.mar --disable-token-auth --enable-model-api | ||
curl http://127.0.0.1:8080/predictions/fastrcnn -T examples/object_detector/detectron2/person.jpg | ||
``` | ||
* Note : The objects detected have scores greater than "0.5". This threshold value is set in object_detector handler. | ||
|
||
* Output | ||
|
||
```json | ||
[ | ||
{ | ||
"person": [ | ||
362.34539794921875, | ||
161.9876251220703, | ||
515.53662109375, | ||
385.2342834472656 | ||
], | ||
"score": 0.9977679252624512 | ||
}, | ||
{ | ||
"handbag": [ | ||
67.37423706054688, | ||
277.63787841796875, | ||
111.6810073852539, | ||
400.26470947265625 | ||
], | ||
"score": 0.9925485253334045 | ||
}, | ||
{ | ||
"handbag": [ | ||
228.7159423828125, | ||
145.87753295898438, | ||
303.5065612792969, | ||
231.10513305664062 | ||
], | ||
"score": 0.9921919703483582 | ||
} | ||
] | ||
``` |
249 changes: 249 additions & 0 deletions
249
examples/object_detector/detectron2/detectron2-handler.py
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,249 @@ | ||
import io | ||
import json | ||
import torch | ||
import logging | ||
import numpy as np | ||
from os import path | ||
from detectron2.config import get_cfg | ||
from ts.handler_utils.timer import timed | ||
from PIL import Image, UnidentifiedImageError | ||
from detectron2.engine import DefaultPredictor | ||
from detectron2.utils.logger import setup_logger | ||
try: | ||
import pillow_heif | ||
import pillow_avif | ||
import pillow_jxl | ||
# Register openers for extended formats | ||
pillow_heif.register_heif_opener() | ||
# For pillow_avif and pillow_jxl, openers are registered upon import | ||
except ImportError as e: | ||
raise ImportError( | ||
"Please install 'pillow-heif', 'pillow-avif', and 'pillow-jxl' to handle extended image formats. " | ||
f"Missing package error: {e}" | ||
) | ||
######################################################################################################################################## | ||
setup_logger() | ||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
######################################################################################################################################## | ||
class ModelHandler: | ||
""" | ||
A base ModelHandler implementation for loading and running Detectron2 models with TorchServe. | ||
Compatible with both CPU and GPU. | ||
""" | ||
def __init__(self): | ||
""" | ||
Initialize the ModelHandler instance. | ||
""" | ||
self.error = None | ||
self._context = None | ||
self._batch_size = 0 | ||
self.initialized = False | ||
self.predictor = None | ||
self.model_file = "model.pth" | ||
self.config_file = "config.yaml" | ||
self.device = "cpu" | ||
if torch.cuda.is_available(): | ||
self.device = "cuda" | ||
logger.info("Using GPU for inference.") | ||
else: | ||
logger.info("Using CPU for inference.") | ||
|
||
def initialize(self, context): | ||
""" | ||
Load the model and initialize the predictor. | ||
Args: | ||
context (Context): Initial context contains model server system properties. | ||
""" | ||
logger.info("Initializing model...") | ||
|
||
self._context = context | ||
self._batch_size = context.system_properties.get("batch_size", 1) | ||
model_dir = context.system_properties.get("model_dir") | ||
model_path = path.join(model_dir, self.model_file) | ||
config_path = path.join(model_dir, self.config_file) | ||
logger.debug(f"Checking model file: {model_path} exists: {path.exists(model_path)}") | ||
logger.debug(f"Checking config file: {config_path} exists: {path.exists(config_path)}") | ||
if not path.exists(model_path): | ||
error_msg = f"Model file {model_path} does not exist." | ||
logger.error(error_msg) | ||
self.error = error_msg | ||
self.initialized = False | ||
return | ||
if not path.exists(config_path): | ||
error_msg = f"Config file {config_path} does not exist." | ||
logger.error(error_msg) | ||
self.error = error_msg | ||
self.initialized = False | ||
return | ||
try: | ||
cfg = get_cfg() | ||
cfg.merge_from_file(config_path) | ||
cfg.MODEL.WEIGHTS = model_path | ||
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 | ||
cfg.MODEL.DEVICE = self.device | ||
self.predictor = DefaultPredictor(cfg) | ||
logger.info("Predictor initialized successfully.") | ||
if self.predictor is None: | ||
raise RuntimeError("Predictor initialization failed, the predictor is None.") | ||
self.initialized = True | ||
logger.info("Model initialization complete.") | ||
except Exception as e: | ||
error_msg = "Error during model initialization" | ||
logger.exception(error_msg) | ||
self.error = str(e) | ||
self.initialized = False | ||
|
||
@timed | ||
def preprocess(self, batch): | ||
""" | ||
Transform raw input into model input data. | ||
|
||
Args: | ||
batch (List[Dict]): List of raw requests, should match batch size. | ||
|
||
Returns: | ||
List[np.ndarray]: List of preprocessed images. | ||
""" | ||
logger.info(f"Pre-processing started for a batch of {len(batch)}.") | ||
|
||
images = [] | ||
for idx, request in enumerate(batch): | ||
request_body = request.get("body") | ||
if request_body is None: | ||
error_msg = f"Request {idx} does not contain 'body'." | ||
logger.error(error_msg) | ||
raise ValueError(error_msg) | ||
try: | ||
image_stream = io.BytesIO(request_body) | ||
try: | ||
pil_image = Image.open(image_stream) | ||
pil_image = pil_image.convert("RGB") | ||
img = np.array(pil_image) | ||
img = img[:, :, ::-1] | ||
except UnidentifiedImageError as e: | ||
error_msg = f"Failed to identify image for request {idx}. Error: {e}" | ||
logger.error(error_msg) | ||
raise ValueError(error_msg) | ||
except Exception as e: | ||
error_msg = f"Failed to decode image for request {idx}. Error: {e}" | ||
logger.error(error_msg) | ||
raise ValueError(error_msg) | ||
images.append(img) | ||
except Exception as e: | ||
logger.exception(f"Error preprocessing request {idx}") | ||
raise e | ||
logger.info(f"Pre-processing finished for a batch of {len(batch)}.") | ||
return images | ||
|
||
@timed | ||
def inference(self, model_input): | ||
""" | ||
Perform inference on the model input. | ||
|
||
Args: | ||
model_input (List[np.ndarray]): List of preprocessed images. | ||
|
||
Returns: | ||
List[Dict]: List of inference outputs. | ||
""" | ||
logger.info(f"Inference started for a batch of {len(model_input)}.") | ||
|
||
outputs = [] | ||
for idx, image in enumerate(model_input): | ||
try: | ||
logger.debug(f"Processing image {idx}: shape={image.shape}, dtype={image.dtype}") | ||
output = self.predictor(image) | ||
outputs.append(output) | ||
except Exception as e: | ||
logger.exception(f"Error during inference on image {idx}") | ||
raise e | ||
logger.info(f"Inference finished for a batch of {len(model_input)}.") | ||
return outputs | ||
@timed | ||
def postprocess(self, inference_outputs): | ||
""" | ||
Post-process the inference outputs to a serializable format. | ||
|
||
Args: | ||
inference_outputs (List[Dict]): List of inference outputs. | ||
|
||
Returns: | ||
List[str]: List of JSON strings containing predictions. | ||
""" | ||
logger.info(f"Post-processing for a batch of {len(inference_outputs)}.") | ||
responses = [] | ||
for idx, output in enumerate(inference_outputs): | ||
try: | ||
predictions = output["instances"].to("cpu") | ||
logger.debug(f"Available prediction fields: {predictions.get_fields().keys()}") | ||
response = {} | ||
if predictions.has("pred_classes"): | ||
classes = predictions.pred_classes.numpy().tolist() | ||
response["classes"] = classes | ||
if predictions.has("pred_boxes"): | ||
boxes = predictions.pred_boxes.tensor.numpy().tolist() | ||
response["boxes"] = boxes | ||
if predictions.has("scores"): | ||
scores = predictions.scores.numpy().tolist() | ||
response["scores"] = scores | ||
if predictions.has("pred_masks"): | ||
response["masks_present"] = True | ||
responses.append(json.dumps(response)) | ||
except Exception as e: | ||
logger.exception(f"Error during post-processing of output {idx}") | ||
raise e | ||
logger.info(f"Post-processing finished for a batch of {len(inference_outputs)}.") | ||
|
||
return responses | ||
|
||
@timed | ||
def handle(self, data, context): | ||
""" | ||
Entry point for TorchServe to interact with the ModelHandler. | ||
|
||
Args: | ||
data (List[Dict]): Input data. | ||
context (Context): Model server context. | ||
|
||
Returns: | ||
List[str]: List of predictions. | ||
""" | ||
logger.info("Handling request...") | ||
if not self.initialized: | ||
self.initialize(context) | ||
if not self.initialized: | ||
error_message = f"Model failed to initialize: {self.error}" | ||
logger.error(error_message) | ||
return [error_message] | ||
|
||
if data is None: | ||
error_message = "No data received for inference." | ||
logger.error(error_message) | ||
return [error_message] | ||
|
||
try: | ||
model_input = self.preprocess(data) | ||
model_output = self.inference(model_input) | ||
output = self.postprocess(model_output) | ||
return output | ||
except Exception as e: | ||
error_message = f"Error in handling request: {str(e)}" | ||
logger.exception(error_message) | ||
return [error_message] | ||
######################################################################################################################################## | ||
_service = ModelHandler() | ||
|
||
def handle(data, context): | ||
""" | ||
Entry point for TorchServe to interact with the ModelHandler. | ||
|
||
Args: | ||
data (List[Dict]): Input data. | ||
context (Context): Model server context. | ||
|
||
Returns: | ||
List[str]: List of predictions. | ||
""" | ||
return _service.handle(data, context) | ||
######################################################################################################################################## |
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,14 @@ | ||
opencv-python==4.10.0.84 | ||
python-multipart==0.0.9 | ||
torch==2.2.0 | ||
torchvision==0.17.0 | ||
transformers==4.44.2 | ||
torchvision==0.17.0 | ||
numpy==1.24.4 | ||
torchserve==0.12.0 | ||
torch-model-archiver==0.12.0 | ||
torch-workflow-archiver==0.2.15 | ||
pillow==11.0.0 | ||
pillow-avif-plugin==1.4.6 | ||
pillow-jxl-plugin==1.2.8 | ||
pillow_heif==0.20.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can detectron2 process a batch of images? Can we send the batch instead of looping over each image
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Detectron2 can process a batch of images, and we can send them.