o
    ϯi                     @   s   d dl mZmZmZ d dlZd dlmZmZmZm	Z	m
Z
mZ d dlmZ ddeee	eeeee
e
e
e
dZdddddd	d	d
d
dddddZdddZG dd dZdS )    )AnyDictOptionalN)_AUGMENTATION_MAX_LEVELAugmentTransform_decreasing_int_to_arg_decreasing_to_arg_increasing_magnitude_to_arg"_increasing_randomly_negate_to_arg)	OpSampler)AutoContrastEqualizeRotate	PosterizeSolarizeShearXShearY
TranslateX
TranslateYAdjustSaturationAdjustContrastAdjustBrightnessAdjustSharpness)r      )   r   )   r   )r   g333333?)r   gUUUUUU?)皙?g?floatr   )sampling_data_typesampling_minc                   @   sr   e Zd ZdZ						ddededed	ed
eeee	f  deeee	f  ddfddZ
dejdejfddZdS )AugMixa  
    This implements AugMix for video. AugMix generates several chains of augmentations
    on the original video, which are then mixed together with each other and with the
    original video to create an augmented video. The input video tensor should have
    shape (T, C, H, W).

    AugMix: A Simple Data Processing Method to Improve Robustness and Uncertainty
    (https://arxiv.org/pdf/1912.02781.pdf)
             ?N	magnitudealphawidthdepthtransform_hparassampling_hparasreturnc                    s   t |ts	J d|dkr|tksJ dt d|dks!J d|dks)J d|_tjjt|g| _tjj	
||_	 fd	d
tt D }|dkr`t||dd_dS t|dddd_dS )a  
        Args:
            magnitude (int): Magnitude used for transform function. Default is 3.
            alpha (float): Parameter for choosing mixing weights from the beta
                and Dirichlet distributions. Default is 1.0.
            width (int): The number of transformation chains. Default is 3.
            depth (int): The number of transformations in each chain. If depth is -1,
                each chain will have a random length between 1 and 3 inclusive.
                Default is -1.
            transform_hparas (Optional[Dict[Any]]): Transform hyper parameters.
                Needs to have key fill. By default, the fill value is (0.5, 0.5, 0.5).
            sampling_hparas (Optional[Dict[Any]]): Hyper parameters for sampling. If
                gaussian sampling is used, it needs to have key sampling_std. By
                default, it uses SAMPLING_AUGMIX_DEFAULT_HPARAS.
        zmagnitude must be an intr   z magnitude must be between 1 and z
 inclusiveg        zalpha must be greater than 0r   zwidth must be greater than 0c                    s*   g | ]}t |jd ttd ptdqS )r"   uniform)transform_namer$   problevel_to_argtransform_max_parasr(   sampling_typer)   )r   
_magnitude_AUGMIX_LEVEL_TO_ARG_TRANSFORM_AUGMIX_MAX_PARAMSSAMPLING_AUGMIX_DEFAULT_HPARAS).0r,   r)   selfr(    R/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/transforms/augmix.py
<listcomp>i   s    z#AugMix.__init__.<locals>.<listcomp>T)num_sample_opreplacementr!   )r;   randomly_sample_depthr<   N)
isinstanceintr   r1   torchdistributions	dirichlet	DirichlettensorbetaBetalistr3   keysr   	augmix_fn)r7   r$   r%   r&   r'   r(   r)   transforms_listr8   r6   r9   __init__C   s4   

zAugMix.__init__videoc                 C   s   | j  }| j  }tj|jtjd}|D ]}||| | 7 }q|j	tj
kr7|| d| |  tj
S || d| |  S )z
        Perform AugMix to the input video tensor.

        Args:
            video (torch.Tensor): Input video tensor with shape (T, C, H, W).
        )dtyper   )rB   samplerE   itemr@   zerosshapefloat32rI   rM   uint8type)r7   rL   mixing_weightsmmixedmwr8   r8   r9   __call__   s   
zAugMix.__call__)r!   r"   r!   r#   NN)__name__
__module____qualname____doc__r?   r   r   r   strr   rK   r@   TensorrY   r8   r8   r8   r9   r    8   s0    
Ar    )typingr   r   r   r@   %pytorchvideo.transforms.augmentationsr   r   r   r   r	   r
   "pytorchvideo.transforms.transformsr   r2   r3   r4   r    r8   r8   r8   r9   <module>   sD    
