class_docstr: DatasetBuilder for controlled_noisy_web_labels dataset.
class_docstr_tok: ["DatasetBuilder","for","controlled_noisy_web_labels","dataset","."]
code: def _get_clean_images(self, fnames, archive):
      clean_image_dict = {}
  
      fnames = set(fnames)
  
      # The files names in mini-ImageNet contain the absolute index wrt to the
      # synset. For example, {synset_name}_{idx}.jpg, where synset_name is
      # something like n01440764, and idx is between 1 and 1000. We want to create
      # a mapping between synset and the indices of the images that we need for
      # the corresponding synset
      synset_to_index = {}
      for name in fnames:
          # Remove .jpg
          name = name[:-4]
          # Synset name is the first 8 characters
          synset_name = name[:9]
          # Synset index is the last 8 characters
          synset_index = int(name[-4:])
  
          if synset_name in synset_to_index:
              synset_to_index[synset_name].append(synset_index)
          else:
              synset_to_index[synset_name] = [synset_index]
  
      for fname, fobj in archive:
          label = fname[:-4]  # fname is something like 'n01632458.tar'
          # TODO(b/117643231): in py3, the following lines trigger tarfile module
          # to call `fobj.seekable()`, which Gfile doesn't have. We should find an
          # alternative, as this loads ~150MB in RAM.
  
          if (
              label not in _IMNET_MINI_SYNSETS
              or label not in synset_to_index
          ):
              continue
          fobj_mem = io.BytesIO(fobj.read())
  
          # We obtain all filenames for a given subset (i.e. n01440764_10,
          # n0144076_2020) and sort them, so that we can find the indices of images
          # that we need to keep
          all_image_names = [
              image_fname
              for image_fname, _ in tfds.download.iter_archive(
                  fobj_mem, tfds.download.ExtractMethod.TAR_STREAM
              )
          ]
  
          # Remove .JPEG, and remove the part after _, i.e. want to retain
          # 1 from n01440764_1
          image_indices = [
              int(name[:-5].split("_")[-1]) for name in all_image_names
          ]
  
          # Sort filenames
          sorted_indices = np.argsort(image_indices)
  
          # Get the indices of images to select
          images_to_select = np.asarray(synset_to_index[label]) - 1
  
          # Get the filenames of images to select
          selected_indices = sorted_indices[images_to_select]
          selected_fnames = [
              all_image_names[idx] for idx in selected_indices
          ]
          fname_to_idx = dict(
              zip(selected_fnames, synset_to_index[label])
          )
  
          # We use set here for faster lookup in the loop
          selected_fnames = set(selected_fnames)
  
          # integer label corresponding to the synset
          synset_int_label = _IMNET_MINI_SYNSETS.index(label)
  
          # Go to the beginning of the file
          fobj.seek(0)
          fobj_mem = io.BytesIO(fobj.read())
  
          for image_fname, image in tfds.download.iter_archive(
              fobj_mem, tfds.download.ExtractMethod.TAR_STREAM
          ):  # pytype: disable=wrong-arg-types  # gen-stub-imports
              if image_fname in selected_fnames:
                  # Convert the image ID back to mini-Imagenet style, e.g.
                  # n0144076400000009.jpg, for a quick access later
                  image_index = str(fname_to_idx[image_fname]).zfill(8)
                  new_image_id = label + image_index + ".jpg"
                  buf = image.read()
                  clean_image_dict[new_image_id] = (
                      self._resize_image(buf),
                      synset_int_label,
                  )
  
      return clean_image_dict
  
file_path: datasets/tensorflow_datasets/image_classification/controlled_noisy_web_labels/controlled_noisy_web_labels.py
imports: import csv
  import dataclasses
  import io
  import json
  import os
  from etils import epath
  import numpy
  from tensorflow_datasets.core.utils.lazy_imports_utils import tensorflow
  import tensorflow_datasets.public_api
label_desc: Use image node_id 3 for calling the ControlledNoisyWebLabels obj's underlying member method code with example usage: obj._get_clean_images(fnames, archive) and returns: clean_image_dict