-
Notifications
You must be signed in to change notification settings - Fork 33.6k
Add native masked MSE loss for Sapiens2ForPoseEstimation #46764
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
Open
Sainava
wants to merge
12
commits into
huggingface:main
Choose a base branch
from
Sainava:feat/46518-sapiens2-pose-loss
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
181ff3e
Add native masked MSE loss for Sapiens2ForPoseEstimation (#46518)
Sainava 49b0033
Fix trailing whitespace in test file
Sainava d83872e
Merge branch 'main' into feat/46518-sapiens2-pose-loss
Sainava 8d8a847
Add supervised fine-tuning documentation for Sapiens2 pose estimation
Sainava fcfa574
Fix method binding bug on _loss_function by wrapping in staticmethod
Sainava f996ad9
Merge branch 'main' into feat/46518-sapiens2-pose-loss
Sainava 4a99e4c
Merge branch 'main' into feat/46518-sapiens2-pose-loss
Sainava cdf9695
Switch test to floats_tensor and fix docs heatmap dimensions
Sainava 35ce0bc
Merge branch 'main' into feat/46518-sapiens2-pose-loss
Sainava 5056650
Simplify loss function by relying on upstream weight expansion
Sainava e94d79f
Update dummy label_weights shape in tests to match heatmaps
Sainava 840b2c3
Centralize loss function and test backward pass
Sainava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,19 +183,39 @@ def create_and_check_for_semantic_segmentation(self, config, pixel_values, label | |
| (self.batch_size, config.num_labels, expected_h, expected_h), | ||
| ) | ||
|
|
||
| def create_and_check_for_pose_estimation(self, config, pixel_values, labels): | ||
| def create_and_check_for_pose_estimation(self, config, pixel_values, labels, label_weights): | ||
| model = Sapiens2ForPoseEstimation(config) | ||
| model.to(torch_device) | ||
| model.eval() | ||
|
|
||
| with torch.no_grad(): | ||
| result = model(pixel_values) | ||
|
|
||
| patch_height = self.image_size // self.patch_size | ||
| expected_h = patch_height * (2 ** len(config.head_config.upsample_out_channels)) | ||
|
|
||
| self.parent.assertEqual( | ||
| result.heatmaps.shape, | ||
| (self.batch_size, config.num_labels, expected_h, expected_h), | ||
| ) | ||
|
|
||
| with torch.no_grad(): | ||
| result_with_loss = model( | ||
| pixel_values, | ||
| labels=labels, | ||
| ) | ||
|
|
||
| self.parent.assertIsNotNone(result_with_loss.loss) | ||
|
|
||
| with torch.no_grad(): | ||
| result_with_weights = model( | ||
| pixel_values, | ||
| labels=labels, | ||
| label_weights=label_weights, | ||
| ) | ||
|
|
||
| self.parent.assertIsNotNone(result_with_weights.loss) | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imo we should maybe also check that the backward doesnt result in runtime error so would avoid the no grads here and call a backward |
||
| def create_and_check_for_normal_estimation(self, config, pixel_values, labels): | ||
| model = Sapiens2ForNormalEstimation(config) | ||
| model.to(torch_device) | ||
|
|
@@ -278,6 +298,18 @@ def prepare_config_and_inputs_for_common(self): | |
| inputs_dict = {"pixel_values": pixel_values} | ||
| return config, inputs_dict | ||
|
|
||
| def prepare_config_and_inputs_for_pose_estimation(self): | ||
| config_and_inputs = self.prepare_config_and_inputs() | ||
| config, pixel_values = config_and_inputs[0], config_and_inputs[1] | ||
|
|
||
| patch_height = self.image_size // self.patch_size | ||
| expected_h = patch_height * (2 ** len(config.head_config.upsample_out_channels)) | ||
|
|
||
| labels = floats_tensor([self.batch_size, config.num_labels, expected_h, expected_h]) | ||
| label_weights = floats_tensor([self.batch_size, config.num_labels, expected_h, expected_h]) | ||
|
|
||
| return config, pixel_values, labels, label_weights | ||
|
|
||
|
|
||
| @require_torch | ||
| class Sapiens2ModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase): | ||
|
|
@@ -351,7 +383,7 @@ def test_for_semantic_segmentation(self): | |
| self.model_tester.create_and_check_for_semantic_segmentation(*config_and_inputs) | ||
|
|
||
| def test_for_pose_estimation(self): | ||
| config_and_inputs = self.model_tester.prepare_config_and_inputs_for_semantic_segmentation() | ||
| config_and_inputs = self.model_tester.prepare_config_and_inputs_for_pose_estimation() | ||
| self.model_tester.create_and_check_for_pose_estimation(*config_and_inputs) | ||
|
|
||
| def test_for_pointmap_estimation(self): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
please lets add this to
transformers/src/transformers/loss/loss_utils.py
Line 172 in 49f6b78
so something along
"Sapiens2ForPoseEstimation": torch.nn.functional.mse_loss,