
    ڧgN                        d dl Z d dlmZmZ d dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ dd	lmZ 	 	 	 	 	 ddedededee         deeef         deeef         deeef         dee         defdZ G d dej                  ZdS )    N)OptionalTuple)nnTensor)init)_pair)	Parameter)_assert_has_ops   )_log_api_usage_once   r   r   r   inputoffsetweightbiasstridepaddingdilationmaskreturnc                 4   t           j                                        s2t           j                                        st	          t
                     t                       |j        d         }|du}	|.t          j        | j        d         df| j	        | j
                  }|!t          j        || j	        | j
                  }t          |          \  }
}t          |          \  }}t          |          \  }}|j        dd         \  }}| j        \  }}}}|j        d         d|z  |z  z  }||j        d         z  }|dk    r&t          d|j        d          dd|z  |z             t           j        j                            | |||||
||||||||	          S )	a?  
    Performs Deformable Convolution v2, described in
    `Deformable ConvNets v2: More Deformable, Better Results
    <https://arxiv.org/abs/1811.11168>`__ if :attr:`mask` is not ``None`` and
    Performs Deformable Convolution, described in
    `Deformable Convolutional Networks
    <https://arxiv.org/abs/1703.06211>`__ if :attr:`mask` is ``None``.

    Args:
        input (Tensor[batch_size, in_channels, in_height, in_width]): input tensor
        offset (Tensor[batch_size, 2 * offset_groups * kernel_height * kernel_width, out_height, out_width]):
            offsets to be applied for each position in the convolution kernel.
        weight (Tensor[out_channels, in_channels // groups, kernel_height, kernel_width]): convolution weights,
            split into groups of size (in_channels // groups)
        bias (Tensor[out_channels]): optional bias of shape (out_channels,). Default: None
        stride (int or Tuple[int, int]): distance between convolution centers. Default: 1
        padding (int or Tuple[int, int]): height/width of padding of zeroes around
            each image. Default: 0
        dilation (int or Tuple[int, int]): the spacing between kernel elements. Default: 1
        mask (Tensor[batch_size, offset_groups * kernel_height * kernel_width, out_height, out_width]):
            masks to be applied for each position in the convolution kernel. Default: None

    Returns:
        Tensor[batch_sz, out_channels, out_h, out_w]: result of convolution

    Examples::
        >>> input = torch.rand(4, 3, 10, 10)
        >>> kh, kw = 3, 3
        >>> weight = torch.rand(5, 3, kh, kw)
        >>> # offset and mask should have the same spatial size as the output
        >>> # of the convolution. In this case, for an input of 10, stride of 1
        >>> # and kernel size of 3, without padding, the output size is 8
        >>> offset = torch.rand(4, 2 * kh * kw, 8, 8)
        >>> mask = torch.rand(4, kh * kw, 8, 8)
        >>> out = deform_conv2d(input, offset, weight, mask=mask)
        >>> print(out.shape)
        >>> # returns
        >>>  torch.Size([4, 5, 8, 8])
    r   Nr   )devicedtyper   zthe shape of the offset tensor at dimension 1 is not valid. It should be a multiple of 2 * weight.size[2] * weight.size[3].
Got offset.shape[1]=z,, while 2 * weight.size[2] * weight.size[3]=)torchjitis_scripting
is_tracingr   deform_conv2dr
   shapezerosr   r   r   RuntimeErroropstorchvision)r   r   r   r   r   r   r   r   out_channelsuse_maskstride_hstride_wpad_hpad_wdil_hdil_w	weights_h	weights_w_n_in_channelsn_offset_grpsn_weight_grpss                         W/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/ops/deform_conv.pyr!   r!      s   b 9!!## +EI,@,@,B,B +M***<?L4H|{EKNA.u|5;WWW|{<EKPPPvHh>>LE5??LE5!<,Iy"[A}aLOI	(ABM!V\!_4M|#)<?| |`adm`mpy`y| |
 
 	
 9 ..      c                        e Zd ZdZ	 	 	 	 	 ddedededed	ed
