
    Ph}                     f    d dl Z d dlmZmZmZmZ d dlmZ ddlm	Z	m
Z
mZ ddlmZ  G d de      Zy)	    N)AnyCallableOptionalTuple)Image   )_decompressdownload_file_from_google_driveverify_str_arg)VisionDatasetc                        e Zd ZdZdddddddddd	Z	 	 	 	 dd
ed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 )PCAMa2  `PCAM Dataset   <https://github.com/basveeling/pcam>`_.

    The PatchCamelyon dataset is a binary classification dataset with 327,680
    color images (96px x 96px), extracted from histopathologic scans of lymph node
    sections. Each image is annotated with a binary label indicating presence of
    metastatic tissue.

    This dataset requires the ``h5py`` package which you can install with ``pip install h5py``.

    Args:
         root (string): Root directory of the dataset.
         split (string, optional): The dataset split, supports ``"train"`` (default), ``"test"`` or ``"val"``.
         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.
         download (bool, optional): If True, downloads the dataset from the internet and puts it into ``root/pcam``. If
             dataset is already downloaded, it is not downloaded again.
    )z&camelyonpatch_level_2_split_train_x.h5z!1Ka0XfEMiwgCYPdTI-vv6eUElOBnKFKQ2 1571f514728f59376b705fc836ff4b63)z&camelyonpatch_level_2_split_train_y.h5z!1269yhu3pZDP8UYFQs-NYs3FPwuK-nGSG 35c2d7259d906cfc8143347bb8e05be7)imagestargets)z%camelyonpatch_level_2_split_test_x.h5!1qV65ZqZvWzuIVthK8eVDhIwrbnsJdbg_ d8c2d60d490dbd479f8199bdfa0cf6ec)z%camelyonpatch_level_2_split_test_y.h5!17BHrSrwWKjYsOgTMmoqrIjDy6Fa2o_gP 60a7035772fbdb7f34eb86d4420cf66a)z&camelyonpatch_level_2_split_valid_x.h5z!1hgshYGWK8V-eGRy8LToWJJgDU_rXWVJ3 d5b63470df7cfa627aeec8b9dc0c066e)z&camelyonpatch_level_2_split_valid_y.h5z!1bH8ZRbhSVAhScTS0p9-ZzGnX91cHT3uO 2b85f58b927af9964a4c15b8f7e8f179traintestvalrootsplit	transformtarget_transformdownloadc                 >   	 dd l }|| _         t        |dd      | _        t
        |   |||       t        j                  | j                        dz  | _
        |r| j                          | j                         st        d      y # t        $ r t        d      w xY w)Nr   zYh5py is not found. This dataset needs to have h5py installed: please run pip install h5pyr   r   )r   r    pcamz;Dataset not found. You can use download=True to download it)h5pyImportErrorRuntimeErrorr   _splitsuper__init__pathlibPathr   _base_folder	_download_check_exists)selfr   r   r   r    r!   r$   	__class__s          dC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torchvision/datasets/pcam.pyr)   zPCAM.__init__E   s    	DI %UG5MNEUV#LL3f<NN!!#\]] $  	k 	s   B Breturnc                     | j                   | j                     d   d   }| j                  j                  | j                  |z        5 }|d   j
                  d   cd d d        S # 1 sw Y   y xY w)Nr   r   x)_FILESr'   r$   Filer,   shape)r/   images_fileimages_datas      r1   __len__zPCAM.__len__a   sZ    kk$++.x8;YY^^D--;<s#))!, =<<s   A$$A-idxc                 l   | j                   | j                     d   d   }| j                  j                  | j                  |z        5 }t        j                  |d   |         j                  d      }d d d        | j                   | j                     d   d   }| j                  j                  | j                  |z        5 }t        |d   |dddf         }d d d        | j                  r| j                        }| j                  r| j                        }fS # 1 sw Y   xY w# 1 sw Y   SxY w)Nr   r   r4   RGBr   y)r5   r'   r$   r6   r,   r   	fromarrayconvertintr   r    )r/   r;   r8   r9   imagetargets_filetargets_datatargets           r1   __getitem__zPCAM.__getitem__f   s   kk$++.x8;YY^^D--;<OOK$4S$9:BB5IE = {{4;;/	:1=YY^^D--<=c*31a<89F > >>NN5)E  **62Ff} =< >=s   +DD*D'*D3c                       j                    j                     d   d   } j                    j                     d   d   }t         fd||fD              S )Nr   r   r   c              3   p   K   | ]-  }j                   j                  |      j                          / y w)N)r,   joinpathexists).0h5_filer/   s     r1   	<genexpr>z%PCAM._check_exists.<locals>.<genexpr>y   s.     kOjG4$$--g6==?Ojs   36)r5   r'   all)r/   r8   rC   s   `  r1   r.   zPCAM._check_existsv   sS    kk$++.x8;{{4;;/	:1=kP[]iOjkkk    c                    | j                         ry | j                  | j                     j                         D ]N  \  }}}|dz   }t	        |t        | j                        ||       t        t        | j                  |z               P y )Nz.gz)filenamemd5)r.   r5   r'   valuesr
   strr,   r	   )r/   	file_namefile_idrR   archive_names        r1   r-   zPCAM._download{   sv    '+{{4;;'?'F'F'H#Iw$u,L+GS9J9J5KVbhklD--<=> (IrO   )r   NNF)r2   N)__name__
__module____qualname____doc__r5   rT   r   r   boolr)   rA   r:   r   r   rF   r.   r-   __classcell__)r0   s   @r1   r   r   
   s    *





3%FT (,/3^^ ^ H%	^
 #8,^ ^8- -
s uS#X  lt l
?rO   r   )r*   typingr   r   r   r   PILr   utilsr	   r
   r   visionr   r    rO   r1   <module>rc      s(     1 1  O O !x?= x?rO   