o
    }oiR                     @   s@  d Z ddlmZmZ ddlmZmZmZmZm	Z	 ddl
Z
ddlmZ zddlm  mZ ddlmZmZmZmZmZmZmZ dZW n eefyN   dZY nw ddlmZ d	Zd
ZeG dd dZ G dd dej!Z"dd Z#					dde$de%deee&df  deee&df  de%de$dee	ee'ef e f  fddZ(dS )z
This code is adapted from public repo
https://github.com/mlfoundations/open_clip/blob/28c994406e39a5babc749c76871d92f33e9c558d/src/open_clip/transform.py
by @yaoyu-33
    )asdict	dataclass)AnyDictOptionalTupleUnionN)
CenterCropComposeInterpolationMode	NormalizeRandomResizedCropResizeToTensorTF)logging)g3<4'?gwgM?gy{ ?)gB91?gwt.?g	U?c                   @   s   e Zd ZU dZdZeeef ed< dZe	eeef  ed< dZ
e	eeeeeef f  ed< dZe	e ed< dZe	e ed< dZe	e ed	< d
Zeed< dS )AugmentationCfgzAugmentation Config)g?      ?scaleNratiocolor_jitterinterpolationre_probre_countFuse_timm)__name__
__module____qualname____doc__r   r   float__annotations__r   r   r   r   r   strr   r   intr   bool r#   r#   u/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/multimodal/data/clip/augmentations/augmentations.pyr   /   s   
 "r   c                       s2   e Zd ZdZejddf fdd	Zdd Z  ZS )ResizeMaxSizezResize modulemaxr   c                    sN   t    t|tstdt| || _|| _|dkrtnt| _	|| _
d S )NzSize should be int. Got min)super__init__
isinstancer!   	TypeErrortypemax_sizer   r'   fnfill)selfr-   r   r.   r/   	__class__r#   r$   r)   ?   s   


zResizeMaxSize.__init__c                    s   t |tjr|jd d \}}n|j\}}| jtt||   dkrets)J dt	 fdd||fD }t
||| j}| j|d  }| j|d  }t
j||d |d ||d  ||d  g| jd}|S )	N   r   1Torchvision imports failed but they are required.c                 3   s    | ]	}t |  V  qd S N)round).0dimr   r#   r$   	<genexpr>Q   s    z(ResizeMaxSize.forward.<locals>.<genexpr>r      )paddingr/   )r*   torchTensorshapesizer-   r   r&   TORCHVISION_AVAILABLEtupleFresizer   padr/   )r0   imgheightwidthnew_sizepad_hpad_wr#   r9   r$   forwardH   s   
2zResizeMaxSize.forward)	r   r   r   r   r   BICUBICr)   rL   __classcell__r#   r#   r1   r$   r%   <   s    	r%   c                 C   s
   |  dS )NRGB)convert)imager#   r#   r$   _convert_to_rgbY   s   
rR   
image_sizeis_trainmean.stdresize_longest_max
fill_coloraug_cfgc              	   C   s  t sJ d|p	t}t|ttfs|fd }|pt}t|ttfs&|fd }t| ttfr9| d | d kr9| d } t|trFtdi |}n|pJt }t||d}|rdd t	|
 D }|dd	}	|	rdd
lm}
 t| ttfrt| dks{J d| dd   }nd| | f}|dd |dd  |
d|dd||dd|}|S tt| |dtjdtt |g}|rtdt|  d |S |rt| |dg}nt| tjdt| g}|tt |g t|S )Nr4      r   r;   )rU   rV   c                 S   s   i | ]\}}|d ur||qS r5   r#   )r7   kvr#   r#   r$   
<dictcomp>|   s    z#image_transform.<locals>.<dictcomp>r   F)create_transformr3   )rZ   r   randomr   Tg        pixel)
input_sizeis_traininghfliprU   rV   re_moder   )r   r   z:Unused augmentation cfg items, specify `use_timm` to use (z).)r/   )r   r#   )rA   OPENAI_DATASET_MEANr*   listrB   OPENAI_DATASET_STDdictr   r   r   itemspop	timm.datar^   len
setdefaultr
   r   r   rM   rR   r   r   warningkeysr%   r   r	   extend)rS   rT   rU   rV   rW   rX   rY   	normalizeaug_cfg_dictr   r^   rb   train_transform
transformsr#   r#   r$   image_transform^   s~   





rv   )NNFr   N))r   dataclassesr   r   typingr   r   r   r   r   r=   torch.nnnn!torchvision.transforms.functionalru   
functionalrC   torchvision.transformsr	   r
   r   r   r   r   r   rA   ImportErrorModuleNotFoundError
nemo.utilsr   rf   rh   r   Moduler%   rR   r!   r"   r   r    rv   r#   r#   r#   r$   <module>   sN   $
