o
    wi#/                     @   sH   d dl Z d dlZd dlmZmZmZ dZG dd dZG dd dZ	dS )    N)ImageImageEnhanceImageOps
   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )ImageNetPolicya  Definition of an ImageNetPolicy.

    Implements a fixed AutoAugment data augmentation policy targeted at
    ImageNet training by randomly applying at runtime one of the 25 pre-defined
    data augmentation sub-policies provided in Reference [1].

    Usage example as a Pytorch Transform:
    >>> transform=transforms.Compose([transforms.Resize(256),
    >>>                               ImageNetPolicy(),
    >>>                               transforms.ToTensor()])
       r   r   c                  C   s  t dddddd|t dddd	dd|t d
ddd
dd|t dddddd|t d
ddddd|t d
ddddd|t dddd
dd|t dddd
dd|t dddddd|t d
ddddd|t dddddd|t dddd
dd|t d
ddd
dd|t dddd
dd|t dddddd|t dddddd|t dddddd|t dddddd|t dddd
dd|t dddd
dd|t d
ddddd|t dddd	dd|t dddd
dd|t dddddd|t d
ddd
dd|g| _dS )zInitialize an ImageNetPolicy.

        Args:
            fillcolor (tuple): RGB color components of the color to be used for
            filling when needed (default: (128, 128, 128), which
            corresponds to gray).
        	posterizeg?   rotateg333333?	   solarize   autocontrastequalizeg?         g?         ?   colorr           invertcontrast	sharpnessshearXN)	SubPolicypolicies)self	fillcolor r!   n/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/collections/vision/data/megatron/autoaugment.py__init__%   s4   
zImageNetPolicy.__init__c                 C   s$   t dt| jd }| j| |S )z,Define call method for ImageNetPolicy class.r      )randomrandintlenr   )r   img
policy_idxr!   r!   r"   __call__N   s   zImageNetPolicy.__call__c                 C   s   dS )z,Define repr method for ImageNetPolicy class.r   r!   )r   r!   r!   r"   __repr__S   s   zImageNetPolicy.__repr__N)r   )__name__
__module____qualname____doc__r#   r*   r+   r!   r!   r!   r"   r      s
    
)r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zDefinition of a SubPolicy.

    A SubPolicy consists of two augmentation operations,
    each of those parametrized as operation, probability, magnitude.
    The two operations are applied sequentially on the image upon call.
    c                    s.  g d}||v r||v sJ dd|  krdkr-n J dd|  kr,dks1J d J dt |trDd|  krCdksHJ d J dt |tr[d|  krZdks_J d	 J d	td
 }	tdd|	tdd|	tdd|	tdd|	tdd|	tdd|	ttdd|	dtjtdd|	tdd|	tdd|	tdd|	dg|	 dg|	 dg|	 d}
dd  fdd fdd fdd fddfdddd dd dd dd dd dd d d d!d d"d d}|| _|| | _	|
| | | _
|| _|| | _|
| | | _d#S )$a+  Initialize a SubPolicy.

        Args:
            operation1 (str): Key specifying the first augmentation operation.
            There are fourteen key values altogether (see supported_ops below
            listing supported operations). probability1 (float): Probability
            within [0., 1.] of applying the first augmentation operation.
            magnitude_idx1 (int): Integer specifiying the strength of the first
            operation as an index further used to derive the magnitude from a
            range of possible values.
            operation2 (str): Key specifying the second augmentation operation.
            probability2 (float): Probability within [0., 1.] of applying the
            second augmentation operation.
            magnitude_idx2 (int): Integer specifiying the strength of the
            second operation as an index further used to derive the magnitude
            from a range of possible values.
            fillcolor (tuple): RGB color components of the color to be used for
            filling.
        Returns:
        )r   shearY
translateX
translateYr   r   r	   r   r   r   
brightnessr   r   r   zCSubPolicy:one of oper1 or oper2 refers to an unsupported operation.r   r   z5SubPolicy: prob1 and prob2 should be within [0., 1.].r   r   zASubPolicy: idx1 should be specified as an integer within [0, 10].zASubPolicy: idx2 should be specified as an integer within [0, 10].r$   g333333?gt ?   g?r
   r      c                 S   s6   |  d|}t|td|jd|}| | jS )a  Define rotation transformation with fill.

            The input image is first rotated, then it is blended together with
            a gray mask of the same size. Note that fillcolor as defined
            elsewhere in this module doesn't apply here.

            Args:
                magnitude (float): rotation angle in degrees.
            Returns:
                rotated_filled (PIL Image): rotated image with gray filling for
                disoccluded areas unveiled by the rotation.
            RGBA)r   r   r   r   )convertr   r   	compositenewsizemode)r(   	magnituderotatedrotated_filledr!   r!   r"   rotate_with_fill   s   z,SubPolicy.__init__.<locals>.rotate_with_fillc              	      s4   | j | jtjd|tddg ddddftj dS )Nr$   r   r    	transformr:   r   AFFINEr%   choiceBICUBICr(   r<   rA   r!   r"   <lambda>       z$SubPolicy.__init__.<locals>.<lambda>c                    s4   | j | jtjddd|tddg ddftj dS Nr$   r   r@   rA   rB   rG   rA   r!   r"   rH      rI   c              
      s:   | j | jtjdd|| jd  tddg dddf dS rJ   rC   r:   r   rD   r%   rE   rG   rA   r!   r"   rH          &c                    s:   | j | jtjddddd|| jd  tddg f dS rJ   rK   rG   rA   r!   r"   rH      rL   c                    s
    | |S Nr!   rG   )r?   r!   r"   rH         
 c                 S   "   t | d|tddg  S Nr$   r@   )r   Colorenhancer%   rE   rG   r!   r!   r"   rH      s   " c                 S      t | |S rM   )r   r	   rG   r!   r!   r"   rH          c                 S   rS   rM   )r   r   rG   r!   r!   r"   rH      rT   c                 S   rO   rP   )r   ContrastrR   r%   rE   rG   r!   r!   r"   rH         
 c                 S   rO   rP   )r   	SharpnessrR   r%   rE   rG   r!   r!   r"   rH      rV   c                 S   rO   rP   )r   
BrightnessrR   r%   rE   rG   r!   r!   r"   rH      rV   c                 S   
   t | S rM   )r   r   rG   r!   r!   r"   rH      rN   c                 S   rY   rM   )r   r   rG   r!   r!   r"   rH      rN   c                 S   rY   rM   )r   r   rG   r!   r!   r"   rH      rN   N)
isinstanceint
_MAX_LEVELnplinspaceroundastypeint64probability1
operation1
magnitude1probability2
operation2
magnitude2)r   rc   rb   magnitude_idx1rf   re   magnitude_idx2r    supported_ops
num_levelsranges	func_dictr!   )r    r?   r"   r#   `   s   





/

zSubPolicy.__init__c                 C   s<   t   | jk r| || j}t   | jk r| || j}|S )z'Define call method for SubPolicy class.)r%   rb   rc   rd   re   rf   rg   )r   r(   r!   r!   r"   r*      s
   zSubPolicy.__call__N)r,   r-   r.   r/   r#   r*   r!   r!   r!   r"   r   X   s
     r   )
r%   numpyr]   PILr   r   r   r\   r   r   r!   r!   r!   r"   <module>   s   @