docstr: Test rendering objects from ShapeNetCore.
docstr_tok: ["Test","rendering","objects","from","ShapeNetCore","."]
code: def test_render_shapenet_core(self):
      """
      Test rendering objects from ShapeNetCore.
      """
      # Setup device and seed for random selections.
      device = torch.device("cuda:0")
      torch.manual_seed(39)
  
      # Load category piano from ShapeNetCore.
      piano_dataset = ShapeNetCore(SHAPENET_PATH, synsets=["piano"])
  
      # Rendering settings.
      R, T = look_at_view_transform(1.0, 1.0, 90)
      cameras = FoVPerspectiveCameras(R=R, T=T, device=device)
      raster_settings = RasterizationSettings(image_size=512)
      lights = PointLights(
          location=torch.tensor([0.0, 1.0, -2.0], device=device)[None],
          # TODO: debug the source of the discrepancy in two images when rendering on GPU.
          diffuse_color=((0, 0, 0),),
          specular_color=((0, 0, 0),),
          device=device,
      )
  
      # Render first three models in the piano category.
      pianos = piano_dataset.render(
          idxs=list(range(3)),
          device=device,
          cameras=cameras,
          raster_settings=raster_settings,
          lights=lights,
      )
      # Check that there are three images in the batch.
      self.assertEqual(pianos.shape[0], 3)
  
      # Compare the rendered models to the reference images.
      for idx in range(3):
          piano_rgb = pianos[idx, ..., :3].squeeze().cpu()
          if DEBUG:
              Image.fromarray(
                  (piano_rgb.numpy() * 255).astype(np.uint8)
              ).save(
                  DATA_DIR
                  / (
                      "DEBUG_shapenet_core_render_piano_by_idxs_%s.png"
                      % idx
                  )
              )
          image_ref = load_rgb_image(
              "test_shapenet_core_render_piano_%s.png" % idx, DATA_DIR
          )
          self.assertClose(piano_rgb, image_ref, atol=0.05)
  
      # Render the same piano models but by model_ids this time.
      pianos_2 = piano_dataset.render(
          model_ids=[
              "13394ca47c89f91525a3aaf903a41c90",
              "14755c2ee8e693aba508f621166382b0",
              "156c4207af6d2c8f1fdc97905708b8ea",
          ],
          device=device,
          cameras=cameras,
          raster_settings=raster_settings,
          lights=lights,
      )
  
      # Compare the rendered models to the reference images.
      for idx in range(3):
          piano_rgb_2 = pianos_2[idx, ..., :3].squeeze().cpu()
          if DEBUG:
              Image.fromarray(
                  (piano_rgb_2.numpy() * 255).astype(np.uint8)
              ).save(
                  DATA_DIR
                  / (
                      "DEBUG_shapenet_core_render_piano_by_ids_%s.png"
                      % idx
                  )
              )
          image_ref = load_rgb_image(
              "test_shapenet_core_render_piano_%s.png" % idx, DATA_DIR
          )
          self.assertClose(piano_rgb_2, image_ref, atol=0.05)
  
      #######################
      # Render by categories
      #######################
  
      # Load ShapeNetCore.
      shapenet_dataset = ShapeNetCore(SHAPENET_PATH)
  
      # Render a mixture of categories and specify the number of models to be
      # randomly sampled from each category.
      mixed_objs = shapenet_dataset.render(
          categories=["faucet", "chair"],
          sample_nums=[2, 1],
          device=device,
          cameras=cameras,
          raster_settings=raster_settings,
          lights=lights,
      )
      # Compare the rendered models to the reference images.
      for idx in range(3):
          mixed_rgb = mixed_objs[idx, ..., :3].squeeze().cpu()
          if DEBUG:
              Image.fromarray(
                  (mixed_rgb.numpy() * 255).astype(np.uint8)
              ).save(
                  DATA_DIR
                  / (
                      "DEBUG_shapenet_core_render_mixed_by_categories_%s.png"
                      % idx
                  )
              )
          image_ref = load_rgb_image(
              "test_shapenet_core_render_mixed_by_categories_%s.png"
              % idx,
              DATA_DIR,
          )
          self.assertClose(mixed_rgb, image_ref, atol=0.05)
  
      # Render a mixture of categories without specifying sample_nums.
      mixed_objs_2 = shapenet_dataset.render(
          categories=["faucet", "chair"],
          device=device,
          cameras=cameras,
          raster_settings=raster_settings,
          lights=lights,
      )
      # Compare the rendered models to the reference images.
      for idx in range(2):
          mixed_rgb_2 = mixed_objs_2[idx, ..., :3].squeeze().cpu()
          if DEBUG:
              Image.fromarray(
                  (mixed_rgb_2.numpy() * 255).astype(np.uint8)
              ).save(
                  DATA_DIR
                  / (
                      "DEBUG_shapenet_core_render_without_sample_nums_%s.png"
                      % idx
                  )
              )
          image_ref = load_rgb_image(
              "test_shapenet_core_render_without_sample_nums_%s.png"
              % idx,
              DATA_DIR,
          )
          self.assertClose(mixed_rgb_2, image_ref, atol=0.05)
  
file_path: pytorch3d/tests/test_shapenet_core.py
imports: import os
  import unittest
  import numpy
  import torch
  from PIL import Image
  from pytorch3d.datasets import collate_batched_meshes, ShapeNetCore
  from pytorch3d.renderer import FoVPerspectiveCameras, look_at_view_transform, PointLights, RasterizationSettings
  from torch.utils.data import DataLoader
  from .common_testing import get_tests_dir, load_rgb_image, TestCaseMixin
label_desc: Use image node_id 5 for calling the TestShapenetCore obj's underlying member method code with example usage: obj.test_render_shapenet_core() without return types