o
    ॵiF                     @   s   d dl Z d dlmZ d dlmZ d dlZd dlZd dlm	Z	 ddl
mZmZ e G dd deZd	d
 Ze G dd deZe G dd deZdd Ze G dd deZe G dd deZe G dd deZdS )    N)Sequence)Mapping)default_group   )PREPROCESSORSbuild_preprocessorc                   @   s*   e Zd ZdZd
ddZdd Zdd	 ZdS )Composea(  Compose a data pipeline with a sequence of transforms.
    Args:
        transforms (list[dict | callable]):
            Either config dicts of transforms or transform objects.
        profiling (bool, optional): If set True, will profile and
            print preprocess time for each step.
    NFc              	   C   s   t |tsJ || _g | _|| _|D ]<}t |tr:| jd u r$t|t}n$zt||}W n ty9   t|t}Y nw t	|r?n	t
dt| | j| qd S )Nz.transform must be callable or a dict, but got )
isinstancer   	profiling
transforms
field_namedictr   r   KeyErrorcallable	TypeErrortypeappend)selfr   r   r
   	transform r   S/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/preprocessors/common.py__init__   s,   

zCompose.__init__c                 C   sT   | j D ]$}| jrt }||}| jr t| dt |   |d u r' d S q|S )Nz time )r   r
   timeprint)r   datatstartr   r   r   __call__/   s   
zCompose.__call__c                 C   s2   | j jd }| jD ]	}|d| 7 }q	|d7 }|S )N(z
    z
))	__class____name__r   )r   format_stringr   r   r   r   __repr__=   s
   
zCompose.__repr__)NFr    
__module____qualname____doc__r   r   r"   r   r   r   r   r      s
    
r   c                 C   s   t | tjr| S t | tjrt| S t | tr"t | ts"t| S t | t	r-t
| gS t | tr8t| gS tdt|  d)a8  Convert objects of various python types to :obj:`torch.Tensor`.

    Supported types are: :class:`numpy.ndarray`, :class:`torch.Tensor`,
    :class:`Sequence`, :class:`int` and :class:`float`.

    Args:
        data (torch.Tensor | numpy.ndarray | Sequence | int | float): Data to
            be converted.
    type  cannot be converted to tensor.)r	   torchTensornpndarray
from_numpyr   strtensorint
LongTensorfloatFloatTensorr   r   r   r   r   r   	to_tensorE   s   



r5   c                   @   *   e Zd ZdZd	ddZdd Zdd ZdS )
ToTensoru  Convert target object to tensor.

    Args:
        keys (Sequence[str]): Key of data to be converted to Tensor.
            Only valid when data is type of `Mapping`. If `keys` is None,
            all values of keys ​​will be converted to tensor by default.
    Nc                 C   
   || _ d S Nkeysr   r;   r   r   r   r   h      
zToTensor.__init__c                 C   V   t |tr%| jd u rt| | _| jD ]}||v r"t|| ||< q|S t|}|S r9   )r	   r   r;   listr5   r   r   keyr   r   r   r   k      


zToTensor.__call__c                 C      | j jd| j d S Nz(keys=)r   r    r;   r   r   r   r   r"   x      zToTensor.__repr__r9   r#   r   r   r   r   r7   ^   
    
r7   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Filtera  This is usually the last stage of the dataloader transform.
    Only data of reserved keys will be kept and passed directly to the model, others will be removed.

    Args:
        keys (Sequence[str]): Keys of data to be reserved, others will be removed.
    c                 C   r8   r9   )reserved_keys)r   rK   r   r   r   r      r=   zFilter.__init__c                 C   s6   t |tsJ i }| jD ]}||v r|| ||< q|S r9   )r	   r   rK   )r   r   reserved_datarA   r   r   r   r      s   
zFilter.__call__c                 C   rC   rD   )r   r    rK   rG   r   r   r   r"      rH   zFilter.__repr__Nr#   r   r   r   r   rJ   |   s
    
rJ   c                 C   s   t | tjr
|  S t | tjr| S t | tr!t | ts!t| S t | t	r.tj| tj
dS t | tr;tj| tjdS tdt|  d)zConvert objects of various python types to `numpy.ndarray`.

    Args:
        data (torch.Tensor | numpy.ndarray | Sequence | int | float): Data to
            be converted.
    )dtyper'   r(   )r	   r)   r*   numpyr+   r,   r   r.   asarrayr0   int64r2   float64r   r   r4   r   r   r   to_numpy   s   


rR   c                   @   r6   )
ToNumpyu!  Convert target object to numpy.ndarray.

    Args:
        keys (Sequence[str]): Key of data to be converted to numpy.ndarray.
            Only valid when data is type of `Mapping`. If `keys` is None,
            all values of keys ​​will be converted to numpy.ndarray by default.
    Nc                 C   r8   r9   r:   r<   r   r   r   r      r=   zToNumpy.__init__c                 C   r>   r9   )r	   r   r;   r?   rR   r@   r   r   r   r      rB   zToNumpy.__call__c                 C   rC   rD   rF   rG   r   r   r   r"      rH   zToNumpy.__repr__r9   r#   r   r   r   r   rS      rI   rS   c                   @   s.   e Zd ZdZg g fddZdd Zdd ZdS )	RenamezDChange the name of the input keys to output keys, respectively.
    c                 C   s   || _ || _d S r9   )
input_keysoutput_keys)r   rU   rV   r   r   r   r      s   
zRename.__init__c                 C   sL   t |tr$t| j| jD ]\}}||v r#||vr#|| ||< || q|S r9   )r	   r   ziprU   rV   pop)r   r   in_keyout_keyr   r   r   r      s   

zRename.__call__c                 C   rC   rD   rF   rG   r   r   r   r"      rH   zRename.__repr__Nr#   r   r   r   r   rT      s
    rT   c                   @   s   e Zd Zdd Zdd ZdS )Identityc                 C   s   d S r9   r   rG   r   r   r   r         zIdentity.__init__c                 C   s   |S r9   r   )r   itemr   r   r   r      r\   zIdentity.__call__N)r    r$   r%   r   r   r   r   r   r   r[      s    r[   )r   collections.abcr   typingr   rN   r+   r)   modelscope.utils.registryr   builderr   r   register_moduleobjectr   r5   r7   rJ   rR   rS   rT   r[   r   r   r   r   <module>   s*   6