
    3j                        S r SSKJr  SSKJr  SSKrSSKJs  Jr	  SSK
JrJr  SSKJr  SS jrSSS jjrSSS	 jjr S         SS
 jjrg)zImplementation of "differentiable spatial to numerical" (soft-argmax) operations.

As described in the paper "Numerical Coordinate Regression with Convolutional Neural Networks" by Nibali et al.
    )annotations)OptionalNKORNIA_CHECK_IS_TENSORKORNIA_CHECK_SHAPE)create_meshgridc                6    [        U 5        [        U / SQ5        g )N)BCHWr   )tensors    U/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/geometry/subpix/dsnt.py$_validate_batched_image_tensor_inputr   "   s    6"v34    c                &   [        U 5        U R                  u  p#pEUc  [        R                  " S5      nUR	                  U R
                  U R                  S9nU R                  X#S5      n[        R                  " Xa-  SS9nUR                  X#XE5      $ )a  Apply the Softmax function over features in each image channel.

Note that this function behaves differently to :py:class:`torch.nn.Softmax2d`, which
instead applies Softmax over features at each spatial location.

Args:
    input: the input torch.Tensor with shape :math:`(B, N, H, W)`.
    temperature: factor to apply to input, adjusting the "smoothness" of the output distribution.

Returns:
   a 2D probability distribution per image channel with shape :math:`(B, N, H, W)`.

Examples:
    >>> heatmaps = torch.tensor([[[
    ... [0., 0., 0.],
    ... [0., 0., 0.],
    ... [0., 1., 2.]]]])
    >>> spatial_softmax2d(heatmaps)
    tensor([[[[0.0585, 0.0585, 0.0585],
              [0.0585, 0.0585, 0.0585],
              [0.0585, 0.1589, 0.4319]]]])

g      ?devicedtype)dim)
r   shapetorchr   tor   r   viewFsoftmax)inputtemperature
batch_sizechannelsheightwidthxx_softs           r   spatial_softmax2dr&   '   s{    0 )/*/++'J&ll3'..EKK.HK

:,AYYqB/F;;zV;;r   c                   [        U 5        U R                  u  p#pE[        XEXR                  5      nUR	                  U R
                  5      nUS   R                  S5      nUS   R                  S5      nU R                  X#S5      n	[        R                  " X-  SSS9n
[        R                  " Xy-  SSS9n[        R                  " X/S5      nUR                  X#S5      $ )aV  Compute the expectation of coordinate values using spatial probabilities.

The input heatmap is assumed to represent a valid spatial probability distribution,
which can be achieved using :func:`~kornia.geometry.subpixel.spatial_softmax2d`.

Args:
    input: the input torch.Tensor representing dense spatial probabilities with shape :math:`(B, N, H, W)`.
    normalized_coordinates: whether to return the coordinates normalized in the range
      of :math:`[-1, 1]`. Otherwise, it will return the coordinates in the range of the input shape.

Returns:
   expected value of the 2D coordinates with shape :math:`(B, N, 2)`. Output order of the coordinates is (x, y).

Examples:
    >>> heatmaps = torch.tensor([[[
    ... [0., 0., 0.],
    ... [0., 0., 0.],
    ... [0., 1., 0.]]]])
    >>> spatial_expectation2d(heatmaps, False)
    tensor([[[1., 2.]]])

.r   r   .   T)keepdim   )r   r   r   r   r   r   reshaper   r   sumcat)r   normalized_coordinatesr    r!   r"   r#   gridpos_xpos_y
input_flat
expected_y
expected_xoutputs                r   spatial_expectation2dr8   L   s    . )/*/++'J& 6*@,,OD775;;DL  $EL  $EJ"5J 5-r4@J5-r4@JYY
/4F;;zQ//r   c                   UR                   U R                   :X  a  UR                  U R                  :X  d  [        S5      eUu  pEU R                   nU R                  nU(       a/  [        R                  " SSXWUS9n[        R                  " SSXGUS9n	O4[        R                  " SUS-
  XWUS9n[        R                  " SUS-
  XGUS9n	U S   R                  S5      n
U S   R                  S5      nUS   R                  S5      nUS   R                  S5      nX-
  S-  nX-
  S-  nS	[        R                  " US-  5      -  nS	[        R                  " US-  5      -  n[        R                  " UU-  5      n[        R                  " UU-  5      nUUR                  SS
S9S-   -  nUUR                  SS
S9S-   -  nUR                  S5      UR                  S5      -  $ )a  Render the PDF of a 2D Gaussian distribution.

Args:
    mean: the mean location of the Gaussian to render, :math:`(\mu_x, \mu_y)`. Shape: :math:`(*, 2)`.
    std: the standard deviation of the Gaussian to render, :math:`(\sigma_x, \sigma_y)`.
      Shape :math:`(*, 2)`. Should be able to be broadcast with `mean`.
    size: the (height, width) of the output image.
    normalized_coordinates: whether ``mean`` and ``std`` are assumed to use coordinates normalized
      in the range of :math:`[-1, 1]`. Otherwise, coordinates are assumed to be in the range of the output shape.

Returns:
    torch.Tensor including rendered points with shape :math:`(*, H, W)`.

z1Expected inputs to have the same dtype and devicer   r*   r   r   r(   r)   r,   g      T)r   r+   g:0yE>)	r   r   	TypeErrorr   linspace	unsqueeze
reciprocalexpr.   )meanstdsizer0   r"   r#   r   r   xsysmu_xmu_ysigma_xsigma_y	dist_x_sq	dist_y_sqk_xk_ygauss_xgauss_ys                       r   render_gaussian2drO   y   s   " II#

dkk(AKLLMFJJE[[F ^^B5uE^^B6F^^Auqy%eL^^Avz6N<!!"%D<!!"%D&k##B'G&k##B'G q Iq I !!'1*-
-C
!!'1*-
-C ii	C(Gii	C(G T:TABGT:TABGR 7#4#4R#888r   )r   torch.TensorreturnNone)N)r   rP   r   zOptional[torch.Tensor]rQ   rP   )T)r   rP   r0   boolrQ   rP   )
r@   rP   rA   rP   rB   ztuple[int, int]r0   rS   rQ   rP   )__doc__
__future__r   typingr   r   torch.nn.functionalnn
functionalr   kornia.core.checkr   r   kornia.geometry.gridr   r   r&   r8   rO    r   r   <module>r]      sf   $
 #     H 05
"<J*0\ bf99
99)991@99Z^9999r   