Skip to content

Commit

Permalink
suppress errors in vision/fair/pytorch3d
Browse files Browse the repository at this point in the history
Differential Revision: D37172764

fbshipit-source-id: a2ec367e56de2781a17f5e708eb5832ec9d7e6b4
  • Loading branch information
Pyre Bot Jr authored and facebook-github-bot committed Jun 15, 2022
1 parent ea4f326 commit 7978ffd
Show file tree
Hide file tree
Showing 61 changed files with 188 additions and 80 deletions.
6 changes: 6 additions & 0 deletions pytorch3d/common/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def solve(A: torch.Tensor, B: torch.Tensor) -> torch.Tensor: # pragma: no cover
# PyTorch version >= 1.8.0
return torch.linalg.solve(A, B)

# pyre-fixme[16]: `Tuple` has no attribute `solution`.
return torch.solve(B, A).solution


Expand Down Expand Up @@ -67,9 +68,14 @@ def meshgrid_ij(
Like torch.meshgrid was before PyTorch 1.10.0, i.e. with indexing set to ij
"""
if (
# pyre-fixme[16]: Callable `meshgrid` has no attribute `__kwdefaults__`.
torch.meshgrid.__kwdefaults__ is not None
and "indexing" in torch.meshgrid.__kwdefaults__
):
# PyTorch >= 1.10.0
# pyre-fixme[6]: For 1st param expected `Union[List[Tensor], Tensor]` but
# got `Union[Sequence[Tensor], Tensor]`.
return torch.meshgrid(*A, indexing="ij")
# pyre-fixme[6]: For 1st param expected `Union[List[Tensor], Tensor]` but got
# `Union[Sequence[Tensor], Tensor]`.
return torch.meshgrid(*A)
2 changes: 1 addition & 1 deletion pytorch3d/common/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def make_device(device: Device) -> torch.device:
A matching torch.device object
"""
device = torch.device(device) if isinstance(device, str) else device
if device.type == "cuda" and device.index is None: # pyre-ignore[16]
if device.type == "cuda" and device.index is None:
# If cuda but with no index, then the current cuda device is indicated.
# In that case, we fix to that device
device = torch.device(f"cuda:{torch.cuda.current_device()}")
Expand Down
13 changes: 7 additions & 6 deletions pytorch3d/common/workaround/symeig3x3.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ def forward(
if inputs.shape[-2:] != (3, 3):
raise ValueError("Only inputs of shape (..., 3, 3) are supported.")

inputs_diag = inputs.diagonal(dim1=-2, dim2=-1) # pyre-ignore[16]
inputs_diag = inputs.diagonal(dim1=-2, dim2=-1)
inputs_trace = inputs_diag.sum(-1)
q = inputs_trace / 3.0

# Calculate squared sum of elements outside the main diagonal / 2
# pyre-fixme[58]: `**` is not supported for operand types `Tensor` and `int`.
p1 = ((inputs**2).sum(dim=(-1, -2)) - (inputs_diag**2).sum(-1)) / 2
# pyre-fixme[58]: `**` is not supported for operand types `Tensor` and `int`.
p2 = ((inputs_diag - q[..., None]) ** 2).sum(dim=-1) + 2.0 * p1.clamp(self._eps)

p = torch.sqrt(p2 / 6.0)
Expand Down Expand Up @@ -195,8 +197,9 @@ def _get_ev0(self, char_poly: torch.Tensor) -> torch.Tensor:
cross_products[..., :1, :]
)

# pyre-fixme[58]: `**` is not supported for operand types `Tensor` and `int`.
norms_sq = (cross_products**2).sum(dim=-1)
max_norms_index = norms_sq.argmax(dim=-1) # pyre-ignore[16]
max_norms_index = norms_sq.argmax(dim=-1)

# Pick only the cross-product with highest squared norm for each input
max_cross_products = self._gather_by_index(
Expand Down Expand Up @@ -227,9 +230,7 @@ def _gather_by_index(
index_shape = list(source.shape)
index_shape[dim] = 1

return source.gather(dim, index.expand(index_shape)).squeeze( # pyre-ignore[16]
dim
)
return source.gather(dim, index.expand(index_shape)).squeeze(dim)

def _get_uv(self, w: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Expand All @@ -243,7 +244,7 @@ def _get_uv(self, w: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
Tuple of U and V unit-length vector tensors of shape (..., 3)
"""

min_idx = w.abs().argmin(dim=-1) # pyre-ignore[16]
min_idx = w.abs().argmin(dim=-1)
rotation_2d = self._rotations_3d[min_idx].to(w)

u = F.normalize((rotation_2d @ w[..., None])[..., 0], dim=-1)
Expand Down
2 changes: 1 addition & 1 deletion pytorch3d/datasets/r2n2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ def compute_extrinsic_matrix(
# rotates the model 90 degrees about the x axis. To compensate for this quirk we
# roll that rotation into the extrinsic matrix here
rot = torch.tensor([[1, 0, 0, 0], [0, 0, -1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])
# pyre-fixme[16]: `Tensor` has no attribute `mm`.
RT = RT.mm(rot.to(RT))

return RT
Expand Down Expand Up @@ -180,6 +179,7 @@ def read_binvox_coords(
size, translation, scale = _read_binvox_header(f)
storage = torch.ByteStorage.from_buffer(f.read())
data = torch.tensor([], dtype=torch.uint8)
# pyre-fixme[28]: Unexpected keyword argument `source`.
data.set_(source=storage)
vals, counts = data[::2], data[1::2]
idxs = _compute_idxs(vals, counts)
Expand Down
3 changes: 3 additions & 0 deletions pytorch3d/datasets/shapenet_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ def _handle_render_inputs(
sampled_idxs = self._sample_idxs_from_category(
sample_num=sample_num, category=category
)
# pyre-fixme[6]: For 1st param expected `Union[List[Tensor],
# typing.Tuple[Tensor, ...]]` but got `Tuple[Tensor, List[int]]`.
idxs_tensor = torch.cat((idxs_tensor, sampled_idxs))
idxs = idxs_tensor.tolist()
# Check if the indices are valid if idxs are supplied.
Expand Down Expand Up @@ -283,4 +285,5 @@ def _sample_idxs_from_category(
"category " + category if category is not None else "all categories",
)
warnings.warn(msg)
# pyre-fixme[7]: Expected `List[int]` but got `Tensor`.
return sampled_idxs
4 changes: 3 additions & 1 deletion pytorch3d/implicitron/dataset/json_index_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,16 @@ def _resize_image(
)
imre = torch.nn.functional.interpolate(
torch.from_numpy(image)[None],
# pyre-ignore[6]
scale_factor=minscale,
mode=mode,
align_corners=False if mode == "bilinear" else None,
recompute_scale_factor=True,
)[0]
# pyre-fixme[19]: Expected 1 positional argument.
imre_ = torch.zeros(image.shape[0], self.image_height, self.image_width)
imre_[:, 0 : imre.shape[1], 0 : imre.shape[2]] = imre
# pyre-fixme[6]: For 2nd param expected `int` but got `Optional[int]`.
# pyre-fixme[6]: For 3rd param expected `int` but got `Optional[int]`.
mask = torch.zeros(1, self.image_height, self.image_width)
mask[:, 0 : imre.shape[1] - 1, 0 : imre.shape[2] - 1] = 1.0
return imre_, minscale, mask
Expand Down
2 changes: 2 additions & 0 deletions pytorch3d/implicitron/dataset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def is_known_frame(
Given a list `frame_type` of frame types in a batch, return a tensor
of boolean flags expressing whether the corresponding frame is a known frame.
"""
# pyre-fixme[7]: Expected `BoolTensor` but got `Tensor`.
return torch.tensor(
[ft.endswith(DATASET_TYPE_KNOWN) for ft in frame_type],
dtype=torch.bool,
Expand All @@ -37,6 +38,7 @@ def is_train_frame(
Given a list `frame_type` of frame types in a batch, return a tensor
of boolean flags expressing whether the corresponding frame is a training frame.
"""
# pyre-fixme[7]: Expected `BoolTensor` but got `Tensor`.
return torch.tensor(
[ft.startswith(DATASET_TYPE_TRAIN) for ft in frame_type],
dtype=torch.bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,7 @@ def eval_batch(

imode = "bilinear" if k == "image_render" else "nearest"
cloned_render[k] = (
# pyre-fixme[6]: For 2nd param expected `Optional[int]` but got
# `Tuple[Any, ...]`.
F.interpolate(field[:1], size=image_resol, mode=imode)
.detach()
.clone()
F.interpolate(field[:1], size=image_resol, mode=imode).detach().clone()
)

frame_data = copy.deepcopy(frame_data)
Expand Down Expand Up @@ -408,7 +404,6 @@ def _reduce_camera_iou_overlap(ious: torch.Tensor, topk: int = 2) -> torch.Tenso
Returns:
single-element Tensor
"""
# pyre-ignore[16] topk not recognized
return ious.topk(k=min(topk, len(ious) - 1)).values.mean()


Expand Down
2 changes: 2 additions & 0 deletions pytorch3d/implicitron/models/autodecoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def forward(self, x: Union[torch.LongTensor, List[str]]) -> Optional[torch.Tenso

if isinstance(x[0], str):
try:
# pyre-fixme[9]: x has type `Union[List[str], LongTensor]`; used as
# `Tensor`.
x = torch.tensor(
# pyre-ignore[29]
[self._sequence_map[elem] for elem in x],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ def forward(
if self.image_rescale != 1.0 and imgs_input is not None:
imgs_resized = Fu.interpolate(
imgs_input,
# pyre-fixme[6]: For 2nd param expected `Optional[List[float]]` but
# got `float`.
scale_factor=self.image_rescale,
mode="bilinear",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ def forward(
self.embed_fn is None and fun_viewpool is None and global_code is None
):
return torch.tensor(
[], device=rays_points_world.device, dtype=rays_points_world.dtype
[],
device=rays_points_world.device,
dtype=rays_points_world.dtype
# pyre-fixme[6]: For 2nd param expected `int` but got `Union[Module,
# Tensor]`.
).view(0, self.out_dim)

embedding = None
Expand All @@ -128,13 +132,15 @@ def forward(
)

x = embedding
# pyre-fixme[29]: `Union[BoundMethod[typing.Callable(torch._C._TensorBase.__s...
for layer_idx in range(self.num_layers - 1):
if layer_idx in self.skip_in:
x = torch.cat([x, embedding], dim=-1) / 2**0.5

# pyre-fixme[29]: `Union[torch.Tensor, torch.nn.Module]` is not a function.
x = self.linear_layers[layer_idx](x)

# pyre-fixme[29]: `Union[BoundMethod[typing.Callable(torch._C._TensorBase...
if layer_idx < self.num_layers - 2:
# pyre-fixme[29]: `Union[torch.Tensor, torch.nn.Module]` is not a function.
x = self.softplus(x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ def __init__(
self.last = torch.nn.Linear(dimout, output_dim)
_xavier_init(self.last)

# pyre-fixme[8]: Attribute has type `Tuple[ModuleList, ModuleList]`; used as
# `ModuleList`.
self.layers_pool, self.layers_ray = (
torch.nn.ModuleList(layers_pool),
torch.nn.ModuleList(layers_ray),
Expand Down
4 changes: 4 additions & 0 deletions pytorch3d/implicitron/models/model_dbir.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def forward(
mask_fg = (fg_probability > 0.5).type_as(image_rgb)

point_cloud = get_rgbd_point_cloud(
# pyre-fixme[6]: For 1st param expected `Union[List[int], int,
# LongTensor]` but got `Tensor`.
camera[is_known_idx],
image_rgb[is_known_idx],
depth_map[is_known_idx],
Expand All @@ -101,6 +103,8 @@ def forward(

pcl_size = point_cloud.num_points_per_cloud().item()
if (self.max_points > 0) and (pcl_size > self.max_points):
# pyre-fixme[6]: For 1st param expected `int` but got `Union[bool,
# float, int]`.
prm = torch.randperm(pcl_size)[: self.max_points]
point_cloud = Pointclouds(
point_cloud.points_padded()[:, prm, :],
Expand Down
6 changes: 0 additions & 6 deletions pytorch3d/implicitron/models/renderer/lstm_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,7 @@ def forward(
msg = (
f"{t}: mu={float(signed_distance.mean()):1.2e};"
+ f" std={float(signed_distance.std()):1.2e};"
# pyre-fixme[6]: Expected `Union[bytearray, bytes, str,
# typing.SupportsFloat, typing_extensions.SupportsIndex]` for 1st
# param but got `Tensor`.
+ f" mu_d={float(ray_bundle_t.lengths.mean()):1.2e};"
# pyre-fixme[6]: Expected `Union[bytearray, bytes, str,
# typing.SupportsFloat, typing_extensions.SupportsIndex]` for 1st
# param but got `Tensor`.
+ f" std_d={float(ray_bundle_t.lengths.std()):1.2e};"
)
logger.info(msg)
Expand Down
2 changes: 0 additions & 2 deletions pytorch3d/implicitron/models/renderer/ray_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ def forward(
):
sample_mask = torch.nn.functional.interpolate(
mask,
# pyre-fixme[6]: Expected `Optional[int]` for 2nd param but got
# `List[int]`.
size=[self.image_height, self.image_width],
mode="nearest",
)[:, 0]
Expand Down
32 changes: 26 additions & 6 deletions pytorch3d/implicitron/models/renderer/ray_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ def forward(

ray_directions = ray_directions.reshape(-1, 3)
mask_intersect = mask_intersect.reshape(-1)
# pyre-fixme[9]: object_mask has type `BoolTensor`; used as `Tensor`.
object_mask = object_mask.reshape(-1)

in_mask = ~network_object_mask & object_mask & ~sampler_mask
out_mask = ~object_mask & ~sampler_mask

# pyre-fixme[16]: `Tensor` has no attribute `__invert__`.
mask_left_out = (in_mask | out_mask) & ~mask_intersect
if (
mask_left_out.sum() > 0
Expand Down Expand Up @@ -410,10 +410,17 @@ def ray_sampler(
if n_p_out > 0:
out_pts_idx = torch.argmin(sdf_val[p_out_mask, :], -1)
sampler_pts[mask_intersect_idx[p_out_mask]] = points[p_out_mask, :, :][
torch.arange(n_p_out), out_pts_idx, :
# pyre-fixme[6]: For 1st param expected `Union[bool, float, int]`
# but got `Tensor`.
torch.arange(n_p_out),
out_pts_idx,
:,
]
sampler_dists[mask_intersect_idx[p_out_mask]] = pts_intervals[
p_out_mask, :
p_out_mask,
:
# pyre-fixme[6]: For 1st param expected `Union[bool, float, int]` but
# got `Tensor`.
][torch.arange(n_p_out), out_pts_idx]

# Get Network object mask
Expand All @@ -434,10 +441,16 @@ def ray_sampler(
secant_pts
]
z_low = pts_intervals[secant_pts][
torch.arange(n_secant_pts), sampler_pts_ind[secant_pts] - 1
# pyre-fixme[6]: For 1st param expected `Union[bool, float, int]`
# but got `Tensor`.
torch.arange(n_secant_pts),
sampler_pts_ind[secant_pts] - 1,
]
sdf_low = sdf_val[secant_pts][
torch.arange(n_secant_pts), sampler_pts_ind[secant_pts] - 1
# pyre-fixme[6]: For 1st param expected `Union[bool, float, int]`
# but got `Tensor`.
torch.arange(n_secant_pts),
sampler_pts_ind[secant_pts] - 1,
]
cam_loc_secant = cam_loc.reshape(-1, 3)[mask_intersect_idx[secant_pts]]
ray_directions_secant = ray_directions.reshape((-1, 3))[
Expand Down Expand Up @@ -514,6 +527,7 @@ def minimal_sdf_points(
mask_max_dis = max_dis[mask].unsqueeze(-1)
mask_min_dis = min_dis[mask].unsqueeze(-1)
steps = (
# pyre-fixme[6]: For 1st param expected `int` but got `Tensor`.
steps.unsqueeze(0).repeat(n_mask_points, 1) * (mask_max_dis - mask_min_dis)
+ mask_min_dis
)
Expand All @@ -533,8 +547,13 @@ def minimal_sdf_points(
mask_sdf_all = torch.cat(mask_sdf_all).reshape(-1, n)
min_vals, min_idx = mask_sdf_all.min(-1)
min_mask_points = mask_points_all.reshape(-1, n, 3)[
torch.arange(0, n_mask_points), min_idx
# pyre-fixme[6]: For 2nd param expected `Union[bool, float, int]` but
# got `Tensor`.
torch.arange(0, n_mask_points),
min_idx,
]
# pyre-fixme[6]: For 2nd param expected `Union[bool, float, int]` but got
# `Tensor`.
min_mask_dist = steps.reshape(-1, n)[torch.arange(0, n_mask_points), min_idx]

return min_mask_points, min_mask_dist
Expand All @@ -553,6 +572,7 @@ def _get_sphere_intersection(
# cam_loc = cam_loc.unsqueeze(-1)
# ray_cam_dot = torch.bmm(ray_directions, cam_loc).squeeze()
ray_cam_dot = (ray_directions * cam_loc).sum(-1) # n_images x n_rays
# pyre-fixme[58]: `**` is not supported for operand types `Tensor` and `int`.
under_sqrt = ray_cam_dot**2 - (cam_loc.norm(2, dim=-1) ** 2 - r**2)

under_sqrt = under_sqrt.reshape(-1)
Expand Down
10 changes: 8 additions & 2 deletions pytorch3d/implicitron/models/renderer/sdf_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ def forward(
eik_bounding_box: float = self.object_bounding_sphere
n_eik_points = batch_size * num_pixels // 2
eikonal_points = torch.empty(
n_eik_points, 3, device=self._bg_color.device
n_eik_points,
3,
# pyre-fixme[6]: For 3rd param expected `Union[None, str, device]`
# but got `Union[device, Tensor, Module]`.
device=self._bg_color.device,
).uniform_(-eik_bounding_box, eik_bounding_box)
eikonal_pixel_points = points.clone()
eikonal_pixel_points = eikonal_pixel_points.detach()
Expand Down Expand Up @@ -196,7 +200,9 @@ def forward(
pooling_fn=None, # TODO
)
mask_full.view(-1, 1)[~surface_mask] = torch.sigmoid(
-self.soft_mask_alpha * sdf_output[~surface_mask]
# pyre-fixme[6]: For 1st param expected `Tensor` but got `float`.
-self.soft_mask_alpha
* sdf_output[~surface_mask]
)

# scatter points with surface_mask
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,6 @@ def _get_ray_dir_dot_prods(camera: CamerasBase, pts: torch.Tensor):
# torch.Tensor, torch.nn.modules.module.Module]` is not a function.
# pyre-fixme[29]: `Union[BoundMethod[typing.Callable(torch.Tensor.permute)[[N...
camera_rep.T[:, None],
# pyre-fixme[29]: `Union[BoundMethod[typing.Callable(torch.Tensor.permute)[[N...
camera_rep.R.permute(0, 2, 1),
).reshape(-1, *([1] * (pts.ndim - 2)), 3)
# cam_centers_rep = camera_rep.get_camera_center().reshape(
Expand Down Expand Up @@ -649,6 +648,7 @@ def _avgmaxstd_reduction_function(
x_aggr = torch.cat(pooled_features, dim=-1)

# zero out features that were all masked out
# pyre-fixme[16]: `bool` has no attribute `type_as`.
any_active = (w.max(dim=dim, keepdim=True).values > 1e-4).type_as(x_aggr)
x_aggr = x_aggr * any_active[..., None]

Expand Down Expand Up @@ -676,6 +676,7 @@ def _std_reduction_function(
):
if mu is None:
mu = _avg_reduction_function(x, w, dim=dim)
# pyre-fixme[58]: `**` is not supported for operand types `Tensor` and `int`.
std = wmean((x - mu) ** 2, w, dim=dim, eps=1e-2).clamp(1e-4).sqrt()
# FIXME: somehow this is extremely heavy in mem?
return std
Expand Down
Loading

0 comments on commit 7978ffd

Please sign in to comment.