o
    Niz,                     @   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Z	ddl
m  mZ ddlmZ dZdZdZe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S )zCaltech birds dataset.    )absolute_import)division)print_functionNa.  Caltech-UCSD Birds 200 (CUB-200) is an image dataset with photos 
of 200 bird species (mostly North American). The total number of 
categories of birds is 200 and there are 6033 images in the 2010 
dataset and 11,788 images in the 2011 dataset.
Annotations include bounding boxes, segmentation labels.
z4http://www.vision.caltech.edu/visipedia/CUB-200.htmla  @techreport{WelinderEtal2010,
Author = {P. Welinder and S. Branson and T. Mita and C. Wah and F. Schroff and S. Belongie and P. Perona},
Institution = {California Institute of Technology},
Number = {CNS-TR-2010-001},
Title = {{Caltech-UCSD Birds 200}},
Year = {2010}
}
z((\w*)/)*(\d*).(\w*)/(\w*.jpg)$c                   @   sH   e Zd ZdZejdZedd Z	dd Z
dd Zd	d
 Zdd ZdS )CaltechBirds2010zCaltech Birds 2010 dataset.0.1.0c                 C   s   t | jddddS )Nz?http://www.vision.caltech.edu/visipedia-data/CUB-200/images.tgzz>http://www.vision.caltech.edu/visipedia-data/CUB-200/lists.tgzzDhttp://www.vision.caltech.edu/visipedia-data/CUB-200/annotations.tgzname
images_url	split_urlannotations_urlCaltechBirdsInfor   self r   j/home/ubuntu/.local/lib/python3.10/site-packages/tensorflow_datasets/image_classification/caltech_birds.py_caltech_birds_info9      z$CaltechBirds2010._caltech_birds_infoc                 C   Z   t jj| tt jt j t j t jjddt j t j	 t jjddddt
tdS N   )num_classes)NN   )shapeimagezimage/filenamelabel
label_namebboxsegmentation_mask)r   r   )builderdescriptionfeaturessupervised_keyshomepagecitationtfdscoreDatasetInfo_DESCRIPTIONr"   FeaturesDictImageText
ClassLabelBBoxFeature_URL	_CITATIONr   r   r   r   _infoB      	zCaltechBirds2010._infoc              
   C   s  | | jj| jj| jjg}|| jj| jjg}tj|d d}tj|d d}t	j
