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

Add ViTPose #30530

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open

Conversation

NielsRogge
Copy link
Contributor

@NielsRogge NielsRogge commented Apr 28, 2024

What does this PR do?

This PR adds ViTPose as introduced in ViTPose: Simple Vision Transformer Baselines for Human Pose Estimation.

Here's a demo notebook - note that the API might change: https://colab.research.google.com/drive/15_3gjcC0wtKSH85k76zewt81eUJIEWWA?usp=sharing.

To do:

  • get rid of cv2 dependency (?)

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@SangbumChoi
Copy link
Contributor

@NielsRogge Hi Niels! Does this current PR works properly? (I want to do some test for this)

@amyeroberts
Copy link
Collaborator

Thanks for working on this @NielsRogge!

I can see there's a few bits unfinished e.g. tests. Is there a particular bit of code you'd like me to look at for a maintainers perspective?

For the issue description, there's a related model request is here: #24915

@amyeroberts
Copy link
Collaborator

@NielsRogge I'm unsubscribing atm, so that I don't get notifications on every new push. You just need to ping me again with my username when it's ready for review and I'll get notified

@NielsRogge
Copy link
Contributor Author

NielsRogge commented May 16, 2024

It would be great to have a first round of review as the PR is in a ready state. @amyeroberts



name_to_path = {
"vitpose-base-simple": "/Users/nielsrogge/Documents/ViTPose/vitpose-b-simple.pth",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"vitpose-base-simple": "/Users/nielsrogge/Documents/ViTPose/vitpose-b-simple.pth",
"vitpose-base-simple": "https:\/\/4mjpca.sn.files.1drv.com\/y4mip6jbupeZ3YzICoNJYUb6yGEheWXkicKj0tvp1Sfq8BztlH8ieD63z2ZRYiTBzvDxKXFqd_wa5m8NHnBsURmpClZySMSJjS3hxrU2bFArawJ5mAVZsni4LmsfWs_K1dnIzDumXXuanSopYKm0O-Bx5z4JerIfGoE6riAtY_ni5_paFl46jGTE82U8J10Cm3gxHv2DSfOkrgV7SkmUKvnjg\/vitpose-b-simple.pth?download&psid=1",

Copy link
Contributor

Choose a reason for hiding this comment

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

import requests

def download_file(url, local_filename):
    # Sending requests with stream=True allows downloading large files
    with requests.get(url, stream=True) as response:
        response.raise_for_status()  # Raise an exception if the request fails
        with open(local_filename, 'wb') as file:
            # Efficiently download large files by iterating over content in chunks
            for chunk in response.iter_content(chunk_size=8192):
                file.write(chunk)
    return local_filename

# Given URL and the filename to save
url = "https://4mjpca.sn.files.1drv.com/y4mip6jbupeZ3YzICoNJYUb6yGEheWXkicKj0tvp1Sfq8BztlH8ieD63z2ZRYiTBzvDxKXFqd_wa5m8NHnBsURmpClZySMSJjS3hxrU2bFArawJ5mAVZsni4LmsfWs_K1dnIzDumXXuanSopYKm0O-Bx5z4JerIfGoE6riAtY_ni5_paFl46jGTE82U8J10Cm3gxHv2DSfOkrgV7SkmUKvnjg/vitpose-b-simple.pth?download&psid=1"
local_filename = "vitpose-b-simple.pth"

# Execute file download
download_file(url, local_filename)
print(f"File downloaded as {local_filename}")

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can convert this code with the cloud uploaded weight!

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for adding this model!

I've only done a first high-level pass. Normally I'd ask for the backbone to be added in a separate PR, but as the modeling files are relatively small, I think it's OK.

Main comments are about the image processing: poat-processing should take and return torch tensors; there should be more tests to make sure the pre and post processing work on batched inputs and outputs are as expected, particularly for the custom transforms; cv2 logic should be removed

Comment on lines +41 to +44
if is_cv2_available():
# TODO get rid of cv2?
import cv2

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes

Comment on lines +312 to +317
cv2_image = (
image
if input_data_format == ChannelDimension.LAST
else to_channel_dimension_format(image, ChannelDimension.LAST, input_data_format)
)
image = cv2.warpAffine(cv2_image, transformation, size, flags=cv2.INTER_LINEAR)
Copy link
Collaborator

Choose a reason for hiding this comment

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

All cv2 logic should be removed

Comment on lines +318 to +319
# transform image back to input_data_format
image = to_channel_dimension_format(image, input_data_format, ChannelDimension.LAST)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't needed data_format is always set

loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
Classification (or regression if config.num_labels==1) loss.
heatmaps (`torch.FloatTensor` of shape `(batch_size, num_keypoints, height, width)`):
Heatmaps.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could do with a bit of expanding "heatmaps = heatmaps" is a bit redundant

Comment on lines +232 to +234
config.backbone_hidden_size = self.backbone.config.hidden_size
config.image_size = self.backbone.config.image_size
config.patch_size = self.backbone.config.patch_size
Copy link
Collaborator

Choose a reason for hiding this comment

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

These attributes should be checked for and an exception raised if they're not present

def __init__(
self,
backbone_config: PretrainedConfig = None,
backbone=None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Args below should all have typing too

initializer_range (`float`, *optional*, defaults to 0.02):
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
scale_factor (`int`, *optional*, defaults to 4):
Factor to upscale te feature maps coming from the ViT backbone.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Factor to upscale te feature maps coming from the ViT backbone.
Factor to upscale the feature maps coming from the ViT backbone.

@@ -533,6 +533,62 @@ def _center_to_corners_format_tf(bboxes_center: "tf.Tensor") -> "tf.Tensor":
return bboxes_corners


def coco_to_pascal_voc(bboxes: np.ndarray) -> np.ndarray:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets just keep these in the vitpose image processor module.

elif width < aspect_ratio * height:
width = height * aspect_ratio

# pixel std is 200.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Always?


## Overview

The ViTPose model was proposed in [ViTPose: Simple Vision Transformer Baselines for Human Pose Estimation](https://arxiv.org/abs/2204.12484) by Yufei Xu, Jing Zhang, Qiming Zhang, Dacheng Tao. ViTPose employs a standard [Vision Transformer](vit) as backbone for the task of keypoint estimation.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you expand this model explanation a bit please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants