Commit d57daa6f authored by Patrick Labatut's avatar Patrick Labatut Committed by Facebook GitHub Bot
Browse files

Address black + isort fbsource linter warnings

Summary: Address black + isort fbsource linter warnings from D20558374 (previous diff)

Reviewed By: nikhilaravi

Differential Revision: D20558373

fbshipit-source-id: d3607de4a01fb24c0d5269634563a7914bddf1c8
parent eb512ffd
......@@ -8,6 +8,7 @@ TODO: python 3.8 when pytorch 1.4.
"""
import os.path
import jinja2
import yaml
......@@ -45,9 +46,7 @@ def workflow_pair(
):
w = []
base_workflow_name = (
f"{prefix}binary_linux_{btype}_py{python_version}_{cu_version}"
)
base_workflow_name = f"{prefix}binary_linux_{btype}_py{python_version}_{cu_version}"
w.append(
generate_base_workflow(
......@@ -94,9 +93,7 @@ def generate_base_workflow(
return {f"binary_linux_{btype}": d}
def generate_upload_workflow(
*, base_workflow_name, btype, cu_version, filter_branch
):
def generate_upload_workflow(*, base_workflow_name, btype, cu_version, filter_branch):
d = {
"name": f"{base_workflow_name}_upload",
"context": "org-member",
......
......@@ -22,6 +22,7 @@ from recommonmark.states import DummyStateMachine
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.ext.autodoc import between
# Monkey patch to fix recommonmark 0.4 doc reference issues.
orig_run_role = DummyStateMachine.run_role
......@@ -154,9 +155,7 @@ html_theme_options = {"collapse_navigation": True}
def url_resolver(url):
if ".html" not in url:
url = url.replace("../", "")
return (
"https://github.com/facebookresearch/pytorch3d/blob/master/" + url
)
return "https://github.com/facebookresearch/pytorch3d/blob/master/" + url
else:
if DEPLOY:
return "http://pytorch3d.readthedocs.io/" + url
......@@ -188,9 +187,7 @@ def setup(app):
# Register a sphinx.ext.autodoc.between listener to ignore everything
# between lines that contain the word IGNORE
app.connect(
"autodoc-process-docstring", between("^.*IGNORE.*$", exclude=True)
)
app.connect("autodoc-process-docstring", between("^.*IGNORE.*$", exclude=True))
app.add_transform(AutoStructify)
return app
......@@ -15,7 +15,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Render a coloured point cloud\n",
"# Render a colored point cloud\n",
"\n",
"This tutorial shows how to:\n",
"- set up a renderer \n",
......@@ -84,7 +84,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load a point cloud and corresponding colours\n",
"### Load a point cloud and corresponding colors\n",
"\n",
"Load a `.ply` file and create a **Point Cloud** object. \n",
"\n",
......
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
from .camera_visualization import (
get_camera_wireframe,
plot_camera_scene,
plot_cameras,
)
from .camera_visualization import get_camera_wireframe, plot_camera_scene, plot_cameras
from .plot_image_grid import image_grid
......@@ -34,13 +34,9 @@ def image_grid(
cols = 1
gridspec_kw = {"wspace": 0.0, "hspace": 0.0} if fill else {}
fig, axarr = plt.subplots(
rows, cols, gridspec_kw=gridspec_kw, figsize=(15, 9)
)
fig, axarr = plt.subplots(rows, cols, gridspec_kw=gridspec_kw, figsize=(15, 9))
bleed = 0
fig.subplots_adjust(
left=bleed, bottom=bleed, right=(1 - bleed), top=(1 - bleed)
)
fig.subplots_adjust(left=bleed, bottom=bleed, right=(1 - bleed), top=(1 - bleed))
for ax, im in zip(axarr.ravel(), images):
if rgb:
......
......@@ -4,4 +4,5 @@
from .obj_io import load_obj, load_objs_as_meshes, save_obj
from .ply_io import load_ply, save_ply
__all__ = [k for k in globals().keys() if not k.startswith("_")]
......@@ -2,16 +2,16 @@
"""This module implements utility functions for loading and saving meshes."""
import numpy as np
import os
import pathlib
import warnings
from collections import namedtuple
from typing import List, Optional
import numpy as np
import torch
from fvcore.common.file_io import PathManager
from PIL import Image
from pytorch3d.structures import Meshes, Textures, join_meshes
......@@ -51,9 +51,7 @@ def _read_image(file_name: str, format=None):
# Faces & Aux type returned from load_obj function.
_Faces = namedtuple("Faces", "verts_idx normals_idx textures_idx materials_idx")
_Aux = namedtuple(
"Properties", "normals verts_uvs material_colors texture_images"
)
_Aux = namedtuple("Properties", "normals verts_uvs material_colors texture_images")
def _format_faces_indices(faces_indices, max_index):
......@@ -247,9 +245,7 @@ def load_objs_as_meshes(files: list, device=None, load_textures: bool = True):
image = list(tex_maps.values())[0].to(device)[None]
tex = Textures(verts_uvs=verts_uvs, faces_uvs=faces_uvs, maps=image)
mesh = Meshes(
verts=[verts], faces=[faces.verts_idx.to(device)], textures=tex
)
mesh = Meshes(verts=[verts], faces=[faces.verts_idx.to(device)], textures=tex)
mesh_list.append(mesh)
if len(mesh_list) == 1:
return mesh_list[0]
......@@ -308,9 +304,7 @@ def _parse_face(
# Subdivide faces with more than 3 vertices. See comments of the
# load_obj function for more details.
for i in range(len(face_verts) - 2):
faces_verts_idx.append(
(face_verts[0], face_verts[i + 1], face_verts[i + 2])
)
faces_verts_idx.append((face_verts[0], face_verts[i + 1], face_verts[i + 2]))
if len(face_normals) > 0:
faces_normals_idx.append(
(face_normals[0], face_normals[i + 1], face_normals[i + 2])
......@@ -367,8 +361,7 @@ def _load(f_obj, data_dir, load_textures=True):
tx = [float(x) for x in line.split()[1:3]]
if len(tx) != 2:
raise ValueError(
"Texture %s does not have 2 values. Line: %s"
% (str(tx), str(line))
"Texture %s does not have 2 values. Line: %s" % (str(tx), str(line))
)
verts_uvs.append(tx)
elif line.startswith("vn "):
......@@ -397,17 +390,13 @@ def _load(f_obj, data_dir, load_textures=True):
# Repeat for normals and textures if present.
if len(faces_normals_idx) > 0:
faces_normals_idx = _format_faces_indices(
faces_normals_idx, normals.shape[0]
)
faces_normals_idx = _format_faces_indices(faces_normals_idx, normals.shape[0])
if len(faces_textures_idx) > 0:
faces_textures_idx = _format_faces_indices(
faces_textures_idx, verts_uvs.shape[0]
)
if len(faces_materials_idx) > 0:
faces_materials_idx = torch.tensor(
faces_materials_idx, dtype=torch.int64
)
faces_materials_idx = torch.tensor(faces_materials_idx, dtype=torch.int64)
# Load materials
material_colors, texture_images = None, None
......
......@@ -4,15 +4,17 @@
"""This module implements utility functions for loading and saving meshes."""
import numpy as np
import pathlib
import struct
import sys
import warnings
from collections import namedtuple
from typing import Optional, Tuple
import numpy as np
import torch
_PlyTypeData = namedtuple("_PlyTypeData", "size struct_char np_type")
_PLY_TYPES = {
......@@ -257,11 +259,7 @@ def _try_read_ply_constant_list_ascii(f, definition: _PlyElementType):
"ignore", message=".* Empty input file.*", category=UserWarning
)
data = np.loadtxt(
f,
dtype=np_type,
comments=None,
ndmin=2,
max_rows=definition.count,
f, dtype=np_type, comments=None, ndmin=2, max_rows=definition.count
)
except ValueError:
f.seek(start_point)
......@@ -301,9 +299,7 @@ def _parse_heterogenous_property_ascii(datum, line_iter, property: _Property):
length = int(value)
except ValueError:
raise ValueError("A list length was not a number.")
list_value = np.zeros(
length, dtype=_PLY_TYPES[property.data_type].np_type
)
list_value = np.zeros(length, dtype=_PLY_TYPES[property.data_type].np_type)
for i in range(length):
inner_value = next(line_iter, None)
if inner_value is None:
......@@ -404,8 +400,7 @@ def _read_ply_element_struct(f, definition: _PlyElementType, endian_str: str):
values. There is one column for each property.
"""
format = "".join(
_PLY_TYPES[property.data_type].struct_char
for property in definition.properties
_PLY_TYPES[property.data_type].struct_char for property in definition.properties
)
format = endian_str + format
pattern = struct.Struct(format)
......@@ -414,10 +409,7 @@ def _read_ply_element_struct(f, definition: _PlyElementType, endian_str: str):
bytes_data = f.read(needed_bytes)
if len(bytes_data) != needed_bytes:
raise ValueError("Not enough data for %s." % definition.name)
data = [
pattern.unpack_from(bytes_data, i * size)
for i in range(definition.count)
]
data = [pattern.unpack_from(bytes_data, i * size) for i in range(definition.count)]
return data
......@@ -475,9 +467,7 @@ def _try_read_ply_constant_list_binary(
return output
def _read_ply_element_binary(
f, definition: _PlyElementType, big_endian: bool
) -> list:
def _read_ply_element_binary(f, definition: _PlyElementType, big_endian: bool) -> list:
"""
Decode all instances of a single element from a binary .ply file.
......@@ -515,9 +505,7 @@ def _read_ply_element_binary(
data = []
for _i in range(definition.count):
datum = []
for property, property_struct in zip(
definition.properties, property_structs
):
for property, property_struct in zip(definition.properties, property_structs):
size = property_struct.size
initial_data = f.read(size)
if len(initial_data) != size:
......@@ -656,28 +644,19 @@ def load_ply(f):
if face is None:
raise ValueError("The ply file has no face element.")
if (
not isinstance(vertex, np.ndarray)
or vertex.ndim != 2
or vertex.shape[1] != 3
):
if not isinstance(vertex, np.ndarray) or vertex.ndim != 2 or vertex.shape[1] != 3:
raise ValueError("Invalid vertices in file.")
verts = torch.tensor(vertex, dtype=torch.float32)
face_head = next(head for head in header.elements if head.name == "face")
if (
len(face_head.properties) != 1
or face_head.properties[0].list_size_type is None
):
if len(face_head.properties) != 1 or face_head.properties[0].list_size_type is None:
raise ValueError("Unexpected form of faces data.")
# face_head.properties[0].name is usually "vertex_index" or "vertex_indices"
# but we don't need to enforce this.
if isinstance(face, np.ndarray) and face.ndim == 2:
if face.shape[1] < 3:
raise ValueError("Faces must have at least 3 vertices.")
face_arrays = [
face[:, [0, i + 1, i + 2]] for i in range(face.shape[1] - 2)
]
face_arrays = [face[:, [0, i + 1, i + 2]] for i in range(face.shape[1] - 2)]
faces = torch.tensor(np.vstack(face_arrays), dtype=torch.int64)
else:
face_list = []
......@@ -687,9 +666,7 @@ def load_ply(f):
if face_item.shape[0] < 3:
raise ValueError("Faces must have at least 3 vertices.")
for i in range(face_item.shape[0] - 2):
face_list.append(
[face_item[0], face_item[i + 1], face_item[i + 2]]
)
face_list.append([face_item[0], face_item[i + 1], face_item[i + 2]])
faces = torch.tensor(face_list, dtype=torch.int64)
return verts, faces
......
......@@ -6,4 +6,5 @@ from .mesh_edge_loss import mesh_edge_loss
from .mesh_laplacian_smoothing import mesh_laplacian_smoothing
from .mesh_normal_consistency import mesh_normal_consistency
__all__ = [k for k in globals().keys() if not k.startswith("_")]
......@@ -2,13 +2,10 @@
import torch
import torch.nn.functional as F
from pytorch3d.ops.nearest_neighbor_points import nn_points_idx
def _validate_chamfer_reduction_inputs(
batch_reduction: str, point_reduction: str
):
def _validate_chamfer_reduction_inputs(batch_reduction: str, point_reduction: str):
"""Check the requested reductions are valid.
Args:
......@@ -18,17 +15,11 @@ def _validate_chamfer_reduction_inputs(
points, can be one of ["none", "mean", "sum"].
"""
if batch_reduction not in ["none", "mean", "sum"]:
raise ValueError(
'batch_reduction must be one of ["none", "mean", "sum"]'
)
raise ValueError('batch_reduction must be one of ["none", "mean", "sum"]')
if point_reduction not in ["none", "mean", "sum"]:
raise ValueError(
'point_reduction must be one of ["none", "mean", "sum"]'
)
raise ValueError('point_reduction must be one of ["none", "mean", "sum"]')
if batch_reduction == "none" and point_reduction == "none":
raise ValueError(
'batch_reduction and point_reduction cannot both be "none".'
)
raise ValueError('batch_reduction and point_reduction cannot both be "none".')
def chamfer_distance(
......@@ -87,10 +78,7 @@ def chamfer_distance(
(x.sum((1, 2)) * weights).sum() * 0.0,
(x.sum((1, 2)) * weights).sum() * 0.0,
)
return (
(x.sum((1, 2)) * weights) * 0.0,
(x.sum((1, 2)) * weights) * 0.0,
)
return ((x.sum((1, 2)) * weights) * 0.0, (x.sum((1, 2)) * weights) * 0.0)
return_normals = x_normals is not None and y_normals is not None
cham_norm_x = x.new_zeros(())
......
......@@ -2,6 +2,7 @@
from itertools import islice
import torch
......@@ -76,10 +77,7 @@ def mesh_normal_consistency(meshes):
with torch.no_grad():
edge_idx = face_to_edge.reshape(F * 3) # (3 * F,) indexes into edges
vert_idx = (
faces_packed.view(1, F, 3)
.expand(3, F, 3)
.transpose(0, 1)
.reshape(3 * F, 3)
faces_packed.view(1, F, 3).expand(3, F, 3).transpose(0, 1).reshape(3 * F, 3)
)
edge_idx, edge_sort_idx = edge_idx.sort()
vert_idx = vert_idx[edge_sort_idx]
......@@ -132,9 +130,7 @@ def mesh_normal_consistency(meshes):
loss = 1 - torch.cosine_similarity(n0, n1, dim=1)
verts_packed_to_mesh_idx = verts_packed_to_mesh_idx[vert_idx[:, 0]]
verts_packed_to_mesh_idx = verts_packed_to_mesh_idx[
vert_edge_pair_idx[:, 0]
]
verts_packed_to_mesh_idx = verts_packed_to_mesh_idx[vert_edge_pair_idx[:, 0]]
num_normals = verts_packed_to_mesh_idx.bincount(minlength=N)
weights = 1.0 / num_normals[verts_packed_to_mesh_idx].float()
......
......@@ -10,4 +10,5 @@ from .sample_points_from_meshes import sample_points_from_meshes
from .subdivide_meshes import SubdivideMeshes
from .vert_align import vert_align
__all__ = [k for k in globals().keys() if not k.startswith("_")]
......@@ -3,7 +3,6 @@
import torch
import torch.nn.functional as F
from pytorch3d.structures import Meshes
......@@ -200,8 +199,6 @@ def cubify(voxels, thresh, device=None) -> Meshes:
grid_verts.index_select(0, (idleverts[n] == 0).nonzero()[:, 0])
for n in range(N)
]
faces_list = [
nface - idlenum[n][nface] for n, nface in enumerate(faces_list)
]
faces_list = [nface - idlenum[n][nface] for n, nface in enumerate(faces_list)]
return Meshes(verts=verts_list, faces=faces_list)
......@@ -3,11 +3,10 @@
import torch
import torch.nn as nn
from pytorch3d import _C
from torch.autograd import Function
from torch.autograd.function import once_differentiable
from pytorch3d import _C
class GraphConv(nn.Module):
"""A single graph convolution layer."""
......@@ -60,9 +59,7 @@ class GraphConv(nn.Module):
number of output features per vertex.
"""
if verts.is_cuda != edges.is_cuda:
raise ValueError(
"verts and edges tensors must be on the same device."
)
raise ValueError("verts and edges tensors must be on the same device.")
if verts.shape[0] == 0:
# empty graph.
return verts.new_zeros((0, self.output_dim)) * verts.sum()
......
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
import torch
from pytorch3d import _C
......
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
import torch
from pytorch3d import _C
from torch.autograd import Function
from torch.autograd.function import once_differentiable
from pytorch3d import _C
class _MeshFaceAreasNormals(Function):
"""
......
......@@ -2,7 +2,6 @@
import torch
from pytorch3d import _C
......@@ -31,9 +30,7 @@ def nn_points_idx(p1, p2, p2_normals=None) -> torch.Tensor:
"""
N, P1, D = p1.shape
with torch.no_grad():
p1_nn_idx = _C.nn_points_idx(
p1.contiguous(), p2.contiguous()
) # (N, P1)
p1_nn_idx = _C.nn_points_idx(p1.contiguous(), p2.contiguous()) # (N, P1)
p1_nn_idx_expanded = p1_nn_idx.view(N, P1, 1).expand(N, P1, D)
p1_nn_points = p2.gather(1, p1_nn_idx_expanded)
if p2_normals is None:
......
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
import torch
from pytorch3d import _C
from torch.autograd import Function
from torch.autograd.function import once_differentiable
from pytorch3d import _C
class _PackedToPadded(Function):
"""
......
......@@ -7,8 +7,8 @@ batches of meshes.
"""
import sys
from typing import Tuple, Union
import torch
import torch
from pytorch3d.ops.mesh_face_areas_normals import mesh_face_areas_normals
from pytorch3d.ops.packed_to_padded import packed_to_padded
......@@ -53,9 +53,7 @@ def sample_points_from_meshes(
# Only compute samples for non empty meshes
with torch.no_grad():
areas, _ = mesh_face_areas_normals(
verts, faces
) # Face areas can be zero.
areas, _ = mesh_face_areas_normals(verts, faces) # Face areas can be zero.
max_faces = meshes.num_faces_per_mesh().max().item()
areas_padded = packed_to_padded(
areas, mesh_to_face[meshes.valid], max_faces
......@@ -80,21 +78,17 @@ def sample_points_from_meshes(
a = v0[sample_face_idxs] # (N, num_samples, 3)
b = v1[sample_face_idxs]
c = v2[sample_face_idxs]
samples[meshes.valid] = (
w0[:, :, None] * a + w1[:, :, None] * b + w2[:, :, None] * c
)
samples[meshes.valid] = w0[:, :, None] * a + w1[:, :, None] * b + w2[:, :, None] * c
if return_normals:
# Intialize normals tensor with fill value 0 for empty meshes.
# Normals for the sampled points are face normals computed from
# the vertices of the face in which the sampled point lies.
normals = torch.zeros(
(num_meshes, num_samples, 3), device=meshes.device
)
normals = torch.zeros((num_meshes, num_samples, 3), device=meshes.device)
vert_normals = (v1 - v0).cross(v2 - v1, dim=1)
vert_normals = vert_normals / vert_normals.norm(
dim=1, p=2, keepdim=True
).clamp(min=sys.float_info.epsilon)
vert_normals = vert_normals / vert_normals.norm(dim=1, p=2, keepdim=True).clamp(
min=sys.float_info.epsilon
)
vert_normals = vert_normals[sample_face_idxs]
normals[meshes.valid] = vert_normals
......
......@@ -3,7 +3,6 @@
import torch
import torch.nn as nn
from pytorch3d.structures import Meshes
......@@ -193,16 +192,12 @@ class SubdivideMeshes(nn.Module):
edges = meshes[0].edges_packed()
# The set of faces is the same across the different meshes.
new_faces = self._subdivided_faces.view(1, -1, 3).expand(
self._N, -1, -1
)
new_faces = self._subdivided_faces.view(1, -1, 3).expand(self._N, -1, -1)
# Add one new vertex at the midpoint of each edge by taking the average
# of the vertices that form each edge.
new_verts = verts[:, edges].mean(dim=2)
new_verts = torch.cat(
[verts, new_verts], dim=1
) # (sum(V_n)+sum(E_n), 3)
new_verts = torch.cat([verts, new_verts], dim=1) # (sum(V_n)+sum(E_n), 3)
new_feats = None
# Calculate features for new vertices.
......@@ -212,15 +207,11 @@ class SubdivideMeshes(nn.Module):
# padded, i.e. (N*V, D) to (N, V, D).
feats = feats.view(verts.size(0), verts.size(1), feats.size(1))
if feats.dim() != 3:
raise ValueError(
"features need to be of shape (N, V, D) or (N*V, D)"
)
raise ValueError("features need to be of shape (N, V, D) or (N*V, D)")
# Take average of the features at the vertices that form each edge.
new_feats = feats[:, edges].mean(dim=2)
new_feats = torch.cat(
[feats, new_feats], dim=1
) # (sum(V_n)+sum(E_n), 3)
new_feats = torch.cat([feats, new_feats], dim=1) # (sum(V_n)+sum(E_n), 3)
new_meshes = Meshes(verts=new_verts, faces=new_faces)
......@@ -270,9 +261,7 @@ class SubdivideMeshes(nn.Module):
) # (sum(V_n)+sum(E_n),)
verts_ordered_idx_init = torch.zeros(
new_verts_per_mesh.sum(),
dtype=torch.int64,
device=meshes.device,
new_verts_per_mesh.sum(), dtype=torch.int64, device=meshes.device
) # (sum(V_n)+sum(E_n),)
# Reassign vertex indices so that existing and new vertices for each
......@@ -288,9 +277,7 @@ class SubdivideMeshes(nn.Module):
# Calculate the indices needed to group the existing and new faces
# for each mesh.
face_sort_idx = create_faces_index(
num_faces_per_mesh, device=meshes.device
)
face_sort_idx = create_faces_index(num_faces_per_mesh, device=meshes.device)
# Reorder the faces to sequentially group existing and new faces
# for each mesh.
......@@ -361,9 +348,7 @@ def create_verts_index(verts_per_mesh, edges_per_mesh, device=None):
E = edges_per_mesh.sum() # e.g. 21
verts_per_mesh_cumsum = verts_per_mesh.cumsum(dim=0) # (N,) e.g. (4, 9, 15)
edges_per_mesh_cumsum = edges_per_mesh.cumsum(
dim=0
) # (N,) e.g. (5, 12, 21)
edges_per_mesh_cumsum = edges_per_mesh.cumsum(dim=0) # (N,) e.g. (5, 12, 21)
v_to_e_idx = verts_per_mesh_cumsum.clone()
......@@ -373,9 +358,7 @@ def create_verts_index(verts_per_mesh, edges_per_mesh, device=None):
] # e.g. (4, 9, 15) + (0, 5, 12) = (4, 14, 27)
# vertex to edge offset.
v_to_e_offset = (
V - verts_per_mesh_cumsum
) # e.g. 15 - (4, 9, 15) = (11, 6, 0)
v_to_e_offset = V - verts_per_mesh_cumsum # e.g. 15 - (4, 9, 15) = (11, 6, 0)
v_to_e_offset[1:] += edges_per_mesh_cumsum[
:-1
] # e.g. (11, 6, 0) + (0, 5, 12) = (11, 11, 12)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment