CrossEntropyIoULoss2D

CrossEntropyIoULoss2D is a combination of the Generalized Intersection over Union and Cross-Entropy losses. In simple words, it is the average of the outputs of these two losses. As of today, none of the Deep Learning frameworks has a built-in CrossEntropyIoULoss2D, so it has to be implemented manually (you can use the code below as an example).

Code implementation

PyTorch

        # importing the library
import torch
import torch.nn as nn
import torchvision.ops as ops
 
# Cross-Entropy Loss
 
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
 
cross_entropy_loss = nn.CrossEntropyLoss()
output_cross_entropy = cross_entropy_loss(input, target)
output_cross_entropy.backward()

# Generalized IoU Loss

input = torch.Tensor([[1, 1, 2, 2], [2, 2, 3, 3], [3, 3, 4, 4]])
target = torch.Tensor([[0, 0, 2, 2], [2, 2, 4, 4], [4, 4, 6, 6]])

generalized_iou_loss = ops.generalized_box_iou(input, target)

 
print('output: ', torch.cat([generalized_iou_loss.mean().view(1, 1), 
                            output_cross_entropy.view(1,1)]).mean())
    
Last updated on Jul 18, 2022

Removing the risk from vision AI.

Only 13% of vision AI projects make it to production, with Hasty we boost that number to 100%.