U
    (d                     @   s`   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 deZdS )	    N)AnyCallableOptionalTuple)Image   )download_file_from_google_drive_decompressverify_str_arg)VisionDatasetc                       s   e Zd ZdZdddddddddd	Zdeeee ee ed fddZ	e
dddZe
eeef dddZedddZddddZ  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-vv6eUElOBnKFKQ2Z 1571f514728f59376b705fc836ff4b63)z&camelyonpatch_level_2_split_train_y.h5z!1269yhu3pZDP8UYFQs-NYs3FPwuK-nGSGZ 35c2d7259d906cfc8143347bb8e05be7)imagestargets)z%camelyonpatch_level_2_split_test_x.h5Z!1qV65ZqZvWzuIVthK8eVDhIwrbnsJdbg_Z d5b63470df7cfa627aeec8b9dc0c066e)z%camelyonpatch_level_2_split_test_y.h5Z!17BHrSrwWKjYsOgTMmoqrIjDy6Fa2o_gPZ 2b85f58b927af9964a4c15b8f7e8f179)z&camelyonpatch_level_2_split_valid_x.h5z!1hgshYGWK8V-eGRy8LToWJJgDU_rXWVJ3Z d8c2d60d490dbd479f8199bdfa0cf6ec)z&camelyonpatch_level_2_split_valid_y.h5z!1bH8ZRbhSVAhScTS0p9-ZzGnX91cHT3uOZ 60a7035772fbdb7f34eb86d4420cf66atraintestvalr   NF)rootsplit	transformtarget_transformdownloadc                    s   zdd l }|| _ W n tk
r.   tdY nX t|dd| _t j|||d t| j	d | _
|rn|   |  s~tdd S )Nr   zYh5py is not found. This dataset needs to have h5py installed: please run pip install h5pyr   r   )r   r   Z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__ =/tmp/pip-unpacked-wheel-vx7f76es/torchvision/datasets/pcam.pyr   E   s    

zPCAM.__init__)returnc              
   C   sL   | j | j d d }| j| j| }|d jd W  5 Q R  S Q R X d S )Nr   r   x)_FILESr   r   Filer    shape)r#   images_fileimages_datar&   r&   r'   __len__a   s    zPCAM.__len__)idxr(   c              	   C   s   | j | j d d }| j| j| }t|d | d}W 5 Q R X | j | j d d }| j| j| }t|d |dddf }W 5 Q R X | j	r| 	|}| j
r| 
|}||fS )Nr   r   r)   ZRGBr   y)r*   r   r   r+   r    r   Z	fromarrayconvertintr   r   )r#   r0   r-   r.   imagetargets_fileZtargets_datatargetr&   r&   r'   __getitem__f   s    ""

zPCAM.__getitem__c                    sB    j  j d d } j  j d d }t fdd||fD S )Nr   r   r   c                 3   s   | ]} j | V  qd S )N)r    joinpathexists).0Zh5_filer#   r&   r'   	<genexpr>y   s     z%PCAM._check_exists.<locals>.<genexpr>)r*   r   all)r#   r-   r5   r&   r;   r'   r"   v   s    zPCAM._check_existsc                 C   s\   |   rd S | j| j  D ]:\}}}|d }t|t| j||d tt| j|  qd S )Nz.gz)filenamemd5)r"   r*   r   valuesr   strr    r	   )r#   	file_nameZfile_idr?   archive_namer&   r&   r'   r!   {   s    zPCAM._download)r   NNF)__name__
__module____qualname____doc__r*   rA   r   r   boolr   r3   r/   r   r   r7   r"   r!   __classcell__r&   r&   r$   r'   r   
   s4   *    r   )r   typingr   r   r   r   ZPILr   utilsr   r	   r
   Zvisionr   r   r&   r&   r&   r'   <module>   s
   