o
    ^i"                     @   s   d dl Z d dlZ d dlZd dlmZ d dlmZmZmZm	Z	 d dl
mZ ddlmZmZmZ ddlmZ G dd	 d	eZG d
d deZdS )    N)Path)AnyCallableOptionalUnion)Image   )download_and_extract_archiveextract_archiveverify_str_arg)VisionDatasetc                       s   e Zd ZdZ				ddeeef deee ef dee	 dee	 d	e
d
df fddZded
eeef fddZd
e
fddZd
efddZdddZd
efddZ  ZS )
Caltech101a?  `Caltech 101 <https://data.caltech.edu/records/20086>`_ Dataset.

    .. warning::

        This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format.

    Args:
        root (str or ``pathlib.Path``): Root directory of dataset where directory
            ``caltech101`` exists or will be saved to if download is set to True.
        target_type (string or list, optional): Type of target to use, ``category`` or
            ``annotation``. Can also be a list to output a tuple with all specified
            target types.  ``category`` represents the target class, and
            ``annotation`` is a list of points from a hand-generated outline.
            Defaults to ``category``.
        transform (callable, optional): A function/transform that takes in a PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        download (bool, optional): If true, downloads the dataset from the internet and
            puts it in root directory. If dataset is already downloaded, it is not
            downloaded again.

            .. warning::

                To download the dataset `gdown <https://github.com/wkentaro/gdown>`_ is required.
    categoryNFroottarget_type	transformtarget_transformdownloadreturnc           	   	      s   t  jtj|d||d tj| jdd t|tr|g}dd |D | _	|r,| 
  |  s4tdtttj| jd| _| jd	 d
dddd tt fdd| j| _g | _g | _t| jD ]&\}}tttj| jd|}| jtd|d  | j||g  qgd S )N
caltech101r   r   Texist_okc                 S   s   g | ]}t |d dqS )r   )r   
annotation)r   ).0t r   W/var/www/html/RAG/RAG_venv/lib/python3.10/site-packages/torchvision/datasets/caltech.py
<listcomp>5   s    z'Caltech101.__init__.<locals>.<listcomp>HDataset not found or corrupted. You can use download=True to download it101_ObjectCategoriesBACKGROUND_GoogleFaces_2Faces_3Motorbikes_16Airplanes_Side_2)Faces
Faces_easy
Motorbikes	airplanesc                    s   |  v r |  S | S Nr   )xname_mapr   r   <lambda>I   s    z%Caltech101.__init__.<locals>.<lambda>r   )super__init__ospathjoinmakedirsr   
isinstancestrr   r   _check_integrityRuntimeErrorsortedlistdir
categoriesremovelistmapannotation_categoriesindexy	enumeratelenextendrange)	selfr   r   r   r   r   icn	__class__r,   r   r0   )   s0   
zCaltech101.__init__r@   c                 C   s  ddl }ttj| jd| j| j|  d| j	| dd}g }| j
D ]6}|dkr3|| j|  q$|dkrZ|jtj| jd	| j| j|  d
| j	| dd}||d  q$t|dkret|n|d }| jdurs| |}| jdur}| |}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where the type of target specified by target_type.
        r   Nr    image_04d.jpgr   r   Annotationsannotation_z.matobj_contourr   )scipy.ior   openr1   r2   r3   r   r;   rA   r@   r   appendioloadmatr?   rC   tupler   r   )rF   r@   scipyimgtargetr   datar   r   r   __getitem__R   s<   	




zCaltech101.__getitem__c                 C      t jt j| jdS )Nr    r1   r2   existsr3   r   rF   r   r   r   r7   }      zCaltech101._check_integrityc                 C   
   t | jS r*   rC   r@   r`   r   r   r   __len__      
zCaltech101.__len__c                 C   s   |   rd S td| jddd tj| jd}t|D ]}|dr.ttj||| j qt	
| ttj| jd d S )NzBhttps://data.caltech.edu/records/mzrjq-6wc02/files/caltech-101.zipzcaltech-101.zip 3138e1922a9193bfa496528edbbc45d0)download_rootfilenamemd5zcaltech-101z.gz)r7   r	   r   r1   r2   r3   r:   endswithr
   shutilrmtreer<   )rF   gzip_folder	gzip_filer   r   r   r      s   

zCaltech101.downloadc                 C   s   dj di | jS )NzTarget type: {target_type}r   )format__dict__r`   r   r   r   
extra_repr   s   zCaltech101.extra_repr)r   NNFr   N)__name__
__module____qualname____doc__r   r6   r   r=   r   r   boolr0   intrW   r   r\   r7   rd   r   rq   __classcell__r   r   rJ   r   r      s0    
)+
r   c                       s   e Zd ZdZ			ddedee dee deddf
 fd	d
Zde	de
eef fddZdefddZde	fddZdddZ  ZS )
Caltech256a  `Caltech 256 <https://data.caltech.edu/records/20087>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory of dataset where directory
            ``caltech256`` exists or will be saved to if download is set to True.
        transform (callable, optional): A function/transform that takes in a PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        download (bool, optional): If true, downloads the dataset from the internet and
            puts it in root directory. If dataset is already downloaded, it is not
            downloaded again.
    NFr   r   r   r   r   c              
      s   t  jtj|d||d tj| jdd |r|   |  s$t	dt
ttj| jd| _g | _g | _t| jD ]+\}}tdd ttj| jd|D }| jtd	|d	  | j||g  q=d S )
N
caltech256r   Tr   r   256_ObjectCategoriesc                 S   s   g | ]	}| d r|qS )rN   )rj   )r   itemr   r   r   r      s    z'Caltech256.__init__.<locals>.<listcomp>r   )r/   r0   r1   r2   r3   r4   r   r   r7   r8   r9   r:   r;   r@   rA   rB   rC   rD   rE   )rF   r   r   r   r   rG   rH   rI   rJ   r   r   r0      s$   zCaltech256.__init__r@   c                 C   s   t tj| jd| j| j|  | j| d dd| j| dd}| j| }| j	dur2| 	|}| j
dur<| 
|}||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        r|   r   03d_rM   rN   N)r   rS   r1   r2   r3   r   r;   rA   r@   r   r   )rF   r@   rY   rZ   r   r   r   r\      s   "
	



zCaltech256.__getitem__c                 C   r]   )Nr|   r^   r`   r   r   r   r7      ra   zCaltech256._check_integrityc                 C   rb   r*   rc   r`   r   r   r   rd      re   zCaltech256.__len__c                 C   s"   |   rd S td| jddd d S )NzKhttps://data.caltech.edu/records/nyy15-4j048/files/256_ObjectCategories.tarz256_ObjectCategories.tar 67b4f42ca05d46448c6bb8ecd2220f6d)rh   ri   )r7   r	   r   r`   r   r   r   r      s   
zCaltech256.download)NNFrr   )rs   rt   ru   rv   r6   r   r   rw   r0   rx   rW   r   r\   r7   rd   r   ry   r   r   rJ   r   rz      s(    rz   )r1   os.pathrk   pathlibr   typingr   r   r   r   PILr   utilsr	   r
   r   visionr   r   rz   r   r   r   r   <module>   s     