3
Á3W^U'  ã               @   sd   d dl Z d dlZd dlmZ d dlZddlmZmZmZ ddl	m
Z
 d dlmZ G dd„ de
ƒZdS )	é    N)Ú
namedtupleé   )Úextract_archiveÚverify_str_argÚiterable_to_str)ÚVisionDataset)ÚImagec            -       s$  e Zd ZdZedddddddd	d
gƒZedddddddd‹ƒeddddddddŒƒedddddddd�ƒeddddddddŽƒedddddddd�ƒedddddddd�ƒedddddddd‘ƒed d!dd"dddd’ƒed%d&dd"dddd“ƒed*d+dd"dddd”ƒed/d0dd"dddd•ƒed4d5dd6dddd–ƒed8d9dd6dddd—ƒed<d=dd6dddd˜ƒed@dAdd6dddd™ƒedDdEdd6ddddšƒedGdHdd6dddd›ƒedKdLddMddddœƒedNdOddMdddd�ƒedPdQddMddddžƒedSdTd!dMddddŸƒedVdWd&dXdddd ƒed[d\d+dXdddd¡ƒed_d`d0d_dddd¢ƒedbdcd5dddddd£ƒedfdgd9dddddd¤ƒedhdid=djd!ddd¥ƒedkdldAdjd!ddd¦ƒedmdndEdjd!ddd§ƒedodpddjd!ddd¨ƒedqdRddjd!ddd©ƒedsdtdHdjd!dddªƒedvdwdLdjd!ddd«ƒedxdydOdjd!ddd¬ƒed{d­d®djd!ddd¯ƒg#Zd°‡ fdd€„	Zd�d‚„ Zdƒd„„ Z	d…d†„ Z
d‡dˆ„ Zd‰dŠ„ Z‡  ZS )±Ú
Cityscapesal  `Cityscapes <http://www.cityscapes-dataset.com/>`_ Dataset.

    Args:
        root (string): Root directory of dataset where directory ``leftImg8bit``
            and ``gtFine`` or ``gtCoarse`` are located.
        split (string, optional): The image split to use, ``train``, ``test`` or ``val`` if mode="gtFine"
            otherwise ``train``, ``train_extra`` or ``val``
        mode (string, optional): The quality mode to use, ``gtFine`` or ``gtCoarse``
        target_type (string or list, optional): Type of target to use, ``instance``, ``semantic``, ``polygon``
            or ``color``. Can also be a list to output a tuple with all specified target types.
        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.
        transforms (callable, optional): A function/transform that takes input sample and its target as entry
            and returns a transformed version.

    Examples:

        Get semantic segmentation target

        .. code-block:: python

            dataset = Cityscapes('./data/cityscapes', split='train', mode='fine',
                                 target_type='semantic')

            img, smnt = dataset[0]

        Get multiple targets

        .. code-block:: python

            dataset = Cityscapes('./data/cityscapes', split='train', mode='fine',
                                 target_type=['instance', 'color', 'polygon'])

            img, (inst, col, poly) = dataset[0]

        Validate on the "coarse" set

        .. code-block:: python

            dataset = Cityscapes('./data/cityscapes', split='val', mode='coarse',
                                 target_type='semantic')

            img, smnt = dataset[0]
    ÚCityscapesClassÚnameÚidZtrain_idÚcategoryZcategory_idZhas_instancesZignore_in_evalÚcolorZ	unlabeledr   éÿ   ÚvoidFTzego vehicler   zrectification borderé   z
out of roié   Zstaticé   Zdynamicé   éo   éJ   Zgroundé   éQ   Zroadé   Zflaté€   é@   Zsidewalké   éô   é#   éè   Zparkingé	   éú   éª   é    z
rail tracké
   éæ   é–   éŒ   Zbuildingé   ZconstructionéF   Zwallé   éf   éœ   Zfenceé   é¾   é™   z
guard railé   é´   é¥   Zbridgeé   éd   Ztunnelé   éx   éZ   Zpoleé   ÚobjectZ	polegroupé   ztraffic lighté   é   ztraffic signé   éÜ   Z
vegetationé   Znatureék   éŽ   Zterrainé   é˜   éû   Zskyé   é‚   Zpersoné   Zhumané<   Zrideré   Zcaré   ZvehicleZtrucké   Zbusé   Zcaravané   Útrailerén   Útrainé   éP   Z
motorcycleé    Zbicycleé!   éw   zlicense plateÚfineÚinstanceNc                sv  t t| ƒj||||ƒ |dkr"dnd| _tjj| jd|ƒ| _tjj| j| j|ƒ| _	|| _
|| _g | _g | _t|ddƒ |dkr„d}nd}d}	|	j||t|ƒƒ}	t|d||	ƒ t|tƒs¾|g| _
dd„ | j
D ƒ tjj| jƒ pìtjj| j	ƒ �r¸|d
k�rtjj| jdjdƒƒ}
ntjj| jdjdƒƒ}
| jdk�rPtjj| jdj| jdƒƒ}n&| jdk�rvtjj| jdj| jdƒƒ}tjj|
ƒ�r°tjj|ƒ�r°t|
| jd� t|| jd� ntdƒ‚x¸tj| jƒD ]¨}tjj| j|ƒ}tjj| j	|ƒ}x€tj|ƒD ]r}g }xD| j
D ]:}dj|jdƒd | j| j|ƒƒ}|jtjj||ƒƒ �qW | jjtjj||ƒƒ | jj|ƒ �qöW �qÆW d S )NrV   ZgtFineZgtCoarseZleftImg8bitÚmodeÚcoarserP   ÚtestÚvalÚtrain_extrazOUnknown value '{}' for argument split if mode is '{}'. Valid values are {{{}}}.Úsplitc          	   S   s   g | ]}t |d dƒ‘qS )Útarget_typerW   ÚsemanticÚpolygonr   )rW   r_   r`   r   )r   )Ú.0Úvalue© rc   úF/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/cityscapes.pyú
<listcomp>|   s   z'Cityscapes.__init__.<locals>.<listcomp>zleftImg8bit{}z_trainextra.zipz_trainvaltest.zipz{}{}z.zip)Ú	from_pathZto_pathz‹Dataset not found or incomplete. Please make sure all required folders for the specified "split" and "mode" are inside the "root" directoryz{}_{}Z_leftImg8bitr   )rV   rY   )rP   rZ   r[   )rP   r\   r[   )Úsuperr	   Ú__init__rX   ÚosÚpathÚjoinÚrootZ
images_dirZtargets_dirr^   r]   ÚimagesÚtargetsr   Úformatr   Ú
isinstanceÚlistÚisdirÚisfiler   ÚRuntimeErrorÚlistdirÚ_get_target_suffixÚappend)Úselfrl   r]   rX   r^   Z	transformZtarget_transformÚ
transformsZvalid_modesÚmsgZimage_dir_zipZtarget_dir_zipZcityZimg_dirÚ
target_dirÚ	file_nameZtarget_typesÚtZtarget_name)Ú	__class__rc   rd   rh   e   sT    

"
zCityscapes.__init__c             C   s¬   t j| j| ƒjdƒ}g }xRt| jƒD ]D\}}|dkrL| j| j| | ƒ}nt j| j| | ƒ}|j|ƒ q&W t	|ƒdkr‚t
|ƒn|d }| jdk	r¤| j||ƒ\}}||fS )a-  
        Args:
            index (int): Index
        Returns:
            tuple: (image, target) where target is a tuple of all target types if target_type is a list with more
            than one item. Otherwise target is a json object if target_type="polygon", else the image segmentation.
        ZRGBr`   r   r   N)r   Úopenrm   ÚconvertÚ	enumerater^   Ú
