-
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
f45e5ea
f81c035
3dd5ce5
c48e52b
65c6715
7f85f0a
715d1be
c441bdf
7c3c87e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Object Detection using TorchServe and Detectron2 | ||
|
||
## Overview | ||
|
||
This folder leverages **TorchServe** to deploy a Detectron2-based object detection model using a custom handler. It provides scalable and efficient object detection capabilities with support for both CPU and GPU environments. | ||
|
||
--- | ||
|
||
## Table of Contents | ||
|
||
1. [Pre-requirements](#pre-requirements) | ||
2. [Installation](#installation) | ||
3. [Usage](#usage) | ||
4. [Documentation](#documentation) | ||
5. [Contributors](#contributors) | ||
|
||
--- | ||
|
||
## Pre-requirements | ||
|
||
- **Python 3.8 or higher** (tested on Python 3.10.15). | ||
|
||
--- | ||
|
||
## Installation | ||
|
||
Follow these steps to set up the project: | ||
|
||
1. Clone the repository: | ||
|
||
```bash | ||
git clone https://github.com/pytorch/serve.git | ||
``` | ||
|
||
2. Make sure the terminal's current directory is set to the folder where this README file is located: | ||
|
||
```bash | ||
cd serve/examples/object_detector/detectron2 | ||
``` | ||
|
||
3. Install dependencies: | ||
|
||
```bash | ||
pip install -r requirements.txt | ||
pip install git+https://github.com/facebookresearch/detectron2.git && pip install numpy==1.21.6 | ||
``` | ||
|
||
--- | ||
|
||
## Usage | ||
|
||
Refer to the [Documentation](#documentation) for detailed usage instructions. | ||
|
||
--- | ||
|
||
## Documentation | ||
|
||
For detailed information on using TorchServe and Detectron2 for object detection, refer to the documentation provided in the [Upstart Commerce Blog](https://upstartcommerce.com/optimizing-pytorch-model-serving-at-scale-with-torchserve/). | ||
|
||
--- | ||
|
||
## Contributors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this section |
||
|
||
- **[Muhammad Mudassar](https://github.com/Mudassar-MLE)** | ||
- [LinkedIn](https://www.linkedin.com/in/muhammad-mudassar-a65645192/) | ||
- [Email](mailto:[email protected]) | ||
--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
import io | ||
import json | ||
import time | ||
import torch | ||
import logging | ||
import numpy as np | ||
from os import path | ||
from detectron2.config import get_cfg | ||
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 | ||
|
||
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 | ||
|
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
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 | ||
|
||
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. | ||
""" | ||
start_time = time.time() | ||
logger.info(f"Post-processing started at {start_time} 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 | ||
elapsed_time = time.time() - start_time | ||
logger.info(f"Post-processing finished for a batch of {len(inference_outputs)} in {elapsed_time:.2f} seconds.") | ||
|
||
return responses | ||
|
||
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...") | ||
start_time = time.time() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have a utility to measure the time, please check this handler |
||
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: | ||
preprocess_start = time.time() | ||
model_input = self.preprocess(data) | ||
preprocess_time = time.time() - preprocess_start | ||
|
||
inference_start = time.time() | ||
model_output = self.inference(model_input) | ||
inference_time = time.time() - inference_start | ||
|
||
postprocess_start = time.time() | ||
output = self.postprocess(model_output) | ||
postprocess_time = time.time() - postprocess_start | ||
|
||
total_time = time.time() - start_time | ||
logger.info( | ||
f"Handling request finished in {total_time:.2f} seconds. " | ||
f"(Preprocess: {preprocess_time:.2f}s, " | ||
f"Inference: {inference_time:.2f}s, " | ||
f"Postprocess: {postprocess_time:.2f}s)" | ||
) | ||
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) | ||
######################################################################################################################################## |
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 |
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.
Please take a look at other examples and show all the steps in this README. Anyone should be able to replicate the example looking at just the README.
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.
Thank you for the guidance! I’ve now updated the README file.