ededef fdZddZddedede	e         defdZ
defdZ xZS )DeformConv2dz$
    See :func:`deform_conv2d`.
    r   r   Tin_channelsr'   kernel_sizer   r   r   groupsr   c	           	         t                                                       t          |            ||z  dk    rt          d          ||z  dk    rt          d          || _        || _        t          |          | _        t          |          | _        t          |          | _	        t          |          | _
        || _        t          t          j        |||z  | j        d         | j        d                             | _        |r't          t          j        |                    | _        n|                     dd            |                                  d S )Nr   z'in_channels must be divisible by groupsz(out_channels must be divisible by groupsr   r   )super__init__r   
ValueErrorr9   r'   r   r:   r   r   r   r;   r	   r   emptyr   r   register_parameterreset_parameters)
selfr9   r'   r:   r   r   r   r;   r   	__class__s
            r5   r>   zDeformConv2d.__init__s   sC    	D!!!1$$FGGG& A%%GHHH&( --FmmW~~hKkV&;T=Ma=PRVRbcdReff
 
  	2!%+l";";<<DII##FD111r6   r   Nc                    t          j        | j        t          j        d                     | j        Qt          j        | j                  \  }}dt          j        |          z  }t          j        | j        | |           d S d S )N   )ar   )r   kaiming_uniform_r   mathsqrtr   _calculate_fan_in_and_fan_outuniform_)rC   fan_inr1   bounds       r5   rB   zDeformConv2d.reset_parameters   sz    dkTYq\\::::9 :4;GGIFA	&)))EM$)eVU33333 ! r6   r   r   r   c           
      b    t          ||| j        | j        | j        | j        | j        |          S )a  
        Args:
            input (Tensor[batch_size, in_channels, in_height, in_width]): input tensor
            offset (Tensor[batch_size, 2 * offset_groups * kernel_height * kernel_width, out_height, out_width]):
                offsets to be applied for each position in the convolution kernel.
            mask (Tensor[batch_size, offset_groups * kernel_height * kernel_width, out_height, out_width]):
                masks to be applied for each position in the convolution kernel.
        )r   r   r   r   )r!   r   r   r   r   r   )rC   r   r   r   s       r5   forwardzDeformConv2d.forward   s=     KI;L]	
 	
 	
 		
r6   c           	      $   | j         j         d| j         d| j         d| j         d| j         	}|| j        dk    r
d| j         ndz  }|| j        dk    r
d	| j         ndz  }|| j        d
k    r
d| j         ndz  }|| j	        dndz  }|dz  }|S )N(z, z, kernel_size=z	, stride=r   z
, padding= r   z, dilation=r   z	, groups=z, bias=False))
rD   __name__r9   r'   r:   r   r   r   r;   r   )rC   ss     r5   __repr__zDeformConv2d.__repr__   s    ~& & && &"& & "-& & 	& & 	
 	
DLF,B,B($,(((J	dmv.E.E*4=***2M	$+*:*:&&&&B	ty0^^b8	Sr6   )r   r   r   r   T)r   N)N)rU   
__module____qualname____doc__intboolr>   rB   r   r   rP   strrW   __classcell__)rD   s   @r5   r8   r8   n   s         $  $ $  $  	$ 
 $  $  $  $  $  $  $  $  $  $ L4 4 4 4
 
V 
V 
8F;K 
W] 
 
 
 
(#        r6   r8   )Nr   r   r   N)rI   typingr   r   r   r   r   torch.nnr   torch.nn.modules.utilsr   torch.nn.parameterr	   torchvision.extensionr
   utilsr   r[   r!   Moduler8    r6   r5   <module>rg      s    " " " " " " " "                ( ( ( ( ( ( ( ( ( ( ( ( 1 1 1 1 1 1 ' ' ' ' ' ' "$% &!] ]]] ] 6
	]
 #s(O] 38_] CHo] 6
] ] ] ] ]@U U U U U29 U U U U Ur6   