lint things

Summary:
Lint related fixes: Improve internal/OSS consistency. Fix the fight between black and certain pyre-ignore markers by moving them to the line before.
Use clang-format-8 automatically if present. Small number of pyre fixes.

arc doesn't run pyre at the moment, so I put back the explicit call to pyre. I don't know if there's an option somewhere to change this.

Reviewed By: nikhilaravi

Differential Revision: D19780518

fbshipit-source-id: ef1c243392322fa074130f6cff2dd8a6f7738a7f
This commit is contained in:
Jeremy Reizenstein 2020-02-21 05:03:38 -08:00 committed by Facebook Github Bot
parent 9e21659fc5
commit e491efb81f
11 changed files with 66 additions and 52 deletions

View File

@ -42,7 +42,7 @@ conda install jupyter
pip install scikit-image matplotlib imageio pip install scikit-image matplotlib imageio
# Tests/Linting # Tests/Linting
pip install black isort flake8 pip install black isort flake8 flake8-bugbear flake8-comprehensions
``` ```
## Build/Install Pytorch3d ## Build/Install Pytorch3d
@ -109,4 +109,4 @@ After installing, verify whether all unit tests have passed
``` ```
cd tests cd tests
python3 -m unittest discover -p *.py python3 -m unittest discover -p *.py
``` ```

View File

@ -13,7 +13,7 @@
} }
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
DIR="${DIR}/.." DIR=$(dirname "${DIR}")
echo "Running isort..." echo "Running isort..."
isort -y -sp "${DIR}" isort -y -sp "${DIR}"
@ -25,6 +25,15 @@ echo "Running flake..."
flake8 "${DIR}" flake8 "${DIR}"
echo "Running clang-format ..." 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

View File

@ -8,7 +8,7 @@ import os
import pathlib import pathlib
import warnings import warnings
from collections import namedtuple from collections import namedtuple
from typing import List from typing import List, Optional
import torch import torch
from fvcore.common.file_io import PathManager from fvcore.common.file_io import PathManager
from PIL import Image from PIL import Image
@ -519,7 +519,7 @@ def load_mtl(f_mtl, material_names: List, data_dir: str):
return material_properties, texture_images 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. 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. # 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: if verts.dim() != 2 or verts.size(1) != 3:
raise ValueError("Argument 'verts' should be of shape (num_verts, 3).") raise ValueError("Argument 'verts' should be of shape (num_verts, 3).")
if faces.dim() != 2 or faces.size(1) != 3: if faces.dim() != 2 or faces.size(1) != 3:

View File

