o
    ,h5                     @   sh   d dl Z d dlmZ d dlZd dlZd dlmZ G dd dejZ	G dd dejZ
G dd de	ZdS )	    N)ndimagec                       s<   e Zd ZdZd fdd	ZddejfddZd	d
 Z  Z	S )DiceLossz
    Loss function from https://arxiv.org/abs/1707.03237,
    where iou computation is introduced heatmap manner to measure the
    diversity bwtween tow heatmaps.
    ư>c                    s   t t|   || _d S N)superr   __init__eps)selfr   	__class__ [/var/www/html/scripts/venv/lib/python3.10/site-packages/easyocr/DBNet/decoders/dice_loss.pyr      s   
zDiceLoss.__init__Npredc                 C   s(   |  dksJ |  | ||||S )z
        pred: one or two heatmaps of shape (N, 1, H, W),
            the losses of tow heatmaps are added together.
        gt: (N, 1, H, W)
        mask: (N, H, W)
           )dim_compute)r	   r   gtmaskweightsr   r   r   forward   s   zDiceLoss.forwardc                 C   s   |  dkr"|d d dd d d d f }|d d dd d d d f }|j|jks*J |j|jks2J |d urB|j|jks>J || }|| |  }||  ||   | j }dd| |  }|dksgJ |S )Nr   r      g       @)r   shapesumr   )r	   r   r   r   r   intersectionunionlossr   r   r   r      s   zDiceLoss._compute)r   r   )
__name__
__module____qualname____doc__r   torchTensorr   r   __classcell__r   r   r
   r   r      s
    
r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	LeakyDiceLosszV
    Variation from DiceLoss.
    The coverage and union are computed separately.
    r         @c                    s   t t|   || _|| _d S r   )r   r#   r   r   coverage_scale)r	   r   r%   r
   r   r   r   2   s   
zLeakyDiceLoss.__init__c                 C   s   |  dkr"|d d dd d d d f }|d d dd d d d f }|j|jks*J |j|jks2J || |  ||  | j  }|dksIJ d| }|| |  ||  | j  }|dksdJ d| }|| j | }|t||dfS )Nr   r   r   )coverageexcede)r   r   r   r   r%   dict)r	   r   r   r   r&   r'   r   r   r   r   r   7   s   ""zLeakyDiceLoss.forward)r   r$   )r   r   r   r   r   r   r"   r   r   r
   r   r#   -   s    r#   c                   @   sH   e Zd ZdZg dZ		ddd	Zdd
dZdd Zdd Zdd Z	dS )InstanceDiceLossa  
    DiceLoss normalized on each instance.
    Input:
        pred: (N, 1, H, W)
        gt: (N, 1, H, W)
        mask: (N, H, W)
    Note: This class assume that input tensors are on gpu,
        while cput computation is required to find union areas.
    )meanr   none333333?皙?Nd   r   c                 C   sN   t j|  || _|| _|| _| jd u rd| _| j| jv sJ || _|| _d S )Nr*   )	nnModuler   	threshold
iou_thresh	reduction	REDUCTIONmax_regionsr   )r	   r1   r2   r3   r5   r   r   r   r   r   T   s   

zInstanceDiceLoss.__init__c                 C   s   |    }g }g }t|jd D ]@}|| }|dur!||}t|d \}}	t| j|	}	g }
td|	D ]}t	
||k|jt	j}|
| q7||
 q||fS )z
        Args:
            tensor_on_gpu: (N, 1, H, W)
            blur: Lambda. If exists, each instance will be blured using `blur`.
        r   Nr   )cpudetachnumpyranger   r   labelminr5   r    
from_numpytodevicetypefloat32append)r	   tensor_on_gpublurtensorinstance_mapsinstance_countsbatch_indexinstance	lable_mapinstance_countinstance_mapindexr   r   r   r:   `   s&   zInstanceDiceLoss.labelc                 C   s&   ||   }t||   ||   S r   )r   max)r	   r   r   overlapr   r   r   ioux   s   zInstanceDiceLoss.iouc                 C   s    |d u r|S |d u r|S || S r   r   )r	   destvaluer   r   r   replace_or_add|   s
   zInstanceDiceLoss.replace_or_addc                 C   s  t j  | || jk\}}| |\}}g }t|D ]\}}	|| }
|	d u s,|
d u r-qd }ttt|
}|	D ]]}d }t|
D ]7\}}| 	||| j
krx| || d || d || || dkt j }| ||}||v rx|| qA|d u r| || d || d || | }| ||}q9	 |D ]}| || || d || d || |
|  }q|d ur|| q| jdkr|}|S | jdv sJ t|}t|}| jdkr|| }|S )Nr   r+   )r   r*   r*   )r    cudasynchronizer:   r1   	enumeratesetr9   lenrO   r2   r   r?   r@   rR   removerA   r3   r   )r	   r   r   r   pred_label_maps_gt_label_mapslossesrG   gt_instance_mapspred_instance_mapssingle_lossmask_not_matchedgt_instance_mapinstance_lossinstance_indexpred_instance_map
match_lossr   countr   r   r   r      sb   





zInstanceDiceLoss.forward)r,   r-   Nr.   r   r   )
r   r   r   r   r4   r   r:   rO   rR   r   r   r   r   r   r)   H   s    	

r)   )r    torch.nnr/   r8   npcv2scipyr   r0   r   r#   r)   r   r   r   r   <module>   s    %