
    x h@                     ~    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 ddlmZ  G d d	e      Zy)
    N)Sequence)AnyCallableOptionalUnion)Image   )download_and_extract_archiveverify_str_arg)VisionDatasetc                        e Zd ZdZdZdZ	 	 	 	 	 	 ddeeej                  f   dedee
e   ef   dee   dee   d	ee   d
ef fdZdefdZdedeeef   fdZdefdZddZ xZS )OxfordIIITPeta  `Oxford-IIIT Pet Dataset   <https://www.robots.ox.ac.uk/~vgg/data/pets/>`_.

    Args:
        root (str or ``pathlib.Path``): Root directory of the dataset.
        split (string, optional): The dataset split, supports ``"trainval"`` (default) or ``"test"``.
        target_types (string, sequence of strings, optional): Types of target to use. Can be ``category`` (default) or
            ``segmentation``. Can also be a list to output a tuple with all specified target types. The types represent:

                - ``category`` (int): Label for one of the 37 pet categories.
                - ``binary-category`` (int): Binary label for cat or dog.
                - ``segmentation`` (PIL image): Segmentation trimap of the image.

            If empty, ``None`` will be returned as target.

        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.
        download (bool, optional): If True, downloads the dataset from the internet and puts it into
            ``root/oxford-iiit-pet``. If dataset is already downloaded, it is not downloaded again.
    ))z=https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz 5c4f3ee8e5d25df40f4fd59a7f44e54c)zBhttps://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz 95a8c909bbe2e81eed6a22bccdf3f68f)categorybinary-categorysegmentationrootsplittarget_types
transforms	transformtarget_transformdownloadc           
      ~   t        |dd      | _        t        |t              r|g}|D cg c]  }t        |d| j                         c}| _        t        |   ||||       t        j                  | j                        dz  | _        | j                  dz  | _        | j                  dz  | _        | j                  dz  | _        |r| j                          | j!                         st#        d	      g }	g | _        g | _        t)        | j                  | j                   d
z        5 }
|
D ]  }|j+                         j-                         \  }}}}|	j/                  |       | j$                  j/                  t1        |      dz
         | j&                  j/                  t1        |      dz
          	 d d d        ddg| _        t5        t7        |	| j$                        D ch c]  \  }}|j9                  dd      d   |f c}}d       D cg c],  \  }}dj;                  d |j-                  d      D              . c}}| _        t?        t7        | j2                  tA        tC        | j2                                          | _"        t?        t7        | j<                  tA        tC        | j<                                          | _#        |	D cg c]  }| j                  | dz   c}| _$        |	D cg c]  }| j                  | dz   c}| _%        y c c}w # 1 sw Y   jxY wc c}}w c c}}w c c}w c c}w )Nr   )trainvaltestr   )r   r   r   zoxford-iiit-petimagesannotationstrimapsz;Dataset not found. You can use download=True to download itz.txtr	   CatDog_r   c                     | d   S )Nr	    )image_id_and_labels    /home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/torchvision/datasets/oxford_iiit_pet.py<lambda>z(OxfordIIITPet.__init__.<locals>.<lambda>W   s    /A!/D     )key c              3   <   K   | ]  }|j                           y wN)title).0parts     r'   	<genexpr>z)OxfordIIITPet.__init__.<locals>.<genexpr>T   s     AdTZZ\As   z.jpgz.png)&r   _split
isinstancestr_VALID_TARGET_TYPES_target_typessuper__init__pathlibPathr   _base_folder_images_folder_anns_folder_segs_folder	_download_check_existsRuntimeError_labels_bin_labelsopenstripr   appendintbin_classessortedziprsplitjoinclassesdictrangelenbin_class_to_idxclass_to_idx_images_segs)selfr   r   r   r   r   r   r   target_type	image_idsfilelineimage_idlabel	bin_labelr#   raw_cls	__class__s                    r'   r8   zOxfordIIITPet.__init__+   s    %UG5IJlC((>Leq
VaN;8P8PQ
 	*	\lm#LL36GG"//(: --= --	9NN!!#\]]	$##T&::; 	<t <04

0B0B0D-%A  *##CJN3  ''I(:;	<	< "5> %LOPY[_[g[gLhi5(//#q)!,e4iD
 HHAgmmC.@AA
 !%S)9)95TEUEUAV;W%X Y T\\5T\\9J3K!LMPYZH++
$.??ZLUVd''XJd*;;V
M
&	< 	< j
 [Vs*   L)B
L#!L)
1L/L55L:L&returnc                 ,    t        | j                        S r-   )rP   rS   )rU   s    r'   __len__zOxfordIIITPet.__len__`   s    4<<  r)   idxc                    t        j                  | j                  |         j                  d      }g }| j                  D ]{  }|dk(  r|j                  | j                  |          '|dk(  r|j                  | j                  |          K|j                  t        j                  | j                  |                } |sd }nt        |      dk(  r|d   }nt        |      }| j                  r| j                  ||      \  }}||fS )NRGBr   r   r	   r   )r   rD   rS   convertr6   rF   rB   rC   rT   rP   tupler   )rU   rb   imagetargetrV   s        r'   __getitem__zOxfordIIITPet.__getitem__c   s    

4<<,-55e<-- 	;Kj(dll3/0 11d..s34ejjC9:	; F[AAYF6]F?? OOE6:ME6f}r)   c                     | j                   | j                  fD ]B  }t        j                  j	                  |      r t        j                  j                  |      rB y y)NFT)r<   r=   ospathexistsisdir)rU   folders     r'   r@   zOxfordIIITPet._check_exists{   sI    **D,=,=> 	FGGNN6*rww}}V/D	 r)   c                     | j                         ry | j                  D ]&  \  }}t        |t        | j                        |       ( y )N)download_rootmd5)r@   
_RESOURCESr
   r4   r;   )rU   urlrr   s      r'   r?   zOxfordIIITPet._download   sB     	]HC(C@Q@Q<RX[\	]r)   )r   r   NNNF)r_   N)__name__
__module____qualname____doc__rs   r5   r   r4   r9   r:   r   r   r   boolr8   rG   ra   rf   r   ri   r@   r?   __classcell__)r^   s   @r'   r   r      s    .J J
  2<)-(,/33WC%&3W 3W HSM3./	3W
 X&3W H%3W #8,3W 3Wj! !s uS#X 0t ]r)   r   )rk   os.pathr9   collections.abcr   typingr   r   r   r   PILr   utilsr
   r   visionr   r   r%   r)   r'   <module>r      s0    	   $ 1 1  ? !z]M z]r)   