Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
bb87276d
Unverified
提交
bb87276d
authored
7月 31, 2020
作者:
Glenn Jocher
提交者:
GitHub
7月 31, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update build_targets() (#589)
Signed-off-by:
Glenn Jocher
<
glenn.jocher@ultralytics.com
>
上级
8bf3cff8
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
48 行增加
和
55 行删除
+48
-55
utils.py
utils/utils.py
+48
-55
没有找到文件。
utils/utils.py
浏览文件 @
bb87276d
...
@@ -308,7 +308,7 @@ def compute_ap(recall, precision):
...
@@ -308,7 +308,7 @@ def compute_ap(recall, precision):
def
bbox_iou
(
box1
,
box2
,
x1y1x2y2
=
True
,
GIoU
=
False
,
DIoU
=
False
,
CIoU
=
False
):
def
bbox_iou
(
box1
,
box2
,
x1y1x2y2
=
True
,
GIoU
=
False
,
DIoU
=
False
,
CIoU
=
False
):
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
box2
=
box2
.
t
()
box2
=
box2
.
T
# Get the coordinates of bounding boxes
# Get the coordinates of bounding boxes
if
x1y1x2y2
:
# x1, y1, x2, y2 = box1
if
x1y1x2y2
:
# x1, y1, x2, y2 = box1
...
@@ -347,7 +347,7 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
...
@@ -347,7 +347,7 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
v
=
(
4
/
math
.
pi
**
2
)
*
torch
.
pow
(
torch
.
atan
(
w2
/
h2
)
-
torch
.
atan
(
w1
/
h1
),
2
)
v
=
(
4
/
math
.
pi
**
2
)
*
torch
.
pow
(
torch
.
atan
(
w2
/
h2
)
-
torch
.
atan
(
w1
/
h1
),
2
)
with
torch
.
no_grad
():
with
torch
.
no_grad
():
alpha
=
v
/
(
1
-
iou
+
v
+
1e-16
)
alpha
=
v
/
(
1
-
iou
+
v
+
1e-16
)
return
iou
-
(
rho2
/
c2
+
v
*
alpha
)
# CIoU
return
iou
-
(
rho2
/
c2
+
v
*
alpha
)
# CIoU
return
iou
return
iou
...
@@ -369,8 +369,8 @@ def box_iou(box1, box2):
...
@@ -369,8 +369,8 @@ def box_iou(box1, box2):
# box = 4xn
# box = 4xn
return
(
box
[
2
]
-
box
[
0
])
*
(
box
[
3
]
-
box
[
1
])
return
(
box
[
2
]
-
box
[
0
])
*
(
box
[
3
]
-
box
[
1
])
area1
=
box_area
(
box1
.
t
()
)
area1
=
box_area
(
box1
.
T
)
area2
=
box_area
(
box2
.
t
()
)
area2
=
box_area
(
box2
.
T
)
# inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
# inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
inter
=
(
torch
.
min
(
box1
[:,
None
,
2
:],
box2
[:,
2
:])
-
torch
.
max
(
box1
[:,
None
,
:
2
],
box2
[:,
:
2
]))
.
clamp
(
0
)
.
prod
(
2
)
inter
=
(
torch
.
min
(
box1
[:,
None
,
2
:],
box2
[:,
2
:])
-
torch
.
max
(
box1
[:,
None
,
:
2
],
box2
[:,
:
2
]))
.
clamp
(
0
)
.
prod
(
2
)
...
@@ -439,70 +439,62 @@ class BCEBlurWithLogitsLoss(nn.Module):
...
@@ -439,70 +439,62 @@ class BCEBlurWithLogitsLoss(nn.Module):
def
compute_loss
(
p
,
targets
,
model
):
# predictions, targets, model
def
compute_loss
(
p
,
targets
,
model
):
# predictions, targets, model
device
=
targets
.
device
device
=
targets
.
device
ft
=
torch
.
cuda
.
FloatTensor
if
p
[
0
]
.
is_cuda
else
torch
.
Tensor
lcls
,
lbox
,
lobj
=
torch
.
zeros
(
3
,
1
,
device
=
device
)
lcls
,
lbox
,
lobj
=
ft
([
0
])
.
to
(
device
),
ft
([
0
])
.
to
(
device
),
ft
([
0
])
.
to
(
device
)
tcls
,
tbox
,
indices
,
anchors
=
build_targets
(
p
,
targets
,
model
)
# targets
tcls
,
tbox
,
indices
,
anchors
=
build_targets
(
p
,
targets
,
model
)
# targets
h
=
model
.
hyp
# hyperparameters
h
=
model
.
hyp
# hyperparameters
red
=
'mean'
# Loss reduction (sum or mean)
# Define criteria
# Define criteria
BCEcls
=
nn
.
BCEWithLogitsLoss
(
pos_weight
=
ft
([
h
[
'cls_pw'
]]),
reduction
=
red
)
.
to
(
device
)
BCEcls
=
nn
.
BCEWithLogitsLoss
(
pos_weight
=
torch
.
Tensor
([
h
[
'cls_pw'
]])
)
.
to
(
device
)
BCEobj
=
nn
.
BCEWithLogitsLoss
(
pos_weight
=
ft
([
h
[
'obj_pw'
]]),
reduction
=
red
)
.
to
(
device
)
BCEobj
=
nn
.
BCEWithLogitsLoss
(
pos_weight
=
torch
.
Tensor
([
h
[
'obj_pw'
]])
)
.
to
(
device
)
#
c
lass label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3
#
C
lass label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3
cp
,
cn
=
smooth_BCE
(
eps
=
0.0
)
cp
,
cn
=
smooth_BCE
(
eps
=
0.0
)
#
f
ocal loss
#
F
ocal loss
g
=
h
[
'fl_gamma'
]
# focal loss gamma
g
=
h
[
'fl_gamma'
]
# focal loss gamma
if
g
>
0
:
if
g
>
0
:
BCEcls
,
BCEobj
=
FocalLoss
(
BCEcls
,
g
),
FocalLoss
(
BCEobj
,
g
)
BCEcls
,
BCEobj
=
FocalLoss
(
BCEcls
,
g
),
FocalLoss
(
BCEobj
,
g
)
#
per output
#
Losses
nt
=
0
# number of targets
nt
=
0
# number of targets
np
=
len
(
p
)
# number of outputs
np
=
len
(
p
)
# number of outputs
balance
=
[
4.0
,
1.0
,
0.4
]
if
np
==
3
else
[
4.0
,
1.0
,
0.4
,
0.1
]
# P3-5 or P3-6
balance
=
[
4.0
,
1.0
,
0.4
]
if
np
==
3
else
[
4.0
,
1.0
,
0.4
,
0.1
]
# P3-5 or P3-6
for
i
,
pi
in
enumerate
(
p
):
# layer index, layer predictions
for
i
,
pi
in
enumerate
(
p
):
# layer index, layer predictions
b
,
a
,
gj
,
gi
=
indices
[
i
]
# image, anchor, gridy, gridx
b
,
a
,
gj
,
gi
=
indices
[
i
]
# image, anchor, gridy, gridx
tobj
=
torch
.
zeros_like
(
pi
[
...
,
0
]
)
.
to
(
device
)
# target obj
tobj
=
torch
.
zeros_like
(
pi
[
...
,
0
]
,
device
=
device
)
# target obj
n
b
=
b
.
shape
[
0
]
# number of targets
n
=
b
.
shape
[
0
]
# number of targets
if
n
b
:
if
n
:
nt
+=
n
b
# cumulative targets
nt
+=
n
# cumulative targets
ps
=
pi
[
b
,
a
,
gj
,
gi
]
# prediction subset corresponding to targets
ps
=
pi
[
b
,
a
,
gj
,
gi
]
# prediction subset corresponding to targets
#
GIoU
#
Regression
pxy
=
ps
[:,
:
2
]
.
sigmoid
()
*
2.
-
0.5
pxy
=
ps
[:,
:
2
]
.
sigmoid
()
*
2.
-
0.5
pwh
=
(
ps
[:,
2
:
4
]
.
sigmoid
()
*
2
)
**
2
*
anchors
[
i
]
pwh
=
(
ps
[:,
2
:
4
]
.
sigmoid
()
*
2
)
**
2
*
anchors
[
i
]
pbox
=
torch
.
cat
((
pxy
,
pwh
),
1
)
.
to
(
device
)
# predicted box
pbox
=
torch
.
cat
((
pxy
,
pwh
),
1
)
.
to
(
device
)
# predicted box
giou
=
bbox_iou
(
pbox
.
t
(),
tbox
[
i
],
x1y1x2y2
=
False
,
G
IoU
=
True
)
# giou(prediction, target)
giou
=
bbox_iou
(
pbox
.
T
,
tbox
[
i
],
x1y1x2y2
=
False
,
C
IoU
=
True
)
# giou(prediction, target)
lbox
+=
(
1.0
-
giou
)
.
sum
()
if
red
==
'sum'
else
(
1.0
-
giou
)
.
mean
()
# giou loss
lbox
+=
(
1.0
-
giou
)
.
mean
()
# giou loss
# Obj
# Obj
ectness
tobj
[
b
,
a
,
gj
,
gi
]
=
(
1.0
-
model
.
gr
)
+
model
.
gr
*
giou
.
detach
()
.
clamp
(
0
)
.
type
(
tobj
.
dtype
)
# giou ratio
tobj
[
b
,
a
,
gj
,
gi
]
=
(
1.0
-
model
.
gr
)
+
model
.
gr
*
giou
.
detach
()
.
clamp
(
0
)
.
type
(
tobj
.
dtype
)
# giou ratio
# Class
# Class
ification
if
model
.
nc
>
1
:
# cls loss (only if multiple classes)
if
model
.
nc
>
1
:
# cls loss (only if multiple classes)
t
=
torch
.
full_like
(
ps
[:,
5
:],
cn
)
.
to
(
device
)
# targets
t
=
torch
.
full_like
(
ps
[:,
5
:],
cn
,
device
=
device
)
# targets
t
[
range
(
n
b
),
tcls
[
i
]]
=
cp
t
[
range
(
n
),
tcls
[
i
]]
=
cp
lcls
+=
BCEcls
(
ps
[:,
5
:],
t
)
# BCE
lcls
=
lcls
+
BCEcls
(
ps
[:,
5
:],
t
)
# BCE
# Append targets to text file
# Append targets to text file
# with open('targets.txt', 'a') as file:
# with open('targets.txt', 'a') as file:
# [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)]
# [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)]
lobj
+=
BCEobj
(
pi
[
...
,
4
],
tobj
)
*
balance
[
i
]
# obj loss
lobj
=
lobj
+
BCEobj
(
pi
[
...
,
4
],
tobj
)
*
balance
[
i
]
# obj loss
s
=
3
/
np
# output count scaling
s
=
3
/
np
# output count scaling
lbox
*=
h
[
'giou'
]
*
s
lbox
*=
h
[
'giou'
]
*
s
lobj
*=
h
[
'obj'
]
*
s
*
(
1.4
if
np
==
4
else
1.
)
lobj
*=
h
[
'obj'
]
*
s
*
(
1.4
if
np
==
4
else
1.
)
lcls
*=
h
[
'cls'
]
*
s
lcls
*=
h
[
'cls'
]
*
s
bs
=
tobj
.
shape
[
0
]
# batch size
bs
=
tobj
.
shape
[
0
]
# batch size
if
red
==
'sum'
:
g
=
3.0
# loss gain
lobj
*=
g
/
bs
if
nt
:
lcls
*=
g
/
nt
/
model
.
nc
lbox
*=
g
/
nt
loss
=
lbox
+
lobj
+
lcls
loss
=
lbox
+
lobj
+
lcls
return
loss
*
bs
,
torch
.
cat
((
lbox
,
lobj
,
lcls
,
loss
))
.
detach
()
return
loss
*
bs
,
torch
.
cat
((
lbox
,
lobj
,
lcls
,
loss
))
.
detach
()
...
@@ -510,40 +502,40 @@ def compute_loss(p, targets, model): # predictions, targets, model
...
@@ -510,40 +502,40 @@ def compute_loss(p, targets, model): # predictions, targets, model
def
build_targets
(
p
,
targets
,
model
):
def
build_targets
(
p
,
targets
,
model
):
# Build targets for compute_loss(), input targets(image,class,x,y,w,h)
# Build targets for compute_loss(), input targets(image,class,x,y,w,h)
det
=
model
.
module
.
model
[
-
1
]
if
type
(
model
)
in
(
nn
.
parallel
.
DataParallel
,
nn
.
parallel
.
DistributedDataParallel
)
\
det
=
model
.
module
.
model
[
-
1
]
if
torch_utils
.
is_parallel
(
model
)
else
model
.
model
[
-
1
]
# Detect() module
else
model
.
model
[
-
1
]
# Detect() module
na
,
nt
=
det
.
na
,
targets
.
shape
[
0
]
# number of anchors, targets
na
,
nt
=
det
.
na
,
targets
.
shape
[
0
]
# number of anchors, targets
tcls
,
tbox
,
indices
,
anch
=
[],
[],
[],
[]
tcls
,
tbox
,
indices
,
anch
=
[],
[],
[],
[]
gain
=
torch
.
ones
(
6
,
device
=
targets
.
device
)
# normalized to gridspace gain
gain
=
torch
.
ones
(
7
,
device
=
targets
.
device
)
# normalized to gridspace gain
off
=
torch
.
tensor
([[
1
,
0
],
[
0
,
1
],
[
-
1
,
0
],
[
0
,
-
1
]],
device
=
targets
.
device
)
.
float
()
# overlap offsets
ai
=
torch
.
arange
(
na
,
device
=
targets
.
device
)
.
float
()
.
view
(
na
,
1
)
.
repeat
(
1
,
nt
)
# same as .repeat_interleave(nt)
at
=
torch
.
arange
(
na
)
.
view
(
na
,
1
)
.
repeat
(
1
,
nt
)
# anchor tensor, same as .repeat_interleave(nt)
targets
=
torch
.
cat
((
targets
.
repeat
(
na
,
1
,
1
),
ai
[:,
:,
None
]),
2
)
# append anchor indices
g
=
0.5
# bias
off
=
torch
.
tensor
([[
0
,
0
],
[
1
,
0
],
[
0
,
1
],
[
-
1
,
0
],
[
0
,
-
1
],
# j,k,l,m
# [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
],
device
=
targets
.
device
)
.
float
()
*
g
# offsets
g
=
0.5
# offset
style
=
'rect4'
for
i
in
range
(
det
.
nl
):
for
i
in
range
(
det
.
nl
):
anchors
=
det
.
anchors
[
i
]
anchors
=
det
.
anchors
[
i
]
gain
[
2
:]
=
torch
.
tensor
(
p
[
i
]
.
shape
)[[
3
,
2
,
3
,
2
]]
# xyxy gain
gain
[
2
:
6
]
=
torch
.
tensor
(
p
[
i
]
.
shape
)[[
3
,
2
,
3
,
2
]]
# xyxy gain
# Match targets to anchors
# Match targets to anchors
a
,
t
,
offsets
=
[],
targets
*
gain
,
0
t
,
offsets
=
targets
*
gain
,
0
if
nt
:
if
nt
:
r
=
t
[
None
,
:,
4
:
6
]
/
anchors
[:,
None
]
# wh ratio
# Matches
r
=
t
[:,
:,
4
:
6
]
/
anchors
[:,
None
]
# wh ratio
j
=
torch
.
max
(
r
,
1.
/
r
)
.
max
(
2
)[
0
]
<
model
.
hyp
[
'anchor_t'
]
# compare
j
=
torch
.
max
(
r
,
1.
/
r
)
.
max
(
2
)[
0
]
<
model
.
hyp
[
'anchor_t'
]
# compare
# j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)
=
wh_iou(anchors(3,2), gwh(n,2))
# j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)
=
wh_iou(anchors(3,2), gwh(n,2))
a
,
t
=
at
[
j
],
t
.
repeat
(
na
,
1
,
1
)
[
j
]
# filter
t
=
t
[
j
]
# filter
#
overlap
s
#
Offset
s
gxy
=
t
[:,
2
:
4
]
# grid xy
gxy
=
t
[:,
2
:
4
]
# grid xy
z
=
torch
.
zeros_like
(
gxy
)
gxi
=
gain
[[
2
,
3
]]
-
gxy
# inverse
if
style
==
'rect2'
:
j
,
k
=
((
gxy
%
1.
<
g
)
&
(
gxy
>
1.
))
.
T
j
,
k
=
((
gxy
%
1.
<
g
)
&
(
gxy
>
1.
))
.
T
l
,
m
=
((
gxi
%
1.
<
g
)
&
(
gxi
>
1.
))
.
T
a
,
t
=
torch
.
cat
((
a
,
a
[
j
],
a
[
k
]),
0
),
torch
.
cat
((
t
,
t
[
j
],
t
[
k
]),
0
)
j
=
torch
.
stack
((
torch
.
ones_like
(
j
),
j
,
k
,
l
,
m
))
offsets
=
torch
.
cat
((
z
,
z
[
j
]
+
off
[
0
],
z
[
k
]
+
off
[
1
]),
0
)
*
g
t
=
t
.
repeat
((
5
,
1
,
1
))[
j
]
elif
style
==
'rect4'
:
offsets
=
(
torch
.
zeros_like
(
gxy
)[
None
]
+
off
[:,
None
])[
j
]
j
,
k
=
((
gxy
%
1.
<
g
)
&
(
gxy
>
1.
))
.
T
l
,
m
=
((
gxy
%
1.
>
(
1
-
g
))
&
(
gxy
<
(
gain
[[
2
,
3
]]
-
1.
)))
.
T
a
,
t
=
torch
.
cat
((
a
,
a
[
j
],
a
[
k
],
a
[
l
],
a
[
m
]),
0
),
torch
.
cat
((
t
,
t
[
j
],
t
[
k
],
t
[
l
],
t
[
m
]),
0
)
offsets
=
torch
.
cat
((
z
,
z
[
j
]
+
off
[
0
],
z
[
k
]
+
off
[
1
],
z
[
l
]
+
off
[
2
],
z
[
m
]
+
off
[
3
]),
0
)
*
g
# Define
# Define
b
,
c
=
t
[:,
:
2
]
.
long
()
.
T
# image, class
b
,
c
=
t
[:,
:
2
]
.
long
()
.
T
# image, class
...
@@ -553,6 +545,7 @@ def build_targets(p, targets, model):
...
@@ -553,6 +545,7 @@ def build_targets(p, targets, model):
gi
,
gj
=
gij
.
T
# grid xy indices
gi
,
gj
=
gij
.
T
# grid xy indices
# Append
# Append
a
=
t
[:,
6
]
.
long
()
# anchor indices
indices
.
append
((
b
,
a
,
gj
,
gi
))
# image, anchor, grid indices
indices
.
append
((
b
,
a
,
gj
,
gi
))
# image, anchor, grid indices
tbox
.
append
(
torch
.
cat
((
gxy
-
gij
,
gwh
),
1
))
# box
tbox
.
append
(
torch
.
cat
((
gxy
-
gij
,
gwh
),
1
))
# box
anch
.
append
(
anchors
[
a
])
# anchors
anch
.
append
(
anchors
[
a
])
# anchors
...
@@ -599,7 +592,7 @@ def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, merge=False,
...
@@ -599,7 +592,7 @@ def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, merge=False,
# Detections matrix nx6 (xyxy, conf, cls)
# Detections matrix nx6 (xyxy, conf, cls)
if
multi_label
:
if
multi_label
:
i
,
j
=
(
x
[:,
5
:]
>
conf_thres
)
.
nonzero
(
as_tuple
=
False
)
.
t
()
i
,
j
=
(
x
[:,
5
:]
>
conf_thres
)
.
nonzero
(
as_tuple
=
False
)
.
T
x
=
torch
.
cat
((
box
[
i
],
x
[
i
,
j
+
5
,
None
],
j
[:,
None
]
.
float
()),
1
)
x
=
torch
.
cat
((
box
[
i
],
x
[
i
,
j
+
5
,
None
],
j
[:,
None
]
.
float
()),
1
)
else
:
# best class only
else
:
# best class only
conf
,
j
=
x
[:,
5
:]
.
max
(
1
,
keepdim
=
True
)
conf
,
j
=
x
[:,
5
:]
.
max
(
1
,
keepdim
=
True
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论