当前位置:网站首页>Loss function: Diou loss handwriting implementation
Loss function: Diou loss handwriting implementation
2022-06-30 13:22:00 【Computer vision Archer】
Here's the pure diou Code
'''
Calculate two box The distance from the center point of d
'''
# d = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
d = math.sqrt((pred[:, -1] - target[:, -1]) ** 2 + (pred[:, -2] - target[:, -2]) ** 2)
# On the left x
pred_l = pred[:, -1] - pred[:, -1] / 2
target_l = target[:, -1] - target[:, -1] / 2
# above y
pred_t = pred[:, -2] - pred[:, -2] / 2
target_t = target[:, -2] - target[:, -2] / 2
# On the right x
pred_r = pred[:, -1] + pred[:, -1] / 2
target_r = target[:, -1] + target[:, -1] / 2
# Underside y
pred_b = pred[:, -2] + pred[:, -2] / 2
target_b = target[:, -2] + target[:, -2] / 2
'''
Calculate two box Of bound Diagonal distance of
'''
bound_l = torch.min(pred_l, target_l) # left
bound_r = torch.max(pred_r, target_r) # right
bound_t = torch.min(pred_t, target_t) # top
bound_b = torch.max(pred_b, target_b) # bottom
c = math.sqrt((bound_r - bound_l) ** 2 + (bound_b - bound_t) ** 2)
dloss = iou - (d ** 2) / (c ** 2)
loss = 1 - dloss.clamp(min=-1.0, max=1.0)
First step Calculate two box The distance from the center point of d
The first thing to know is pred and target What is the output of
pred[:,:2] first : Represents multiple pictures , the second :2 Represents the first two values , Represents the center point of the rectangle (Y,X)
pred[:,2:] first : Represents multiple pictures , the second 2: Express after Two numerical , Represents the length and width of the rectangle (H,W)
target[:,:2] Empathy ,
d =
Calculate the left and right upper and lower coordinates according to the above analysis lrtb
Then calculate the internal 2 The diagonal length of the smallest circumscribed rectangle of a rectangle c
d Is the distance between the center points of the two prediction rectangles
Accept the extremes below
A When the centers of the two boxes are aligned ,d/c=0,iou Probably 0-1
A When the two boxes are far apart ,d/c=1,iou=0
therefore d/c Belong to 0-1
dloss=iou-d/c Belong to -1 To 1
So set loss=1-dloss Belong to 0-2
Exhibition iou\giou\diou Code , This is a YOLOX Its own loss function , among dloss I wrote it myself
YOLOX Is downloaded from
GitHub - Megvii-BaseDetection/YOLOX: YOLOX is a high-performance anchor-free YOLO, exceeding yolov3~v5 with MegEngine, ONNX, TensorRT, ncnn, and OpenVINO supported. Documentation: https://yolox.readthedocs.io/YOLOX is a high-performance anchor-free YOLO, exceeding yolov3~v5 with MegEngine, ONNX, TensorRT, ncnn, and OpenVINO supported. Documentation: https://yolox.readthedocs.io/ - GitHub - Megvii-BaseDetection/YOLOX: YOLOX is a high-performance anchor-free YOLO, exceeding yolov3~v5 with MegEngine, ONNX, TensorRT, ncnn, and OpenVINO supported. Documentation: https://yolox.readthedocs.io/https://github.com/Megvii-BaseDetection/YOLOX
class IOUloss(nn.Module):
def __init__(self, reduction="none", loss_type="iou"):
super(IOUloss, self).__init__()
self.reduction = reduction
self.loss_type = loss_type
def forward(self, pred, target):
assert pred.shape[0] == target.shape[0]
pred = pred.view(-1, 4)
target = target.view(-1, 4)
tl = torch.max(
(pred[:, :2] - pred[:, 2:] / 2), (target[:, :2] - target[:, 2:] / 2)
)
# pred target All are [H,W,Y,X]
# (Y,X)-(H,W) top left corner
br = torch.min(
(pred[:, :2] + pred[:, 2:] / 2), (target[:, :2] + target[:, 2:] / 2)
)
# (X,Y)+(H,W) The lower right corner
area_p = torch.prod(pred[:, 2:], 1) # HxW
area_g = torch.prod(target[:, 2:], 1)
en = (tl < br).type(tl.type()).prod(dim=1)
area_i = torch.prod(br - tl, 1) * en
area_u = area_p + area_g - area_i
iou = (area_i) / (area_u + 1e-16)
if self.loss_type == "iou":
loss = 1 - iou ** 2
elif self.loss_type == "giou":
c_tl = torch.min(
(pred[:, :2] - pred[:, 2:] / 2), (target[:, :2] - target[:, 2:] / 2)
)
c_br = torch.max(
(pred[:, :2] + pred[:, 2:] / 2), (target[:, :2] + target[:, 2:] / 2)
)
area_c = torch.prod(c_br - c_tl, 1)
giou = iou - (area_c - area_u) / area_c.clamp(1e-16)
loss = 1 - giou.clamp(min=-1.0, max=1.0)
# pred[:, :2] pred[:, 2:]
# (Y,X) (H,W)
# target[:, :2] target[:, 2:]
# (Y,X) (H,W)
elif self.loss_type == "diou":
'''
Calculate two box The distance from the center point of d
'''
# d = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
d = math.sqrt((pred[:, -1] - target[:, -1]) ** 2 + (pred[:, -2] - target[:, -2]) ** 2)
# On the left x
pred_l = pred[:, -1] - pred[:, -1] / 2
target_l = target[:, -1] - target[:, -1] / 2
# above y
pred_t = pred[:, -2] - pred[:, -2] / 2
target_t = target[:, -2] - target[:, -2] / 2
# On the right x
pred_r = pred[:, -1] + pred[:, -1] / 2
target_r = target[:, -1] + target[:, -1] / 2
# Underside y
pred_b = pred[:, -2] + pred[:, -2] / 2
target_b = target[:, -2] + target[:, -2] / 2
'''
Calculate two box Of bound Diagonal distance of
'''
bound_l = torch.min(pred_l, target_l) # left
bound_r = torch.max(pred_r, target_r) # right
bound_t = torch.min(pred_t, target_t) # top
bound_b = torch.max(pred_b, target_b) # bottom
c = math.sqrt((bound_r - bound_l) ** 2 + (bound_b - bound_t) ** 2)
dloss = iou - (d ** 2) / (c ** 2)
loss = 1 - dloss.clamp(min=-1.0, max=1.0)
# Step1
# def DIoU(a, b):
# d = a.center_distance(b)
# c = a.bound_diagonal_distance(b)
# return IoU(a, b) - (d ** 2) / (c ** 2)
# Step2-1
# def center_distance(self, other):
# '''
# Calculate two box The distance from the center point of
# '''
# return euclidean_distance(self.center, other.center)
# Step2-2
# def euclidean_distance(p1, p2):
# '''
# Calculate the Euclidean distance between two points
# '''
# x1, y1 = p1
# x2, y2 = p2
# return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
# Step3
# def bound_diagonal_distance(self, other):
# '''
# Calculate two box Of bound Diagonal distance of
# '''
# bound = self.boundof(other)
# return euclidean_distance((bound.x, bound.y), (bound.r, bound.b))
# Step3-2
# def boundof(self, other):
# '''
# Calculation box and other The edge of the outsourcing box , bring 2 individual box The smallest rectangle in the box
# '''
# xmin = min(self.x, other.x)
# ymin = min(self.y, other.y)
# xmax = max(self.r, other.r)
# ymax = max(self.b, other.b)
# return BBox(xmin, ymin, xmax, ymax)
# Step3-3
# def euclidean_distance(p1, p2):
# '''
# Calculate the Euclidean distance between two points
# '''
# x1, y1 = p1
# x2, y2 = p2
# return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
if self.reduction == "mean":
loss = loss.mean()
elif self.reduction == "sum":
loss = loss.sum()
return loss
边栏推荐
- MySQL judges the calculation result and divides it by 100
- SQL编程问题,测试用例不通过
- DeFi“钱从哪来”?一个大多数人都没搞清楚的问题
- postman 自動生成 curl 代碼片段
- [recruitment (Guangzhou)] Chenggong Yi (Guangzhou) Net core middle and Senior Development Engineer
- Dark horse notes - common date API
- 排查问题的方法论(适用于任何多方合作中产生的问题排查)
- QT read / write excel--qxlsx worksheet display / hide status setting 4
- 正则系列之断言Assertions
- mysql拒绝访问、管理员身份打开的
猜你喜欢
微信小程序报错:TypeError: Cannot read property ‘setData‘ of undefined
【C】 In depth understanding of pointers and callback functions (Introduction to simulating qsort)
Idea 2021.3 golang error: rning: undefined behavior version of delve is too old for go version 1.18
黑马笔记---包装类,正则表达式,Arrays类
RK356x U-Boot研究所(命令篇)3.2 help命令的用法
Resource realization applet opening wechat official small store tutorial
DeFi“钱从哪来”?一个大多数人都没搞清楚的问题
[qnx hypervisor 2.2 user manual]6.2.3 communication between guest and external
JMeter learning notes
Goods and services - platform properties
随机推荐
ABAP工具箱 V1.0(附实现思路)
QT read / write excel--qxlsx worksheet display / hide status setting 4
On the simplification and acceleration of join operation
写信宝小程序开源
Docker installation of mysql8 and sqlyong connection error 2058 solution [jottings]
【C语言深度解剖】float变量在内存中存储原理&&指针变量与“零值”比较
ABAP toolbox v1.0 (with implementation ideas)
Apache Doris Compaction优化百科全书
Unity脚本的基础语法(4)-访问其他游戏对象
Dark horse notes - common date API
Database usage in QT
Basic syntax of unity script (1) - common operations of game objects
IDEA 2021.3 执行 golang 报错:RNING: undefined behavior version of Delve is too old for Go version 1.18
SQL编程问题,测试用例不通过
Dark horse notes - collection (common methods and traversal methods of collection)
微信小程序报错:TypeError: Cannot read property ‘setData‘ of undefined
Open source of xinzhibao applet
Write, append, read, and copy of golang files: examples of using bufio packages
The spiral matrix of the force buckle rotates together (you can understand it)
产品经理专业知识50篇(七)-如何建立一套完整的用户成长体系?