Skip to content
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

Remove normalization from bounding box coordinates in yolov8n pytorch #1179

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions tutorials/mct_model_garden/models_pytorch/yolov8/yolov8.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
# The following code was mostly duplicated from https://github.com/ultralytics/ultralytics
# and changed to generate an equivalent PyTorch model suitable for quantization.
# Main changes:
# * Modify layers to make them more suitable for quantization.
# * Modify layers to make them more suitable for quantization
# * torch.fx compatibility
# * Detect head (mainly the box decoding part that was optimized for model quantization)
# * Inheritance class from HuggingFace
# * Implement box decoding into Detect Layer
# ==============================================================================
"""
Yolov8n Object Detection Model - PyTorch implementation

This code contains a PyTorch implementation of Yolov8n object detection model, following
https://github.com/ultralytics/ultralytics. This implementation includes a slightly modified version of yolov8
detection-head (mainly the box decoding part) that was optimized for model quantization.
https://github.com/ultralytics/ultralytics.

Usage:
model, cfg_dict = yolov8_pytorch("yolov8n.yaml")
pretrained_weights = torch.load('/path/to/pretrained/yolov8n.pt')['model'].state_dict()
model.load_state_dict(pretrained_weights, strict=False)
model.eval()

Notes and Limitations:
- The model has been tested only with the default settings from Ultralytics, specifically using a 640x640 input resolution and 80 object classes.
- Anchors and strides are hardcoded as constants within the model, meaning they are not included in the weights file from Ultralytics.

The code is organized as follows:
- Classes definitions of Yolov8n building blocks: Conv, Bottleneck, C2f, SPPF, Upsample, Concaat, DFL and Detect
Expand Down Expand Up @@ -242,12 +253,7 @@ def __init__(self, nc: int = 80,
self.dfl = DFL(self.reg_max) if self.reg_max > 1 else nn.Identity()
anchors, strides = (x.transpose(0, 1) for x in make_anchors(self.feat_sizes,
self.stride, 0.5))
strides = strides / self.img_size
anchors = anchors * strides
self.relu1 = nn.ReLU()
self.relu2 = nn.ReLU()
self.relu3 = nn.ReLU()
self.relu4 = nn.ReLU()

self.register_buffer('anchors', anchors)
self.register_buffer('strides', strides)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"source": [
"from tutorials.mct_model_garden.models_pytorch.yolov8.yolov8 import ModelPyTorch, yaml_load, model_predict\n",
"cfg_dict = yaml_load(\"tutorials/mct_model_garden/models_pytorch/yolov8/yolov8n.yaml\", append_filename=True) # model dict\n",
"model = ModelPyTorch.from_pretrained(\"SSI-DNN/pytorch_yolov8n_640x640_bb_decoding\", cfg=cfg_dict)"
"model = ModelPyTorch.from_pretrained(\"SSI-DNN/pytorch_yolov8n_detection_640x640\", cfg=cfg_dict)"
],
"metadata": {
"collapsed": false
Expand Down Expand Up @@ -445,7 +445,7 @@
"INPUT_RESOLUTION = 640\n",
"\n",
"# Define resizing information to map between the model's output and the original image dimensions\n",
"output_resize = {'shape': (INPUT_RESOLUTION, INPUT_RESOLUTION), 'aspect_ratio_preservation': True}\n",
"output_resize = {'shape': (INPUT_RESOLUTION, INPUT_RESOLUTION), 'aspect_ratio_preservation': True, 'normalized_coords': False}\n",
"\n",
"# Wrapped the model with PostProcess NMS.\n",
"# Define PostProcess params\n",
Expand Down
Loading