@ -63,21 +63,22 @@ def packed_to_padded(inputs, first_idxs, max_size):
Torch wrapper that handles allowed input shapes. See description below. Torch wrapper that handles allowed input shapes. See description below.
Args: Args:
inputs: FloatTensor of shape (F,) or (F, D), representing the packed batch tensor. inputs: FloatTensor of shape (F,) or (F, D), representing the packed
e.g. areas for faces in a batch of meshes. batch tensor, e.g. areas for faces in a batch of meshes.
first_idxs: LongTensor of shape (N,) where N is the number of first_idxs: LongTensor of shape (N,) where N is the number of
elements in the batch and `first_idxs[i] = f` elements in the batch and `first_idxs[i] = f`
means that the inputs for batch element i begin at `inputs[f]`. means that the inputs for batch element i begin at `inputs[f]`.
max_size: Max length of an element in the batch. max_size: Max length of an element in the batch.
Returns: Returns:
inputs_padded: FloatTensor of shape (N, max_size) or (N, max_size, D) where max_size is inputs_padded: FloatTensor of shape (N, max_size) or (N, max_size, D)
max of `sizes`. The values for batch element i which start at where max_size is max of `sizes`. The values for batch element i
`inputs[first_idxs[i]]` will be copied to `inputs_padded[i, :]`, which start at `inputs[first_idxs[i]]` will be copied to
with zeros padding out the extra inputs. `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). To handle the allowed input shapes, we convert the inputs tensor of shape
We reshape the output back to (N, max_size) from (N, max_size, 1). (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) # if inputs is of shape (F,), reshape into (F, 1)
flat = False flat = False
@ -101,8 +102,8 @@ class _PaddedToPacked(Function):
""" """
Args: Args:
ctx: Context object used to calculate gradients. ctx: Context object used to calculate gradients.
inputs: FloatTensor of shape (N, max_size, D), representing the padded tensor. inputs: FloatTensor of shape (N, max_size, D), representing
e.g. areas for faces in a batch of meshes. 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 first_idxs: LongTensor of shape (N,) where N is the number of
elements in the batch and `first_idxs[i] = f` elements in the batch and `first_idxs[i] = f`
means that the inputs for batch element i begin at `inputs_packed[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): 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: Args:
inputs: FloatTensor of shape (N, max_size) or (N, max_size, D), representing the inputs: FloatTensor of shape (N, max_size) or (N, max_size, D),
padded tensor. e.g. areas for faces in a batch of meshes. 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 first_idxs: LongTensor of shape (N,) where N is the number of
elements in the batch and `first_idxs[i] = f` elements in the batch and `first_idxs[i] = f`
means that the inputs for batch element i begin at `inputs_packed[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: FloatTensor of shape (F,) or (F, D) where
`inputs_packed[first_idx[i]:] = inputs[i, :]`. `inputs_packed[first_idx[i]:] = inputs[i, :]`.
To handle the allowed input shapes, we convert the inputs tensor of shape (N, max_size) To handle the allowed input shapes, we convert the inputs tensor of shape
to (N, max_size, 1). We reshape the output back to (F,) from (F, 1). (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)) # if inputs is of shape (N, max_size), reshape into (N, max_size, 1))
flat = False flat = False

View File

@ -103,9 +103,8 @@ class OpenGLPerspectiveCameras(TensorProperties):
znear = kwargs.get("znear", self.znear) # pyre-ignore[16] znear = kwargs.get("znear", self.znear) # pyre-ignore[16]
zfar = kwargs.get("zfar", self.zfar) # pyre-ignore[16] zfar = kwargs.get("zfar", self.zfar) # pyre-ignore[16]
fov = kwargs.get("fov", self.fov) # pyre-ignore[16] fov = kwargs.get("fov", self.fov) # pyre-ignore[16]
aspect_ratio = kwargs.get( # pyre-ignore[16]
"aspect_ratio", self.aspect_ratio aspect_ratio = kwargs.get("aspect_ratio", self.aspect_ratio)
) # pyre-ignore[16]
degrees = kwargs.get("degrees", self.degrees) degrees = kwargs.get("degrees", self.degrees)
P = torch.zeros( P = torch.zeros(
@ -529,12 +528,10 @@ class SfMPerspectiveCameras(TensorProperties):
[0, 0, 1, 0], [0, 0, 1, 0],
] ]
""" """
principal_point = kwargs.get( # pyre-ignore[16]
"principal_point", self.principal_point principal_point = kwargs.get("principal_point", self.principal_point)
) # pyre-ignore[16] # pyre-ignore[16]
focal_length = kwargs.get( focal_length = kwargs.get("focal_length", self.focal_length)
"focal_length", self.focal_length
) # pyre-ignore[16]
P = _get_sfm_calibration_matrix( P = _get_sfm_calibration_matrix(
self._N, self.device, focal_length, principal_point, False self._N, self.device, focal_length, principal_point, False
@ -699,12 +696,10 @@ class SfMOrthographicCameras(TensorProperties):
[0, 0, 0, 1], [0, 0, 0, 1],
] ]
""" """
principal_point = kwargs.get( # pyre-ignore[16]
"principal_point", self.principal_point principal_point = kwargs.get("principal_point", self.principal_point)
) # pyre-ignore[16] # pyre-ignore[16]
focal_length = kwargs.get( focal_length = kwargs.get("focal_length", self.focal_length)
"focal_length", self.focal_length
) # pyre-ignore[16]
P = _get_sfm_calibration_matrix( P = _get_sfm_calibration_matrix(
self._N, self.device, focal_length, principal_point, True self._N, self.device, focal_length, principal_point, True

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. # Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
from typing import List, Union from typing import List, Optional, Union
import torch import torch
import torchvision.transforms as T import torchvision.transforms as T
@ -81,10 +81,10 @@ def _extend_tensor(input_tensor: torch.Tensor, N: int) -> torch.Tensor:
class Textures(object): class Textures(object):
def __init__( def __init__(
self, self,
maps: Union[List, torch.Tensor] = None, maps: Union[List, torch.Tensor, None] = None,
faces_uvs: torch.Tensor = None, faces_uvs: Optional[torch.Tensor] = None,
verts_uvs: torch.Tensor = None, verts_uvs: Optional[torch.Tensor] = None,
verts_rgb: torch.Tensor = None, verts_rgb: Optional[torch.Tensor] = None,
): ):
""" """
Args: Args:
@ -105,7 +105,7 @@ class Textures(object):
msg = "Expected verts_rgb to be of shape (N, V, 3); got %r" msg = "Expected verts_rgb to be of shape (N, V, 3); got %r"
raise ValueError(msg % verts_rgb.shape) raise ValueError(msg % verts_rgb.shape)
if maps is not None: 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" msg = "Expected maps to be of shape (N, H, W, 3); got %r"
raise ValueError(msg % repr(maps.shape)) raise ValueError(msg % repr(maps.shape))
elif isinstance(maps, list): elif isinstance(maps, list):

View File

@ -2,6 +2,7 @@
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. # Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
import functools import functools
from typing import Optional
import torch import torch
@ -246,7 +247,10 @@ def matrix_to_euler_angles(matrix, convention: str):
def random_quaternions( 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, Generate random quaternions representing rotations,
@ -272,7 +276,10 @@ def random_quaternions(
def random_rotations( 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. Generate random rotations as 3x3 rotation matrices.
@ -295,7 +302,7 @@ def random_rotations(
def random_rotation( 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. Generate a single random 3x3 rotation matrix.

View File

@ -3,6 +3,7 @@
import math import math
import warnings import warnings
from typing import Optional
import torch import torch
from .rotation_conversions import _axis_angle_rotation from .rotation_conversions import _axis_angle_rotation
@ -250,7 +251,7 @@ class Transform3d:
out._matrix = matrix out._matrix = matrix
return out 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 Use this transform to transform a set of 3D points. Assumes row major
ordering of the input points. ordering of the input points.

View File

@ -4,7 +4,6 @@
import argparse import argparse
import json import json
import os import os
import nbformat import nbformat
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from nbconvert import HTMLExporter, ScriptExporter from nbconvert import HTMLExporter, ScriptExporter

View File

@ -6,7 +6,8 @@ include_trailing_comma=True
multi_line_output=3 multi_line_output=3
known_standard_library=numpy,setuptools known_standard_library=numpy,setuptools
known_myself=pytorch3d 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 no_lines_before=STDLIB,THIRDPARTY
sections=FUTURE,STDLIB,THIRDPARTY,myself,FIRSTPARTY,LOCALFOLDER sections=FUTURE,STDLIB,THIRDPARTY,myself,FIRSTPARTY,LOCALFOLDER
default_section=FIRSTPARTY default_section=FIRSTPARTY

View File

@ -8,7 +8,6 @@ import unittest
import torch import torch
from pytorch3d.transforms.rotation_conversions import ( from pytorch3d.transforms.rotation_conversions import (
_axis_angle_rotation,
euler_angles_to_matrix, euler_angles_to_matrix,
matrix_to_euler_angles, matrix_to_euler_angles,
matrix_to_quaternion, matrix_to_quaternion,