U
    h!                     @  s  d dl mZ d dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZmZmZmZmZ d dlZd dlZd dlmZ d dlmZ d dlmZmZmZ d dlmZmZmZ d d	l m!Z!m"Z" d
dddddZ#dddddZ$dddddZ%dddddZ&dd Z'dddd d!Z(d"dd#d$d%Z)d&d'd(d)d*Z*d+d,d-d.d/Z+d0d1d2d3d4Z,d0d5d2d6d7Z-d0d8d2d9d:Z.d&d;d<d=d>d?Z/d0d@d<dAdBdCZ0d0d@d<dAdDdEZ1dS )F    )annotationsN)suppress)	AnyCallableDictListLiteralSequenceTupleTypeUnion)
tv_tensors)sequence_to_str)_check_sequence_input_setup_angle_setup_size)get_dimensionsget_sizeis_pure_tensor)	_FillType_FillTypeJITz.Union[int, float, Sequence[Union[int, float]]]strzSequence[float])argnamereturnc                 C  s   t | tttfs&t| dt|  t | trTt| dkrTtd| dt|  t | tr| D ](}t |ttfsbt| dt| qbt | ttfrt| t| g} nHt | trt| dkrt| d t| d g} nt| d t| d g} | S )Nz2 should be a number or a sequence of numbers. Got )      zIf z0 is a sequence its length should be 1 or 2. Got z& should be a sequence of numbers. Got r   r   )
isinstanceintfloatr	   	TypeErrortypelen
ValueError)r   r   element r%   R/var/www/html/venv/lib/python3.8/site-packages/torchvision/transforms/v2/_utils.py_setup_number_or_seq   s    

r'   z3Union[_FillType, Dict[Union[Type, str], _FillType]]None)fillr   c                 C  sH   t | tr"|  D ]}t| qn"| d k	rDt | tjttfsDtdd S )NzNGot inappropriate fill arg, only Numbers, tuples, lists and dicts are allowed.)	r   dictvalues_check_fill_argnumbersNumbertuplelistr    )r)   valuer%   r%   r&   r,   )   s
    
r,   r   r   c                 C  s0   | d kr| S t | ttfs,dd t| D } | S )Nc                 S  s   g | ]}t |qS r%   )r   ).0vr%   r%   r&   
<listcomp>;   s     z%_convert_fill_arg.<locals>.<listcomp>)r   r   r   r0   )r)   r%   r%   r&   _convert_fill_arg2   s
    r5   z$Dict[Union[Type, str], _FillTypeJIT]c                 C  sD   t |  t| tr4|  D ]\}}t|| |< q| S dt| iS d S )Nothers)r,   r   r*   itemsr5   )r)   kr3   r%   r%   r&   _setup_fill_arg?   s    
r9   c                 C  s,   || kr| | S d| kr | d S t d d S )Nr6   zWThis should never happen, please open an issue on the torchvision repo if you hit this.)RuntimeError)Z	fill_dictZ	inpt_typer%   r%   r&   	_get_fillJ   s
    r;   zUnion[int, Sequence[int]])paddingr   c                 C  sL   t | tjttfstdt | ttfrHt| dkrHtdt|  dd S )NzGot inappropriate padding arg)r   r      z<Padding must be an int or a 1, 2, or 4 element tuple, not a z element tuple)r   r-   r.   r/   r0   r    r"   r#   )r<   r%   r%   r&   _check_padding_argS   s    r>   z5Literal[('constant', 'edge', 'reflect', 'symmetric')])padding_moder   c                 C  s   | dkrt dd S )N)ZconstantedgeZreflectZ	symmetriczBPadding mode should be either constant, edge, reflect or symmetric)r#   )r?   r%   r%   r&   _check_padding_mode_arg]   s    rA   r   ztorch.Tensor)inputsr   c              	   C  s   t | ttfr| d } t| r"| S t | tjjs@td|  dd}tt	 t
