3
Á3W^þ   ã               @   sl   d dl mZ d dlmZ d dlZd dlZddlmZ ddlm	Z	m
Z
mZ G dd„ deƒZG d	d
„ d
eƒZdS )é    )Úprint_function)ÚImageNé   )ÚVisionDataset)Údownload_and_extract_archiveÚmakedir_exist_okÚverify_str_argc                   sJ   e Zd ZdZd‡ fdd„	Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	‡  Z
S )Ú
Caltech101a/  `Caltech 101 <http://www.vision.caltech.edu/Image_Datasets/Caltech101/>`_ Dataset.

    Args:
        root (string): 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 an 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.
    ÚcategoryNFc       	         s"  t t| ƒjtjj|dƒ||d� t| jƒ t|t	ƒs:|g}dd„ |D ƒ| _
|rV| jƒ  | jƒ sftdƒ‚ttjtjj| jdƒƒƒ| _| jjdƒ d	d
dddœ‰ t	t‡ fdd„| jƒƒ| _g | _g | _xZt| jƒD ]L\}}ttjtjj| jd|ƒƒƒ}| jjtd|d ƒƒ | jj||g ƒ qÎW d S )NZ
caltech101)Ú	transformÚtarget_transformc             S   s   g | ]}t |d dƒ‘qS )Útarget_typer
   Ú
annotation)r
   r   )r   )Ú.0Út© r   úC/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/caltech.pyú
<listcomp>%   s   z'Caltech101.__init__.<locals>.<listcomp>zDataset not found or corrupted.z) You can use download=True to download itÚ101_ObjectCategoriesZBACKGROUND_GoogleZFaces_2ZFaces_3ZMotorbikes_16ZAirplanes_Side_2)ZFacesZ
Faces_easyZ
MotorbikesZ	airplanesc                s   | ˆ krˆ |  S | S )Nr   )Úx)Úname_mapr   r   Ú<lambda>9   s    z%Caltech101.__init__.<locals>.<lambda>r   zHDataset not found or corrupted. You can use download=True to download it)Úsuperr	   Ú__init__ÚosÚpathÚjoinr   ÚrootÚ
isinstanceÚlistr   ÚdownloadÚ_check_integrityÚRuntimeErrorÚsortedÚlistdirÚ
categoriesÚremoveÚmapÚannotation_categoriesÚindexÚyÚ	enumerateÚlenÚextendÚrange)	Úselfr   r   r   r   r    ÚiÚcÚn)Ú	__class__)r   r   r      s4    


zCaltech101.__init__c          	   C   sþ   ddl }tjtjj| jd| j| j|  dj	| j
| ƒƒƒ}g }xr| jD ]h}|dkrd|j| j| ƒ qF|dkrF|jjtjj| jd| j| j|  dj	| j
| ƒƒƒ}|j|d	 ƒ qFW t|ƒd
krÆt|ƒn|d }| jdk	râ| j|ƒ}| jdk	rö| j|ƒ}||fS )zž
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where the type of target specified by target_type.
        r   Nr   zimage_{:04d}.jpgr
   r   ZAnnotationszannotation_{:04d}.matZobj_contourr   )Zscipy.ior   Úopenr   r   r   r   r%   r*   Úformatr)   r   ÚappendÚioZloadmatr(   r,   Útupler   r   )r/   r)   ZscipyÚimgÚtargetr   Údatar   r   r   Ú__getitem__B   s*    



zCaltech101.__getitem__c             C   s   t jjt jj| jdƒƒS )Nr   )r   r   Úexistsr   r   )r/   r   r   r   r!   e   s    zCaltech101._check_integrityc             C   s
   t | jƒS )N)r,   r)   )r/   r   r   r   Ú__len__i   s    zCaltech101.__len__c             C   s<   | j ƒ rtdƒ d S td| jddd� td| jddd� d S )	Nz%Files already downloaded and verifiedzShttp://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gzz101_ObjectCategories.tar.gzZ b224c7392d521a49829488ab0f1120d9)ÚfilenameÚmd5zGhttp://www.vision.caltech.edu/Image_Datasets/Caltech101/Annotations.tarz101_Annotations.tarZ 6f83eeb1f24d99cab4eb377263132c91)r!   Úprintr   r   )r/   r   r   r   r    l   s    zCaltech101.downloadc             C   s   dj f | jŽS )NzTarget type: {target_type})r5   Ú__dict__)r/   r   r   r   Ú
extra_repr|   s    zCaltech101.extra_repr)r
   NNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r<   r!   r>   r    rC   Ú__classcell__r   r   )r3   r   r	   
   s    $#r	   c                   sB   e Zd ZdZd‡ fdd„	Zdd„ Zdd	„ Zd
d„ Zdd„ Z‡  Z	S )Ú
Caltech256aà  `Caltech 256 <http://www.vision.caltech.edu/Image_Datasets/Caltech256/>`_ Dataset.

    Args:
        root (string): 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 an 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.
    NFc                sÎ   t t| ƒjtjj|dƒ||d� t| jƒ |r6| jƒ  | j	ƒ sFt
dƒ‚ttjtjj| jdƒƒƒ| _g | _g | _xZt| jƒD ]L\}}ttjtjj| jd|ƒƒƒ}| jjtd|d ƒƒ | jj||g ƒ qzW d S )NZ
caltech256)r   r   zDataset not found or corrupted.z) You can use download=True to download itÚ256_ObjectCategoriesr   zHDataset not found or corrupted. You can use download=True to download it)r   rI   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r)   r*   r+   r,   r-   r.   )r/   r   r   r   r    r0   r1   r2   )r3   r   r   r   �   s     
zCaltech256.__init__c          	   C   sx   t jtjj| jd| j| j|  dj| j| d | j	| ƒƒƒ}| j| }| j
dk	r\| j
|ƒ}| jdk	rp| j|ƒ}||fS )z–
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is index of the target class.
        rJ   z{:03d}_{:04d}.jpgr   N)r   r4   r   r   r   r   r%   r*   r5   r)   r   r   )r/   r)   r9   r:   r   r   r   r<   ¤   s     




zCaltech256.__getitem__c             C   s   t jjt jj| jdƒƒS )NrJ   )r   r   r=   r   r   )r/   r   r   r   r!   »   s    zCaltech256._check_integrityc             C   s
   t | jƒS )N)r,   r)   )r/   r   r   r   r>   ¿   s    zCaltech256.__len__c             C   s*   | j ƒ rtdƒ d S td| jddd� d S )Nz%Files already downloaded and verifiedzPhttp://www.vision.caltech.edu/Image_Datasets/Caltech256/256_ObjectCategories.tarz256_ObjectCategories.tarZ 67b4f42ca05d46448c6bb8ecd2220f6d)r?   r@   )r!   rA   r   r   )r/   r   r   r   r    Â   s    zCaltech256.download)NNF)
rD   rE   rF   rG   r   r<   r!   r>   r    rH   r   r   )r3   r   rI   €   s   rI   )Ú
__future__r   ZPILr   r   Úos.pathZvisionr   Úutilsr   r   r   r	   rI   r   r   r   r   Ú<module>   s   v