From 5444c53ceed57969ee1a8e1186b3e3010d437c19 Mon Sep 17 00:00:00 2001 From: Jeremy Reizenstein Date: Wed, 3 Jun 2020 11:29:57 -0700 Subject: [PATCH] Avoid plain division involving integers Summary: To avoid pytorch warnings and future behaviour changes, stop using torch.div and / with tensors of integers. Reviewed By: gkioxari, mruberry Differential Revision: D21857955 fbshipit-source-id: fb9f3000f3d953352cdc721d2a5f73d3a4bbf4b7 --- pytorch3d/ops/cubify.py | 6 +++--- pytorch3d/structures/meshes.py | 2 +- tests/test_rasterizer.py | 7 ++----- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/pytorch3d/ops/cubify.py b/pytorch3d/ops/cubify.py index 6de5decb..cd82ec97 100644 --- a/pytorch3d/ops/cubify.py +++ b/pytorch3d/ops/cubify.py @@ -18,9 +18,9 @@ def unravel_index(idx, dims) -> torch.Tensor: if len(dims) != 4: raise ValueError("Expects a 4-element list.") N, H, W, D = dims - n = torch.div(idx, H * W * D) - h = torch.div(idx - n * H * W * D, W * D) - w = torch.div(idx - n * H * W * D - h * W * D, D) + n = idx // (H * W * D) + h = (idx - n * H * W * D) // (W * D) + w = (idx - n * H * W * D - h * W * D) // D d = idx - n * H * W * D - h * W * D - w * D return torch.stack((n, h, w, d), dim=1) diff --git a/pytorch3d/structures/meshes.py b/pytorch3d/structures/meshes.py index 036d26de..a72eb5dc 100644 --- a/pytorch3d/structures/meshes.py +++ b/pytorch3d/structures/meshes.py @@ -1031,7 +1031,7 @@ class Meshes(object): unique_mask[1:] = sorted_hash[1:] != sorted_hash[:-1] unique_idx = sort_idx[unique_mask] - self._edges_packed = torch.stack([u / V, u % V], dim=1) + self._edges_packed = torch.stack([u // V, u % V], dim=1) self._edges_packed_to_mesh_idx = edge_to_mesh[unique_idx] face_to_edge = torch.arange(3 * F).view(3, F).t() diff --git a/tests/test_rasterizer.py b/tests/test_rasterizer.py index b65147a5..da1c95e4 100644 --- a/tests/test_rasterizer.py +++ b/tests/test_rasterizer.py @@ -24,11 +24,8 @@ DEBUG = False # Set DEBUG to true to save outputs from the tests. def convert_image_to_binary_mask(filename): with Image.open(filename) as raw_image: image = torch.from_numpy(np.array(raw_image)) - min = image.min() - max = image.max() - image_norm = (image - min) / (max - min) - image_norm[image_norm > 0] == 1.0 - image_norm = image_norm.to(torch.int64) + mx = image.max() + image_norm = (image == mx).to(torch.int64) return image_norm