o
    Ni                     @   s~   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddl	m
  mZ ddlmZ dZdZd	ZG d
d dejjZdS )zdSprites dataset.    )absolute_import)division)print_functionN)movesz@misc{dsprites17,
author = {Loic Matthey and Irina Higgins and Demis Hassabis and Alexander Lerchner},
title = {dSprites: Disentanglement testing Sprites dataset},
howpublished= {https://github.com/deepmind/dsprites-dataset/},
year = "2017",
}
zqhttps://github.com/deepmind/dsprites-dataset/blob/master/dsprites_ndarray_co1sh3sc6or40x32y32_64x64.hdf5?raw=truea  dSprites is a dataset of 2D shapes procedurally generated from 6 ground truth
independent latent factors. These factors are *color*, *shape*, *scale*,
*rotation*, *x* and *y* positions of a sprite.

All possible combinations of these latents are present exactly once,
generating N = 737280 total images.

### Latent factor values

*   Color: white
*   Shape: square, ellipse, heart
*   Scale: 6 values linearly spaced in [0.5, 1]
*   Orientation: 40 values in [0, 2 pi]
*   Position X: 32 values in [0, 1]
*   Position Y: 32 values in [0, 1]

We varied one latent at a time (starting from Position Y, then Position X, etc),
and sequentially stored the images in fixed order.
Hence the order along the first dimension is fixed and allows you to map back to
the value of the latents corresponding to that image.

We chose the latents values deliberately to have the smallest step changes
while ensuring that all pixel outputs were different. No noise was added.
c                   @   sD   e Zd ZdZejddZejdgZdd Z	dd Z
d	d
 ZdS )DspriteszdSprites data set.2.0.0z6New split API (https://tensorflow.org/datasets/splits)z2.1.0c                 C   s   t jjddt jjddt jjddt jjddt jjddt jjddt jjg tjdt jjg tjdt jjg tjdt jjg tjdt jjg tjdd	}| jd
kr[t j |d< t j	j
| tt j|dtdS )N)@   r      )shape   )num_classes   (       )r
   dtypeimagelabel_shapelabel_scalelabel_orientationlabel_x_positionlabel_y_positionvalue_shapevalue_scalevalue_orientationvalue_x_positionvalue_y_positionr   idz,https://github.com/deepmind/dsprites-dataset)builderdescriptionfeatureshomepagecitation)tfdsr    Image
ClassLabelTensortffloat32versionTextcoreDatasetInfo_DESCRIPTIONFeaturesDict	_CITATION)selffeatures_dict r2   V/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image/dsprites.py_infoM   s*   

zDsprites._infoc                 C   s&   | t}tjjtjjt|ddgS )N)filepath)name
gen_kwargs)download_URLr#   r+   SplitGeneratorSplitTRAINdict)r0   
dl_managerr5   r2   r2   r3   _split_generatorse   s   
zDsprites._split_generatorsc                 c   s    t jjj|d!}t|d }t|d d }t|d d }W d   n1 s.w   Y  tt	|||D ]?\}\}}}	t
t|d|d |d	 |d
 |d |d |	d |	d	 |	d
 |	d |	d d}
| jdkrvd||
d< ||
fV  q<dS )zGenerates examples for the dSprites data set.

    Args:
      filepath: path to the dSprites hdf5 file.

    Yields:
      Dictionaries with images, latent classes, and latent values.
    rimgslatentsclassesvaluesNr	      r         r   r   z{:06d}r   )r#   r+   lazy_importsh5pyFilenparray	enumerater   zipr=   expand_dimsr)   format)r0   r5   	h5datasetimage_arrayclass_arrayvalues_arrayir   rC   rD   recordr2   r2   r3   _generate_exampleso   s4   

zDsprites._generate_examplesN)__name__
__module____qualname____doc__r#   r+   VersionVERSIONSUPPORTED_VERSIONSr4   r?   rX   r2   r2   r2   r3   r   D   s    

r   )r\   
__future__r   r   r   numpyrL   sixr   tensorflow.compat.v2compatv2r'   tensorflow_datasets.public_api
public_apir#   r/   r9   r-   r+   GeneratorBasedBuilderr   r2   r2   r2   r3   <module>   s   	