o
    V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                       sN   e Zd ZdZdee ddf fddZdedefdd	Zde	f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``.

    r
   returnNc                    s2   t    t|tstd|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__ X/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/transforms/v2/_container.pyr   (   s   


zCompose.__init__inputsc                 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.forwardc                 C   *   g }| j D ]
}|d|  qd|S Nz    
r
   appendjoinr   format_stringtr   r   r   
extra_repr7      

zCompose.extra_repr)__name__
__module____qualname____doc__r   r   r   r   r"   strr,   __classcell__r   r   r   r   r   
   s
    r   c                       sz   e Zd ZdZejZddeee	 e
jf deddf fddZdeeef fd	d
ZdedefddZdef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
          ?r
   pr   Nc                    sV   t    t|ttjfstd|| _d|  kr!dks&td 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   r6   r   r
   r6   r   r   r   r   T   s   

zRandomApply.__init__c                 C   s   | j | jdS )Nr
   r6   r9   )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 ]}|| }|r#|n|f}q|S )Nr   r   )r   torchrandr6   r
   r   r   r   r   r"   b   s   
zRandomApply.forwardc                 C   r#   r$   r&   r)   r   r   r   r,   m   r-   zRandomApply.extra_repr)r5   )r.   r/   r0   r1   _transformsr4   _v1_transform_clsr   r   r   r	   r7   floatr   r   r2   r   r:   r"   r,   r3   r   r   r   r   r4   >   s    *r4   c                       sP   e Zd ZdZ	ddee deee  ddf fddZ	de
de
f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.
    Nr
   r6   r   c                    s   t |ts	td|d u rdgt| }nt|t|kr+t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   ).0probtotalr   r   
<listcomp>   s    z)RandomChoice.__init__.<locals>.<listcomp>)
r   r   r   r   r   r   r   r
   sumr6   r8   r   rC   r   r      s   

zRandomChoice.__init__r   c                 G   s*   t tt| jd}| j| }|| S r   )intr;   multinomialtensorr6   r
   )r   r   idxr    r   r   r   r"      s   
zRandomChoice.forward)N)r.   r/   r0   r1   r   r   r   r   r?   r   r   r"   r3   r   r   r   r   r@   t   s    
r@   c                       s@   e Zd ZdZdee ddf fddZdedef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
    r
   r   Nc                    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;   randpermr
   )r   r   r   rJ   r    r!   r   r   r   r"      s   
zRandomOrder.forward)
r.   r/   r0   r1   r   r   r   r   r"   r3   r   r   r   r   rK      s    rK   )typingr   r   r   r   r   r   r   r;   r	   torchvisionr
   r=   torchvision.transforms.v2r   r   r4   r@   rK   r   r   r   r   <module>   s   $ 46%