a ?f=/ @sPddlZddlZddlZddlmZdgZejeZ ejej Z ej ej dddZ ej ej dddZd:ej eej d d d Zd;ej eej dddZdej eeeeej d!d"d#Zej eeej d$d%d&Zej ejej e e e e fdd'd(Zej e e ej d)d*d+Zej ejej e fdd,d-Zej e ej d.d/d0Zd?ej eejeejeeeeeej d1 d2d3Zd@ej ej eeej d4d5d6Z dAej ejeejejeefej!eej feeeeej d8 d9dZ"dS)BN) functionalimresize)xreturncCs*t|d|d}|j|jd}|S)N?dtype)torch logical_andgtletor )rZrange_around_0contr9/home/mamuyuan/syz/ai-goms-pytorch_v2/my_utils/bicubic.pynearest_contribution srcCs,|}|d}d||j|jd}|S)Nr)absr rr )raxrange_01rrrrlinear_contributions rr)rarc Cs|}||}||}|d}t|d|d}|d||d|d}||j|jd}||d||d||d|}||j|jd}||} | S)Nrr)rr r r r rr ) rrrax2Zax3rZrange_12Zcont_01Zcont_12rrrrcubic_contributions (r@)rsigmarcCsH|d|dk}t|d d|d}||j|jd}|S)Nrrrr)rr exppowrr )rr!Z range_3sigmarrrrgaussian_contribution)sr$T)kernelscale antialiasingrc Cstd|}|dkrd}ntd|r0||}n|}|ddkrV|ddd|}n|d8}|dd|d}tNtj| ||d}t|d d}t||}|| }Wd n1s0Y|S) z3 For downsampling with integer scale only. rcubicrzPass!rrr)stepsN) int ValueErrorr no_gradlinspacerviewmatmultsum) r%r&r'Zdownsampling_factorZkernel_size_orig kernel_sizerrkrrrdiscrete_kernel0s"    *r6)rdimpad_prepad_postrc Csx|\}}}}|dks |dkr|||||||}|d|||ddf|t|D]2} |d|| dddf|d| ddfq`t|D]8} |d||| ddf|d| d ddfqn|||||||}|d|||f|t|D](} |d|| df|d| fqt|D].} |d||| f|d| d fqD|S)a Apply reflect padding to the given Tensor. Note that it is slightly different from the PyTorch functional.pad, where boundary elements are used only once. Instead, we follow the MATLAB implementation which uses boundary elements twice. For example, [a, b, c, d] would become [b, a, b, c, d, c] with the PyTorch implementation, while our implementation yields [a, a, b, c, d, d]. r.Nr)size new_zeroscopy_range) rr7r8r9bchwZpadding_bufferprrrreflect_paddingOs  0 8 & ,rDreflect)rr7r8r9 padding_typercCs6|dur |S|dkr$t||||}ntd||S)NrEz{} padding is not supported!)rDr,format)rr7r8r9rFx_padrrrpaddingrs rI)baser3x_sizercCsp|}|}||d}|dkr@| }|}||7}nd}||krb||d}|}nd}|||fS)Nrr)longminmaxitem)rJr3rKZr_minZr_maxr8r9rrr get_paddings   rPr(r)distr3r%r!antialiasing_factorrc Cs||t|}t|D]\}}|||q||9}|dkrJt|}n$|dkr`t||d}ntd|||jddd}|S)Nr(gaussian)r!z{} kernel is not supported!rTr7keepdim) r<len enumerater=rr$r,rGr2) rQr3r%r!rR buffer_posidxZ buffer_subweightrrr get_weights r[)rr7r3rcCs|dks|dkr6|df}|d|d}|d}n$d|f}|d}|d|d}t||}||dd||}|S)Nrr:rr*r)r;Funfoldr/)rr7r3r5h_outZw_outr]rrrreshape_tensors   r_cCs|dkr|\}}}}nT|dkr>|\}}}d}n4|dkr`|\}}d}}ntd||dd||}|||||fS)Nrrrz{}-dim Tensor is not supported!r*r)r7r;r,rG contiguousr/)rr?r@rArBrrr reshape_inputs     ra)rr?r@rcCsV|d}|d}|dur.|||||}n$|durF||||}n |||}|S)Nr:r*)r;r/)rr?r@rhrwrrrreshape_outputs   rdcCs4|jtjks|jtjkr(|j}|}nd}||fS)N)r r float32float64floatrr rrr cast_inputs  ri)rr rcCs<|dur8|js|}|tjur,|dd}|j|d}|S)Nrr)is_floating_pointroundr uint8clamprrhrrr cast_outputs   ro) rr7r;r&r%r!rFr'rcCs|dkr |S|dkrd}ntd|}|rH|dkrH|} t|| }nd} |d7}ttjd|d||j|jd} | d|d} | |dd} | | } t| |||| d } t | || |\}}} Wd n1s0Yt |||||d }t |||}|dks |d krJ|d | d d f}| d|| dd} n"|d | f}| d|d| d} || }|jddd}|S)z Args: x (torch.Tensor): A torch.Tensor of dimension (B x C, 1, H, W). dim (int): scale (float): size (int): Return: rr(rrr)r)r devicer)r%r!rRNrFr:.rTrT)mathfloorceilr r-r.r rqr[rPr;rIr_r/r2)rr7r;r&r%r!rFr'r3rRposrJrQrZr8r9rHr]samplerrr resize_1dsF  6  rx)rr5r&rFrc Cs|d}|d}|d}|j|j|jd}|dd||}|||dd}tj||j|jdd}|||dd}||}||d}||d} t|d|||d}t|d| | |d}t j ||d |d } | S) Nrr:r*)r rqF)r rq requires_gradrrrr)rIstride) r;rr rqr/repeatr eyerIr\conv2d) rr5r&rFr@k_hk_wepad_hpad_wyrrrdownsampling_2d@s     rr) rr&sizesr%r!rotation_degreerFr'rcCs2|dur|durtd|dur0|dur0tdt|\}}} } } |durnt| |t| |f}||f} |dur|d| |d| f} t|\}} t|tr||||d}t|df|d| dd|}t|d f|d| dd|}n$t|tj rt ||t d|d }t ||| }t || }|S) aS Args: x (torch.Tensor): scale (float): sizes (tuple(int, int)): kernel (str, default='cubic'): sigma (float, default=2): rotation_degree (float, default=0): padding_type (str, default='reflect'): antialiasing (bool, default=True): Return: torch.Tensor: Nz(One of scale or sizes must be specified!z0Please specify scale or sizes to avoid conflict!rr)r%r!rFr'r:)r;r&r*)r&)r,rarsruri isinstancestrrxr Tensorrr+rdro)rr&rr%r!rrFr'r?r@rArBscalesr kwargsrrrrXs0    "  )r)r )T)rE)r(r r)r(r rET)rE)NNr(rrrET)#rstypingr torch.nnrr\__all__Optionalr+_Ir Z_Drrrrgrr$rboolr6rDrITuplerPr[r_rardrirorxrUnionrrrrrs     (       L