Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
b569ed6d
提交
b569ed6d
authored
7月 20, 2020
作者:
Glenn Jocher
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pretrained model loading bug fix (#450)
Signed-off-by:
Glenn Jocher
<
glenn.jocher@ultralytics.com
>
上级
07a82f4d
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
11 行增加
和
8 行删除
+11
-8
train.py
train.py
+11
-8
没有找到文件。
train.py
浏览文件 @
b569ed6d
import
argparse
import
argparse
import
torch
import
torch.distributed
as
dist
import
torch.distributed
as
dist
import
torch.nn.functional
as
F
import
torch.nn.functional
as
F
import
torch.optim
as
optim
import
torch.optim
as
optim
import
torch.optim.lr_scheduler
as
lr_scheduler
import
torch.optim.lr_scheduler
as
lr_scheduler
import
torch.utils.data
import
torch.utils.data
from
torch.utils.tensorboard
import
SummaryWriter
from
torch.nn.parallel
import
DistributedDataParallel
as
DDP
from
torch.nn.parallel
import
DistributedDataParallel
as
DDP
from
torch.utils.tensorboard
import
SummaryWriter
import
test
# import test.py to get mAP after each epoch
import
test
# import test.py to get mAP after each epoch
from
models.yolo
import
Model
from
models.yolo
import
Model
...
@@ -70,7 +69,7 @@ def train(hyp, tb_writer, opt, device):
...
@@ -70,7 +69,7 @@ def train(hyp, tb_writer, opt, device):
# Since I see lots of print here, the logging configuration is skipped here. We may see repeated outputs.
# Since I see lots of print here, the logging configuration is skipped here. We may see repeated outputs.
# Configure
# Configure
init_seeds
(
2
+
local_rank
)
init_seeds
(
2
+
local_rank
)
with
open
(
opt
.
data
)
as
f
:
with
open
(
opt
.
data
)
as
f
:
data_dict
=
yaml
.
load
(
f
,
Loader
=
yaml
.
FullLoader
)
# model dict
data_dict
=
yaml
.
load
(
f
,
Loader
=
yaml
.
FullLoader
)
# model dict
train_path
=
data_dict
[
'train'
]
train_path
=
data_dict
[
'train'
]
...
@@ -131,7 +130,8 @@ def train(hyp, tb_writer, opt, device):
...
@@ -131,7 +130,8 @@ def train(hyp, tb_writer, opt, device):
# load model
# load model
try
:
try
:
ckpt
[
'model'
]
=
{
k
:
v
for
k
,
v
in
ckpt
[
'model'
]
.
float
()
.
state_dict
()
.
items
()
if
k
in
model
.
state_dict
()}
ckpt
[
'model'
]
=
{
k
:
v
for
k
,
v
in
ckpt
[
'model'
]
.
float
()
.
state_dict
()
.
items
()
if
k
in
model
.
state_dict
()
and
model
.
state_dict
()[
k
]
.
shape
==
v
.
shape
}
model
.
load_state_dict
(
ckpt
[
'model'
],
strict
=
False
)
model
.
load_state_dict
(
ckpt
[
'model'
],
strict
=
False
)
except
KeyError
as
e
:
except
KeyError
as
e
:
s
=
"
%
s is not compatible with
%
s. This may be due to model differences or
%
s may be out of date. "
\
s
=
"
%
s is not compatible with
%
s. This may be due to model differences or
%
s may be out of date. "
\
...
@@ -187,7 +187,8 @@ def train(hyp, tb_writer, opt, device):
...
@@ -187,7 +187,8 @@ def train(hyp, tb_writer, opt, device):
# Trainloader
# Trainloader
dataloader
,
dataset
=
create_dataloader
(
train_path
,
imgsz
,
batch_size
,
gs
,
opt
,
hyp
=
hyp
,
augment
=
True
,
dataloader
,
dataset
=
create_dataloader
(
train_path
,
imgsz
,
batch_size
,
gs
,
opt
,
hyp
=
hyp
,
augment
=
True
,
cache
=
opt
.
cache_images
,
rect
=
opt
.
rect
,
local_rank
=
local_rank
,
world_size
=
opt
.
world_size
)
cache
=
opt
.
cache_images
,
rect
=
opt
.
rect
,
local_rank
=
local_rank
,
world_size
=
opt
.
world_size
)
mlc
=
np
.
concatenate
(
dataset
.
labels
,
0
)[:,
0
]
.
max
()
# max label class
mlc
=
np
.
concatenate
(
dataset
.
labels
,
0
)[:,
0
]
.
max
()
# max label class
nb
=
len
(
dataloader
)
# number of batches
nb
=
len
(
dataloader
)
# number of batches
assert
mlc
<
nc
,
'Label class
%
g exceeds nc=
%
g in
%
s. Possible class labels are 0-
%
g'
%
(
mlc
,
nc
,
opt
.
data
,
nc
-
1
)
assert
mlc
<
nc
,
'Label class
%
g exceeds nc=
%
g in
%
s. Possible class labels are 0-
%
g'
%
(
mlc
,
nc
,
opt
.
data
,
nc
-
1
)
...
@@ -242,7 +243,8 @@ def train(hyp, tb_writer, opt, device):
...
@@ -242,7 +243,8 @@ def train(hyp, tb_writer, opt, device):
if
local_rank
in
[
-
1
,
0
]:
if
local_rank
in
[
-
1
,
0
]:
w
=
model
.
class_weights
.
cpu
()
.
numpy
()
*
(
1
-
maps
)
**
2
# class weights
w
=
model
.
class_weights
.
cpu
()
.
numpy
()
*
(
1
-
maps
)
**
2
# class weights
image_weights
=
labels_to_image_weights
(
dataset
.
labels
,
nc
=
nc
,
class_weights
=
w
)
image_weights
=
labels_to_image_weights
(
dataset
.
labels
,
nc
=
nc
,
class_weights
=
w
)
dataset
.
indices
=
random
.
choices
(
range
(
dataset
.
n
),
weights
=
image_weights
,
k
=
dataset
.
n
)
# rand weighted idx
dataset
.
indices
=
random
.
choices
(
range
(
dataset
.
n
),
weights
=
image_weights
,
k
=
dataset
.
n
)
# rand weighted idx
# Broadcast.
# Broadcast.
if
local_rank
!=
-
1
:
if
local_rank
!=
-
1
:
indices
=
torch
.
zeros
([
dataset
.
n
],
dtype
=
torch
.
int
)
indices
=
torch
.
zeros
([
dataset
.
n
],
dtype
=
torch
.
int
)
...
@@ -402,7 +404,7 @@ def train(hyp, tb_writer, opt, device):
...
@@ -402,7 +404,7 @@ def train(hyp, tb_writer, opt, device):
plot_results
()
# save as results.png
plot_results
()
# save as results.png
print
(
'
%
g epochs completed in
%.3
f hours.
\n
'
%
(
epoch
-
start_epoch
+
1
,
(
time
.
time
()
-
t0
)
/
3600
))
print
(
'
%
g epochs completed in
%.3
f hours.
\n
'
%
(
epoch
-
start_epoch
+
1
,
(
time
.
time
()
-
t0
)
/
3600
))
dist
.
destroy_process_group
()
if
local_rank
not
in
[
-
1
,
0
]
else
None
dist
.
destroy_process_group
()
if
local_rank
not
in
[
-
1
,
0
]
else
None
torch
.
cuda
.
empty_cache
()
torch
.
cuda
.
empty_cache
()
return
results
return
results
...
@@ -431,7 +433,8 @@ if __name__ == '__main__':
...
@@ -431,7 +433,8 @@ if __name__ == '__main__':
parser
.
add_argument
(
'--single-cls'
,
action
=
'store_true'
,
help
=
'train as single-class dataset'
)
parser
.
add_argument
(
'--single-cls'
,
action
=
'store_true'
,
help
=
'train as single-class dataset'
)
parser
.
add_argument
(
"--sync-bn"
,
action
=
"store_true"
,
help
=
"Use sync-bn, only avaible in DDP mode."
)
parser
.
add_argument
(
"--sync-bn"
,
action
=
"store_true"
,
help
=
"Use sync-bn, only avaible in DDP mode."
)
# Parameter For DDP.
# Parameter For DDP.
parser
.
add_argument
(
'--local_rank'
,
type
=
int
,
default
=-
1
,
help
=
"Extra parameter for DDP implementation. Don't use it manually."
)
parser
.
add_argument
(
'--local_rank'
,
type
=
int
,
default
=-
1
,
help
=
"Extra parameter for DDP implementation. Don't use it manually."
)
opt
=
parser
.
parse_args
()
opt
=
parser
.
parse_args
()
last
=
get_latest_run
()
if
opt
.
resume
==
'get_last'
else
opt
.
resume
# resume from most recent run
last
=
get_latest_run
()
if
opt
.
resume
==
'get_last'
else
opt
.
resume
# resume from most recent run
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论