dd |  D }W 5 Q R X |dkrtt	 t
dd |  D }W 5 Q R X |dkrtd| | S )	ap  
    This heuristic covers three cases:

    1. The input is tuple or list whose second item is a labels tensor. This happens for already batched
       classification inputs for MixUp and CutMix (typically after the Dataloder).
    2. The input is a tuple or list whose second item is a dictionary that contains the labels tensor
       under a label-like (see below) key. This happens for the inputs of detection models.
    3. The input is a dictionary that is structured as the one from 2.

    What is "label-like" key? We first search for an case-insensitive match of 'labels' inside the keys of the
    dictionary. This is the name our detection models expect. If we can't find that, we look for a case-insensitive
    match of the term 'label' anywhere inside the key, i.e. 'FooLaBeLBar'. If we can't find that either, the dictionary
    contains no "label-like" key.
    r   zWhen using the default labels_getter, the input passed to forward must be a dictionary or a two-tuple whose second item is a dictionary or a tensor, but got z	 instead.Nc                 s  s   | ]}|  d kr|V  qdS )labelsNlowerr2   keyr%   r%   r&   	<genexpr>   s      z1_find_labels_default_heuristic.<locals>.<genexpr>c                 s  s   | ]}d |  kr|V  qdS )labelNrD   rF   r%   r%   r&   rH      s      zCould not infer where the labels are in the sample. Try passing a callable as the labels_getter parameter?If there are no labels in the sample by design, pass labels_getter=None.)r   r/   r0   r   collectionsabcMappingr#   r   StopIterationnextkeys)rB   Zcandidate_keyr%   r%   r&   _find_labels_default_heuristicb   s&    

 
 rP   z&Union[str, Callable[[Any], Any], None]zCallable[[Any], Any])labels_getterr   c                 C  s<   | dkrt S t| r| S | d kr(dd S td|  dd S )Ndefaultc                 S  s   d S Nr%   )_r%   r%   r&   <lambda>       z&_parse_labels_getter.<locals>.<lambda>zGlabels_getter should either be 'default', a callable, or None, but got .)rP   callabler#   )rQ   r%   r%   r&   _parse_labels_getter   s    rY   z	List[Any]ztv_tensors.BoundingBoxes)flat_inputsr   c                 C  s6   zt dd | D W S  tk
r0   tdY nX d S )Nc                 s  s   | ]}t |tjr|V  qd S rS   )r   r   BoundingBoxesr2   inptr%   r%   r&   rH      s      z%get_bounding_boxes.<locals>.<genexpr>z*No bounding boxes were found in the sample)rN   rM   r#   )rZ   r%   r%   r&   get_bounding_boxes   s    r^   zTuple[int, int, int]c                 C  sV   dd | D }|st dn"t|dkr>tdtt| | \}}}|||fS )Nc                 S  s2   h | ]*}t |ttjtjjtjfrtt|qS r%   )
check_typer   r   ImagePILVideor/   r   r\   r%   r%   r&   	<setcomp>   s   zquery_chw.<locals>.<setcomp>z)No image or video was found in the sampler   z/Found multiple CxHxW dimensions in the sample: r    r"   r#   r   sortedpop)rZ   Zchwschwr%   r%   r&   	query_chw   s    
rj   zTuple[int, int]c                 C  sR   dd | D }|st dn"t|dkr>tdtt| | \}}||fS )Nc              
   S  s:   h | ]2}t |ttjtjjtjtjtjfrtt	|qS r%   )
r_   r   r   r`   ra   rb   Maskr[   r/   r   r\   r%   r%   r&   rc      s   zquery_size.<locals>.<setcomp>z=No image, video, mask or bounding box was found in the sampler   z-Found multiple HxW dimensions in the sample: rd   )rZ   sizesrh   ri   r%   r%   r&   
query_size   s    
rm   z.Tuple[Union[Type, Callable[[Any], bool]], ...]bool)objtypes_or_checksr   c                 C  s2   |D ](}t |trt | |rn|| r dS qdS NTFr   r!   )ro   rp   type_or_checkr%   r%   r&   r_      s    r_   z"Union[Type, Callable[[Any], bool]])rZ   rp   r   c                 G  s   | D ]}t ||r dS qdS rq   )r_   )rZ   rp   r]   r%   r%   r&   has_any   s    
rt   c                 G  s@   |D ]6}| D ]&}t |tr&t ||rn||r qq dS qdS )NFTrr   )rZ   rp   rs   r]   r%   r%   r&   has_all   s    ru   )2
__future__r   collections.abcrJ   r-   
contextlibr   typingr   r   r   r   r   r	   r
   r   r   Z	PIL.Imagera   ZtorchZtorchvisionr   Ztorchvision._utilsr   Z!torchvision.transforms.transformsr   r   r   Z$torchvision.transforms.v2.functionalr   r   r   Z+torchvision.transforms.v2.functional._utilsr   r   r'   r,   r5   r9   r;   r>   rA   rP   rY   r^   rj   rm   r_   rt   ru   r%   r%   r%   r&   <module>   s4   ,		
,