diff --git a/INSTALL.md b/INSTALL.md index ed9e2b6b..f0257157 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -42,7 +42,7 @@ conda install jupyter pip install scikit-image matplotlib imageio # Tests/Linting -pip install black isort flake8 +pip install black isort flake8 flake8-bugbear flake8-comprehensions ``` ## Build/Install Pytorch3d @@ -109,4 +109,4 @@ After installing, verify whether all unit tests have passed ``` cd tests python3 -m unittest discover -p *.py -``` \ No newline at end of file +``` diff --git a/dev/linter.sh b/dev/linter.sh index 5923a6d9..c5b9b02b 100755 --- a/dev/linter.sh +++ b/dev/linter.sh @@ -13,7 +13,7 @@ } DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -DIR="${DIR}/.." +DIR=$(dirname "${DIR}") echo "Running isort..." isort -y -sp "${DIR}" @@ -25,6 +25,15 @@ echo "Running flake..." flake8 "${DIR}" echo "Running clang-format ..." -find "${DIR}" -regex ".*\.\(cpp\|c\|cc\|cu\|cuh\|cxx\|h\|hh\|hpp\|hxx\|tcc\|mm\|m\)" -print0 | xargs -0 clang-format -i +clangformat=$(command -v clang-format-8 || echo clang-format) +find "${DIR}" -regex ".*\.\(cpp\|c\|cc\|cu\|cuh\|cxx\|h\|hh\|hpp\|hxx\|tcc\|mm\|m\)" -print0 | xargs -0 "${clangformat}" -i -(cd "${DIR}"; command -v arc > /dev/null && arc lint) || true +# (cd "${DIR}"; command -v arc > /dev/null && arc lint) || true + +# Run pyre internally only. +if [[ -f tests/TARGETS ]] +then + echo "Running pyre..." + echo "To restart/kill pyre server, run 'pyre restart' or 'pyre kill' in fbcode/" + ( cd ~/fbsource/fbcode; pyre -l vision/fair/pytorch3d/ ) +fi diff --git a/pytorch3d/io/obj_io.py b/pytorch3d/io/obj_io.py index ff40125c..258a6220 100644 --- a/pytorch3d/io/obj_io.py +++ b/pytorch3d/io/obj_io.py @@ -8,7 +8,7 @@ import os import pathlib import warnings from collections import namedtuple -from typing import List +from typing import List, Optional import torch from fvcore.common.file_io import PathManager from PIL import Image @@ -519,7 +519,7 @@ def load_mtl(f_mtl, material_names: List, data_dir: str): return material_properties, texture_images -def save_obj(f, verts, faces, decimal_places: int = None): +def save_obj(f, verts, faces, decimal_places: Optional[int] = None): """ Save a mesh to an .obj file. @@ -544,7 +544,7 @@ def save_obj(f, verts, faces, decimal_places: int = None): # TODO (nikhilar) Speed up this function. -def _save(f, verts, faces, decimal_places: int = None): +def _save(f, verts, faces, decimal_places: Optional[int] = None): if verts.dim() != 2 or verts.size(1) != 3: raise ValueError("Argument 'verts' should be of shape (num_verts, 3).") if faces.dim() != 2 or faces.size(1) != 3: diff --git a/pytorch3d/ops/packed_to_padded.py b/pytorch3d/ops/packed_to_padded.py index c64894b7..8c2869b4 100644 --- a/pytorch3d/ops/packed_to_padded.py +++ b/pytorch3d/ops/packed_to_padded.py @@ -63,21 +63,22 @@ def packed_to_padded(inputs, first_idxs, max_size): Torch wrapper that handles allowed input shapes. See description below. Args: - inputs: FloatTensor of shape (F,) or (F, D), representing the packed batch tensor. - e.g. areas for faces in a batch of meshes. + inputs: FloatTensor of shape (F,) or (F, D), representing the packed + batch tensor, e.g. areas for faces in a batch of meshes. first_idxs: LongTensor of shape (N,) where N is the number of elements in the batch and `first_idxs[i] = f` means that the inputs for batch element i begin at `inputs[f]`. max_size: Max length of an element in the batch. Returns: - inputs_padded: FloatTensor of shape (N, max_size) or (N, max_size, D) where max_size is - max of `sizes`. The values for batch element i which start at - `inputs[first_idxs[i]]` will be copied to `inputs_padded[i, :]`, - with zeros padding out the extra inputs. - - To handle the allowed input shapes, we convert the inputs tensor of shape (F,) to (F, 1). - We reshape the output back to (N, max_size) from (N, max_size, 1). + inputs_padded: FloatTensor of shape (N, max_size) or (N, max_size, D) + where max_size is max of `sizes`. The values for batch element i + which start at `inputs[first_idxs[i]]` will be copied to + `inputs_padded[i, :]`, with zeros padding out the extra inputs. + + To handle the allowed input shapes, we convert the inputs tensor of shape + (F,) to (F, 1). We reshape the output back to (N, max_size) from + (N, max_size, 1). """ # if inputs is of shape (F,), reshape into (F, 1) flat = False @@ -101,8 +102,8 @@ class _PaddedToPacked(Function): """ Args: ctx: Context object used to calculate gradients. - inputs: FloatTensor of shape (N, max_size, D), representing the padded tensor. - e.g. areas for faces in a batch of meshes. + inputs: FloatTensor of shape (N, max_size, D), representing + the padded tensor, e.g. areas for faces in a batch of meshes. first_idxs: LongTensor of shape (N,) where N is the number of elements in the batch and `first_idxs[i] = f` means that the inputs for batch element i begin at `inputs_packed[f]`. @@ -141,11 +142,12 @@ class _PaddedToPacked(Function): def padded_to_packed(inputs, first_idxs, num_inputs): """ - Torch wrapper that handles allowed input shapes. See description below. + Torch wrapper that handles allowed input shapes. See description below. Args: - inputs: FloatTensor of shape (N, max_size) or (N, max_size, D), representing the - padded tensor. e.g. areas for faces in a batch of meshes. + inputs: FloatTensor of shape (N, max_size) or (N, max_size, D), + representing the padded tensor, e.g. areas for faces in a batch of + meshes. first_idxs: LongTensor of shape (N,) where N is the number of elements in the batch and `first_idxs[i] = f` means that the inputs for batch element i begin at `inputs_packed[f]`. @@ -155,8 +157,9 @@ def padded_to_packed(inputs, first_idxs, num_inputs): inputs_packed: FloatTensor of shape (F,) or (F, D) where `inputs_packed[first_idx[i]:] = inputs[i, :]`. - To handle the allowed input shapes, we convert the inputs tensor of shape (N, max_size) - to (N, max_size, 1). We reshape the output back to (F,) from (F, 1). + To handle the allowed input shapes, we convert the inputs tensor of shape + (N, max_size) to (N, max_size, 1). We reshape the output back to (F,) from + (F, 1). """ # if inputs is of shape (N, max_size), reshape into (N, max_size, 1)) flat = False diff --git a/pytorch3d/renderer/cameras.py b/pytorch3d/renderer/cameras.py index 4e13ad55..c856ffeb 100644 --- a/pytorch3d/renderer/cameras.py +++ b/pytorch3d/renderer/cameras.py @@ -103,9 +103,8 @@ class OpenGLPerspectiveCameras(TensorProperties): znear = kwargs.get("znear", self.znear) # pyre-ignore[16] zfar = kwargs.get("zfar", self.zfar) # pyre-ignore[16] fov = kwargs.get("fov", self.fov) # pyre-ignore[16] - aspect_ratio = kwargs.get( - "aspect_ratio", self.aspect_ratio - ) # pyre-ignore[16] + # pyre-ignore[16] + aspect_ratio = kwargs.get("aspect_ratio", self.aspect_ratio) degrees = kwargs.get("degrees", self.degrees) P = torch.zeros( @@ -529,12 +528,10 @@ class SfMPerspectiveCameras(TensorProperties): [0, 0, 1, 0], ] """ - principal_point = kwargs.get( - "principal_point", self.principal_point - ) # pyre-ignore[16] - focal_length = kwargs.get( - "focal_length", self.focal_length - ) # pyre-ignore[16] + # pyre-ignore[16] + principal_point = kwargs.get("principal_point", self.principal_point) + # pyre-ignore[16] + focal_length = kwargs.get("focal_length", self.focal_length) P = _get_sfm_calibration_matrix( self._N, self.device, focal_length, principal_point, False @@ -699,12 +696,10 @@ class SfMOrthographicCameras(TensorProperties): [0, 0, 0, 1], ] """ - principal_point = kwargs.get( - "principal_point", self.principal_point - ) # pyre-ignore[16] - focal_length = kwargs.get( - "focal_length", self.focal_length - ) # pyre-ignore[16] + # pyre-ignore[16] + principal_point = kwargs.get("principal_point", self.principal_point) + # pyre-ignore[16] + focal_length = kwargs.get("focal_length", self.focal_length) P = _get_sfm_calibration_matrix( self._N, self.device, focal_length, principal_point, True diff --git a/pytorch3d/structures/textures.py b/pytorch3d/structures/textures.py index 5173aff7..4daf69bb 100644 --- a/pytorch3d/structures/textures.py +++ b/pytorch3d/structures/textures.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. -from typing import List, Union +from typing import List, Optional, Union import torch import torchvision.transforms as T @@ -81,10 +81,10 @@ def _extend_tensor(input_tensor: torch.Tensor, N: int) -> torch.Tensor: class Textures(object): def __init__( self, - maps: Union[List, torch.Tensor] = None, - faces_uvs: torch.Tensor = None, - verts_uvs: torch.Tensor = None, - verts_rgb: torch.Tensor = None, + maps: Union[List, torch.Tensor, None] = None, + faces_uvs: Optional[torch.Tensor] = None, + verts_uvs: Optional[torch.Tensor] = None, + verts_rgb: Optional[torch.Tensor] = None, ): """ Args: @@ -105,7 +105,7 @@ class Textures(object): msg = "Expected verts_rgb to be of shape (N, V, 3); got %r" raise ValueError(msg % verts_rgb.shape) if maps is not None: - if torch.is_tensor(map) and map.ndim != 4: + if torch.is_tensor(maps) and maps.ndim != 4: msg = "Expected maps to be of shape (N, H, W, 3); got %r" raise ValueError(msg % repr(maps.shape)) elif isinstance(maps, list): diff --git a/pytorch3d/transforms/rotation_conversions.py b/pytorch3d/transforms/rotation_conversions.py index 7c01b569..5de39383 100644 --- a/pytorch3d/transforms/rotation_conversions.py +++ b/pytorch3d/transforms/rotation_conversions.py @@ -2,6 +2,7 @@ # Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. import functools +from typing import Optional import torch @@ -246,7 +247,10 @@ def matrix_to_euler_angles(matrix, convention: str): def random_quaternions( - n: int, dtype: torch.dtype = None, device=None, requires_grad=False + n: int, + dtype: Optional[torch.dtype] = None, + device=None, + requires_grad=False, ): """ Generate random quaternions representing rotations, @@ -272,7 +276,10 @@ def random_quaternions( def random_rotations( - n: int, dtype: torch.dtype = None, device=None, requires_grad=False + n: int, + dtype: Optional[torch.dtype] = None, + device=None, + requires_grad=False, ): """ Generate random rotations as 3x3 rotation matrices. @@ -295,7 +302,7 @@ def random_rotations( def random_rotation( - dtype: torch.dtype = None, device=None, requires_grad=False + dtype: Optional[torch.dtype] = None, device=None, requires_grad=False ): """ Generate a single random 3x3 rotation matrix. diff --git a/pytorch3d/transforms/transform3d.py b/pytorch3d/transforms/transform3d.py index 7b88a5aa..cd901f98 100644 --- a/pytorch3d/transforms/transform3d.py +++ b/pytorch3d/transforms/transform3d.py @@ -3,6 +3,7 @@ import math import warnings +from typing import Optional import torch from .rotation_conversions import _axis_angle_rotation @@ -250,7 +251,7 @@ class Transform3d: out._matrix = matrix return out - def transform_points(self, points, eps: float = None): + def transform_points(self, points, eps: Optional[float] = None): """ Use this transform to transform a set of 3D points. Assumes row major ordering of the input points. diff --git a/scripts/parse_tutorials.py b/scripts/parse_tutorials.py index e4ec568e..6bc61108 100644 --- a/scripts/parse_tutorials.py +++ b/scripts/parse_tutorials.py @@ -4,7 +4,6 @@ import argparse import json import os - import nbformat from bs4 import BeautifulSoup from nbconvert import HTMLExporter, ScriptExporter diff --git a/setup.cfg b/setup.cfg index 8bbe1356..fec0140d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,7 +6,8 @@ include_trailing_comma=True multi_line_output=3 known_standard_library=numpy,setuptools known_myself=pytorch3d -known_third_party=fvcore,torch,torchvision,matplotlib,mpl_toolkits,PIL,yaml,jinja2,requests +known_third_party=fvcore,torch,torchvision,matplotlib,mpl_toolkits,PIL,yaml + jinja2,requests,nbformat,nbconvert,bs4 no_lines_before=STDLIB,THIRDPARTY sections=FUTURE,STDLIB,THIRDPARTY,myself,FIRSTPARTY,LOCALFOLDER default_section=FIRSTPARTY diff --git a/tests/test_rotation_conversions.py b/tests/test_rotation_conversions.py index 1f14c72e..f4800aac 100644 --- a/tests/test_rotation_conversions.py +++ b/tests/test_rotation_conversions.py @@ -8,7 +8,6 @@ import unittest import torch from pytorch3d.transforms.rotation_conversions import ( - _axis_angle_rotation, euler_angles_to_matrix, matrix_to_euler_angles, matrix_to_quaternion,