o
    NiG                     @   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d ZdS )zShapes3D dataset.    )absolute_import)division)print_functionN)movesz@misc{3dshapes18,
  title={3D Shapes Dataset},
  author={Burgess, Chris and Kim, Hyunjik},
  howpublished={https://github.com/deepmind/3dshapes-dataset/},
  year={2018}
}
z4https://storage.googleapis.com/3d-shapes/3dshapes.h5ak  3dshapes is a dataset of 3D shapes procedurally generated from 6 ground truth
independent latent factors. These factors are *floor colour*, *wall colour*, *object colour*,
*scale*, *shape* and *orientation*.

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

### Latent factor values

*   floor hue: 10 values linearly spaced in [0, 1]
*   wall hue: 10 values linearly spaced in [0, 1]
*   object hue: 10 values linearly spaced in [0, 1]
*   scale: 8 values linearly spaced in [0, 1]
*   shape: 4 values in [0, 1, 2, 3]
*   orientation: 15 values linearly spaced in [-30, 30]

We varied one latent at a time (starting from orientation, then shape, etc), and sequentially stored the images in fixed order in the `images` array. The corresponding values of the factors are stored in the same order in the `labels` array.
c                   @   s6   e Zd ZdZejddZdd Zdd Z	dd	 Z
d
S )Shapes3dzShapes3d data set.z2.0.0z6New split API (https://tensorflow.org/datasets/splits)c                 C   s   t jj| tt j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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t jjg t	j
dd	d
tdS )N)@   r      )shape
   )num_classes         )r	   dtypeimagelabel_floor_huelabel_wall_huelabel_object_huelabel_scalelabel_shapelabel_orientationvalue_floor_huevalue_wall_huevalue_object_huevalue_scalevalue_shapevalue_orientationz%https://github.com/deepmind/3d-shapes)builderdescriptionfeatureshomepagecitation)tfdscoreDatasetInfo_DESCRIPTIONr    FeaturesDictImage
ClassLabelTensortffloat32	_CITATION)self r/   V/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image/shapes3d.py_infoB   s*   zShapes3d._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)r.   
dl_managerr2   r/   r/   r0   _split_generatorsf   s   
zShapes3d._split_generatorsc                 c   s   t jjj|d}t|d }t|d }W d   n1 s#w   Y  tj|tjd}t	|j
d D ]}t|dd|f |dd|f< q7tt|||D ]4\}\}}}	||d |d |d |d	 |d
 |d |	d |	d |	d |	d	 |	d
 |	d d}
||
fV  qSdS )zGenerate examples for the Shapes3d dataset.

    Args:
      filepath: path to the Shapes3d hdf5 file.

    Yields:
      Dictionaries with images and the different labels.
    rimageslabelsNr      r      r   r      r   )r#   r$   lazy_importsh5pyFilenparray
zeros_likeint64ranger	   _discretize	enumerater   zip)r.   r2   	h5datasetimage_arrayvalues_arraylabels_arrayir   r?   valuesrecordr/   r/   r0   _generate_examplesp   s6   "zShapes3d._generate_examplesN)__name__
__module____qualname____doc__r#   r$   VersionVERSIONr1   r<   rV   r/   r/   r/   r0   r   <   s    $
r   c                 C   sv   t | }t |}t j|jt jd}t j|jt jd||< || }t jd|dd |dd kf }| | d S )z)Discretizes array values to class labels.r@   TrA   N)	rG   asarrayargsortzerossizeintparanger_cumsum)aarrindexinverse_indexobsr/   r/   r0   rL      s   

"rL   )rZ   
__future__r   r   r   numpyrG   sixr   tensorflow.compat.v2compatv2r+   tensorflow_datasets.public_api
public_apir#   r-   r6   r&   r$   GeneratorBasedBuilderr   rL   r/   r/   r/   r0   <module>   s   	_