Skip to content

for cam in cameras raises ValueError #1408

Closed
@Mason-McGough

Description

@Mason-McGough

🐛 Bugs / Unexpected behaviors

Using for to loop over child classes of CamerasBase attempts to index beyond len(cameras) and raises a ValueError.

Instructions To Reproduce the Issue:

With the following script I am able to reproduce the issue.

from pytorch3d.renderer import (
    look_at_view_transform,
    PerspectiveCameras
)

n_iters = 10
R, T = look_at_view_transform(2.7, 0, 180)
R, T = R.repeat(n_iters, 1, 1), T.repeat(n_iters, 1)
cameras = PerspectiveCameras(R=R, T=T)

for i, cam in enumerate(cameras):
    print(f'{i}: {cam}')

This produces the following output:

0: PerspectiveCameras()
1: PerspectiveCameras()
2: PerspectiveCameras()
3: PerspectiveCameras()
4: PerspectiveCameras()
5: PerspectiveCameras()
6: PerspectiveCameras()
7: PerspectiveCameras()
8: PerspectiveCameras()
9: PerspectiveCameras()
Traceback (most recent call last):
  File "/home/mmcgoug/Workspace/NeRF/pytorch3d/_test_cameras_iterator.py", line 11, in <module>
    for i, cam in enumerate(cameras):
  File "/home/mmcgoug/miniconda3/envs/pytorch3d/lib/python3.9/site-packages/pytorch3d/renderer/cameras.py", line 413, in __getitem__
    raise ValueError(f"Index {max(index)} is out of bounds for select cameras")
ValueError: Index 10 is out of bounds for select cameras

Seems that the iterator eventually produces an index>len(cameras), which causes it to raise a ValueError at this line. Since CamerasBase does not define an __iter__ method, should __getitem__ instead raise StopIteration somewhere?

The issue shows up for any value n_iters>0 I have tried. I have also tried catching StopIteration explicitly but it does not seem to be raised.

Versions:
pytorch==1.13.0
pytorch-cuda==11.6
pytorch3d==0.7.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions