Skip to content

Fix #518: pass non-tensor metadata through get_data_shape() - #619

Open
prawnsgupta wants to merge 1 commit into
JdeRobot:masterfrom
prawnsgupta:fix/518-get-data-shape-passthrough
Open

Fix #518: pass non-tensor metadata through get_data_shape()#619
prawnsgupta wants to merge 1 commit into
JdeRobot:masterfrom
prawnsgupta:fix/518-get-data-shape-passthrough

Conversation

@prawnsgupta

Copy link
Copy Markdown

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:

from perceptionmetrics.utils.torch import get_data_shape
import torch
get_data_shape((torch.zeros(3, 224, 224), "image_001.png", 7))
# AttributeError: 'str' object has no attribute 'shape'   (torch.py:46)

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.

tests/test_utils_torch.py ......... 9 passed

`black --check` clean on both files.

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.
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.

Bug in utils/torch.py base-case handling and lack of unit test coverage

1 participant