U
    h                     @   s   d dl mZmZmZmZmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZ G dd deZG dd	 d	eZG d
d deZG dd deZdS )    )AnyCallableDictListOptionalSequenceUnionN)nn)
transforms)	Transformc                       sJ   e Zd ZdZee dd fddZeedddZe	d	d
dZ
  ZS )Composea  Composes several transforms together.

    This transform does not support torchscript.
    Please, see the note below.

    Args:
        transforms (list of ``Transform`` objects): list of transforms to compose.

    Example:
        >>> transforms.Compose([
        >>>     transforms.CenterCrop(10),
        >>>     transforms.PILToTensor(),
        >>>     transforms.ConvertImageDtype(torch.float),
        >>> ])

    .. note::
        In order to script the transformations, please use ``torch.nn.Sequential`` as below.

        >>> transforms = torch.nn.Sequential(
        >>>     transforms.CenterCrop(10),
        >>>     transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
        >>> )
        >>> scripted_transforms = torch.jit.script(transforms)

        Make sure to use only scriptable transformations, i.e. that work with ``torch.Tensor``, does not require
        `lambda` functions or ``PIL.Image``.

    Nr
   returnc                    s4   t    t|tstdn|s*td|| _d S )N5Argument transforms should be a sequence of callableszPass at least one transform)super__init__
isinstancer   	TypeError
ValueErrorr
   selfr
   	__class__ V/var/www/html/venv/lib/python3.8/site-packages/torchvision/transforms/v2/_container.pyr   (   s    


zCompose.__init__inputsr   c                 G   s2   t |dk}| jD ]}|| }|r&|n|f}q|S N   )lenr
   r   r   needs_unpacking	transformoutputsr   r   r   forward0   s
    
zCompose.forwardr   c                 C   s*   g }| j D ]}|d|  q
d|S Nz    
r
   appendjoinr   format_stringtr   r   r   
extra_repr7   s    
zCompose.extra_repr)__name__
__module____qualname____doc__r   r   r   r   r$   strr.   __classcell__r   r   r   r   r   
   s   r   c                       st   e Zd ZdZejZdeee	 e
jf edd fddZeeef ddd	Zeed
ddZedddZ  ZS )RandomApplya  Apply randomly a list of transformations with a given probability.

    .. note::
        In order to script the transformation, please use ``torch.nn.ModuleList`` as input instead of list/tuple of
        transforms as shown below:

        >>> transforms = transforms.RandomApply(torch.nn.ModuleList([
        >>>     transforms.ColorJitter(),
        >>> ]), p=0.3)
        >>> scripted_transforms = torch.jit.script(transforms)

        Make sure to use only scriptable transformations, i.e. that work with ``torch.Tensor``, does not require
        `lambda` functions or ``PIL.Image``.

    Args:
        transforms (sequence or torch.nn.Module): list of transformations
        p (float): probability of applying the list of transforms
          ?Nr
   pr   c                    sP   t    t|ttjfs"td|| _d|  kr<dksFn td|| _	d S )NzJArgument transforms should be a sequence of callables or a `nn.ModuleList`g        g      ?z@`p` should be a floating point value in the interval [0.0, 1.0].)
r   r   r   r   r	   
ModuleListr   r
   r   r8   r   r
   r8   r   r   r   r   T   s    
zRandomApply.__init__r%   c                 C   s   | j | jdS )Nr
   r8   r;   )r   r   r   r    _extract_params_for_v1_transform_   s    z,RandomApply._extract_params_for_v1_transformr   c                 G   sR   t |dk}td| jkr,|r$|S |d S | jD ]}|| }|rF|n|f}q2|S )Nr   r   )r   torchZrandr8   r
   r    r   r   r   r$   b   s    
zRandomApply.forwardc                 C   s*   g }| j D ]}|d|  q
d|S r&   r(   r+   r   r   r   r.   m   s    
zRandomApply.extra_repr)r6   )r/   r0   r1   r2   _transformsr5   Z_v1_transform_clsr   r   r   r	   r9   floatr   r   r3   r   r<   r$   r.   r4   r   r   r   r   r5   >   s   &r5   c                       sH   e Zd ZdZd	ee eee  dd fddZ	e
e
dddZ  ZS )
RandomChoicea  Apply single transformation randomly picked from a list.

    This transform does not support torchscript.

    Args:
        transforms (sequence or torch.nn.Module): list of transformations
        p (list of floats or None, optional): probability of each transform being picked.
            If ``p`` doesn't sum to 1, it is automatically normalized. If ``None``
            (default), all transforms have the same probability.
    Nr7   c                    s   t |tstd|d kr*dgt| }n,t|t|krVtdt| dt| t   || _t|  fdd|D | _	d S )Nr   r   z4Length of p doesn't match the number of transforms: z != c                    s   g | ]}|  qS r   r   ).0Zprobtotalr   r   
<listcomp>   s     z)RandomChoice.__init__.<locals>.<listcomp>)
r   r   r   r   r   r   r   r
   sumr8   r:   r   rB   r   r      s    

zRandomChoice.__init__r   c                 G   s*   t tt| jd}| j| }|| S r   )intr=   ZmultinomialZtensorr8   r
   )r   r   idxr"   r   r   r   r$      s    
zRandomChoice.forward)N)r/   r0   r1   r2   r   r   r   r   r?   r   r   r$   r4   r   r   r   r   r@   t   s    
r@   c                       s<   e Zd ZdZee dd fddZeedddZ  Z	S )	RandomOrderzApply a list of transformations in a random order.

    This transform does not support torchscript.

    Args:
        transforms (sequence or torch.nn.Module): list of transformations
    Nr   c                    s&   t |tstdt   || _d S )Nr   )r   r   r   r   r   r
   r   r   r   r   r      s    

zRandomOrder.__init__r   c                 G   sF   t |dk}tt | jD ]$}| j| }|| }|r:|n|f}q|S r   )r   r=   Zrandpermr
   )r   r   r!   rG   r"   r#   r   r   r   r$      s    
zRandomOrder.forward)
r/   r0   r1   r2   r   r   r   r   r$   r4   r   r   r   r   rH      s   rH   )typingr   r   r   r   r   r   r   r=   r	   Ztorchvisionr
   r>   Ztorchvision.transforms.v2r   r   r5   r@   rH   r   r   r   r   <module>   s   $46%