Skip to content

Adding yolov8 masks to detections - #101

Merged
SkalskiP merged 6 commits into
roboflow:mainfrom
hardikdava:main
Jun 6, 2023
Merged

SkalskiP merged 6 commits into
roboflow:mainfrom
hardikdava:main

Conversation

@hardikdava

Copy link
Copy Markdown
Contributor

Description

Current Detection class only support for yolov8 object detection. Segmentation masks information are missing.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How has this change been tested, please provide a testcase or example of how you tested the change?

model = YOLO(model="yolov8s.pt")
results = model.predict(img)[0]
detections = sv.Detections.from_yolov8(results)

mask_annotator = sv.MaskAnnotator()
box_annotator = sv.BoxAnnotator()

img = box_annotator.annotate(scene=img, detections=detections)
img = mask_annotator.annotate(scene=img, detections=detections)

Docs

  • Docs updated? What were the changes:

Comment thread supervision/detection/core.py Outdated
"""
masks = None
if yolov8_results.masks:
from ultralytics.yolo.utils.ops import scale_image

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi 馃憢馃徎 @hardikdava! We can't use ultralytics code directly like that :/ The moment we will do it, we need to change the LICENSE of supervision to a very restrictive YOLOv8 LICENSE.

But I took a look at the scale_image implementation. A lot of it is padding logic that we don't use. And if I'm not mistaken, all we need is :

masks = cv2.resize(masks, (im0_shape[1], im0_shape[0]))

Am I right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, @SkalskiP . I tested by just using cv2.resize function. It works fine without using any functionality from ultralytics. I will make necessary changes.

@SkalskiP

Copy link
Copy Markdown
Collaborator

Hi, @hardikdava 馃憢馃徎! I took a look at the PR. I wanted to add this myself, so the PR is very much welcomed. I see one problem, however. We cant use ultralytics package directly in our code because the moment we do it, we need to change our license from MIT to a very restrictive YOLOv8 license. Please take a look at my comment for more details.

@SkalskiP SkalskiP added the enhancement New feature or request label May 18, 2023
@hardikdava

Copy link
Copy Markdown
Contributor Author

@SkalskiP I made necessary changes. Now take a look and let me know if I need to change anything else.

@SkalskiP

Copy link
Copy Markdown
Collaborator

Hi @hardikdava! First of all sorry for such a late response. I was quite busy. The code looks good. 馃挏 Let me test it quickly and if it works we are merging.

@SkalskiP

Copy link
Copy Markdown
Collaborator

Hi, @hardikdava just tested, and I'm getting: NameError: name 'cv2' is not defined. Looks like you didn't do the cv2 import at the top of the file.

Comment thread supervision/detection/core.py Outdated
if yolov8_results.masks:
masks = yolov8_results.masks.masks.cpu().numpy() # masks, (N, H, W)
masks = np.moveaxis(masks, 0, -1) # masks, (H, W, N)
masks = cv2.resize(masks, (yolov8_results.masks.orig_shape[1], yolov8_results.masks.orig_shape[0]))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I run the code I get:

0: 640x384 1 person, 1 car, 1 dog, 1 backpack, 1 handbag, 153.3ms
Speed: 15.4ms preprocess, 153.3ms inference, 37.7ms postprocess per image at shape (1, 3, 640, 640)
WARNING 鈿狅笍 'Masks.masks' is deprecated. Use 'Masks.data' instead.
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
[<ipython-input-10-6c1542ea3727>](https://localhost:8080/#) in <cell line: 2>()
      1 results = model.predict(source=image)[0]
----> 2 detections = sv.Detections.from_yolov8(results)

[/content/supervision/supervision/detection/core.py](https://localhost:8080/#) in from_yolov8(cls, yolov8_results)
    192             masks = yolov8_results.masks.masks.cpu().numpy()  # masks, (N, H, W)
    193             masks = np.moveaxis(masks, 0, -1)  # masks, (H, W, N)
--> 194             masks = cv2.resize(masks, (yolov8_results.masks.orig_shape[1], yolov8_results.masks.orig_shape[0]))
    195             masks = np.moveaxis(masks, -1, 0)  # masks, (N, H, W)
    196         return cls(

NameError: name 'cv2' is not defined

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SkalskiP I have fixed the error. Please feel free to change any part of the code.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me test once again.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something still does not work as expected. I've done some tests. Here are two images. First produced by YOLOv8 annotators and second by our annotators. Looks like masks are not scaled properly for some reason.

  • YOLOv8 result:

2fb4d321-88a5-4f0b-b9b3-ced5c5fe12ee

  • Supervision result:

download - 2023-05-23T192244 934

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SkalskiP it is due to letterbox. Padding is not zero in this case. We need an information of inference input size.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hardikdava is it possible to extract that info from results?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SkalskiP I rewrote the logic of mask scalling. Please test it again. I have tested it and attached the results (left=supervision result, right=yolov8 result).

bus_res
zidane_res

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SkalskiP any update? I think we are ready to merge it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @hardikdava 馃憢馃徎 ! There are a few formatting changes I'd do, but I don't want to bother you with those. I'll take care of it on my own. We are merging.

- gain and pads are calculated using yolov8 results.
- resize masks to input image size
@SkalskiP
SkalskiP merged commit 1c043d6 into roboflow:main Jun 6, 2023
@SkalskiP SkalskiP mentioned this pull request Jun 6, 2023
3 tasks
@danigarciaoca danigarciaoca mentioned this pull request Jun 14, 2023
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants