Skip to content

Commit 279f4a1

Browse files
bottlerfacebook-github-bot
authored andcommitted
texture map list validation
Summary: Add some more validation of a list of texture maps. Move the initialisation of maps_padded to a new function to reduce complexity. Reviewed By: nikhilaravi Differential Revision: D29263443 fbshipit-source-id: 153e262d2e9af21090570768020fca019e364024
1 parent 2a0660b commit 279f4a1

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

pytorch3d/renderer/mesh/textures.py

+25-13
Original file line numberDiff line numberDiff line change
@@ -707,30 +707,42 @@ def __init__(
707707
else:
708708
raise ValueError("Expected verts_uvs to be a tensor or list")
709709

710+
if isinstance(maps, (list, tuple)):
711+
self._maps_list = maps
712+
else:
713+
self._maps_list = None
714+
self._maps_padded = self._format_maps_padded(maps)
715+
716+
if self._maps_padded.device != self.device:
717+
raise ValueError("maps must be on the same device as verts/faces uvs.")
718+
719+
self.valid = torch.ones((self._N,), dtype=torch.bool, device=self.device)
720+
721+
def _format_maps_padded(
722+
self, maps: Union[torch.Tensor, List[torch.Tensor]]
723+
) -> torch.Tensor:
710724
if isinstance(maps, torch.Tensor):
711725
if maps.ndim != 4 or maps.shape[0] != self._N:
712726
msg = "Expected maps to be of shape (N, H, W, C); got %r"
713727
raise ValueError(msg % repr(maps.shape))
714-
self._maps_padded = maps
715-
self._maps_list = None
716-
elif isinstance(maps, (list, tuple)):
728+
return maps
729+
730+
if isinstance(maps, (list, tuple)):
717731
if len(maps) != self._N:
718732
raise ValueError("Expected one texture map per mesh in the batch.")
719-
self._maps_list = maps
720733
if self._N > 0:
721-
maps = _pad_texture_maps(maps, align_corners=self.align_corners)
734+
if not all(map.ndim == 3 for map in maps):
735+
raise ValueError("Invalid number of dimensions in texture maps")
736+
if not all(map.shape[2] == maps[0].shape[2] for map in maps):
737+
raise ValueError("Inconsistent number of channels in maps")
738+
maps_padded = _pad_texture_maps(maps, align_corners=self.align_corners)
722739
else:
723-
maps = torch.empty(
740+
maps_padded = torch.empty(
724741
(self._N, 0, 0, 3), dtype=torch.float32, device=self.device
725742
)
726-
self._maps_padded = maps
727-
else:
728-
raise ValueError("Expected maps to be a tensor or list.")
743+
return maps_padded
729744

730-
if self._maps_padded.device != self.device:
731-
raise ValueError("maps must be on the same device as verts/faces uvs.")
732-
733-
self.valid = torch.ones((self._N,), dtype=torch.bool, device=self.device)
745+
raise ValueError("Expected maps to be a tensor or list of tensors.")
734746

735747
def clone(self) -> "TexturesUV":
736748
tex = self.__class__(

0 commit comments

Comments
 (0)