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
base: main
Are you sure you want to change the base?
Add ViTPose #30530
Conversation
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. |
@NielsRogge Hi Niels! Does this current PR works properly? (I want to do some test for this) |
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 |
@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 |
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", |
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.
"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", |
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.
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}")
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 convert this code with the cloud uploaded weight!
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.
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
if is_cv2_available(): | ||
# TODO get rid of cv2? | ||
import cv2 | ||
|
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.
Yes
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) |
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.
All cv2 logic should be removed
# transform image back to input_data_format | ||
image = to_channel_dimension_format(image, input_data_format, ChannelDimension.LAST) |
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 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. |
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 could do with a bit of expanding "heatmaps = heatmaps" is a bit redundant
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 |
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.
These attributes should be checked for and an exception raised if they're not present
def __init__( | ||
self, | ||
backbone_config: PretrainedConfig = None, | ||
backbone=None, |
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.
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. |
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.
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: |
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.
Lets just keep these in the vitpose image processor module.
elif width < aspect_ratio * height: | ||
width = height * aspect_ratio | ||
|
||
# pixel std is 200.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.
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. |
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.
Could you expand this model explanation a bit please?
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: