-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Support for transformers v5 post_process functions #1386
Conversation
Hi @shaddu 👋🏻 Thank you for submitting the PR. I have a few questions regarding the scope of the changes that have been and will need to be made in |
Hi @SkalskiP , Thank you for reviewing the PR and providing your comments. The scope was to migrate |
@shaddu I like what you have done so far! Both code quality and functionality look good to me. From my perspective, the most important thing is that I think you can continue the migration in this PR. |
…ntation + post_process_instance_segmentation
Hi @SkalskiP , I have finished adding support for the remaining function. Could you please review it and offer feedback? Thank you |
Hi @shaddu Thank you for your contribution! I'll check this today; hopefully we can merge soon. |
supervision/detection/core.py
Outdated
@@ -483,11 +484,40 @@ class names. If provided, the resulting Detections object will contain | |||
Class names values can be accessed using `detections["class_name"]`. |
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.
We need to update the from_transformers
docstring.
Creates a Detections instance from object detection or segmentation Transformer inference result.
I'd mention we support object detection as well as panoptic, semantic and instance segmentation results.
transformers_results (dict): The output of Transformers model inference. A dictionary containing the
scores
,labels
,boxes
andmasks
keys.
Now it can also be a Tensor
.
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.
Hi @SkalskiP , One quick query, ideally it should now be transformers_results (Union[dict, torch.Tensor])
but to support this we need to import the torch package, should we add the import or leave it in docstrings for now?
supervision/detection/core.py
Outdated
@@ -483,11 +484,40 @@ class names. If provided, the resulting Detections object will contain | |||
Class names values can be accessed using `detections["class_name"]`. | |||
""" # noqa: E501 // docs | |||
|
|||
class_ids = transformers_results["labels"].cpu().detach().numpy().astype(int) | |||
data = {} |
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.
In from_transformers
, there are four places where we do:
if id2label is not None:
class_names = np.array([id2label[class_id] for class_id in class_ids])
data[CLASS_NAME_DATA_FIELD] = class_names
I'd wrap it in a local helper function. It can be defined inside from_transformers
.
def get_data(class_ids: np.ndarray, id2label: Optional[Dict[int, str]]) -> dict:
data = {}
if id2label is not None:
class_names = np.array([id2label[class_id] for class_id in class_ids])
data[CLASS_NAME_DATA_FIELD] = class_names
return data
supervision/detection/core.py
Outdated
mask=masks, | ||
class_id=class_ids, | ||
data=data, | ||
) | ||
else: |
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.
I think we can drop this else
statement.
supervision/detection/core.py
Outdated
|
||
if transformers_results.__class__.__name__ == "Tensor": | ||
segmentation_array = transformers_results.cpu().detach().numpy() | ||
|
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.
Let's make it more compact and drop those extra new lines.
supervision/detection/utils.py
Outdated
Convert a PNG byte string to a binary mask array. | ||
|
||
Args: | ||
- png_string (bytes): A byte string representing the PNG 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.
This docstring format is incompatible with rest of docstings in the project.
supervision/detection/utils.py
Outdated
""" | ||
image = Image.open(io.BytesIO(png_string)) | ||
mask = np.array(image, dtype=np.uint8) | ||
|
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.
Let's drop this extra new line.
supervision/detection/core.py
Outdated
@@ -504,6 +534,60 @@ class names. If provided, the resulting Detections object will contain | |||
class_id=class_ids, | |||
data=data, | |||
) | |||
elif "segments_info" in transformers_results: |
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.
Personally, I would move segments_info = transformers_results["segments_info"]
extraction to if "segmentation" in transformers_results:
and elif "png_string" in transformers_results:
blocks. We will get one indention less.
Hi @shaddu 👋🏻 Awesome work here. I left some comments regarding the current version of the code, but here are some high-level overviews ones:
In general this looks really good already. Thanks a lot for all the help! 🙏🏻 |
Hi @shaddu, Here's a Colab that may help as a starting point. I've got examples of every segmentation type, except for instance segmentation. It would be great to also test with a few other models - not just a panoptic segmentation one. Lastly, I'm curious: does panoptic segmentation add anything to the |
Hi @shaddu 👋🏻 ! Awesome work! I added small changes - changed names of functions and improved docstrings. We are ready to merge! Thanks a lot for the contribution! 🙏🏻 Supporting |
@SkalskiP Thank you for merging the PR. It was a great learning experience. I'll look for more issues to work on in Supervision and if you come across any related issues, I'd be happy to help with them. |
@shaddu, working with you was an awesome experience! 🔥 |
Description
post_process_segmentation
is getting depreciated in transformers version 5. Code changes added are to supportpost_process_semantic_segmentation
Type of change
Please delete options that are not relevant.
How has this change been tested, please provide a testcase or example of how you tested the change?
Detection test Colab
Any specific deployment considerations
No