(dct)
Hook to `json.loads` that builds a DatasetSpecification from a dict.

  Args:
     dct: A dictionary with string keys, corresponding to a JSON file.

  Returns:
    Depending on the '__class__' key of the dictionary, a DatasetSpecification,
    HierarchicalDatasetSpecification, or BiLevelDatasetSpecification. Defaults
    to returning `dct`.
Hook to `json.loads` that builds a DatasetSpecification from a dict.
Hook to json . loads that builds a DatasetSpecification from a dict .
def as_dataset_spec(dct):
  """Hook to `json.loads` that builds a DatasetSpecification from a dict.

  Args:
     dct: A dictionary with string keys, corresponding to a JSON file.

  Returns:
    Depending on the '__class__' key of the dictionary, a DatasetSpecification,
    HierarchicalDatasetSpecification, or BiLevelDatasetSpecification. Defaults
    to returning `dct`.
  """
  if '__class__' not in dct:
    return dct

  if dct['__class__'] not in ('DatasetSpecification',
                              'HierarchicalDatasetSpecification',
                              'BiLevelDatasetSpecification'):
    return dct

  def _key_to_int(dct):
    """Returns a new dictionary whith keys converted to ints."""
    return {int(key): value for key, value in six.iteritems(dct)}

  def _key_to_split(dct):
    """Returns a new dictionary whith keys converted to Split enums."""
    return {
        learning_spec.Split[key]: value for key, value in six.iteritems(dct)
    }

  if dct['__class__'] == 'DatasetSpecification':
    images_per_class = {}
    for class_id, n_images in six.iteritems(dct['images_per_class']):
      # If n_images is a dict, it maps each class ID to a string->int
      # dictionary containing the size of each pool.
      if isinstance(n_images, dict):
        # Convert the number of classes in each pool to int.
        n_images = {
            pool: int(pool_size) for pool, pool_size in six.iteritems(n_images)
        }
      else:
        n_images = int(n_images)
      images_per_class[int(class_id)] = n_images

    return DatasetSpecification(
        name=dct['name'],
        classes_per_split=_key_to_split(dct['classes_per_split']),
        images_per_class=images_per_class,
        class_names=_key_to_int(dct['class_names']),
        path=dct['path'],
        file_pattern=dct['file_pattern'])

  elif dct['__class__'] == 'BiLevelDatasetSpecification':
    return BiLevelDatasetSpecification(
        name=dct['name'],
        superclasses_per_split=_key_to_split(dct['superclasses_per_split']),
        classes_per_superclass=_key_to_int(dct['classes_per_superclass']),
        images_per_class=_key_to_int(dct['images_per_class']),
        superclass_names=_key_to_int(dct['superclass_names']),
        class_names=_key_to_int(dct['class_names']),
        path=dct['path'],
        file_pattern=dct['file_pattern'])

  elif dct['__class__'] == 'HierarchicalDatasetSpecification':
    # Load subgraphs associated to each split, and build global mapping from
    # WordNet ID to Synset objects.
    split_subgraphs = {}
    wn_id_to_node = {}
    for split in learning_spec.Split:
      split_subgraphs[split] = imagenet_specification.import_graph(
          dct['split_subgraphs'][split.name])
      for synset in split_subgraphs[split]:
        wn_id = synset.wn_id
        if wn_id in wn_id_to_node:
          raise ValueError(
              'Multiple `Synset` objects associated to the same WordNet ID')
        wn_id_to_node[wn_id] = synset

    images_per_class = {}
    for split_name, wn_id_counts in six.iteritems(dct['images_per_class']):
      synset_counts = {
          wn_id_to_node[wn_id]: int(count)
          for wn_id, count in six.iteritems(wn_id_counts)
      }
      images_per_class[learning_spec.Split[split_name]] = synset_counts

    return HierarchicalDatasetSpecification(
        name=dct['name'],
        split_subgraphs=split_subgraphs,
        images_per_class=images_per_class,
        class_names=_key_to_int(dct['class_names']),
        path=dct['path'],
        file_pattern=dct['file_pattern'])

  else:
    return dct
def as_dataset_spec ( dct ) : if '__class__' not in dct : return dct if dct [ '__class__' ] not in ( 'DatasetSpecification' , 'HierarchicalDatasetSpecification' , 'BiLevelDatasetSpecification' ) : return dct def _key_to_int ( dct ) : """Returns a new dictionary whith keys converted to ints.""" return { int ( key ) : value for key , value in six . iteritems ( dct ) } def _key_to_split ( dct ) : """Returns a new dictionary whith keys converted to Split enums.""" return { learning_spec . Split [ key ] : value for key , value in six . iteritems ( dct ) } if dct [ '__class__' ] == 'DatasetSpecification' : images_per_class = { } for class_id , n_images in six . iteritems ( dct [ 'images_per_class' ] ) : # If n_images is a dict, it maps each class ID to a string->int # dictionary containing the size of each pool. if isinstance ( n_images , dict ) : # Convert the number of classes in each pool to int. n_images = { pool : int ( pool_size ) for pool , pool_size in six . iteritems ( n_images ) } else : n_images = int ( n_images ) images_per_class [ int ( class_id ) ] = n_images return DatasetSpecification ( name = dct [ 'name' ] , classes_per_split = _key_to_split ( dct [ 'classes_per_split' ] ) , images_per_class = images_per_class , class_names = _key_to_int ( dct [ 'class_names' ] ) , path = dct [ 'path' ] , file_pattern = dct [ 'file_pattern' ] ) elif dct [ '__class__' ] == 'BiLevelDatasetSpecification' : return BiLevelDatasetSpecification ( name = dct [ 'name' ] , superclasses_per_split = _key_to_split ( dct [ 'superclasses_per_split' ] ) , classes_per_superclass = _key_to_int ( dct [ 'classes_per_superclass' ] ) , images_per_class = _key_to_int ( dct [ 'images_per_class' ] ) , superclass_names = _key_to_int ( dct [ 'superclass_names' ] ) , class_names = _key_to_int ( dct [ 'class_names' ] ) , path = dct [ 'path' ] , file_pattern = dct [ 'file_pattern' ] ) elif dct [ '__class__' ] == 'HierarchicalDatasetSpecification' : # Load subgraphs associated to each split, and build global mapping from # WordNet ID to Synset objects. split_subgraphs = { } wn_id_to_node = { } for split in learning_spec . Split : split_subgraphs [ split ] = imagenet_specification . import_graph ( dct [ 'split_subgraphs' ] [ split . name ] ) for synset in split_subgraphs [ split ] : wn_id = synset . wn_id if wn_id in wn_id_to_node : raise ValueError ( 'Multiple `Synset` objects associated to the same WordNet ID' ) wn_id_to_node [ wn_id ] = synset images_per_class = { } for split_name , wn_id_counts in six . iteritems ( dct [ 'images_per_class' ] ) : synset_counts = { wn_id_to_node [ wn_id ] : int ( count ) for wn_id , count in six . iteritems ( wn_id_counts ) } images_per_class [ learning_spec . Split [ split_name ] ] = synset_counts return HierarchicalDatasetSpecification ( name = dct [ 'name' ] , split_subgraphs = split_subgraphs , images_per_class = images_per_class , class_names = _key_to_int ( dct [ 'class_names' ] ) , path = dct [ 'path' ] , file_pattern = dct [ 'file_pattern' ] ) else : return dct