_load_jsonrn   rw   ÚlenÚtuplery   )rx   ÚindexÚimagern   Úir}   Útargetrc   rc   rd   Ú__getitem__    s    	
zCityscapes.__getitem__c             C   s
   t | jƒS )N)rƒ   rm   )rx   rc   rc   rd   Ú__len__»   s    zCityscapes.__len__c             C   s   dddg}dj |ƒjf | jŽS )NzSplit: {split}zMode: {mode}zType: {target_type}Ú
)rk   ro   Ú__dict__)rx   Úlinesrc   rc   rd   Ú
extra_repr¾   s    
zCityscapes.extra_reprc          	   C   s$   t |dƒ�}tj|ƒ}W d Q R X |S )NÚr)r   ÚjsonÚload)rx   rj   ÚfileÚdatarc   rc   rd   r‚   Â   s    zCityscapes._load_jsonc             C   sD   |dkrdj |ƒS |dkr$dj |ƒS |dkr6dj |ƒS dj |ƒS d S )NrW   z{}_instanceIds.pngr_   z{}_labelIds.pngr   z{}_color.pngz{}_polygons.json)ro   )rx   rX   r^   rc   rc   rd   rv   Ç   s    


zCityscapes._get_target_suffix)r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r!   r"   r#   )r%   r&   r'   )r)   r)   r)   )r+   r+   r,   )r.   r/   r/   )r1   r2   r1   )r&   r4   r4   )r&   r6   r7   )r/   r/   r/   )r/   r/   r/   )r!   r"   r<   )r>   r>   r   )r@   rA   r   )rC   rD   rC   )r)   rF   r1   )r>   r=   rH   )r   r   r   )r   r   rA   )r   r   r)   )r   rH   r4   )r   r   r7   )r   r   rO   )r   rR   r4   )r   r   r%   )rU   r(   rS   éÿÿÿÿr”   )r   r   rA   )rP   rV   rW   NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   Úclassesrh   r‰   rŠ   rŽ   r‚   rv   Ú__classcell__rc   rc   )r~   rd   r	      sZ   . :r	   )r�   ri   Úcollectionsr   ÚzipfileÚutilsr   r   r   Zvisionr   ZPILr   r	   rc   rc   rc   rd   Ú<module>   s   