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).
Hello, thank you for using the code provided by Hasty. Please note that some code blocks might not be 100% complete and ready to be run as is. This is done intentionally as we focus on implementing only the most challenging parts that might be tough to pick up from scratch. View our code block as a LEGO block - you can’t use it as a standalone solution, but you can take it and add to your system to complement it. If you have questions about using the tool, please get in touch with us to get direct help from the Hasty team.
# 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())
Hasty is a unified agile ML platform for your entire Vision AI pipeline — with minimal integration effort for you.
Start for free Check out our services