o
    #j.                     @   sl   d dl Z d dlZd dlZd dlmZ d dlZd dlmZ d dl	m
Z
 g ZG dd de
ZG dd deZdS )	    N)Image)_check_exists_and_download)Datasetc                   @   sx   e Zd ZdZdZdZed ZdZed ZdZ	ed Z
d	Zed
 ZdZ						dddZdddZdd Zdd ZdS )MNISTa4
  
    Implementation of `MNIST <http://yann.lecun.com/exdb/mnist/>`_ dataset.

    Args:
        image_path (str, optional): Path to image file, can be set None if
            :attr:`download` is True. Default: None, default data path: ~/.cache/paddle/dataset/mnist.
        label_path (str, optional): Path to label file, can be set None if
            :attr:`download` is True. Default: None, default data path: ~/.cache/paddle/dataset/mnist.
        mode (str, optional): Either train or test mode. Default 'train'.
        transform (Callable, optional): Transform to perform on image, None for no transform. Default: None.
        download (bool, optional): Download dataset automatically if
            :attr:`image_path` :attr:`label_path` is not set. Default: True.
        backend (str, optional): Specifies which type of image to be returned:
            PIL.Image or numpy.ndarray. Should be one of {'pil', 'cv2'}.
            If this option is not set, will get backend from :ref:`paddle.vision.get_image_backend <api_paddle_vision_get_image_backend>`,
            default backend is 'pil'. Default: None.

    Returns:
        :ref:`api_paddle_io_Dataset`. An instance of MNIST dataset.

    Examples:

        .. code-block:: python

            >>> import itertools
            >>> import paddle.vision.transforms as T
            >>> from paddle.vision.datasets import MNIST


            >>> mnist = MNIST()
            >>> print(len(mnist))
            60000

            >>> for i in range(5):  # only show first 5 images
            ...     img, label = mnist[i]
            ...     # do something with img and label
            ...     print(type(img), img.size, label)
            ...     # <class 'PIL.Image.Image'> (28, 28) [5]


            >>> transform = T.Compose(
            ...     [
            ...         T.ToTensor(),
            ...         T.Normalize(
            ...             mean=[127.5],
            ...             std=[127.5],
            ...         ),
            ...     ]
            ... )

            >>> mnist_test = MNIST(
            ...     mode="test",
            ...     transform=transform,  # apply transform to every image
            ...     backend="cv2",  # use OpenCV as image transform backend
            ... )
            >>> print(len(mnist_test))
            10000

            >>> for img, label in itertools.islice(iter(mnist_test), 5):  # only show first 5 images
            ...     # do something with img and label
            ...     print(type(img), img.shape, label)
            ...     # <class 'paddle.Tensor'> [1, 28, 28] [7]
    Zmnistz$https://dataset.bj.bcebos.com/mnist/t10k-images-idx3-ubyte.gzZ 9fb629c4189551a2d022fa330f9573f3t10k-labels-idx1-ubyte.gzZ ec29112dd5afa0611ce80d1b7f02629ctrain-images-idx3-ubyte.gzZ f68b3c2dcbeaaa9fbdd348bbdeb94873train-labels-idx1-ubyte.gzZ d53e105ee54ea40749a09fcbcd1e9432NtrainTc                 C   s"  |  dv sJ d| |d u rtj }|dvr!td| || _|  | _|| _| jd u rU|s7J d|dkr>| jn| j	}|dkrH| j
n| j}t|||| j|| _|| _| jd u r|scJ d| jdkrk| jn| j}	| jdkrv| jn| j}
t||	|
| j|| _|| _|   t | _d S )N)r
   testz*mode should be 'train' or 'test', but got )pilZcv2z4Expected backend are one of ['pil', 'cv2'], but got z?image_path is not set and downloading automatically is disabledr
   z?label_path is not set and downloading automatically is disabled)lowerpaddleZvisionZget_image_backend
ValueErrorbackendmode
image_pathTRAIN_IMAGE_URLTEST_IMAGE_URLTRAIN_IMAGE_MD5TEST_IMAGE_MD5r   NAME
label_pathTRAIN_LABEL_URLTEST_LABEL_URLTRAIN_LABEL_MD5TEST_LABEL_MD5	transform_parse_datasetZget_default_dtypedtype)selfr   r   r   r   downloadr   Z	image_urlZ	image_md5Z	label_urlZ	label_md5 r"   ]/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/vision/datasets/mnist.py__init__h   sX   	





