code: # coding=utf-8
  # Copyright 2022 The TensorFlow Datasets Authors.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  """SummScreen Summarization dataset, non-anonymized, non-tokenized version."""
  
  import json
  import os
  
  import tensorflow as tf
  import tensorflow_datasets.public_api as tfds
  
  _DESCRIPTION = """
  SummScreen Summarization dataset, non-anonymized, non-tokenized version.
  
  Train/val/test splits and filtering are based on the final tokenized dataset,
  but transcripts and recaps provided are based on the untokenized text.
  
  There are two features:
  
    - transcript: Full episode transcripts, each line of dialogue
      separated by newlines
    - recap: Recaps or summaries of episodes
  """
  
  _CITATION = """\
  @article{DBLP:journals/corr/abs-2104-07091,
    author    = {Mingda Chen and
                 Zewei Chu and
                 Sam Wiseman and
                 Kevin Gimpel},
    title     = {SummScreen: {A} Dataset for Abstractive Screenplay Summarization},
    journal   = {CoRR},
    volume    = {abs/2104.07091},
    year      = {2021},
    url       = {https://arxiv.org/abs/2104.07091},
    archivePrefix = {arXiv},
    eprint    = {2104.07091},
    timestamp = {Mon, 19 Apr 2021 16:45:47 +0200},
    biburl    = {https://dblp.org/rec/journals/corr/abs-2104-07091.bib},
    bibsource = {dblp computer science bibliography, https://dblp.org}
  }
  """
  
  _DL_URLS = {
      # pylint: disable=line-too-long
      'tokenized':
          'https://drive.google.com/uc?export=download&id=1BvdIllGBo9d2-bzXQRzWuJXB04XPVmfF',
      'untokenized':
          'https://drive.google.com/uc?export=download&id=1tFpt32USOO2i1FWhtFTsyYyFzuRm2k36',
      # pylint: enable=line-too-long
  }
  
  _RECAP = 'recap'
  _TRANSCRIPT = 'transcript'
  _RECAP_SOURCE_FULL_NAMES = {
      'fd': 'ForeverDreaming',
      'tms': 'TVMegaSite',
  }
  _SPLITS = ['train', 'dev', 'test']
  
  
  def _load_file(path):
    with tf.io.gfile.GFile(path, 'r') as f:
      return f.read()
  
  
  def _load_json(path):
    return json.loads(_load_file(path))
  
  
  def _load_jsonl(path):
    return [json.loads(line) for line in _load_file(path).strip().splitlines()]
  
  
  def _get_filenames_dict(tokenized_path, recap_source: str):
    """Get dictionary of filenames for each split."""
    filenames_dict = {}
    for split in _SPLITS:
      tokenized_data = _load_jsonl(
          os.path.join(tokenized_path, 'SummScreen',
                       _RECAP_SOURCE_FULL_NAMES[recap_source],
                       f'{recap_source}_{split}.json'))
      filenames_dict[split] = [row['filename'] for row in tokenized_data]
    return filenames_dict
  
  
  def _get_paths_dict(untokenized_path, recap_source, filenames_dict):
    """Get dictionary of example paths for each split."""
    paths_dict = {}
    for split, filenames in filenames_dict.items():
      paths_dict[split] = [
          os.path.join(untokenized_path, 'SummScreen_raw', recap_source, filename)
          for filename in filenames
      ]
    return paths_dict
  
  
  class SummscreenConfig(tfds.core.BuilderConfig):
    """BuilderConfig for Summscreen."""
  
    def __init__(self, *, recap_source=None, **kwargs):
      """BuilderConfig for Summscreen.
  
      Args:
        recap_source: str. The directory for the source of recaps to read.
        **kwargs: keyword arguments forwarded to super.
      """
      super(SummscreenConfig, self).__init__(**kwargs)
      self.recap_source = recap_source
  
  
  class Summscreen(tfds.core.GeneratorBasedBuilder):
    """DatasetBuilder for non-tokenized, non-anonymized SummScreen dataset."""
  
    VERSION = tfds.core.Version('1.0.0')
    RELEASE_NOTES = {
        '1.0.0': 'Initial release.',
    }
  
    BUILDER_CONFIGS = [
        SummscreenConfig(
            name='fd',
            description='ForeverDreaming',
            recap_source='fd',
        ),
        SummscreenConfig(
            name='tms',
            description='TVMegaSite',
            recap_source='tms',
        ),
    ]
  
    def _info(self):
      # Should return a tfds.core.DatasetInfo object
      if self._builder_config.recap_source == 'fd':
        features = tfds.features.FeaturesDict({
            _TRANSCRIPT: tfds.features.Text(),
            _RECAP: tfds.features.Text(),
            'episode_number': tfds.features.Text(),
            'episode_title': tfds.features.Text(),
            'show_title': tfds.features.Text(),
            'transcript_author': tfds.features.Text(),
        })
      elif self._builder_config.recap_source == 'tms':
        features = tfds.features.FeaturesDict({
            _TRANSCRIPT:
                tfds.features.Text(),
            _RECAP:
                tfds.features.Text(),
            'episode_summary':
                tfds.features.Text(),
            'show_title':
                tfds.features.Text(),
            'transcript_author':
                tfds.features.Tensor(shape=(None,), dtype=tf.string),
            'recap_author':
                tfds.features.Text(),
        })
      else:
        raise KeyError(
            f'Unknown recap_source {self._builder_config.recap_source}')
  
      return tfds.core.DatasetInfo(
          builder=self,
          description=_DESCRIPTION,
          features=features,
          supervised_keys=(_TRANSCRIPT, _RECAP),
          homepage='https://github.com/mingdachen/SummScreen',
          citation=_CITATION,
      )
  
    def _split_generators(self, dl_manager):
      dl_paths = dl_manager.download_and_extract(_DL_URLS)
      filenames_dict = _get_filenames_dict(
          tokenized_path=dl_paths['tokenized'],
          recap_source=self._builder_config.recap_source,
      )
      paths_dict = _get_paths_dict(
          untokenized_path=dl_paths['untokenized'],
          recap_source=self._builder_config.recap_source,
          filenames_dict=filenames_dict,
      )
      return {
          'train': self._generate_examples(paths=paths_dict['train']),
          'validation': self._generate_examples(paths=paths_dict['dev']),
          'test': self._generate_examples(paths=paths_dict['test']),
      }
  
    def _generate_examples(self, paths):
      for path in paths:
        example = _load_json(path)
        fname = os.path.basename(path)
        if self._builder_config.recap_source == 'fd':
          yield fname, {
              _TRANSCRIPT: '\n'.join(example['Transcript']),
              _RECAP: '\n'.join(example['Recap']),
              'episode_number': example['Episode Number'],
              'episode_title': example['Episode Title'],
              'show_title': example['Show Title'],
              'transcript_author': example['Transcript Author'],
          }
        elif self._builder_config.recap_source == 'tms':
          yield fname, {
              _TRANSCRIPT: '\n'.join(example['Transcript']),
              _RECAP: '\n'.join(example['Recap']),
              'episode_summary': '\n'.join(example['Episode Summary']),
              'show_title': example['Show Title'],
              'transcript_author': example['Transcript Author'],
              'recap_author': example['Recap Author'],
          }
        else:
          raise KeyError(
              f'Unknown recap_source {self._builder_config.recap_source}')
   # coding=utf-8
  # Copyright 2022 The TensorFlow Datasets Authors.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  """FUSS dataset."""
  
  import os
  from absl import logging
  import tensorflow as tf
  import tensorflow_datasets.public_api as tfds
  
  _CITATION = r"""\
  @inproceedings{wisdom2020fuss,
    title = {What's All the {FUSS} About Free Universal Sound Separation Data?},
    author = {Scott Wisdom and Hakan Erdogan and Daniel P. W. Ellis and Romain Serizel and Nicolas Turpault and Eduardo Fonseca and Justin Salamon and Prem Seetharaman and John R. Hershey},
    year = {2020},
    url = {https://arxiv.org/abs/2011.00803},
  }
  
  @inproceedings{fonseca2020fsd50k,
    author = {Eduardo Fonseca and Xavier Favory and Jordi Pons and Frederic Font Corbera and Xavier Serra},
    title = {{FSD}50k: an open dataset of human-labeled sound events},
    year = {2020},
    url = {https://arxiv.org/abs/2010.00475},
  }
  """
  
  _DESCRIPTION = """\
  The Free Universal Sound Separation (FUSS) Dataset is a database of arbitrary
  sound mixtures and source-level references, for use in experiments on arbitrary
  sound separation.
  
  This is the official sound separation data for the DCASE2020 Challenge Task 4:
  Sound Event Detection and Separation in Domestic Environments.
  
  Overview: FUSS audio data is sourced from a pre-release of Freesound dataset
  known as (FSD50k), a sound event dataset composed of Freesound content annotated
  with labels from the AudioSet Ontology. Using the FSD50K labels, these source
  files have been screened such that they likely only contain a single type of
  sound. Labels are not provided for these source files, and are not considered
  part of the challenge. For the purpose of the DCASE Task4 Sound Separation and
  Event Detection challenge,  systems should not use FSD50K labels, even though
  they may become available upon FSD50K release.
  
  To create mixtures, 10 second clips of sources are convolved with simulated room
  impulse responses and added together. Each 10 second mixture contains between
  1 and 4 sources. Source files longer than 10 seconds are considered "background"
  sources. Every mixture contains one background source, which is active for the
  entire duration. We provide: a software recipe to create the dataset, the room
  impulse responses, and the original source audio.
  """
  
  _URL = "https://github.com/google-research/sound-separation/blob/master/datasets/fuss/FUSS_license_doc/README.md"
  _DL_METADATA = {
      "reverberant":
          ("https://zenodo.org/record/3743844/files/FUSS_ssdata_reverb.tar.gz",
           "ssdata_reverb"),
      "unprocessed":
          ("https://zenodo.org/record/3743844/files/FUSS_ssdata.tar.gz", "ssdata"
          ),
  }
  
  
  class Fuss(tfds.core.GeneratorBasedBuilder):
    """FUSS: Free Universal Sound Separation dataset."""
  
    BUILDER_CONFIGS = [
        tfds.core.BuilderConfig(
            name="reverberant",
            description="Default reverberated audio.",
            version=tfds.core.Version("1.2.0")),
        tfds.core.BuilderConfig(
            name="unprocessed",
            description="Unprocessed audio without additional reverberation.",
            version=tfds.core.Version("1.2.0")),
    ]
  
    def _info(self):
      source_labels = ["background0", "foreground0", "foreground1", "foreground2"]
      return tfds.core.DatasetInfo(
          builder=self,
          description=_DESCRIPTION,
          features=tfds.features.FeaturesDict({
              "mixture_audio":
                  tfds.features.Audio(
                      file_format="wav",
                      shape=(160000,),
                      sample_rate=16000,
                      dtype=tf.int16),
              "sources":
                  tfds.features.Sequence({
                      "audio":
                          tfds.features.Audio(
                              file_format="wav",
                              shape=(160000,),
                              sample_rate=16000,
                              dtype=tf.int16),
                      "label":
                          tfds.features.ClassLabel(names=source_labels),
                  }),
              "segments":
                  tfds.features.Sequence({
                      "start_time_seconds": tf.float32,
                      "end_time_seconds": tf.float32,
                      "label": tf.string
                  }),
              "jams":
                  tf.string,
              "id":
                  tf.string,
          }),
          supervised_keys=("mixture_audio", "sources"),
          homepage=_URL,
          citation=_CITATION,
      )
  
    def _split_generators(self, dl_manager):
      url, extracted_dirname = _DL_METADATA[self.builder_config.name]
      base_dir = dl_manager.download_and_extract(url)
      splits = []
      for split_name, split_dir in [(tfds.Split.TRAIN, "train"),
                                    (tfds.Split.VALIDATION, "validation"),
                                    (tfds.Split.TEST, "eval")]:
        splits.append(
            tfds.core.SplitGenerator(
                name=split_name,
                gen_kwargs={
                    "base_dir": os.path.join(base_dir, extracted_dirname),
                    "split": split_dir,
                }))
      return splits
  
    def _parse_segments(self, path):
      segments = []
      if not tf.io.gfile.exists(path):
        # Some segments files are missing in the "unprocessed" set.
        logging.info("Missing segments file: %s", path)
        return segments
      with tf.io.gfile.GFile(path) as f:
        for l in f:
          try:
            start, end, label = l.split()
          except ValueError:
            continue
          segments.append({
              "start_time_seconds": float(start),
              "end_time_seconds": float(end),
              "label": label
          })
      return segments
  
    def _generate_examples(self, base_dir, split):
      """Generates examples for the given split."""
      path = os.path.join(base_dir, "%s_example_list.txt" % split)
      split_dir = os.path.join(base_dir, split)
      with tf.io.gfile.GFile(path) as example_list:
        for line in example_list:
          paths = line.split()
          key = _basename_without_ext(paths[0])
          sources = []
          for p in paths[1:]:
            sources.append({
                "audio": os.path.join(base_dir, p),
                "label": _basename_without_ext(p).split("_")[0],
            })
          segments = self._parse_segments(os.path.join(split_dir, "%s.txt" % key))
          jams = tf.io.gfile.GFile(os.path.join(split_dir,
                                                "%s.jams" % key)).read()
          example = {
              "mixture_audio": os.path.join(base_dir, paths[0]),
              "sources": sources,
              "segments": segments,
              "jams": jams,
              "id": key,
          }
          yield key, example
  
  
  def _basename_without_ext(p):
    basename, _ = os.path.splitext(os.path.basename(p))
    return basename
   # coding=utf-8
  # Copyright 2022 The TensorFlow Datasets Authors.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  """Kitti dataset."""
  
  import collections
  import csv
  import os
  import numpy as np
  import tensorflow as tf
  import tensorflow_datasets.public_api as tfds
  
  _CITATION = """\
  @inproceedings{Geiger2012CVPR,
    author = {Andreas Geiger and Philip Lenz and Raquel Urtasun},
    title = {Are we ready for Autonomous Driving? The KITTI Vision Benchmark Suite},
    booktitle = {Conference on Computer Vision and Pattern Recognition (CVPR)},
    year = {2012}
  }
  """
  _DESCRIPTION = """\
  Kitti contains a suite of vision tasks built using an autonomous driving
  platform. The full benchmark contains many tasks such as stereo, optical flow,
  visual odometry, etc. This dataset contains the object detection dataset,
  including the monocular images and bounding boxes. The dataset contains 7481
  training images annotated with 3D bounding boxes. A full description of the
  annotations can be found in the readme of the object development kit readme on
  the Kitti homepage.
  """
  _HOMEPAGE_URL = "http://www.cvlibs.net/datasets/kitti/"
  _DATA_URL = "https://s3.eu-central-1.amazonaws.com/avg-kitti"
  _IMAGES_FNAME = "data_object_image_2.zip"
  _LABELS_FNAME = "data_object_label_2.zip"
  _DEVKIT_FNAME = "devkit_object.zip"
  _OBJECT_LABELS = [
      "Car",
      "Van",
      "Truck",
      "Pedestrian",
      "Person_sitting",
      "Cyclist",
      "Tram",
      "Misc",
  ]
  # The percentage of trainset videos to put into validation and test sets.
  # The released test images do not have labels.
  _VALIDATION_SPLIT_PERCENT_VIDEOS = 10
  _TEST_SPLIT_PERCENT_VIDEOS = 10
  
  # Raw Kitti representation of a bounding box. Coordinates are in pixels,
  # measured from the top-left hand corner.
  RawBoundingBox = collections.namedtuple("RawBoundingBox",
                                          ["top", "bottom", "left", "right"])
  
  
  class Kitti(tfds.core.GeneratorBasedBuilder):
    """Kitti dataset."""
  
    VERSION = tfds.core.Version("3.2.0")
    SUPPORTED_VERSIONS = [
        tfds.core.Version("3.1.0"),
    ]
    RELEASE_NOTES = {"3.2.0": "Devkit updated."}
  
    def _info(self):
      # Annotation descriptions are in the object development kit.
      annotations = {
          "type": tfds.features.ClassLabel(names=_OBJECT_LABELS),
          "truncated": tfds.features.Tensor(shape=(), dtype=tf.float32),
          "occluded": tfds.features.ClassLabel(num_classes=4),
          "alpha": tfds.features.Tensor(shape=(), dtype=tf.float32),
          "bbox": tfds.features.BBoxFeature(),
          "dimensions": tfds.features.Tensor(shape=(3,), dtype=tf.float32),
          "location": tfds.features.Tensor(shape=(3,), dtype=tf.float32),
          "rotation_y": tfds.features.Tensor(shape=(), dtype=tf.float32),
      }
      return tfds.core.DatasetInfo(
          builder=self,
          description=_DESCRIPTION,
          features=tfds.features.FeaturesDict({
              "image": tfds.features.Image(),
              "image/file_name": tfds.features.Text(),  # E.g. "000001.png".
              "objects": tfds.features.Sequence(annotations),
          }),
          homepage=_HOMEPAGE_URL,
          citation=_CITATION,
      )
  
    def _split_generators(self, dl_manager):
      filenames = {
          "images": _DATA_URL + "/" + _IMAGES_FNAME,
          "annotations": _DATA_URL + "/" + _LABELS_FNAME,
          "devkit": _DATA_URL + "/" + _DEVKIT_FNAME,
      }
      files = dl_manager.download(filenames)
      train_images, validation_images, test_images = _build_splits(
          dl_manager.iter_archive(files["devkit"]))
  
      return [
          tfds.core.SplitGenerator(
              name=tfds.Split.TRAIN,
              gen_kwargs={
                  "images": dl_manager.iter_archive(files["images"]),
                  "annotations": dl_manager.iter_archive(files["annotations"]),
                  "subdir": "training",
                  "image_ids": train_images,
              }),
          tfds.core.SplitGenerator(
              name=tfds.Split.VALIDATION,
              gen_kwargs={
                  "images": dl_manager.iter_archive(files["images"]),
                  "annotations": dl_manager.iter_archive(files["annotations"]),
                  "subdir": "training",
                  "image_ids": validation_images,
              }),
          tfds.core.SplitGenerator(
              name=tfds.Split.TEST,
              gen_kwargs={
                  "images": dl_manager.iter_archive(files["images"]),
                  "annotations": dl_manager.iter_archive(files["annotations"]),
                  "subdir": "training",
                  "image_ids": test_images,
              }),
      ]
  
    def _generate_examples(self, images, annotations, subdir, image_ids):
      """Yields images and annotations.
  
      Args:
        images: object that iterates over the archive of images.
        annotations: object that iterates over the archive of annotations.
        subdir: subdirectory from which to extract images and annotations, e.g.
          training or testing.
        image_ids: file ids for images in this split.
  
      Yields:
        A tuple containing the example's key, and the example.
      """
      cv2 = tfds.core.lazy_imports.cv2
  
      all_annotations = dict()
      for fpath, fobj in annotations:
        prefix, ext = os.path.splitext(fpath)
        if ext != ".txt":
          continue
        if prefix.split(os.path.sep)[0] != subdir:
          continue
  
        # Key is the datapoint id. E.g. training/label_2/label_000016 -> 16.
        all_annotations[int(prefix[-6:])] = _parse_kitti_annotations(fobj)
  
      for fpath, fobj in images:
        prefix, ext = os.path.splitext(fpath)
        if ext != ".png":
          continue
        if prefix.split(os.path.sep)[0] != subdir:
          continue
        image_id = int(prefix[-6:])
        if image_id not in image_ids:
          continue
        annotations = all_annotations[image_id]
        img = cv2.imdecode(
            np.frombuffer(fobj.read(), dtype=np.uint8), cv2.IMREAD_COLOR)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        height, width, _ = img.shape
        for obj in annotations:
          obj["bbox"] = _build_bounding_box(obj["bbox_raw"], height, width)
          del obj["bbox_raw"]
        _, fname = os.path.split(fpath)
        record = {"image": img, "image/file_name": fname, "objects": annotations}
        yield fname, record
  
  
  def _build_bounding_box(bbox, height, width):
    """Builds and returns TFDS bounding box.
  
    Args:
      bbox: RawBoundingBox, bounding box in Kitti coordinates (origin top left).
      height: Image height in pixels.
      width: Image width in pixels.
  
    Returns:
      A TFDS BBox (origin bottom left).
    """
    return tfds.features.BBox(
        ymin=(height - bbox.bottom) / height,
        ymax=(height - bbox.top) / height,
        xmin=bbox.left / width,
        xmax=bbox.right / width,
    )
  
  
  def _parse_kitti_annotations(annotations_csv):
    """Loads and parses the Kitti object annotations.
  
    Args:
      annotations_csv: csv file containing annotations for a single image.
  
    Returns:
      A list of labelled bounding boxes. Each bounding box is stored as a
      dictionary of features.
    """
    annotations = []
    for line in annotations_csv:
      (obj_type, truncated, occluded, alpha, left, top, right, bottom, height,
       width, length, x, y, z,
       rotation_y) = list(csv.reader([line.decode()], delimiter=" "))[0]
      # DontCare objects lack annotations, so skip them.
      if obj_type == "DontCare":
        continue
      annotations.append({
          "type":
              obj_type,
          "truncated":
              float(truncated),
          "occluded":
              int(occluded),
          "alpha":
              float(alpha),
          "bbox_raw":
              RawBoundingBox(
                  top=float(top),
                  bottom=float(bottom),
                  left=float(left),
                  right=float(right)),
          "dimensions": [float(v) for v in [height, width, length]],
          "location": [float(v) for v in [x, y, z]],
          "rotation_y":
              float(rotation_y),
      })
    return annotations
  
  
  def _build_splits(devkit):
    """Splits the train data into train/val/test by video.
  
    Ensures that images from the same video do not traverse the splits.
  
    Args:
      devkit: object that iterates over the devkit archive.
  
    Returns:
      train_images: File ids for the training set images.
      validation_images: File ids for the validation set images.
      test_images: File ids for the test set images.
    """
    mapping_line_ids = None
    mapping_lines = None
    for fpath, fobj in devkit:
      if fpath == os.path.join("mapping", "train_rand.txt"):
        # Converts 1-based line index to 0-based line index.
        mapping_line_ids = [
            int(x.strip()) - 1 for x in fobj.read().decode("utf-8").split(",")
        ]
      elif fpath == os.path.join("mapping", "train_mapping.txt"):
        mapping_lines = fobj.read().splitlines()
        mapping_lines = [x.decode("utf-8") for x in mapping_lines]
  
    assert mapping_line_ids
    assert mapping_lines
  
    video_to_image = collections.defaultdict(list)
    for image_id, mapping_lineid in enumerate(mapping_line_ids):
      line = mapping_lines[mapping_lineid]
      video_id = line.split(" ")[1]
      video_to_image[video_id].append(image_id)
  
    # Sets numpy random state.
    numpy_original_state = np.random.get_state()
    np.random.seed(seed=123)
  
    # Max 1 for testing.
    num_test_videos = max(1,
                          _TEST_SPLIT_PERCENT_VIDEOS * len(video_to_image) // 100)
    num_validation_videos = max(
        1,
        _VALIDATION_SPLIT_PERCENT_VIDEOS * len(video_to_image) // 100)
    test_videos = set(
        np.random.choice(
            sorted(list(video_to_image.keys())), num_test_videos, replace=False))
    validation_videos = set(
        np.random.choice(
            sorted(list(set(video_to_image.keys()) - set(test_videos))),
            num_validation_videos,
            replace=False))
    test_images = []
    validation_images = []
    train_images = []
    for k, v in video_to_image.items():
      if k in test_videos:
        test_images.extend(v)
      elif k in validation_videos:
        validation_images.extend(v)
      else:
        train_images.extend(v)
  
    # Resets numpy random state.
    np.random.set_state(numpy_original_state)
    return train_images, validation_images, test_images
  
apis: tensorflow.io.gfile.GFile
apis: tensorflow.io.gfile.exists tensorflow.io.gfile.GFile
apis: numpy.random.set_state numpy.random.seed numpy.random.get_state