o
    piY                     @   s$  d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ g Zd	Zd
ZdZdZdZdZdZdZdZdd ZeedZeedZ			d$ddZedddddedddfddZedddddedddfdd Z edddddeddfd!dZ!d"d# Z"dS )%a  
This module will download dataset from
http://www.robots.ox.ac.uk/~vgg/data/flowers/102/index.html
and parse train/test dataset into paddle reader creators.

This set contains images of flowers belonging to 102 different categories.
The images were acquired by searching the web and taking pictures. There are a
minimum of 40 images for each category.

The database was used in:

Nilsback, M-E. and Zisserman, A. Automated flower classification over a large
 number of classes.Proceedings of the Indian Conference on Computer Vision,
Graphics and Image Processing (2008)
http://www.robots.ox.ac.uk/~vgg/publications/papers/nilsback08.{pdf,ps.gz}.

    N)	cpu_count)load_image_bytessimple_transform)map_readersxmap_readers)
deprecated
try_import   )downloadz8http://paddlemodels.bj.bcebos.com/flowers/102flowers.tgzz9http://paddlemodels.bj.bcebos.com/flowers/imagelabels.matz3http://paddlemodels.bj.bcebos.com/flowers/setid.matZ 52808999861908f626f3c1f4e79d11faZ e0620be6f572b9609742df49c70aed4dZ a5357ecc9cb78c4bef273ce3793fc85cZtstidZtrnidvalidc                 C   s8   |\}}t |}t|dd| g dd}| d|fS )zB
    map image bytes data to type needed by model input layer
          )g\(Y@gR1]@gQ^@)meanZfloat32)r   r   flattenZastype)Zis_trainsampleimglabel r   ]/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/dataset/flowers.pydefault_mapper:   s   r   TF   c           	         s6    fdd}|rt ||tdt |S t||S )a  
    1. read images from tar file and
        merge images into batch files in 102flowers.tgz_batch/
    2. get a reader to read sample from batch file

    :param data_file: downloaded data file
    :type data_file: string
    :param label_file: downloaded label file
    :type label_file: string
    :param setid_file: downloaded setid file containing information
                        about how to split dataset
    :type setid_file: string
    :param dataset_name: data set name (tstid|trnid|valid)
    :type dataset_name: string
    :param mapper: a function to map image bytes data to type
                    needed by model input layer
    :type mapper: callable
    :param buffered_size: the size of buffer used to process images
    :type buffered_size: int
    :param cycle: whether to cycle through the dataset
    :type cycle: bool
    :return: data reader
    :rtype: callable
    c                  3   s    t d} | d d }|  d }i }|D ]}d|dd}||d  ||< qt }| }d}|D ]}	|	j|v rV||	 }
||	j }|
t|d fV  q:d S )Nzscipy.iolabelsr   z
jpg/image_Z05z.jpgr	   )	r   Zloadmattarfileopen
getmembersnameextractfilereadint)Zscior   ZindexesZ	img2labelir   tfZmemsZfile_idZmemimager   	data_filedataset_name
label_file
setid_filer   r   readerm   s$   


zreader_creator.<locals>.reader   )r   minr   r   )	r#   r%   r&   r$   mapperbuffered_sizeuse_xmapcycler'   r   r"   r   reader_creatorJ   s   #
r.   z2.0.0zpaddle.vision.datasets.Flowersz>Please use new dataset API which supports paddle.io.DataLoader)ZsinceZ	update_tolevelreasonc              
   C   0   t ttdtttdtttdtt| |||dS )a8  
    Create flowers training set reader.
    It returns a reader, each sample in the reader is
    image pixels in [0, 1] and label in [1, 102]
    translated from original color image by steps:
    1. resize to 256*256
    2. random crop to 224*224
    3. flatten
    :param mapper:  a function to map sample.
    :type mapper: callable
    :param buffered_size: the size of buffer used to process images
    :type buffered_size: int
    :param cycle: whether to cycle through the dataset
    :type cycle: bool
    :return: train data reader
    :rtype: callable
    flowersr-   )	r.   r
   DATA_URLDATA_MD5	LABEL_URL	LABEL_MD5	SETID_URL	SETID_MD5
TRAIN_FLAGr*   r+   r,   r-   r   r   r   train      


r<   c              
   C   r1   )a3  
    Create flowers test set reader.
    It returns a reader, each sample in the reader is
    image pixels in [0, 1] and label in [1, 102]
    translated from original color image by steps:
    1. resize to 256*256
    2. random crop to 224*224
    3. flatten
    :param mapper:  a function to map sample.
    :type mapper: callable
    :param buffered_size: the size of buffer used to process images
    :type buffered_size: int
    :param cycle: whether to cycle through the dataset
    :type cycle: bool
    :return: test data reader
    :rtype: callable
    r2   r3   )	r.   r
   r4   r5   r6   r7   r8   r9   	TEST_FLAGr;   r   r   r   test   r=   r?   c                 C   s,   t ttdtttdtttdtt| ||S )a  
    Create flowers validation set reader.
    It returns a reader, each sample in the reader is
    image pixels in [0, 1] and label in [1, 102]
    translated from original color image by steps:
    1. resize to 256*256
    2. random crop to 224*224
    3. flatten
    :param mapper:  a function to map sample.
    :type mapper: callable
    :param buffered_size: the size of buffer used to process images
    :type buffered_size: int
    :return: test data reader
    :rtype: callable
    r2   )	r.   r
   r4   r5   r6   r7   r8   r9   
VALID_FLAG)r*   r+   r,   r   r   r   r      s   


c                   C   s(   t tdt t tdt t tdt d S )Nr2   )r
   r4   r5   r6   r7   r8   r9   r   r   r   r   fetch   s   rA   )r   TF)#__doc__	functoolsr   multiprocessingr   Zpaddle.dataset.imager   r   Zpaddle.readerr   r   Zpaddle.utilsr   r   commonr
   __all__r4   r6   r8   r5   r7   r9   r:   r>   r@   r   partialZtrain_mapperZtest_mapperr.   r<   r?   r   rA   r   r   r   r   <module>   s^   	
=