zMNIST.__init__d   c              	   C   s  g | _ g | _t| jd}| }t| jd}| }d}d}d}t|||\}	}
}}|t	|7 }d}d}t|||\}}|t	|7 }	 ||krPnddt
| d }t|||}|t	|7 }||7 }dt
|| |  d }t|||}t|||| fd}|t	|7 }t|D ]}| j ||d d f  | jt|| gd	 qqKW d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )
Nrbr   z>IIIIz>IIT>BZfloat32int64)imageslabelsgzipGzipFiler   readr   structunpack_fromcalcsizestrnpreshapeastyperangeappendarray)r    buffer_sizeZ
image_fileZimg_bufZ
label_fileZlab_bufZ
step_labelZ
offset_imgZmagic_byte_imgZ	magic_imgZ	image_numrowscolsZ
offset_labZmagic_byte_labZ	magic_labZ	label_numZ	fmt_labelr+   Z
fmt_imagesZimages_tempr*   ir"   r"   r#   r      s^   "zMNIST._parse_datasetc                 C   s   | j | | j| }}t|ddg}| jdkr"tj|ddd}| jd ur,| |}| jdkr8||dfS || j	|dfS )N   r   Zuint8L)r   r)   )
r*   r+   r3   r4   r   r   Z	fromarrayr5   r   r   )r    idximagelabelr"   r"   r#   __getitem__   s   



zMNIST.__getitem__c                 C   s
   t | jS )N)lenr+   )r    r"   r"   r#   __len__   s   
zMNIST.__len__)NNr
   NTN)r%   )__name__
__module____qualname____doc__r   
URL_PREFIXr   r   r   r   r   r   r   r   r$   r   rB   rD   r"   r"   r"   r#   r      s,    @

@2r   c                   @   sH   e Zd ZdZdZdZed ZdZed ZdZ	ed Z
d	Zed
 ZdZdS )FashionMNISTa
  
    Implementation of `Fashion-MNIST <https://github.com/zalandoresearch/fashion-mnist>`_ dataset.

    Args:
        image_path (str, optional): Path to image file, can be set None if
            :attr:`download` is True. Default: None, default data path: ~/.cache/paddle/dataset/fashion-mnist.
        label_path (str, optional): Path to label file, can be set None if
            :attr:`download` is True. Default: None, default data path: ~/.cache/paddle/dataset/fashion-mnist.
        mode (str, optional): Either train or test mode. Default 'train'.
        transform (Callable, optional): Transform to perform on image, None for no transform. Default: None.
        download (bool, optional): Whether to download dataset automatically if
            :attr:`image_path` :attr:`label_path` is not set. Default: True.
        backend (str, optional): Specifies which type of image to be returned:
            PIL.Image or numpy.ndarray. Should be one of {'pil', 'cv2'}.
            If this option is not set, will get backend from :ref:`paddle.vision.get_image_backend <api_paddle_vision_get_image_backend>`,
            default backend is 'pil'. Default: None.

    Returns:
        :ref:`api_paddle_io_Dataset`. An instance of FashionMNIST dataset.

    Examples:

        .. code-block:: python

            >>> import itertools
            >>> import paddle.vision.transforms as T
            >>> from paddle.vision.datasets import FashionMNIST


            >>> fashion_mnist = FashionMNIST()
            >>> print(len(fashion_mnist))
            60000

            >>> for i in range(5):  # only show first 5 images
            ...     img, label = fashion_mnist[i]
            ...     # do something with img and label
            ...     print(type(img), img.size, label)
            ...     # <class 'PIL.Image.Image'> (28, 28) [9]


            >>> transform = T.Compose(
            ...     [
            ...         T.ToTensor(),
            ...         T.Normalize(
            ...             mean=[127.5],
            ...             std=[127.5],
            ...         ),
            ...     ]
            ... )

            >>> fashion_mnist_test = FashionMNIST(
            ...     mode="test",
            ...     transform=transform,  # apply transform to every image
            ...     backend="cv2",  # use OpenCV as image transform backend
            ... )
            >>> print(len(fashion_mnist_test))
            10000

            >>> for img, label in itertools.islice(iter(fashion_mnist_test), 5):  # only show first 5 images
            ...     # do something with img and label
            ...     print(type(img), img.shape, label)
            ...     # <class 'paddle.Tensor'> [1, 28, 28] [9]
    zfashion-mnistz,https://dataset.bj.bcebos.com/fashion_mnist/r   Z bef4ecab320f06d8554ea6380940ec79r   Z bb300cfdad3c16e7a12a480ee83cd310r   Z 8d4fb7e6c68d591d4c3dfef9ec88bf0dr	   Z 25c81989df183df01b3e8a0aad5dffbeN)rE   rF   rG   rH   r   rI   r   r   r   r   r   r   r   r   r"   r"   r"   r#   rJ      s    @rJ   )r,   r/   numpyr3   ZPILr   r   Zpaddle.dataset.commonr   Z	paddle.ior   __all__r   rJ   r"   r"   r"   r#   <module>   s    R