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d
ZG dd dejjZdd Zdd ZdS )zSmallnorb dataset.    )absolute_import)division)print_functionN)movesac  \
@article{LeCun2004LearningMF,
  title={Learning methods for generic object recognition with invariance to pose and lighting},
  author={Yann LeCun and Fu Jie Huang and L{\'e}on Bottou},
  journal={Proceedings of the 2004 IEEE Computer Society Conference on Computer Vision and Pattern Recognition},
  year={2004},
  volume={2},
  pages={II-104 Vol.2}
}
zghttps://cs.nyu.edu/~ylclab/data/norb-v1.0-small/smallnorb-5x46789x9x18x6x2x96x96-training-{type}.mat.gzzfhttps://cs.nyu.edu/~ylclab/data/norb-v1.0-small/smallnorb-5x01235x9x18x6x2x96x96-testing-{type}.mat.gza  \
This database is intended for experiments in 3D object recognition from shape. It contains images of 50 toys belonging to 5 generic categories: four-legged animals, human figures, airplanes, trucks, and cars. The objects were imaged by two cameras under 6 lighting conditions, 9 elevations (30 to 70 degrees every 5 degrees), and 18 azimuths (0 to 340 every 20 degrees).

The training set is composed of 5 instances of each category (instances 4, 6, 7, 8 and 9), and the test set of the remaining 5 instances (instances 0, 1, 2, 3, and 5).
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 )	SmallnorbzSmallnorb 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g ddt jjddt jjddt jjddt jjd	dd
}| jdkr;t j |d< t jj| tt j	|dt
ddS )N)`   r      )shape)zfour-legged animalszhuman figures	airplanestruckscars)names
   )num_classes	         imageimage2label_categoryinstancelabel_elevationlabel_azimuthlabel_lightingr   idz0https://cs.nyu.edu/~ylclab/data/norb-v1.0-small/)r   r   )builderdescriptionfeatureshomepagecitationsupervised_keys)tfdsr   Image
ClassLabelversionTextcoreDatasetInfo_DESCRIPTIONFeaturesDict	_CITATION)selffeatures_dict r/   f/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image_classification/smallnorb.py_info?   s$   

zSmallnorb._infoc              	   C   s   t jddt jddt jddtjddtjddtjddd}||}tjjtjjt	d|d |d |d	 d
dtjjtjj
t	d|d |d |d d
dgS )zReturns splits.dat)typecatinfo)training_dattraining_cattraining_infotesting_dattesting_cattesting_infotrain_r6   r7   r8   )split_prefixdat_pathcat_path	info_path)name
gen_kwargstest_r9   r:   r;   )_TRAINING_URL_TEMPLATEformat_TESTING_URL_TEMPLATEdownload_and_extractr#   r(   SplitGeneratorSplitTRAINdictTEST)r-   
dl_manager	filenamesfilesr/   r/   r0   _split_generatorsa   s6   






	zSmallnorb._split_generatorsc              	   c   s    t |||\}}}tt|||D ]/\}\}	}
}|	d |	d |
|d |d |d |d d}| jdkr=d|||d< ||fV  qd	S )
aG  Generate examples for the Smallnorb dataset.

    Args:
      split_prefix: Prefix that identifies the split.
      dat_path: Path to dat file of the chunk.
      cat_path: Path to cat file of the chunk.
      info_path: Path to info file of the chunk.

    Yields:
      Dictionaries with images and the different labels.
    r   r	         r   r   z{}{:05d}r   N)_load_chunk	enumerater   zipr&   rE   )r-   r=   r>   r?   r@   dat_arrcat_arrinfo_arrir   categoryinfo_vecrecordr/   r/   r0   _generate_examples   s"   
	zSmallnorb._generate_examplesN)__name__
__module____qualname____doc__r#   r(   VersionVERSIONSUPPORTED_VERSIONSr1   rP   r]   r/   r/   r/   r0   r   6   s    
"r   c                 C   sX   t | }t|d}t |}t |}t|}|dddf d |dddf< |||fS )zLoads a data chunk as specified by the paths.

  Args:
    dat_path: Path to dat file of the chunk.
    cat_path: Path to cat file of the chunk.
    info_path: Path to info file of the chunk.

  Returns:
    Tuple with the dat, cat, info_arrays.
  NrQ   )read_binary_matrixnpexpand_dimscopy)r>   r?   r@   	dat_array	cat_array
info_arrayr/   r/   r0   rS      s   
 
rS   c           
      C   s   t jj| d^}| }tdd}ttj	||dd}|dkr)td}n|dkr6tdd}nt
d	ttj	||dd
d}tj	|||dd}td|d
 }tj	||d| d}	W d   n1 siw   Y  |	t|S )a  Reads and returns binary formatted matrix stored in filename.

  The file format is described on the data set page:
  https://cs.nyu.edu/~ylclab/data/norb-v1.0-small/

  Args:
    filename: String with path to the file.

  Returns:
    Numpy array contained in the file.
  rbint32<r	   )dtypecountiUL=uint8iTL=z"Invalid magic value for data type!   )rp   rq   offset   rR   )rp   rt   N)tfiogfileGFilereadrg   rp   newbyteorderint
frombuffer
ValueErrormaxreshapetuple)
filenamefsint32_dtypemagic
data_dtypendimdimsbytes_used_for_shape_infodatar/   r/   r0   rf      s"   
rf   )ra   
__future__r   r   r   numpyrg   sixr   tensorflow.compat.v2compatv2rv   tensorflow_datasets.public_api
public_apir#   r,   rD   rF   r*   r(   GeneratorBasedBuilderr   rS   rf   r/   r/   r/   r0   <module>   s"   g