
    h                         S SK r S SKrS SKJr  S SKJrJrJrJr  S SK	r
S SKJr  SSKJrJrJr  SSKJr   " S S	\5      rg)
    N)Path)AnyCallableOptionalUnion)Image   )download_and_extract_archivedownload_urlverify_str_arg)VisionDatasetc                      ^  \ rS rSrSrSrSrSrSrSr	Sr
    SS
\\\4   S\S\S\S\\   SS	4U 4S jjjrS\S\R$                  4S jrS\S\R*                  4S jrS\S\\\4   4S jrS\4S jrS\4S jrSrU =r$ )	SBDataset   a,  `Semantic Boundaries Dataset <http://home.bharathh.info/pubs/codes/SBD/download.html>`_

The SBD currently contains annotations from 11355 images taken from the PASCAL VOC 2011 dataset.

.. note ::

    Please note that the train and val splits included with this dataset are different from
    the splits in the PASCAL VOC dataset. In particular some "train" images might be part of
    VOC2012 val.
    If you are interested in testing on VOC 2012 val, then use `image_set='train_noval'`,
    which excludes all val images.

.. 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 the Semantic Boundaries Dataset
    image_set (string, optional): Select the image_set to use, ``train``, ``val`` or ``train_noval``.
        Image set ``train_noval`` excludes VOC 2012 val images.
    mode (string, optional): Select target type. Possible values 'boundaries' or 'segmentation'.
        In case of 'boundaries', the target is an array of shape `[num_classes, H, W]`,
        where `num_classes=20`.
    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.
    transforms (callable, optional): A function/transform that takes input sample and its target as entry
        and returns a transformed version. Input sample is PIL image and target is a numpy array
        if `mode='boundaries'` or PIL image if `mode='segmentation'`.
zchttps://www2.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/semantic_contours/benchmark.tgz 82b4d87ceb2ed10f6038a1cba92111cbzbenchmark.tgzz4https://www.cs.cornell.edu/~bharathh/train_noval.txtztrain_noval.txt 79bff800c5f0b1ec6b21080a3c066722Nroot	image_setmodedownload
transformsreturnc                   >  SSK Jn  X`l        [
        TU ]  X5        [        USS5      U l        [        USS5      U l	        SU l
        U R                  n[        R                  R                  US	5      n[        R                  R                  US
5      n	U(       a  [        U R                   U R                  U R"                  U R$                  S9  [        R                  R                  U R                  SS5      n
S H8  n[        R                  R                  X5      n[&        R(                  " X5        M:     U R                  S:X  a+  [+        U R,                  XpR.                  U R0                  5        [        R                  R3                  U5      (       d  [	        S5      e[        R                  R                  XrR5                  S5      S-   5      n[7        [        R                  R                  U5      5       nUR9                  5        Vs/ s H  oR;                  5       PM     nnS S S 5        W Vs/ s H%  n[        R                  R                  XS-   5      PM'     snU l        U Vs/ s H%  n[        R                  R                  XS-   5      PM'     snU l        U R                  S:X  a  U R@                  U l"        g U RB                  U l"        g ! [         a    [	        S5      ef = fs  snf ! , (       d  f       N= fs  snf s  snf )Nr   )loadmatzQScipy is not found. This dataset needs to have scipy installed: pip install scipyr   )trainvaltrain_novalr   )segmentation
boundaries   imgcls)filenamemd5benchmark_RELEASEdataset)r"   r!   instz	train.txtzval.txtr   zHDataset not found or corrupted. You can use download=True to download it
z.txtz.jpgz.matr   )#scipy.ior   _loadmatImportErrorRuntimeErrorsuper__init__r   r   r   num_classesr   ospathjoinr
   urlr#   r$   shutilmover   voc_train_urlvoc_split_filenamevoc_split_md5isdirrstripopen	readlinesstripimagesmasks_get_segmentation_target_get_boundaries_target_get_target)selfr   r   r   r   r   r   sbd_root	image_dirmask_dirextracted_ds_rootfold_pathsplit_ffhx
file_names	__class__s                    P/var/www/auris/envauris/lib/python3.13/site-packages/torchvision/datasets/sbd.pyr.   SBDataset.__init__5   s;   	t(#M 	*'	;@_`"41OP	99GGLL51	77<<%0(499t}}Z^ZbZbc "TYY8KY WC77<<(9=H/ D ~~.T//;R;RTXTfTfgww}}X&&ijj'',,x)9)9$)?&)HI"'',,w'(B-/\\^<^'')^J< ) EOOJqrww||I6z:JOBLM*Qbggll8Z8*M
<@II<W488]a]x]xC  	trss	t8 = )( PMs5   K <K4K/(K46,L.,L
K,/K44
Lfilepathc                 h    U R                  U5      n[        R                  " US   S   S   S   5      $ )NGTclsr   Segmentation)r*   r   	fromarray)rC   rQ   mats      rO   r@   "SBDataset._get_segmentation_targete   s1    mmH%s7|A~>qABB    c           
         U R                  U5      n[        R                  " [        U R                  5       Vs/ s H8  n[        R
                  " US   S   S   S   U   S   R                  5       SS9PM:     snSS9$ s  snf )NrS   r   
Boundaries)axis)r*   npconcatenateranger/   expand_dimstoarray)rC   rQ   rV   is       rO   rA    SBDataset._get_boundaries_targeti   sy    mmH%~~_deieueu_vw_vZ[R^^CLOL9!<Q?BJJLSTU_vw
 	
ws   ?A>indexc                     [         R                  " U R                  U   5      R                  S5      nU R	                  U R
                  U   5      nU R                  b  U R                  X#5      u  p#X#4$ )NRGB)r   r;   r>   convertrB   r?   r   )rC   rc   r!   targets       rO   __getitem__SBDataset.__getitem__p   s_    jjU+,44U;!!$**U"34??&//#6KC{rX   c                 ,    [        U R                  5      $ )N)lenr>   )rC   s    rO   __len__SBDataset.__len__y   s    4;;rX   c                 `    SS/nSR                  U5      R                  " S0 U R                  D6$ )NzImage set: {image_set}zMode: {mode}r(    )r2   format__dict__)rC   liness     rO   
extra_reprSBDataset.extra_repr|   s-    )>:yy&&777rX   )rB   r*   r   r>   r?   r   r/   )r   r   FN)__name__
__module____qualname____firstlineno____doc__r3   r$   r#   r6   r7   r8   r   strr   boolr   r   r.   r   r@   r\   ndarrayrA   inttupler   rh   rl   rs   __static_attributes____classcell__)rN   s   @rO   r   r      s    > pC
,CHJM*6M
 ! )-.yCI.y .y 	.y
 .y X&.y 
.y .y`C C C
s 
rzz 
 sCx    8C 8 8rX   r   )r0   r4   pathlibr   typingr   r   r   r   numpyr\   PILr   utilsr
   r   r   visionr   r   ro   rX   rO   <module>r      s1    	   1 1   M M !q8 q8rX   