o
    Ne                     @   sv   d dl Z d dlZd dlZddlmZ ddlmZmZ z
d dl	m
Z
mZ W n   d dlm
Z
mZ Y dd Zdd	 ZdS )
    N   )_non_static_mode)corelayers)SequenceMappingc                    s    d }t |tjrtj dd  S t |tjtjjfr$tj ddS t |t	j
r1t   S t |ttfr: S t |trH fdd|D S t |trkt|tfddt D sbtddd	 t  D S td
t|)ad  
    Default batch collating function for :code:`paddle.io.DataLoader`,
    get input data as a list of sample datas, each element in list
    if the data of a sample, and sample data should composed of list,
    dictionary, string, number, numpy array and paddle.Tensor, this
    function will parse input data recursively and stack number,
    numpy array and paddle.Tensor datas as batch datas. e.g. for
    following input data:

    [{'image': np.array(shape=[3, 224, 224]), 'label': 1},
     {'image': np.array(shape=[3, 224, 224]), 'label': 3},
     {'image': np.array(shape=[3, 224, 224]), 'label': 4},
     {'image': np.array(shape=[3, 224, 224]), 'label': 5},]
    
    
    This default collate function zipped each number and numpy array
    field together and stack each field as the batch field as follows:

    {'image': np.array(shape=[4, 3, 224, 224]), 'label': np.array([1, 3, 4, 5])}


    Args:  
        batch(list of sample data): batch should be a list of sample data.
    
    Returns:
        Batched data: batched each number, numpy array and paddle.Tensor
                      in input data.
    r   )Zaxisc                    s$   i | ]  t  fd dD qS )c                    s   g | ]}|  qS  r   .0dkeyr   OD:\Projects\ConvertPro\env\Lib\site-packages\paddle/fluid/dataloader/collate.py
<listcomp>E       z1default_collate_fn.<locals>.<dictcomp>.<listcomp>default_collate_fn)r
   batchr   r   
<dictcomp>D   s    z&default_collate_fn.<locals>.<dictcomp>c                 3   s    | ]	}t | kV  qd S )N)len)r
   sample)sample_fields_numr   r   	<genexpr>J   s    z%default_collate_fn.<locals>.<genexpr>z/fileds number not same among samples in a batchc                 S      g | ]}t |qS r   r   )r
   fieldsr   r   r   r   M   r   z&default_collate_fn.<locals>.<listcomp>zSbatch data con only contains: tensor, numpy.ndarray, dict, list, number, but got {})
isinstancenpndarraystackpaddleTensorr   eagerr   numbersNumberarraystrbytesr   r   r   alliterRuntimeErrorzip	TypeErrorformattype)r   r   r   )r   r   r   r      s2   




r   c                    sf   t  tjtjtjjfr S t  ttfr S t  t	r% fdd D S t  t
r1dd  D S  S )a  
    Default batch converting function for :code:`paddle.io.DataLoader`.
    get input data as a list of sample datas, each element in list
    if the data of a sample, and sample data should composed of list,
    dictionary, string, number, numpy array and paddle.Tensor.

    .. note::
        This function is default :attr:`collate_fn` in **Distable
        automatic batching** mode, for **Distable automatic batching**
        mode, please ses :attr:`paddle.io.DataLoader`

    Args:  
        batch(list of sample data): batch should be a list of sample data.
    
    Returns:
        Batched data: batched each number, numpy array and paddle.Tensor
                      in input data.
    c                    s   i | ]	}|t  | qS r   default_convert_fn)r
   r   r   r   r   r   k   s    z&default_convert_fn.<locals>.<dictcomp>c                 S   r   r   r/   r	   r   r   r   r   m   r   z&default_convert_fn.<locals>.<listcomp>)r   r    r!   r   r   r   r"   r&   r'   r   r   r   r   r   r   r0   S   s   

r0   )r    r#   numpyr   Z	frameworkr    r   r   collections.abcr   r   collectionsr   r0   r   r   r   r   <module>   s   8