Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
ca9babb8
Unverified
提交
ca9babb8
authored
1月 15, 2021
作者:
Glenn Jocher
提交者:
GitHub
1月 15, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add ComputeLoss() class (#1950)
上级
f4a78e1b
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
9 行增加
和
7 行删除
+9
-7
test.py
test.py
+4
-4
train.py
train.py
+5
-3
loss.py
utils/loss.py
+0
-0
没有找到文件。
test.py
浏览文件 @
ca9babb8
...
@@ -13,7 +13,6 @@ from models.experimental import attempt_load
...
@@ -13,7 +13,6 @@ from models.experimental import attempt_load
from
utils.datasets
import
create_dataloader
from
utils.datasets
import
create_dataloader
from
utils.general
import
coco80_to_coco91_class
,
check_dataset
,
check_file
,
check_img_size
,
check_requirements
,
\
from
utils.general
import
coco80_to_coco91_class
,
check_dataset
,
check_file
,
check_img_size
,
check_requirements
,
\
box_iou
,
non_max_suppression
,
scale_coords
,
xyxy2xywh
,
xywh2xyxy
,
set_logging
,
increment_path
,
colorstr
box_iou
,
non_max_suppression
,
scale_coords
,
xyxy2xywh
,
xywh2xyxy
,
set_logging
,
increment_path
,
colorstr
from
utils.loss
import
compute_loss
from
utils.metrics
import
ap_per_class
,
ConfusionMatrix
from
utils.metrics
import
ap_per_class
,
ConfusionMatrix
from
utils.plots
import
plot_images
,
output_to_target
,
plot_study_txt
from
utils.plots
import
plot_images
,
output_to_target
,
plot_study_txt
from
utils.torch_utils
import
select_device
,
time_synchronized
from
utils.torch_utils
import
select_device
,
time_synchronized
...
@@ -36,7 +35,8 @@ def test(data,
...
@@ -36,7 +35,8 @@ def test(data,
save_hybrid
=
False
,
# for hybrid auto-labelling
save_hybrid
=
False
,
# for hybrid auto-labelling
save_conf
=
False
,
# save auto-label confidences
save_conf
=
False
,
# save auto-label confidences
plots
=
True
,
plots
=
True
,
log_imgs
=
0
):
# number of logged images
log_imgs
=
0
,
# number of logged images
compute_loss
=
None
):
# Initialize/load model and set device
# Initialize/load model and set device
training
=
model
is
not
None
training
=
model
is
not
None
...
@@ -111,8 +111,8 @@ def test(data,
...
@@ -111,8 +111,8 @@ def test(data,
t0
+=
time_synchronized
()
-
t
t0
+=
time_synchronized
()
-
t
# Compute loss
# Compute loss
if
training
:
if
compute_loss
:
loss
+=
compute_loss
([
x
.
float
()
for
x
in
train_out
],
targets
,
model
)[
1
][:
3
]
# box, obj, cls
loss
+=
compute_loss
([
x
.
float
()
for
x
in
train_out
],
targets
)[
1
][:
3
]
# box, obj, cls
# Run NMS
# Run NMS
targets
[:,
2
:]
*=
torch
.
Tensor
([
width
,
height
,
width
,
height
])
.
to
(
device
)
# to pixels
targets
[:,
2
:]
*=
torch
.
Tensor
([
width
,
height
,
width
,
height
])
.
to
(
device
)
# to pixels
...
...
train.py
浏览文件 @
ca9babb8
...
@@ -29,7 +29,7 @@ from utils.general import labels_to_class_weights, increment_path, labels_to_ima
...
@@ -29,7 +29,7 @@ from utils.general import labels_to_class_weights, increment_path, labels_to_ima
fitness
,
strip_optimizer
,
get_latest_run
,
check_dataset
,
check_file
,
check_git_status
,
check_img_size
,
\
fitness
,
strip_optimizer
,
get_latest_run
,
check_dataset
,
check_file
,
check_git_status
,
check_img_size
,
\
check_requirements
,
print_mutation
,
set_logging
,
one_cycle
,
colorstr
check_requirements
,
print_mutation
,
set_logging
,
one_cycle
,
colorstr
from
utils.google_utils
import
attempt_download
from
utils.google_utils
import
attempt_download
from
utils.loss
import
compute_l
oss
from
utils.loss
import
ComputeL
oss
from
utils.plots
import
plot_images
,
plot_labels
,
plot_results
,
plot_evolution
from
utils.plots
import
plot_images
,
plot_labels
,
plot_results
,
plot_evolution
from
utils.torch_utils
import
ModelEMA
,
select_device
,
intersect_dicts
,
torch_distributed_zero_first
from
utils.torch_utils
import
ModelEMA
,
select_device
,
intersect_dicts
,
torch_distributed_zero_first
...
@@ -227,6 +227,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
...
@@ -227,6 +227,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
results
=
(
0
,
0
,
0
,
0
,
0
,
0
,
0
)
# P, R, mAP@.5, mAP@.5-.95, val_loss(box, obj, cls)
results
=
(
0
,
0
,
0
,
0
,
0
,
0
,
0
)
# P, R, mAP@.5, mAP@.5-.95, val_loss(box, obj, cls)
scheduler
.
last_epoch
=
start_epoch
-
1
# do not move
scheduler
.
last_epoch
=
start_epoch
-
1
# do not move
scaler
=
amp
.
GradScaler
(
enabled
=
cuda
)
scaler
=
amp
.
GradScaler
(
enabled
=
cuda
)
compute_loss
=
ComputeLoss
(
model
)
# init loss class
logger
.
info
(
f
'Image sizes {imgsz} train, {imgsz_test} test
\n
'
logger
.
info
(
f
'Image sizes {imgsz} train, {imgsz_test} test
\n
'
f
'Using {dataloader.num_workers} dataloader workers
\n
'
f
'Using {dataloader.num_workers} dataloader workers
\n
'
f
'Logging results to {save_dir}
\n
'
f
'Logging results to {save_dir}
\n
'
...
@@ -286,7 +287,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
...
@@ -286,7 +287,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
# Forward
# Forward
with
amp
.
autocast
(
enabled
=
cuda
):
with
amp
.
autocast
(
enabled
=
cuda
):
pred
=
model
(
imgs
)
# forward
pred
=
model
(
imgs
)
# forward
loss
,
loss_items
=
compute_loss
(
pred
,
targets
.
to
(
device
)
,
model
)
# loss scaled by batch_size
loss
,
loss_items
=
compute_loss
(
pred
,
targets
.
to
(
device
))
# loss scaled by batch_size
if
rank
!=
-
1
:
if
rank
!=
-
1
:
loss
*=
opt
.
world_size
# gradient averaged between devices in DDP mode
loss
*=
opt
.
world_size
# gradient averaged between devices in DDP mode
if
opt
.
quad
:
if
opt
.
quad
:
...
@@ -344,7 +345,8 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
...
@@ -344,7 +345,8 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
dataloader
=
testloader
,
dataloader
=
testloader
,
save_dir
=
save_dir
,
save_dir
=
save_dir
,
plots
=
plots
and
final_epoch
,
plots
=
plots
and
final_epoch
,
log_imgs
=
opt
.
log_imgs
if
wandb
else
0
)
log_imgs
=
opt
.
log_imgs
if
wandb
else
0
,
compute_loss
=
compute_loss
)
# Write
# Write
with
open
(
results_file
,
'a'
)
as
f
:
with
open
(
results_file
,
'a'
)
as
f
:
...
...
utils/loss.py
浏览文件 @
ca9babb8
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论