class_docstr: Pix3D is a large-scale dataset of diverse image-shape pairs with pixel-level 2D-3D alignment.
class_docstr_tok: ["Pix3D","is","a","large-scale","dataset","of","diverse","image-shape","pairs","with","pixel-level","2D-3D","alignment","."]
docstr: Yields examples.
  
  As Pix3D does not come with a predefined train/test split, we adopt one from
  Mesh R-CNN. The split ensures
  that the 3D models appearing in the train and test sets are disjoint.
  
  Args:
    samples_directory: `str`, path to the directory where Pix3D is stored.
    split_file: `str`, path to .npy file containing the indices of the current
    split.
docstr_tok: ["Yields","examples",".","As","Pix3D","does","not","come","with","a","predefined","train\/test","split",",","we","adopt","one","from","Mesh","R-CNN",".","The","split","ensures","that","the","3D","models","appearing","in","the","train","and","test","sets","are","disjoint",".","Args",":","samples_directory",":","`","str","`",",","path","to","the","directory","where","Pix3D","is","stored",".","split_file",":","`","str","`",",","path","to",".npy","file","containing","the","indices","of","the","current","split","."]
code: def _generate_examples(self, samples_directory, split_file):
      """Yields examples.
  
      As Pix3D does not come with a predefined train/test split, we adopt one from
      Mesh R-CNN. The split ensures
      that the 3D models appearing in the train and test sets are disjoint.
  
      Args:
        samples_directory: `str`, path to the directory where Pix3D is stored.
        split_file: `str`, path to .npy file containing the indices of the current
        split.
      """
  
      with tf.io.gfile.GFile(
          os.path.join(samples_directory, "pix3d.json"), mode="r"
      ) as pix3d_index:
          pix3d = json.load(pix3d_index)
  
      split_samples = map(pix3d.__getitem__, np.load(split_file))
  
      def _build_bbox(box, img_size):
          """Create a BBox with correct order of coordinates.
  
          Args:
            box: Bounding box of the object as provided by Pix3d
            img_size:  size of the image, in the format of [width, height]
  
          Returns:
            tfds.features.BBox.
          """
          xmin, ymin, xmax, ymax = box
          width, height = img_size
          return tfds_features.BBox(
              ymin=ymin / height,
              xmin=xmin / width,
              ymax=ymax / height,
              xmax=xmax / width,
          )
  
      def _build_camera(f, img_size):
          """Prepare features for `Camera` FeatureConnector.
  
          The pose originates from the official Pix3D GitHub repository and
          describes the cameras position with respect to the scene.
  
          The focal length is originally provided in mm, but will be converted to
          pixel here using the fixed sensor with of 32 mm, which also originates
          from the Pix3D GitHub repository.
  
          Link to the official Pix3D repository:
          https://github.com/xingyuansun/pix3d.
  
          Args:
            f: float, denoting the focal length in mm.
            img_size: tuple of two floats, denoting the image height and width.
  
          Returns:
            Dictionary with all Camera Parameters.
          """
          sensor_width = 32.0
          return {
              "pose": {
                  "R": np.array(
                      [
                          [-1.0, 0.0, 0.0],
                          [0.0, -1.0, 0.0],
                          [0.0, 0.0, 1.0],
                      ],
                      dtype=np.float32,
                  ),
                  "t": np.zeros(3, dtype=np.float32),
              },
              "optical_center": (img_size[0] / 2, img_size[1] / 2),
              "f": (f / sensor_width * img_size[0]),
          }
  
      def _build_2d_keypoints(keypoints):
          """Wraps keypoint feature in dict, because TFDS does not allow more than sone unknown dimensions.
  
          Args:
            keypoints: Array of dimension `[N, M, 2]`, where N denotes the number of
              annotators and M is the number of 2D keypoints. Keypoints are stored
              as (origin: top left corner; +x: rightward; +y: downward);
              [-1.0, -1.0] if an annotator marked this keypoint hard to label.
  
          Returns:
            Dictionary containing shape and flattened keypoints.
          """
          if keypoints.ndim != 3 or keypoints.shape[-1] != 2:
              raise ValueError(
                  "2D keypoints should be in shape (N, M, 2)."
              )
  
          return {
              "num_annotators": keypoints.shape[0],
              "num_keypoints": keypoints.shape[1],
              "keypoints": keypoints.ravel(),
          }
  
      for sample in split_samples:
          example = {
              "image": os.path.join(samples_directory, sample["img"]),
              "image/filename": sample["img"],
              "image/source": sample["img_source"],
              "2d_keypoints": _build_2d_keypoints(
                  np.asarray(sample["2d_keypoints"], dtype=np.float32)
              ),
              "mask": os.path.join(samples_directory, sample["mask"]),
              "model": os.path.join(samples_directory, sample["model"]),
              "model/source": sample["model_source"],
              "3d_keypoints": np.loadtxt(
                  os.path.join(
                      samples_directory, sample["3d_keypoints"]
                  ),
                  dtype=np.float32,
              ),
              "voxel": {
                  "path": os.path.join(
                      samples_directory, sample["voxel"]
                  ),
                  "key": "voxel",
              },
              "pose": {
                  "R": sample["rot_mat"],
                  "t": sample["trans_mat"],
              },
              "camera": {
                  "parameters": _build_camera(
                      sample["focal_length"],
                      sample["img_size"],
                  ),
                  "position_with_respect_to_object": sample[
                      "cam_position"
                  ],
                  "inplane_rotation": sample["inplane_rotation"],
              },
              "category": self.CLASS_INDEX.index(sample["category"]),
              "bbox": _build_bbox(sample["bbox"], sample["img_size"]),
              "truncated": sample["truncated"],
              "occluded": sample["occluded"],
              "slightly_occluded": sample["slightly_occluded"],
          }
  
          yield sample["img"], example
  
file_path: graphics/tensorflow_graphics/datasets/pix3d/pix3d.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import json
  import os
  import numpy
  import tensorflow
  from tensorflow_datasets import features
  import tensorflow_datasets.public_api
  from tensorflow_graphics.datasets import features
label_desc: Use image node_id 3 for calling the Pix3d obj's underlying member method code with example usage: obj._generate_examples(samples_directory, split_file) and returns: tfds_features, dict, dict