o
    Me<"                     @   s  d Z ddlm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
 ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlm  m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!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$Z0edd dd!d"e.dddfd%d&Z1edd dd!d"e.ddfd'dZ2d(d) Z3dS )+a  
This module will download dataset from
http://www.robots.ox.ac.uk/~vgg/data/flowers/102/index.html
and parse train/test set intopaddle 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}.

    )print_functionN   )download)load_image_bytes)
load_image)simple_transform)batch_images_from_tar)map_readersxmap_readers)compat)	cpu_count)cPickle)
try_importz8http://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   FD:\Projects\ConvertPro\env\Lib\site-packages\paddle/dataset/flowers.pydefault_mapperF   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  ||< qt }| }d}|D ]}	|	j|v rS||	 }
||	j }|
t|d fV  q7d S )Nzscipy.iolabelsr   zjpg/image_%05d.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   readery   s$   


zreader_creator.<locals>.reader   )r
   minr   r	   )	r'   r)   r*   r(   mapperbuffered_sizeuse_xmapcycler+   r   r&   r   reader_creatorX   s   !
r2   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r1   )	r2   r   DATA_URLDATA_MD5	LABEL_URL	LABEL_MD5	SETID_URL	SETID_MD5
TRAIN_FLAGr.   r/   r0   r1   r   r   r   train      

r@   c              
   C   r5   )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
    r6   r7   )	r2   r   r8   r9   r:   r;   r<   r=   	TEST_FLAGr?   r   r   r   test   rA   rC   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
    r6   )	r2   r   r8   r9   r:   r;   r<   r=   
VALID_FLAG)r.   r/   r0   r   r   r   r      s
   
c                   C   s(   t tdt t tdt t tdt d S )Nr6   )r   r8   r9   r:   r;   r<   r=   r   r   r   r   fetch   s   rE   )r   TF)4__doc__
__future__r   	itertools	functoolscommonr   r   Zpaddle.dataset.imager   r   r   r   Zpaddle.readerr	   r
   Zpaddler   ZcptZpaddle.utils.deprecatedutils
deprecatedosnumpynpmultiprocessingr   sixZ	six.movesr   pickleZpaddle.utilsr   __all__r8   r:   r<   r9   r;   r=   r>   rB   rD   r   partialZtrain_mapperZtest_mapperr2   r@   rC   r   rE   r   r   r   r   <module>   st   
;