
    y h                     B    d dl Z d dlmZ  G d dej                        Zy)    Nc                   x     e Zd ZdZd fd	Z	 ddej                  dej                  dej                  fdZ xZS )	BalanceCrossEntropyLossa  
    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                 F    t         t        |           || _        || _        y )N)superr   __init__negative_ratioeps)selfr   r	   	__class__s      /home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/easyocr/DBNet/decoders/balance_cross_entropy_loss.pyr   z BalanceCrossEntropyLoss.__init__   s     %t57,    predgtmaskc                    |dddddddf   |z  j                         }d|dddddddf   z
  |z  j                         }t        |j                         j                               }t	        t        |j                         j                               t        || j
                  z              }t        j                  j                  ||d      dddddddf   }	|	|j                         z  }
|	|j                         z  }t        j                  |j                  d      |      \  }}|
j                         |j                         z   ||z   | j                  z   z  }|r||	fS |S )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   forwardzBalanceCrossEntropyLoss.forward   sO    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x~~//x~~// ::m&8&8&<nMq%))+m.?.?.AAn,txx79 %%r   )g      @gư>)F)	__name__
__module____qualname____doc__r   r   Tensorr+   __classcell__)r   s   @r   r   r      s=    $ $	llLL llr   r   )r   torch.nnr   Moduler    r   r   <module>r5      s     3bii 3r   