
    h2                        S SK r S SKr S SKJr  S SKJrJrJrJrJ	r	  S SK
Jr  SSKJr  S\S\	\\\S	4   4   S
\4S jrS\S
\4S jrS\	\\4   S
\\\   \\\4   4   4S jr    SS\	\\4   S\\\\4      S\\	\\\S	4   4      S\\\/\4      S\S
\\\\4      4S jjr " S S\5      rSrS\	\\4   S
\R                  4S jrS\	\\4   S
\4S jrS\	\\4   S
\4S jr " S S\5      rg)    N)Path)AnyCallablecastOptionalUnion)Image   )VisionDatasetfilename
extensions.returnc                     U R                  5       R                  [        U[        5      (       a  U5      $ [	        U5      5      $ )zChecks if a file is an allowed extension.

Args:
    filename (string): path to a file
    extensions (tuple of strings): extensions to consider (lowercase)

Returns:
    bool: True if the filename ends with one of given extensions
)lowerendswith
isinstancestrtuple)r   r   s     S/var/www/auris/envauris/lib/python3.13/site-packages/torchvision/datasets/folder.pyhas_file_allowed_extensionr      s7     >>$$:j#3N3NZffTYZdTeff    c                 "    [        U [        5      $ )zChecks if a file is an allowed image extension.

Args:
    filename (string): path to a file

Returns:
    bool: True if the filename ends with a known image extension
)r   IMG_EXTENSIONS)r   s    r   is_image_filer      s     &h??r   	directoryc                     [        S [        R                  " U 5       5       5      nU(       d  [        SU  S35      e[	        U5       VVs0 s H  u  p#X2_M	     nnnX4$ s  snnf )zOFinds the class folders in a dataset.

See :class:`DatasetFolder` for details.
c              3   f   #    U  H'  oR                  5       (       d  M  UR                  v   M)     g 7fN)is_dirname).0entrys     r   	<genexpr>find_classes.<locals>.<genexpr>)   s     U-BEllnZUZZ-Bs   11z"Couldn't find any class folder in .)sortedosscandirFileNotFoundError	enumerate)r   classesicls_nameclass_to_idxs        r   find_classesr/   $   sc    
 URZZ	-BUUG"DYKq QRR3<W3EF3EKAHK3ELF   Gs   Ar.   is_valid_fileallow_emptyc                   ^ [         R                  R                  U 5      n Uc  [        U 5      u  pQOU(       d  [	        S5      eTSL =(       a    USL nTSL=(       a    USLnU(       d  U(       a  [	        S5      eTb  S[
        S[        4U4S jjn[        [        [
        /[        4   U5      n/ n[        5       n	[        UR                  5       5       H  n
X   n[         R                  R                  X
5      n[         R                  R                  U5      (       d  ML  [        [         R                  " USS95       Hs  u  pn[        U5       H^  n[         R                  R                  X5      nU" U5      (       d  M1  UU4nUR                  U5        X;  d  MM  U	R!                  U
5        M`     Mu     M     [        UR                  5       5      U	-
  nU(       aa  U(       dZ  S	S
R                  [        U5      5       S3nTb.  US[#        T[
        5      (       a  TOS
R                  T5       3-  n[%        U5      eU$ )zGenerates a list of samples of a form (path_to_sample, class).

See :class:`DatasetFolder` for details.

Note: The class_to_idx parameter is here optional and will use the logic of the ``find_classes`` function
by default.
NzE'class_to_index' must have at least one entry to collect any samples.zMBoth extensions and is_valid_file cannot be None or not None at the same timexr   c                    > [        U T5      $ r   )r   )r3   r   s    r   r0   #make_dataset.<locals>.is_valid_fileM   s    -a<<r   T)followlinksz$Found no valid file for the classes z, z. zSupported extensions are: )r'   path
expanduserr/   
ValueErrorr   boolr   r   setr&   keysjoinisdirwalkappendaddr   r)   )r   r.   r   r0   r1   _	both_noneboth_something	instancesavailable_classestarget_classclass_index
target_dirrootfnamesfnamer7   itemempty_classesmsgs     `                 r   make_datasetrP   1   s    ""9-I&y1<`aad"<}'<It+IT0INNhii	=S 	=T 	= 3%+.>MI|0023"0WW\\):
ww}}Z((%bggjd&KLODVww||D0 &&,D$$T*#<)--l; (  M 4 ))+,/@@M[4TYYvm?T5U4VVXY!/jUX>Y>Y
_c_h_his_t/uvvC$$r   c                   ~  ^  \ rS rSrSr     SS\\\4   S\\/\	4   S\
\\S4      S\
\   S	\
\   S
\
\\/\4      S\SS4U 4S jjjr\   SS\\\4   S\\\4   S\
\\S4      S
\
\\/\4      S\S\\\\4      4S jj5       rS\\\4   S\\\   \\\4   4   4S jrS\S\\	\	4   4S jrS\4S jrSrU =r$ )DatasetFolderm   a0  A generic data loader.

This default directory structure can be customized by overriding the
:meth:`find_classes` method.

Args:
    root (str or ``pathlib.Path``): Root directory path.
    loader (callable): A function to load a sample given its path.
    extensions (tuple[string]): A list of allowed extensions.
        both extensions and is_valid_file should not be passed.
    transform (callable, optional): A function/transform that takes in
        a sample and returns a transformed version.
        E.g, ``transforms.RandomCrop`` for images.
    target_transform (callable, optional): A function/transform that takes
        in the target and transforms it.
    is_valid_file (callable, optional): A function that takes path of a file
        and check if the file is a valid file (used to check of corrupt files)
        both extensions and is_valid_file should not be passed.
    allow_empty(bool, optional): If True, empty folders are considered to be valid classes.
        An error is raised on empty folders if False (default).

 Attributes:
    classes (list): List of the class names sorted alphabetically.
    class_to_idx (dict): Dict with items (class_name, class_index).
    samples (list): List of (sample path, class_index) tuples
    targets (list): The class_index value for each image in the dataset
NrJ   loaderr   .	transformtarget_transformr0   r1   r   c                   > [         TU ]  XUS9  U R                  U R                  5      u  pU R	                  U R                  U	UUUS9n
X l        X0l        Xl        Xl        Xl	        U
 Vs/ s H  oS   PM	     snU l
        g s  snf )N)rU   rV   )r.   r   r0   r1   r
   )super__init__r/   rJ   rP   rT   r   r+   r.   samplestargets)selfrJ   rT   r   rU   rV   r0   r1   r+   r.   rZ   s	__class__s               r   rY   DatasetFolder.__init__   s     	EUV $ 1 1$)) <##II%!'# $ 
 $(&-.g!g..s   ,Br   r.   c                 4    Uc  [        S5      e[        XX#US9$ )a  Generates a list of samples of a form (path_to_sample, class).

This can be overridden to e.g. read files from a compressed zip file instead of from the disk.

Args:
    directory (str): root dataset directory, corresponding to ``self.root``.
    class_to_idx (Dict[str, int]): Dictionary mapping class name to class index.
    extensions (optional): A list of allowed extensions.
        Either extensions or is_valid_file should be passed. Defaults to None.
    is_valid_file (optional): A function that takes path of a file
        and checks if the file is a valid file
        (used to check of corrupt files) both extensions and
        is_valid_file should not be passed. Defaults to None.
    allow_empty(bool, optional): If True, empty folders are considered to be valid classes.
        An error is raised on empty folders if False (default).

Raises:
    ValueError: In case ``class_to_idx`` is empty.
    ValueError: In case ``extensions`` and ``is_valid_file`` are None or both are not None.
    FileNotFoundError: In case no valid file was found for any class.

Returns:
    List[Tuple[str, int]]: samples of a form (path_to_sample, class)
z*The class_to_idx parameter cannot be None.)r   r0   r1   )r9   rP   )r   r.   r   r0   r1   s        r   rP   DatasetFolder.make_dataset   s.    @  IJJ
ep
 	
r   c                     [        U5      $ )u  Find the class folders in a dataset structured as follows::

    directory/
    ├── class_x
    │   ├── xxx.ext
    │   ├── xxy.ext
    │   └── ...
    │       └── xxz.ext
    └── class_y
        ├── 123.ext
        ├── nsdf3.ext
        └── ...
        └── asd932_.ext

This method can be overridden to only consider
a subset of classes, or to adapt to a different dataset directory structure.

Args:
    directory(str): Root directory path, corresponding to ``self.root``

Raises:
    FileNotFoundError: If ``dir`` has no class folders.

Returns:
    (Tuple[List[str], Dict[str, int]]): List of all classes and dictionary mapping each class to an index.
)r/   )r\   r   s     r   r/   DatasetFolder.find_classes   s    6 I&&r   indexc                     U R                   U   u  p#U R                  U5      nU R                  b  U R                  U5      nU R                  b  U R                  U5      nXC4$ )zu
Args:
    index (int): Index

Returns:
    tuple: (sample, target) where target is class_index of the target class.
)rZ   rT   rU   rV   )r\   rd   r7   targetsamples        r   __getitem__DatasetFolder.__getitem__   s^     ||E*T">>%^^F+F  ,**62F~r   c                 ,    [        U R                  5      $ r   )lenrZ   )r\   s    r   __len__DatasetFolder.__len__   s    4<<  r   )r.   r+   r   rT   rZ   r[   )NNNNF)NNF)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r:   rY   staticmethoddictintlistrP   r/   rh   rl   __static_attributes____classcell__r^   s   @r   rR   rR   m   s   @ 15(,/39=!/CI/ #$/ U38_-	/
 H%/ #8,/  # 56/ / 
/ /8  159=!&
d#&
38n&
 U38_-&
  # 56	&

 &
 
eCHo	&
 &
P'eCI&6 '5cDQTVYQYNAZ;[ ': sCx "! ! !r   rR   )	z.jpgz.jpegz.pngz.ppmz.bmpz.pgmz.tifz.tiffz.webpr7   c                     [        U S5       n[        R                   " U5      nUR                  S5      sS S S 5        $ ! , (       d  f       g = f)NrbRGB)openr	   convert)r7   fimgs      r   
pil_loaderr     s3    	dD	Qjjm{{5! 
		s	   '>
Ac                 d    SS K n UR                  U 5      $ ! [         a    [        U 5      s $ f = f)Nr   )accimager	   OSErrorr   )r7   r   s     r   accimage_loaderr     s3     ~~d##  $ s    //c                 P    SSK Jn  U" 5       S:X  a  [        U 5      $ [        U 5      $ )Nr   )get_image_backendr   )torchvisionr   r   r   )r7   r   s     r   default_loaderr     s&    -j(t$$$r   c                      ^  \ rS rSrSrSS\SS4S\\\4   S\	\
   S\	\
   S\
\/\4   S	\	\
\/\4      S
\4U 4S jjjrSrU =r$ )ImageFolderi  aB  A generic data loader where the images are arranged in this way by default: ::

    root/dog/xxx.png
    root/dog/xxy.png
    root/dog/[...]/xxz.png

    root/cat/123.png
    root/cat/nsdf3.png
    root/cat/[...]/asd932_.png

This class inherits from :class:`~torchvision.datasets.DatasetFolder` so
the same methods can be overridden to customize the dataset.

Args:
    root (str or ``pathlib.Path``): Root directory path.
    transform (callable, optional): A function/transform that takes in a PIL image or torch.Tensor, depends on the given loader,
        and returns a transformed version. E.g, ``transforms.RandomCrop``
    target_transform (callable, optional): A function/transform that takes in the
        target and transforms it.
    loader (callable, optional): A function to load an image given its path.
    is_valid_file (callable, optional): A function that takes path of an Image file
        and check if the file is a valid file (used to check of corrupt files)
    allow_empty(bool, optional): If True, empty folders are considered to be valid classes.
        An error is raised on empty folders if False (default).

 Attributes:
    classes (list): List of the class names sorted alphabetically.
    class_to_idx (dict): Dict with items (class_name, class_index).
    imgs (list): List of (image path, class_index) tuples
NFrJ   rU   rV   rT   r0   r1   c           
      `   > [         TU ]  UUUc  [        OS UUUUS9  U R                  U l        g )N)rU   rV   r0   r1   )rX   rY   r   rZ   imgs)r\   rJ   rU   rV   rT   r0   r1   r^   s          r   rY   ImageFolder.__init__?  s@     	+3N-'# 	 	
 LL	r   )r   )rn   ro   rp   rq   rr   r   r   r   r   r   r   r   r:   rY   rw   rx   ry   s   @r   r   r     s    D )-/3'59=!!CI! H%! #8,	!
 #$!  # 56! ! !r   r   )NNNF)r'   os.pathpathlibr   typingr   r   r   r   r   PILr	   visionr   r   r   r:   r   r   rv   rt   ru   r/   rP   rR   r   r   r   r   r    r   r   <module>r      s   	   7 7  !
g 
g%U3PS8_@T:U 
gZ^ 
g	@C 	@D 	@
!E#t), 
!tCy$sCx.7P1Q 
! .28<599S$Y94S>*9 sE#s(O3459 HcUD[12	9
 9 
%S/9xQ!M Q!h ]"U39% "%++ " %T	*  s   sDy)  c  2!- 2!r   