
    ` j                     B    S SK r S SKJr   " S S\R                  5      rg)    Nc                      ^  \ rS rSrSrS	U 4S jjr S
S\R                  S\R                  S\R                  4S jjrSr	U =r
$ )BalanceCrossEntropyLoss   a  
Balanced cross entropy loss.
Shape:
    - Input: :math:`(N, 1, H, W)`
    - GT: :math:`(N, 1, H, W)`, same shape as the input
    - Mask: :math:`(N, H, W)`, same spatial shape as the input
    - Output: scalar.

Examples::

    >>> m = nn.Sigmoid()
    >>> loss = nn.BCELoss()
    >>> input = torch.randn(3, requires_grad=True)
    >>> target = torch.empty(3).random_(2)
    >>> output = loss(m(input), target)
    >>> output.backward()
c                 B   > [         [        U ]  5         Xl        X l        g )N)superr   __init__negative_ratioeps)selfr	   r
   	__class__s      ڂ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/easyocr/DBNet/decoders/balance_cross_entropy_loss.pyr    BalanceCrossEntropyLoss.__init__   s    %t57,    predgtmaskc                    USS2SSS2SS24   U-  R                  5       nSUSS2SSS2SS24   -
  U-  R                  5       n[        UR                  5       R                  5       5      n[	        [        UR                  5       R                  5       5      [        XpR
                  -  5      5      n[        R                  R                  XSS9SS2SSS2SS24   n	XR                  5       -  n
XR                  5       -  n[        R                  " UR                  S5      U5      u  pU
R                  5       UR                  5       -   Xx-   U R                  -   -  nU(       a  X4$ U$ )z
Args:
    pred: shape :math:`(N, 1, H, W)`, the prediction of network
    gt: shape :math:`(N, 1, H, W)`, the target
    mask: shape :math:`(N, H, W)`, the mask indicates positive regions
Nr      none)	reduction)byteintfloatsumminr	   nn
functionalbinary_cross_entropytorchtopkviewr
   )r   r   r   r   return_originpositivenegativepositive_countnegative_countlosspositive_lossnegative_loss_balance_losss                 r   forwardBalanceCrossEntropyLoss.forward   sA    q1QwK$&,,.Aa!G_,224X^^-1134S!1!5!5!781D1D DEG}}11 2 (()1a
4~~//~~// ::m&8&8&<nM%))+m.?.?.AA,txx79 %%r   )r
   r	   )g      @gư>)F)__name__
__module____qualname____firstlineno____doc__r   r    Tensorr-   __static_attributes____classcell__)r   s   @r   r   r      sB    $ $	llLL ll r   r   )r    torch.nnr   Moduler    r   r   <module>r:      s     3bii 3r   