Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
b0ba101a
Unverified
提交
b0ba101a
authored
3月 20, 2022
作者:
Glenn Jocher
提交者:
GitHub
3月 20, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
`ComputeLoss()` indexing/speed improvements (#7048)
* device as class attribute * Update loss.py * Update loss.py * improve zeros * tensor split
上级
4effd064
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
19 行增加
和
18 行删除
+19
-18
loss.py
utils/loss.py
+19
-18
没有找到文件。
utils/loss.py
浏览文件 @
b0ba101a
...
@@ -89,9 +89,10 @@ class QFocalLoss(nn.Module):
...
@@ -89,9 +89,10 @@ class QFocalLoss(nn.Module):
class
ComputeLoss
:
class
ComputeLoss
:
sort_obj_iou
=
False
# Compute losses
# Compute losses
def
__init__
(
self
,
model
,
autobalance
=
False
):
def
__init__
(
self
,
model
,
autobalance
=
False
):
self
.
sort_obj_iou
=
False
device
=
next
(
model
.
parameters
())
.
device
# get model device
device
=
next
(
model
.
parameters
())
.
device
# get model device
h
=
model
.
hyp
# hyperparameters
h
=
model
.
hyp
# hyperparameters
...
@@ -111,26 +112,28 @@ class ComputeLoss:
...
@@ -111,26 +112,28 @@ class ComputeLoss:
self
.
balance
=
{
3
:
[
4.0
,
1.0
,
0.4
]}
.
get
(
det
.
nl
,
[
4.0
,
1.0
,
0.25
,
0.06
,
0.02
])
# P3-P7
self
.
balance
=
{
3
:
[
4.0
,
1.0
,
0.4
]}
.
get
(
det
.
nl
,
[
4.0
,
1.0
,
0.25
,
0.06
,
0.02
])
# P3-P7
self
.
ssi
=
list
(
det
.
stride
)
.
index
(
16
)
if
autobalance
else
0
# stride 16 index
self
.
ssi
=
list
(
det
.
stride
)
.
index
(
16
)
if
autobalance
else
0
# stride 16 index
self
.
BCEcls
,
self
.
BCEobj
,
self
.
gr
,
self
.
hyp
,
self
.
autobalance
=
BCEcls
,
BCEobj
,
1.0
,
h
,
autobalance
self
.
BCEcls
,
self
.
BCEobj
,
self
.
gr
,
self
.
hyp
,
self
.
autobalance
=
BCEcls
,
BCEobj
,
1.0
,
h
,
autobalance
self
.
device
=
device
for
k
in
'na'
,
'nc'
,
'nl'
,
'anchors'
:
for
k
in
'na'
,
'nc'
,
'nl'
,
'anchors'
:
setattr
(
self
,
k
,
getattr
(
det
,
k
))
setattr
(
self
,
k
,
getattr
(
det
,
k
))
def
__call__
(
self
,
p
,
targets
):
# predictions, targets, model
def
__call__
(
self
,
p
,
targets
):
# predictions, targets
device
=
targets
.
device
lcls
=
torch
.
zeros
(
1
,
device
=
self
.
device
)
# class loss
lcls
,
lbox
,
lobj
=
torch
.
zeros
(
1
,
device
=
device
),
torch
.
zeros
(
1
,
device
=
device
),
torch
.
zeros
(
1
,
device
=
device
)
lbox
=
torch
.
zeros
(
1
,
device
=
self
.
device
)
# box loss
lobj
=
torch
.
zeros
(
1
,
device
=
self
.
device
)
# object loss
tcls
,
tbox
,
indices
,
anchors
=
self
.
build_targets
(
p
,
targets
)
# targets
tcls
,
tbox
,
indices
,
anchors
=
self
.
build_targets
(
p
,
targets
)
# targets
# Losses
# Losses
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
],
device
=
device
)
# target obj
tobj
=
torch
.
zeros
(
pi
.
shape
[:
4
],
device
=
self
.
device
)
# target obj
n
=
b
.
shape
[
0
]
# number of targets
n
=
b
.
shape
[
0
]
# number of targets
if
n
:
if
n
:
p
s
=
pi
[
b
,
a
,
gj
,
gi
]
# prediction subset corresponding to target
s
p
xy
,
pwh
,
_
,
pcls
=
pi
[
b
,
a
,
gj
,
gi
]
.
tensor_split
((
2
,
4
,
5
),
dim
=
1
)
# target-subset of prediction
s
# Regression
# Regression
pxy
=
p
s
[:,
:
2
]
.
sigmoid
()
*
2
-
0.5
pxy
=
p
xy
.
sigmoid
()
*
2
-
0.5
pwh
=
(
p
s
[:,
2
:
4
]
.
sigmoid
()
*
2
)
**
2
*
anchors
[
i
]
pwh
=
(
p
wh
.
sigmoid
()
*
2
)
**
2
*
anchors
[
i
]
pbox
=
torch
.
cat
((
pxy
,
pwh
),
1
)
# predicted box
pbox
=
torch
.
cat
((
pxy
,
pwh
),
1
)
# predicted box
iou
=
bbox_iou
(
pbox
.
T
,
tbox
[
i
],
x1y1x2y2
=
False
,
CIoU
=
True
)
# iou(prediction, target)
iou
=
bbox_iou
(
pbox
.
T
,
tbox
[
i
],
x1y1x2y2
=
False
,
CIoU
=
True
)
# iou(prediction, target)
lbox
+=
(
1.0
-
iou
)
.
mean
()
# iou loss
lbox
+=
(
1.0
-
iou
)
.
mean
()
# iou loss
...
@@ -144,9 +147,9 @@ class ComputeLoss:
...
@@ -144,9 +147,9 @@ class ComputeLoss:
# Classification
# Classification
if
self
.
nc
>
1
:
# cls loss (only if multiple classes)
if
self
.
nc
>
1
:
# cls loss (only if multiple classes)
t
=
torch
.
full_like
(
p
s
[:,
5
:],
self
.
cn
,
device
=
device
)
# targets
t
=
torch
.
full_like
(
p
cls
,
self
.
cn
,
device
=
self
.
device
)
# targets
t
[
range
(
n
),
tcls
[
i
]]
=
self
.
cp
t
[
range
(
n
),
tcls
[
i
]]
=
self
.
cp
lcls
+=
self
.
BCEcls
(
p
s
[:,
5
:]
,
t
)
# BCE
lcls
+=
self
.
BCEcls
(
p
cls
,
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:
...
@@ -170,15 +173,15 @@ class ComputeLoss:
...
@@ -170,15 +173,15 @@ class ComputeLoss:
# 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)
na
,
nt
=
self
.
na
,
targets
.
shape
[
0
]
# number of anchors, targets
na
,
nt
=
self
.
na
,
targets
.
shape
[
0
]
# number of anchors, targets
tcls
,
tbox
,
indices
,
anch
=
[],
[],
[],
[]
tcls
,
tbox
,
indices
,
anch
=
[],
[],
[],
[]
gain
=
torch
.
ones
(
7
,
device
=
targets
.
device
)
# normalized to gridspace gain
gain
=
torch
.
ones
(
7
,
device
=
self
.
device
)
# normalized to gridspace gain
ai
=
torch
.
arange
(
na
,
device
=
targets
.
device
)
.
float
()
.
view
(
na
,
1
)
.
repeat
(
1
,
nt
)
# same as .repeat_interleave(nt)
ai
=
torch
.
arange
(
na
,
device
=
self
.
device
)
.
float
()
.
view
(
na
,
1
)
.
repeat
(
1
,
nt
)
# same as .repeat_interleave(nt)
targets
=
torch
.
cat
((
targets
.
repeat
(
na
,
1
,
1
),
ai
[:,
:,
None
]),
2
)
# append anchor indices
targets
=
torch
.
cat
((
targets
.
repeat
(
na
,
1
,
1
),
ai
[:,
:,
None
]),
2
)
# append anchor indices
g
=
0.5
# bias
g
=
0.5
# bias
off
=
torch
.
tensor
([[
0
,
0
],
off
=
torch
.
tensor
([[
0
,
0
],
[
1
,
0
],
[
0
,
1
],
[
-
1
,
0
],
[
0
,
-
1
],
# j,k,l,m
[
1
,
0
],
[
0
,
1
],
[
-
1
,
0
],
[
0
,
-
1
],
# j,k,l,m
# [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
# [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
],
device
=
targets
.
device
)
.
float
()
*
g
# offsets
],
device
=
self
.
device
)
.
float
()
*
g
# offsets
for
i
in
range
(
self
.
nl
):
for
i
in
range
(
self
.
nl
):
anchors
=
self
.
anchors
[
i
]
anchors
=
self
.
anchors
[
i
]
...
@@ -206,14 +209,12 @@ class ComputeLoss:
...
@@ -206,14 +209,12 @@ class ComputeLoss:
offsets
=
0
offsets
=
0
# Define
# Define
b
,
c
=
t
[:,
:
2
]
.
long
()
.
T
# image, class
bc
,
gxy
,
gwh
,
a
=
t
.
unsafe_chunk
(
4
,
dim
=
1
)
# (image, class), grid xy, grid wh, anchors
gxy
=
t
[:,
2
:
4
]
# grid xy
a
,
(
b
,
c
)
=
a
.
long
()
.
view
(
-
1
),
bc
.
long
()
.
T
# anchors, image, class
gwh
=
t
[:,
4
:
6
]
# grid wh
gij
=
(
gxy
-
offsets
)
.
long
()
gij
=
(
gxy
-
offsets
)
.
long
()
gi
,
gj
=
gij
.
T
# grid
xy
indices
gi
,
gj
=
gij
.
T
# grid indices
# Append
# Append
a
=
t
[:,
6
]
.
long
()
# anchor indices
indices
.
append
((
b
,
a
,
gj
.
clamp_
(
0
,
gain
[
3
]
-
1
),
gi
.
clamp_
(
0
,
gain
[
2
]
-
1
)))
# image, anchor, grid indices
indices
.
append
((
b
,
a
,
gj
.
clamp_
(
0
,
gain
[
3
]
-
1
),
gi
.
clamp_
(
0
,
gain
[
2
]
-
1
)))
# 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
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论