Fix #518: pass non-tensor metadata through get_data_shape() - #619
Open
prawnsgupta wants to merge 1 commit into
Open
Fix #518: pass non-tensor metadata through get_data_shape()#619prawnsgupta wants to merge 1 commit into
prawnsgupta wants to merge 1 commit into
Conversation
get_data_shape() assumed every non-sequence base case was a tensor and called tuple(data.shape) on it, raising AttributeError when a batch bundled metadata such as filenames (str) or labels (int) alongside tensors. The else branch now returns the data unchanged, matching the passthrough behaviour of data_to_device() and unsqueeze_data(). Adds tests/test_utils_torch.py covering get_data_shape, data_to_device and unsqueeze_data, including a regression test for the mixed-type batch that previously crashed.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #518
Problem
`get_data_shape()` in `perceptionmetrics/utils/torch.py` assumes every non-sequence base case is a tensor and calls `tuple(data.shape)` on it. When a batch bundles metadata (a filename `str`, a class label `int`, a dict) alongside the image tensor, it crashes:
Fix
The `else` branch now returns the data unchanged, matching the passthrough already used by `data_to_device()` and `unsqueeze_data()` in the same file for non-tensor base cases. This keeps the three recursive helpers consistent.
Tests
Adds `tests/test_utils_torch.py` (9 tests) covering `get_data_shape`, `data_to_device` and `unsqueeze_data`: single tensor, tuple/list type preservation, nested structures, scalar passthrough, and a regression test for the mixed-type batch. The regression test fails on the current code (`AttributeError`) and passes with the fix.
`black --check` clean on both files.