j|}|  }W d    n1 sCw   Y  t	j
j|}|  }W d    n1 s`w   Y  tt}	t	j
j|d D ]X\}
}}|D ]P}|drtj|
|}t	j
j|d}tjjjj
j|ddd	gd
}W d    n1 sw   Y  |	|dd  |d  |	|dd  |d	  qzqstjjtjj||d ||	ddtjjtjj||d ||	ddgS )Nr   zlists/train.txtzlists/test.txtr   z.matrbTr   seg)
squeeze_mevariable_names.   archive
file_namesannotationsr   
gen_kwargs) downloadr   r
   r   r	   download_and_extractospathjointfiogfileGFileread
splitlinescollectionsdefaultdictlistwalkendswithr'   r(   lazy_importsscipyloadmatsplitappendSplitGeneratorSplitTRAINiter_archiveTEST)r   
dl_managerdownload_pathextracted_path
train_path	test_pathf
train_list	test_list
attributesroot_filesfnamerC   matr   r   r   _split_generatorsT   s\   



z"CaltechBirds2010._split_generatorsc                 C   s<   |d | }|d | }|d | }|d | }||||fS )a  Function to get normalized bounding box values.

    Args:
      bbox_annotations: list of bbox values in kitti format
      img_width: image width
      img_height: image height

    Returns:
      Normalized bounding box xmin, ymin, xmax, ymax values
    topleftbottomrightr   )r   bbox_annotations	img_width
img_heightyminxminymaxxmaxr   r   r   _get_bounding_box_values   s
   z)CaltechBirds2010._get_bounding_box_valuesc                 c   s   |D ]\}}| dd}t|}|r"d|ddd |vr#q| }|d  }t|d d }	|d dd	 }
||
 d }|j\}}| 	||
 d	 ||}||||	|t
jjt|d	 d
t|d d
t|d d
t|d d
d|ddddtjf dfV  qdS )a  Generate birds images, labels and bounding box given the directory path.

    Args:
        archive: object that iterates over the zip
        file_names : list of train/test image file names obtained from mat file
        annotations : dict of image file names and bbox attributes, segmentation
          labels

    Yields:
        Image path, Image file name, its corresponding label and
        bounding box values
    \/Nr   r8   r   g      ?r9      )rp   rq   rr   rs   r   )replace_NAME_REmatchrD   rS   groupslowerintr   rt   r'   r"   BBoxminnpnewaxis)r   r;   r<   r=   rf   fobjresmatchesr   	label_key	file_namer   heightwidthr   r   r   r   _generate_examples   s:   
 
z#CaltechBirds2010._generate_examplesN)__name__
__module____qualname____doc__r'   r(   VersionVERSIONpropertyr   r2   rh   rt   r   r   r   r   r   r   4   s    
3r   c                   @   s@   e Zd ZdZejdZedd Z	dd Z
dd Zd	d
 ZdS )CaltechBirds2011zCaltech Birds 2011 dataset.r   c                 C   s   t | jdd ddS )NzJhttp://www.vision.caltech.edu/visipedia-data/CUB-200-2011/CUB_200_2011.tgzzKhttp://www.vision.caltech.edu/visipedia-data/CUB-200-2011/segmentations.tgzr   r   r   r   r   r   r      r   z$CaltechBirds2011._caltech_birds_infoc                 C   r   r   r&   r   r   r   r   r2      r3   zCaltechBirds2011._infoc              
   C   s  | | jjg}|| jj| jjg}tj|d d}tj|d d}tj|d d}g g }}t	t
}	tjj|}
tjj|i}tjj|R}t|
||D ]C\}}}| \}}| \}}t|}| }|	|d dd  | dd   ||krt|dkr|| qV|| qVW d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  tjj|d D ]L\}}}|D ]D}|drtjjtj||d	}tjjjjtj| tj d
dd}W d    n	1 s
w   Y  |	|dd  | qqtjj!tj"j#|$|d ||	ddtjj!tj"j%|$|d ||	ddgS )Nr   zCUB_200_2011/images.txtz!CUB_200_2011/train_test_split.txtzCUB_200_2011/bounding_boxes.txtry   r8   r   z.pngr4   )dtype)flagsr:   r>   )&r@   r   r	   rA   r   rB   rC   rD   rK   rL   rM   rE   rF   rG   rH   ziprS   r|   r}   r~   rT   r   rN   rO   r'   r(   rP   cv2imdecoder   
fromstringrI   uint8rU   rV   rW   rX   rY   )r   rZ   r[   r\   image_names_path
split_path	bbox_pathr`   ra   rb   r_   f1f2lineline1line2img_idxvalidximg_namer   r   rc   rd   re   rf   png_fmaskr   r   r   rh      s   


(
  
z"CaltechBirds2011._split_generatorsc                 C   sd   t |d | }t |d | }t |d t |d  | }t |d t |d  | }||||fS )a  Gets normalized bounding box values (Conversion to KITTI format).

    Args:
      bbox_annotations: list of bbox values in kitti format
      img_width: image width
      img_height: image height

    Returns:
      Normalized bounding box xmin, ymin, xmax, ymax values
    r   r   r9   rz   )float)r   rm   rn   ro   rq   rp   rs   rr   r   r   r   rt   +  s   

z)CaltechBirds2011._get_bounding_box_valuesN)r   r   r   r   r'   r(   r   r   r   r   r2   rh   rt   r   r   r   r   r      s    
<r   c                   @   s   e Zd ZdZdS )r   a
  Contains the information necessary to generate a Caltech Birds dataset.

    Args:
        name (str): name of dataset.
        images_url (str): images URL.
        split_url (str): train/test split file URL.
        annotations_url (str): annotation folder URL.
  N)r   r   r   r   r   r   r   r   r   ?  s    r   _CaltechBirdsInfor   )r   
__future__r   r   r   rK   rB   renumpyr   tensorflow.compat.v2compatv2rE   tensorflow_datasets.public_api
public_apir'   r*   r0   r1   compiler|   r(   GeneratorBasedBuilderr   r   
namedtupler   r   r   r   r   <module>   s.   
	 
p