Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
8bc0027a
Unverified
提交
8bc0027a
authored
12月 16, 2020
作者:
Glenn Jocher
提交者:
GitHub
12月 16, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update loss criteria constructor (#1711)
上级
79972410
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
18 行增加
和
15 行删除
+18
-15
train.py
train.py
+4
-4
tutorial.ipynb
tutorial.ipynb
+1
-1
loss.py
utils/loss.py
+6
-6
torch_utils.py
utils/torch_utils.py
+7
-4
没有找到文件。
train.py
浏览文件 @
8bc0027a
import
argparse
import
logging
import
math
import
os
import
random
import
time
...
...
@@ -7,7 +8,6 @@ from pathlib import Path
from
threading
import
Thread
from
warnings
import
warn
import
math
import
numpy
as
np
import
torch.distributed
as
dist
import
torch.nn
as
nn
...
...
@@ -217,7 +217,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
model
.
nc
=
nc
# attach number of classes to model
model
.
hyp
=
hyp
# attach hyperparameters to model
model
.
gr
=
1.0
# iou loss ratio (obj_loss = 1.0 or iou)
model
.
class_weights
=
labels_to_class_weights
(
dataset
.
labels
,
nc
)
.
to
(
device
)
# attach class weights
model
.
class_weights
=
labels_to_class_weights
(
dataset
.
labels
,
nc
)
.
to
(
device
)
*
nc
# attach class weights
model
.
names
=
names
# Start training
...
...
@@ -238,7 +238,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
if
opt
.
image_weights
:
# Generate indices
if
rank
in
[
-
1
,
0
]:
cw
=
model
.
class_weights
.
cpu
()
.
numpy
()
*
(
1
-
maps
)
**
2
# class weights
cw
=
model
.
class_weights
.
cpu
()
.
numpy
()
*
(
1
-
maps
)
**
2
/
nc
# class weights
iw
=
labels_to_image_weights
(
dataset
.
labels
,
nc
=
nc
,
class_weights
=
cw
)
# image weights
dataset
.
indices
=
random
.
choices
(
range
(
dataset
.
n
),
weights
=
iw
,
k
=
dataset
.
n
)
# rand weighted idx
# Broadcast if DDP
...
...
@@ -330,7 +330,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
if
rank
in
[
-
1
,
0
]:
# mAP
if
ema
:
ema
.
update_attr
(
model
,
include
=
[
'yaml'
,
'nc'
,
'hyp'
,
'gr'
,
'names'
,
'stride'
])
ema
.
update_attr
(
model
,
include
=
[
'yaml'
,
'nc'
,
'hyp'
,
'gr'
,
'names'
,
'stride'
,
'class_weights'
])
final_epoch
=
epoch
+
1
==
epochs
if
not
opt
.
notest
or
final_epoch
:
# Calculate mAP
results
,
maps
,
times
=
test
.
test
(
opt
.
data
,
...
...
tutorial.ipynb
浏览文件 @
8bc0027a
...
...
@@ -1199,7 +1199,7 @@
"\n",
"m1 = lambda x: x * torch.sigmoid(x)\n",
"m2 = torch.nn.SiLU()\n",
"profile(x=torch.randn(16, 3, 640, 640), [m1, m2], n=100)"
"profile(x=torch.randn(16, 3, 640, 640),
ops=
[m1, m2], n=100)"
],
"execution_count": null,
"outputs": []
...
...
utils/loss.py
浏览文件 @
8bc0027a
...
...
@@ -57,8 +57,8 @@ class FocalLoss(nn.Module):
return
loss
.
sum
()
else
:
# 'none'
return
loss
class
QFocalLoss
(
nn
.
Module
):
# Wraps Quality focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)
def
__init__
(
self
,
loss_fcn
,
gamma
=
1.5
,
alpha
=
0.25
):
...
...
@@ -71,7 +71,7 @@ class QFocalLoss(nn.Module):
def
forward
(
self
,
pred
,
true
):
loss
=
self
.
loss_fcn
(
pred
,
true
)
pred_prob
=
torch
.
sigmoid
(
pred
)
# prob from logits
alpha_factor
=
true
*
self
.
alpha
+
(
1
-
true
)
*
(
1
-
self
.
alpha
)
modulating_factor
=
torch
.
abs
(
true
-
pred_prob
)
**
self
.
gamma
...
...
@@ -92,8 +92,8 @@ def compute_loss(p, targets, model): # predictions, targets, model
h
=
model
.
hyp
# hyperparameters
# Define criteria
BCEcls
=
nn
.
BCEWithLogitsLoss
(
pos_weight
=
torch
.
Tensor
([
h
[
'cls_pw'
]]))
.
to
(
device
)
BCEobj
=
nn
.
BCEWithLogitsLoss
(
pos_weight
=
torch
.
Tensor
([
h
[
'obj_pw'
]]))
.
to
(
device
)
BCEcls
=
nn
.
BCEWithLogitsLoss
(
pos_weight
=
torch
.
tensor
([
h
[
'cls_pw'
]],
device
=
device
))
# weight=model.class_weights
)
BCEobj
=
nn
.
BCEWithLogitsLoss
(
pos_weight
=
torch
.
tensor
([
h
[
'obj_pw'
]],
device
=
device
)
)
# Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3
cp
,
cn
=
smooth_BCE
(
eps
=
0.0
)
...
...
@@ -119,7 +119,7 @@ def compute_loss(p, targets, model): # predictions, targets, model
# Regression
pxy
=
ps
[:,
:
2
]
.
sigmoid
()
*
2.
-
0.5
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
)
# predicted box
iou
=
bbox_iou
(
pbox
.
T
,
tbox
[
i
],
x1y1x2y2
=
False
,
CIoU
=
True
)
# iou(prediction, target)
lbox
+=
(
1.0
-
iou
)
.
mean
()
# iou loss
...
...
utils/torch_utils.py
浏览文件 @
8bc0027a
...
...
@@ -81,8 +81,8 @@ def profile(x, ops, n=100, device=None):
# m1 = lambda x: x * torch.sigmoid(x)
# m2 = nn.SiLU()
# profile(x, [m1, m2], n=100) # profile speed over 100 iterations
device
=
device
or
torch
.
device
(
'cuda:0'
if
torch
.
cuda
.
is_available
()
else
'cpu'
)
device
=
device
or
torch
.
device
(
'cuda:0'
if
torch
.
cuda
.
is_available
()
else
'cpu'
)
x
=
x
.
to
(
device
)
x
.
requires_grad
=
True
print
(
torch
.
__version__
,
device
.
type
,
torch
.
cuda
.
get_device_properties
(
0
)
if
device
.
type
==
'cuda'
else
''
)
...
...
@@ -99,8 +99,11 @@ def profile(x, ops, n=100, device=None):
t
[
0
]
=
time_synchronized
()
y
=
m
(
x
)
t
[
1
]
=
time_synchronized
()
_
=
y
.
sum
()
.
backward
()
t
[
2
]
=
time_synchronized
()
try
:
_
=
y
.
sum
()
.
backward
()
t
[
2
]
=
time_synchronized
()
except
:
# no backward method
t
[
2
]
=
float
(
'nan'
)
dtf
+=
(
t
[
1
]
-
t
[
0
])
*
1000
/
n
# ms per op forward
dtb
+=
(
t
[
2
]
-
t
[
1
])
*
1000
/
n
# ms per op backward
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论