o
    + i2                     @  s   d dl mZ d dlmZmZmZ er0d dlmZ d dlm	Z	 d dl
mZ ddlmZmZ ed Z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ed  ZG dd deZdS )    )annotations)TYPE_CHECKINGAnyLiteralN)_DTypeLiteral)
_Transform   )_ImageBackend_ImageDataTypetraintest)Image)_check_exists_and_download)Datasetc                   @  s   e Zd ZU 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ed< ded< ded< ded< ded< ded< ded< ded< 						d.d/d"d#Zd0d%d&Zd1d*d+Zd2d,d-ZdS )3MNISTaX
  
    Implementation of `MNIST <http://yann.lecun.com/exdb/mnist/>`_ dataset.

    Args:
        image_path (str|None, 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|None, 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|None, 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|None, 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)  # type: ignore
            ...     # <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 d53e105ee54ea40749a09fcbcd1e9432_DatasetModemode
str | None
image_path
label_path_Transform[Any, Any] | None	transformr	   backendr   dtypelistlabelsimagesNr   Tdownloadbool_ImageBackend | NonereturnNonec                 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 )Nr   z*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
ValueErrorr   r   r   TRAIN_IMAGE_URLTEST_IMAGE_URLTRAIN_IMAGE_MD5TEST_IMAGE_MD5r   NAMEr   TRAIN_LABEL_URLTEST_LABEL_URLTRAIN_LABEL_MD5TEST_LABEL_MD5r   _parse_datasetZget_default_dtyper   )selfr   r   r   r   r"   r   Z	image_urlZ	image_md5Z	label_urlZ	label_md5 r6   h/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/vision/datasets/mnist.py__init__~   sT   	





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)r!   r    gzipGzipFiler   readr   structunpack_fromcalcsizestrnpreshapeastyperangeappendarray)r5   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!   ir6   r6   r7   r4      s^   "zMNIST._parse_datasetidxint,tuple[_ImageDataType, npt.NDArray[np.int64]]c                 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    rE   rF   r   r   Z	fromarrayrG   r   r   )r5   rO   imagelabelr6   r6   r7   __getitem__   s   



zMNIST.__getitem__c                 C  s
   t | jS )N)lenr    )r5   r6   r6   r7   __len__  s   
zMNIST.__len__)NNr   NTN)r   r   r   r   r   r   r   r   r"   r#   r   r$   r%   r&   )r9   )rO   rP   r%   rQ   )r%   rP   )__name__
__module____qualname____doc__r/   
URL_PREFIXr,   r.   r1   r3   r+   r-   r0   r2   __annotations__r8   r4   rV   rX   r6   r6   r6   r7   r   )   s<   
 @
@
2r   )r
   znpt.NDArray[np.int64]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)  # type: ignore
            ...     # <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)rY   rZ   r[   r\   r/   r]   r,   r.   r1   r3   r+   r-   r0   r2   r6   r6   r6   r7   r_     s    @r_   )
__future__r   typingr   r   r   Znumpy.typingZnptZpaddle._typing.dtype_liker   Z#paddle.vision.transforms.transformsr   rT   r	   r
   r   r>   rA   numpyrE   ZPILr   r)   Zpaddle.dataset.commonr   Z	paddle.ior   __all__tupler   r_   r6   r6   r6   r7   <module>   s&    ]