def test_mesh_renderer_to(self): """ Test moving all the tensors in the mesh renderer to a new device. """ device1 = torch.device("cpu") R, T = look_at_view_transform(1500, 0.0, 0.0) # Init shader settings materials = Materials(device=device1) lights = PointLights(device=device1) lights.location = torch.tensor([0.0, 0.0, +1000.0], device=device1)[None] raster_settings = RasterizationSettings( image_size=256, blur_radius=0.0, faces_per_pixel=1 ) cameras = FoVPerspectiveCameras( device=device1, R=R, T=T, aspect_ratio=1.0, fov=60.0, zfar=100 ) rasterizer = MeshRasterizer(cameras=cameras, raster_settings=raster_settings) blend_params = BlendParams( 1e-4, 1e-4, background_color=torch.zeros(3, dtype=torch.float32, device=device1), ) shader = SoftPhongShader( lights=lights, cameras=cameras, materials=materials, blend_params=blend_params, ) renderer = MeshRenderer(rasterizer=rasterizer, shader=shader) mesh = ico_sphere(2, device1) verts_padded = mesh.verts_padded() textures = TexturesVertex( verts_features=torch.ones_like(verts_padded, device=device1) ) mesh.textures = textures self._check_mesh_renderer_props_on_device(renderer, device1) # Test rendering on cpu output_images = renderer(mesh) self.assertEqual(output_images.device, device1) # Move renderer and mesh to another device and re render # This also tests that background_color is correctly moved to # the new device device2 = torch.device("cuda:0") renderer = renderer.to(device2) mesh = mesh.to(device2) self._check_mesh_renderer_props_on_device(renderer, device2) output_images = renderer(mesh) self.assertEqual(output_images.device, device2)
plt.imsave('./data/2.png', images[0, ..., :3].cpu().numpy()) plt.grid("off") plt.axis("off") # ## 4. Move the light behind the object and re-render # # We can pass arbirary keyword arguments to the `rasterizer`/`shader` via the call to the `renderer` so the renderer does not need to be reinitialized if any of the settings change/ # # In this case, we can simply update the location of the lights and pass them into the call to the renderer. # # The image is now dark as there is only ambient lighting, and there are no specular highlights. # In[6]: # Now move the light so it is on the +Z axis which will be behind the cow. lights.location = torch.tensor([0.0, 0.0, +1.0], device=device)[None] images = renderer(mesh, lights=lights) # In[7]: plt.figure(figsize=(10, 10)) print("saving figure 3!") plt.imsave('./data/3.png', images[0, ..., :3].cpu().numpy()) plt.grid("off") plt.axis("off") # ## 5. Rotate the object, modify the material properties or light properties # # We can also change many other settings in the rendering pipeline. Here we: # # - change the **viewing angle** of the camera