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ZddlZddl	m
  mZ ddlmZ dZeedfZdZG d	d
 d
ejjZG dd deZG dd dedg dZdddZdS )zCIFAR datasets.    )absolute_import)division)print_functionN       z@TECHREPORT{Krizhevsky09learningmultiple,
    author = {Alex Krizhevsky},
    title = {Learning multiple layers of features from tiny images},
    institution = {},
    year = {2009}
}
c                   @   s@   e Zd ZdZejdZdd Ze	dd Z
dd Zd	d
 ZdS )Cifar10z	CIFAR-10.3.0.2c              
   C   sB   t jj| dt jt j t jjtdt jjdddddt	dS )	NzThe CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images.shape
   num_classes)idimagelabelr   r   +https://www.cs.toronto.edu/~kriz/cifar.htmlbuilderdescriptionfeaturessupervised_keyshomepagecitation
tfdscoreDatasetInfor   FeaturesDictTextImage_CIFAR_IMAGE_SHAPE
ClassLabel	_CITATIONself r&   b/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image_classification/cifar.py_info2   s   zCifar10._infoc              	   C   s"   t | jdg ddgddgdgdS )Nz7https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz)zdata_batch_1.binzdata_batch_2.binzdata_batch_3.binzdata_batch_4.binzdata_batch_5.binztest_batch.binzcifar-10-batches-bin/zbatches.meta.txtr   nameurltrain_files
test_filesprefixlabel_files
label_keys	CifarInfor*   r$   r&   r&   r'   _cifar_infoB   s   zCifar10._cifar_infoc           	   	      s   | | jj | j}tj |j t|j|j	D ]5\}}tj |}t
jj|}dd | dD }W d   n1 sBw   Y  || jj| _q fdd}tjjtjjd||jdd	tjjtjjd
||jdd	gS )zReturns SplitGenerators.c                 S   s   g | ]}|r|qS r&   r&   ).0r*   r&   r&   r'   
<listcomp>]   s    z-Cifar10._split_generators.<locals>.<listcomp>
Nc                 3   s     | D ]
}t j |V  qd S )N)ospathjoin)	filenamesf
cifar_pathr&   r'   gen_filenamesa   s   z0Cifar10._split_generators.<locals>.gen_filenamestrain_)split_prefix	filepaths)r*   
gen_kwargstest_)download_and_extractr3   r+   r7   r8   r9   r.   zipr0   r/   tfiogfileGFilereadsplitinfor   namesr   r   SplitGeneratorSplitTRAINr,   TESTr-   )	r%   
dl_manager
cifar_info	label_key
label_filelabels_pathlabel_flabel_namesr>   r&   r<   r'   _split_generatorsQ   s2   zCifar10._split_generatorsc           	      c   sl    | j j}d}|D ]*}t|t|D ] \}}tt||}d|||d< ||d< ||fV  |d7 }qq	dS )ae  Generate CIFAR examples as dicts.

    Shared across CIFAR-{10, 100}. Uses self._cifar_info as
    configuration.

    Args:
      split_prefix (str): Prefix that identifies the split (e.g. "tr" or "te").
      filepaths (list[str]): The files to use to generate the data.

    Yields:
      The cifar examples, as defined in the dataset info features.
    r   z{}{:05d}r   r      N)r3   r0   
_load_datalendictrE   format)	r%   r@   rA   r0   indexr8   labelsnp_imagerecordr&   r&   r'   _generate_examplest   s   

zCifar10._generate_examplesN)__name__
__module____qualname____doc__r   r   VersionVERSIONr(   propertyr3   rY   rc   r&   r&   r&   r'   r   -   s    
#r   c                   @   s0   e Zd ZdZejdZedd Z	dd Z
dS )Cifar100zCIFAR-100 dataset.r   c              	   C   s$   t | jddgdgdddgddgd	S )
Nz8https://www.cs.toronto.edu/~kriz/cifar-100-binary.tar.gzz	train.binztest.binzcifar-100-binary/zcoarse_label_names.txtzfine_label_names.txtcoarse_labelr   r)   r1   r$   r&   r&   r'   r3      s   zCifar100._cifar_infoc                 C   sN   t jj| dt jt j t jjtdt jjddt jjdddddt	d	S )
Na`  This dataset is just like the CIFAR-10, except it has 100 classes containing 600 images each. There are 500 training images and 100 testing images per class. The 100 classes in the CIFAR-100 are grouped into 20 superclasses. Each image comes with a "fine" label (the class to which it belongs) and a "coarse" label (the superclass to which it belongs).r	   d   r      )r   r   r   rl   r   r   r   r   r$   r&   r&   r'   r(      s   zCifar100._infoN)rd   re   rf   rg   r   r   rh   ri   rj   r3   r(   r&   r&   r&   r'   rk      s    
rk   c                   @   s   e Zd ZdZdS )r2   a  Contains the information necessary to generate a CIFAR dataset.

  Attributes:
    name (str): name of dataset.
    url (str): data URL.
    prefix (str): path prefix within the downloaded and extracted file to look
      for `train_files` and `test_files`.
    train_files (list<str>): name of training files within `prefix`.
    test_files (list<str>): name of test files within `prefix`.
    label_files (list<str>): names of the label files in the data.
    label_keys (list<str>): names of the label keys in the data.
  N)rd   re   rf   rg   r&   r&   r&   r'   r2      s    	r2   
_CifarInfo)r*   r+   r.   r,   r-   r/   r0   rZ   c                 c   s    t jj| d}| }W d   n1 sw   Y  d}t|d }||k r]tj|tj||d	|f}||7 }tj|tjd|d	dt
t
fd}|d7 }||fV  ||k s)dS dS )	z!Yields (labels, np_image) tuples.rbNr   rZ   )dtypecountoffseti   r   )rZ      r   )rF   rG   rH   rI   rJ   r\   np
frombufferuint8reshape_CIFAR_IMAGE_SIZE	transpose)r8   labels_numberr;   datars   
max_offsetr`   imgr&   r&   r'   r[      s(   

r[   )rZ   )rg   
__future__r   r   r   collectionsr7   numpyru   tensorflow.compat.v2compatv2rF   tensorflow_datasets.public_api
public_apir   ry   r!   r#   r   GeneratorBasedBuilderr   rk   
namedtupler2   r[   r&   r&   r&   r'   <module>   s    

b'