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  mZ	 ddl
mZ dZdZdZed	 Zed
 ed ed ed dZdZdZG dd dejjZdd Zdd ZdS )Omniglot dataset.    )absolute_import)division)print_functionNaU  @article{lake2015human,
  title={Human-level concept learning through probabilistic program induction},
  author={Lake, Brenden M and Salakhutdinov, Ruslan and Tenenbaum, Joshua B},
  journal={Science},
  volume={350},
  number={6266},
  pages={1332--1338},
  year={2015},
  publisher={American Association for the Advancement of Science}
}
zOmniglot data set for one-shot learning. This dataset contains 1623 different
handwritten characters from 50 different alphabets.
z(https://github.com/brendenlake/omniglot/zraw/master/python/zimages_background.zipzimages_evaluation.zipzimages_background_small1.zipzimages_background_small2.zip)trainevalsmall1small2iW  2   c                   @   s6   e Zd ZdZejddZdd Zdd Z	dd	 Z
d
S )Omniglotr   z3.0.0z6New split API (https://tensorflow.org/datasets/splits)c                 C   sL   t jj| tt jt jjdddt jjtdt	j
t jjtdddttdS )N)i   r      png)shapeencoding_format)num_classesimagealphabetalphabet_char_idlabel)r   r   )builderdescriptionfeaturessupervised_keyshomepagecitation)tfdscoreDatasetInfo_DESCRIPTIONr   FeaturesDictImage
ClassLabel_NUM_ALPHABETStfint64_NUM_CLASSES	_BASE_URL	_CITATION)self r+   e/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image_classification/omniglot.py_infoA   s   
zOmniglot._infoc                 C   s   | t}t| \}}t|| jjd _|| jjd _tj	j
tjjd|d idtj	j
tjjd|d idtj	j
dd|d idtj	j
dd|d idgS )	Nr   r   	directoryr   )name
gen_kwargsr   r   r	   )download_and_extract_DL_URLS
_get_namesvaluessortedinfor   namesr   r   SplitGeneratorSplitTRAINTEST)r*   
dl_managerextracted_dirs	alphabetslabel_namesr+   r+   r,   _split_generatorsT   s2   
zOmniglot._split_generatorsc           	      c   s:    t |D ]}|\}}}}}||||d}||fV  qd S )Nr   )_walk_omniglot_dir)	r*   r.   exampler   r   r   
image_pathimage_idrecordr+   r+   r,   _generate_exampless   s   zOmniglot._generate_examplesN)__name__
__module____qualname____doc__r   r   VersionVERSIONr-   r@   rF   r+   r+   r+   r,   r   ;   s    r   c                 c   s    t j| tjj| d } ttjj| }|D ]Y}t j| |}ttjj|}|D ]D}t|t	dd d }t j||}tjj|}|D ]%}	|	
d\}
}t|
d }
t j||	}d|||	f }|||
||fV  qLq.qdS )z.Walk an Omniglot directory and yield examples.r   	characterN   _z%s_%d_%s)ospathjoinr%   iogfilelistdirr5   intlensplit)r.   r>   r   alphabet_dir
charactersrM   character_idcharacter_dirimagesr   r   rO   rC   rD   r+   r+   r,   rA      s&   rA   c           	         s   t  }i  | D ],}t|D ]%}|\}}}}}|| d||f }| v r. | |ks-J q| |< qq fddt D  | fS )z4Get alphabet and label names, union across all dirs.z%s_%dc                    s   g | ]} | qS r+   r+   ).0kr?   r+   r,   
<listcomp>   s    z_get_names.<locals>.<listcomp>)setrA   addr5   )	dirsr>   drB   r   r   r   rO   
label_namer+   r`   r,   r3      s   

r3   )rJ   
__future__r   r   r   rP   tensorflow.compat.v2compatv2r%   tensorflow_datasets.public_api
public_apir   r)   r    r(   _DL_URLr2   r'   r$   r   GeneratorBasedBuilderr   rA   r3   r+   r+   r+   r,   <module>   s*   D