From 9aeb88b48e3c98e9229f28933cc9e1c2700a593f Mon Sep 17 00:00:00 2001 From: Jeremy Reizenstein Date: Wed, 26 Aug 2020 08:26:04 -0700 Subject: [PATCH] tutorial fixes. validate #cameras Summary: Update the installation cells to import torch. Replace use of deprecated Textures in deform tutorial. Correct the deform tutorial's idea of its own name. Fix typo in batched part of render_textured_meshes which meant only one mesh was being rendered with a batch of cameras. Add an error check in the rasterizer to make the error friendly from such a mistake elsewhere. Reviewed By: gkioxari Differential Revision: D23345462 fbshipit-source-id: 1d5bd25db052f7ef687b7168d7aee5cc4dce8952 --- docs/tutorials/bundle_adjustment.ipynb | 1 + ...sition_optimization_with_differentiable_rendering.ipynb | 7 ++++--- docs/tutorials/dataloaders_ShapeNetCore_R2N2.ipynb | 1 + docs/tutorials/deform_source_mesh_to_target_mesh.ipynb | 3 ++- docs/tutorials/fit_textured_mesh.ipynb | 1 + docs/tutorials/render_colored_points.ipynb | 1 + docs/tutorials/render_textured_meshes.ipynb | 3 ++- pytorch3d/renderer/mesh/rasterizer.py | 6 ++++++ 8 files changed, 18 insertions(+), 5 deletions(-) diff --git a/docs/tutorials/bundle_adjustment.ipynb b/docs/tutorials/bundle_adjustment.ipynb index e5dfe700..67862446 100644 --- a/docs/tutorials/bundle_adjustment.ipynb +++ b/docs/tutorials/bundle_adjustment.ipynb @@ -82,6 +82,7 @@ "source": [ "!pip install torch torchvision\n", "import sys\n", + "import torch\n", "if torch.__version__=='1.6.0+cu101' and sys.platform.startswith('linux'):\n", " !pip install pytorch3d\n", "else:\n", diff --git a/docs/tutorials/camera_position_optimization_with_differentiable_rendering.ipynb b/docs/tutorials/camera_position_optimization_with_differentiable_rendering.ipynb index bf9216a4..ee879db3 100644 --- a/docs/tutorials/camera_position_optimization_with_differentiable_rendering.ipynb +++ b/docs/tutorials/camera_position_optimization_with_differentiable_rendering.ipynb @@ -69,6 +69,7 @@ "source": [ "!pip install torch torchvision\n", "import sys\n", + "import torch\n", "if torch.__version__=='1.6.0+cu101' and sys.platform.startswith('linux'):\n", " !pip install pytorch3d\n", "else:\n", @@ -99,7 +100,7 @@ "from pytorch3d.io import load_obj\n", "\n", "# datastructures\n", - "from pytorch3d.structures import Meshes, Textures\n", + "from pytorch3d.structures import Meshes\n", "\n", "# 3D transformations functions\n", "from pytorch3d.transforms import Rotate, Translate\n", @@ -108,7 +109,7 @@ "from pytorch3d.renderer import (\n", " FoVPerspectiveCameras, look_at_view_transform, look_at_rotation, \n", " RasterizationSettings, MeshRenderer, MeshRasterizer, BlendParams,\n", - " SoftSilhouetteShader, HardPhongShader, PointLights\n", + " SoftSilhouetteShader, HardPhongShader, PointLights, TexturesVertex,\n", ")" ] }, @@ -175,7 +176,7 @@ "\n", "# Initialize each vertex to be white in color.\n", "verts_rgb = torch.ones_like(verts)[None] # (1, V, 3)\n", - "textures = Textures(verts_rgb=verts_rgb.to(device))\n", + "textures = TexturesVertex(verts_features=verts_rgb.to(device))\n", "\n", "# Create a Meshes object for the teapot. Here we have only one mesh in the batch.\n", "teapot_mesh = Meshes(\n", diff --git a/docs/tutorials/dataloaders_ShapeNetCore_R2N2.ipynb b/docs/tutorials/dataloaders_ShapeNetCore_R2N2.ipynb index 8d7e6e49..15c3f9e0 100644 --- a/docs/tutorials/dataloaders_ShapeNetCore_R2N2.ipynb +++ b/docs/tutorials/dataloaders_ShapeNetCore_R2N2.ipynb @@ -44,6 +44,7 @@ "source": [ "!pip install torch torchvision\n", "import sys\n", + "import torch\n", "if torch.__version__=='1.6.0+cu101' and sys.platform.startswith('linux'):\n", " !pip install pytorch3d\n", "else:\n", diff --git a/docs/tutorials/deform_source_mesh_to_target_mesh.ipynb b/docs/tutorials/deform_source_mesh_to_target_mesh.ipynb index 030cad59..f6a83206 100644 --- a/docs/tutorials/deform_source_mesh_to_target_mesh.ipynb +++ b/docs/tutorials/deform_source_mesh_to_target_mesh.ipynb @@ -83,6 +83,7 @@ "source": [ "!pip install torch torchvision\n", "import sys\n", + "import torch\n", "if torch.__version__=='1.6.0+cu101' and sys.platform.startswith('linux'):\n", " !pip install pytorch3d\n", "else:\n", @@ -494,7 +495,7 @@ "bento/extensions/theme/main.css": true }, "colab": { - "name": "FitMesh.ipynb", + "name": "deform_source_mesh_to_target_mesh.ipynb", "provenance": [] }, "kernelspec": { diff --git a/docs/tutorials/fit_textured_mesh.ipynb b/docs/tutorials/fit_textured_mesh.ipynb index 3c4c8250..b751dc3a 100644 --- a/docs/tutorials/fit_textured_mesh.ipynb +++ b/docs/tutorials/fit_textured_mesh.ipynb @@ -61,6 +61,7 @@ "source": [ "!pip install torch torchvision\n", "import sys\n", + "import torch\n", "if torch.__version__=='1.6.0+cu101' and sys.platform.startswith('linux'):\n", " !pip install pytorch3d\n", "else:\n", diff --git a/docs/tutorials/render_colored_points.ipynb b/docs/tutorials/render_colored_points.ipynb index bab57376..954fd354 100644 --- a/docs/tutorials/render_colored_points.ipynb +++ b/docs/tutorials/render_colored_points.ipynb @@ -43,6 +43,7 @@ "source": [ "!pip install torch torchvision\n", "import sys\n", + "import torch\n", "if torch.__version__=='1.6.0+cu101' and sys.platform.startswith('linux'):\n", " !pip install pytorch3d\n", "else:\n", diff --git a/docs/tutorials/render_textured_meshes.ipynb b/docs/tutorials/render_textured_meshes.ipynb index 6dca1a3d..1e3033cc 100644 --- a/docs/tutorials/render_textured_meshes.ipynb +++ b/docs/tutorials/render_textured_meshes.ipynb @@ -66,6 +66,7 @@ "source": [ "!pip install torch torchvision\n", "import sys\n", + "import torch\n", "if torch.__version__=='1.6.0+cu101' and sys.platform.startswith('linux'):\n", " !pip install pytorch3d\n", "else:\n", @@ -542,7 +543,7 @@ "source": [ "# We can pass arbirary keyword arguments to the rasterizer/shader via the renderer\n", "# so the renderer does not need to be reinitialized if any of the settings change.\n", - "images = renderer(mesh, cameras=cameras, lights=lights)" + "images = renderer(meshes, cameras=cameras, lights=lights)" ] }, { diff --git a/pytorch3d/renderer/mesh/rasterizer.py b/pytorch3d/renderer/mesh/rasterizer.py index ac0b7402..424cdb44 100644 --- a/pytorch3d/renderer/mesh/rasterizer.py +++ b/pytorch3d/renderer/mesh/rasterizer.py @@ -94,6 +94,12 @@ class MeshRasterizer(nn.Module): msg = "Cameras must be specified either at initialization \ or in the forward pass of MeshRasterizer" raise ValueError(msg) + + n_cameras = len(cameras) + if n_cameras != 1 and n_cameras != len(meshes_world): + msg = "Wrong number (%r) of cameras for %r meshes" + raise ValueError(msg % (n_cameras, len(meshes_world))) + verts_world = meshes_world.verts_padded() # NOTE: Retaining view space z coordinate for now.