当前位置:网站首页>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边栏推荐
- 基于ThinkPHP5封装-tronapi-波场接口-源码无加密-可二开--附接口文档-作者详细指导-2022年6月30日08:45:27
- Basic syntax of unity script (4) - access to other game objects
- Terms related to JMeter performance test and performance test passing standards
- Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库
- Apache Doris Compaction优化百科全书
- kaniko官方文档 - Build Images In Kubernetes
- 60 个神级 VS Code 插件!!
- 60 divine vs Code plug-ins!!
- 黑马笔记---包装类,正则表达式,Arrays类
- 黑马笔记---List系列集合与泛型
猜你喜欢

RK356x U-Boot研究所(命令篇)3.3 env相关命令的用法

JS converts an array to a two-dimensional array based on the same value

杭州电子商务研究院:官网(网站)是私域的唯一形态

损失函数:DIOU loss手写实现

A keepalived high availability accident made me learn it again!

腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?

【C语言深度解剖】float变量在内存中存储原理&&指针变量与“零值”比较

Assertions of regular series

Rk356x u-boot Institute (command section) 3.2 usage of help command

postman 自动生成 curl 代码片段
随机推荐
Docker installation of mysql8 and sqlyong connection error 2058 solution [jottings]
ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accurately
Apache Doris Compaction优化百科全书
Derivation of Park transformation formula for motor control
Basic syntax of unity script (3) - accessing game object components
2022-06-23 帆软部分公式及sql生成(月份、季度取数)
When MySQL judges that the execution condition is null, it returns 0. Correct parameter count in the call to native function 'isnull',
Mysql根据经纬度查询半径多少以内的数据,画个圈圈查数据库
The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
mqtt-ros模拟发布一个自定义消息类型
Basic syntax of unity script (4) - access to other game objects
Dark horse notes - common date API
Matlab tips (22) matrix analysis -- stepwise regression
Open source of xinzhibao applet
Introduction to the renewal of substrate source code: the pledge amount is greatly reduced, and rocksdb can be completely disabled
Dark horse notes -- List series collections and generics
JMeter learning notes
第四批入围企业公示——年度TOP100智能网联供应商评选
An interesting thing happened in the project
Product manager professional knowledge 50 (7) - how to establish a complete set of user growth system?