Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
e32abb5f
提交
e32abb5f
authored
8月 02, 2020
作者:
Glenn Jocher
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
hyperparameter evolution bug fix (#566)
上级
8056fe2d
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
29 行增加
和
13 行删除
+29
-13
train.py
train.py
+12
-7
utils.py
utils/utils.py
+17
-6
没有找到文件。
train.py
浏览文件 @
e32abb5f
...
@@ -42,7 +42,7 @@ hyp = {'lr0': 0.01, # initial learning rate (SGD=1E-2, Adam=1E-3)
...
@@ -42,7 +42,7 @@ hyp = {'lr0': 0.01, # initial learning rate (SGD=1E-2, Adam=1E-3)
def
train
(
hyp
,
opt
,
device
,
tb_writer
=
None
):
def
train
(
hyp
,
opt
,
device
,
tb_writer
=
None
):
print
(
f
'Hyperparameters {hyp}'
)
print
(
f
'Hyperparameters {hyp}'
)
log_dir
=
tb_writer
.
log_dir
if
tb_writer
else
'runs/evol
ution
'
# run directory
log_dir
=
tb_writer
.
log_dir
if
tb_writer
else
'runs/evol
ve
'
# run directory
wdir
=
str
(
Path
(
log_dir
)
/
'weights'
)
+
os
.
sep
# weights directory
wdir
=
str
(
Path
(
log_dir
)
/
'weights'
)
+
os
.
sep
# weights directory
os
.
makedirs
(
wdir
,
exist_ok
=
True
)
os
.
makedirs
(
wdir
,
exist_ok
=
True
)
last
=
wdir
+
'last.pt'
last
=
wdir
+
'last.pt'
...
@@ -491,6 +491,7 @@ if __name__ == '__main__':
...
@@ -491,6 +491,7 @@ if __name__ == '__main__':
assert
opt
.
local_rank
==
-
1
,
'DDP mode not implemented for --evolve'
assert
opt
.
local_rank
==
-
1
,
'DDP mode not implemented for --evolve'
opt
.
notest
,
opt
.
nosave
=
True
,
True
# only test/save final epoch
opt
.
notest
,
opt
.
nosave
=
True
,
True
# only test/save final epoch
# ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
# ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
yaml_file
=
Path
(
'runs/evolve/hyp_evolved.yaml'
)
# save best result here
if
opt
.
bucket
:
if
opt
.
bucket
:
os
.
system
(
'gsutil cp gs://
%
s/evolve.txt .'
%
opt
.
bucket
)
# download evolve.txt if exists
os
.
system
(
'gsutil cp gs://
%
s/evolve.txt .'
%
opt
.
bucket
)
# download evolve.txt if exists
...
@@ -518,17 +519,21 @@ if __name__ == '__main__':
...
@@ -518,17 +519,21 @@ if __name__ == '__main__':
while
all
(
v
==
1
):
# mutate until a change occurs (prevent duplicates)
while
all
(
v
==
1
):
# mutate until a change occurs (prevent duplicates)
v
=
(
g
*
(
npr
.
random
(
ng
)
<
mp
)
*
npr
.
randn
(
ng
)
*
npr
.
random
()
*
s
+
1
)
.
clip
(
0.3
,
3.0
)
v
=
(
g
*
(
npr
.
random
(
ng
)
<
mp
)
*
npr
.
randn
(
ng
)
*
npr
.
random
()
*
s
+
1
)
.
clip
(
0.3
,
3.0
)
for
i
,
k
in
enumerate
(
hyp
.
keys
()):
# plt.hist(v.ravel(), 300)
for
i
,
k
in
enumerate
(
hyp
.
keys
()):
# plt.hist(v.ravel(), 300)
hyp
[
k
]
=
x
[
i
+
7
]
*
v
[
i
]
# mutate
hyp
[
k
]
=
float
(
x
[
i
+
7
]
*
v
[
i
])
# mutate
# C
lip
to limits
# C
onstrain
to limits
for
k
,
v
in
meta
.
items
():
for
k
,
v
in
meta
.
items
():
hyp
[
k
]
=
np
.
clip
(
hyp
[
k
],
v
[
1
],
v
[
2
])
hyp
[
k
]
=
max
(
hyp
[
k
],
v
[
1
])
# lower limit
hyp
[
k
]
=
min
(
hyp
[
k
],
v
[
2
])
# upper limit
hyp
[
k
]
=
round
(
hyp
[
k
],
5
)
# significant digits
# Train mutation
# Train mutation
results
=
train
(
hyp
.
copy
(),
opt
,
device
)
results
=
train
(
hyp
.
copy
(),
opt
,
device
)
# Write mutation results
# Write mutation results
print_mutation
(
hyp
,
results
,
opt
.
bucket
)
print_mutation
(
hyp
.
copy
(),
results
,
yaml_file
,
opt
.
bucket
)
# Plot results
# Plot results
# plot_evolution_results(hyp)
plot_evolution_results
(
yaml_file
)
print
(
'Hyperparameter evolution complete. Best results saved as:
%
s
\n
Command to train a new model with these '
'hyperparameters: $ python train.py --hyp
%
s'
%
(
f
,
f
))
utils/utils.py
浏览文件 @
e32abb5f
...
@@ -818,11 +818,11 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=10
...
@@ -818,11 +818,11 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=10
return
print_results
(
k
)
return
print_results
(
k
)
def
print_mutation
(
hyp
,
results
,
bucket
=
''
):
def
print_mutation
(
hyp
,
results
,
yaml_file
=
'hyp_evolved.yaml'
,
bucket
=
''
):
# Print mutation results to evolve.txt (for use with train.py --evolve)
# Print mutation results to evolve.txt (for use with train.py --evolve)
a
=
'
%10
s'
*
len
(
hyp
)
%
tuple
(
hyp
.
keys
())
# hyperparam keys
a
=
'
%10
s'
*
len
(
hyp
)
%
tuple
(
hyp
.
keys
())
# hyperparam keys
b
=
'
%10.3
g'
*
len
(
hyp
)
%
tuple
(
hyp
.
values
())
# hyperparam values
b
=
'
%10.3
g'
*
len
(
hyp
)
%
tuple
(
hyp
.
values
())
# hyperparam values
c
=
'
%10.4
g'
*
len
(
results
)
%
results
# results (P, R, mAP
, F1, test_loss
)
c
=
'
%10.4
g'
*
len
(
results
)
%
results
# results (P, R, mAP
@0.5, mAP@0.5:0.95, val_losses x 3
)
print
(
'
\n
%
s
\n
%
s
\n
Evolved fitness:
%
s
\n
'
%
(
a
,
b
,
c
))
print
(
'
\n
%
s
\n
%
s
\n
Evolved fitness:
%
s
\n
'
%
(
a
,
b
,
c
))
if
bucket
:
if
bucket
:
...
@@ -831,11 +831,19 @@ def print_mutation(hyp, results, bucket=''):
...
@@ -831,11 +831,19 @@ def print_mutation(hyp, results, bucket=''):
with
open
(
'evolve.txt'
,
'a'
)
as
f
:
# append result
with
open
(
'evolve.txt'
,
'a'
)
as
f
:
# append result
f
.
write
(
c
+
b
+
'
\n
'
)
f
.
write
(
c
+
b
+
'
\n
'
)
x
=
np
.
unique
(
np
.
loadtxt
(
'evolve.txt'
,
ndmin
=
2
),
axis
=
0
)
# load unique rows
x
=
np
.
unique
(
np
.
loadtxt
(
'evolve.txt'
,
ndmin
=
2
),
axis
=
0
)
# load unique rows
np
.
savetxt
(
'evolve.txt'
,
x
[
np
.
argsort
(
-
fitness
(
x
))],
'
%10.3
g'
)
# save sort by fitness
x
=
x
[
np
.
argsort
(
-
fitness
(
x
))]
# sort
np
.
savetxt
(
'evolve.txt'
,
x
,
'
%10.3
g'
)
# save sort by fitness
if
bucket
:
if
bucket
:
os
.
system
(
'gsutil cp evolve.txt gs://
%
s'
%
bucket
)
# upload evolve.txt
os
.
system
(
'gsutil cp evolve.txt gs://
%
s'
%
bucket
)
# upload evolve.txt
# Save yaml
for
i
,
k
in
enumerate
(
hyp
.
keys
()):
hyp
[
k
]
=
float
(
x
[
0
,
i
+
7
])
with
open
(
yaml_file
,
'w'
)
as
f
:
f
.
write
(
'# Hyperparameter Evolution Results
\n
# Generations:
%
g
\n
# Metrics: '
%
len
(
x
)
+
c
+
'
\n\n
'
)
yaml
.
dump
(
hyp
,
f
,
sort_keys
=
False
)
def
apply_classifier
(
x
,
model
,
img
,
im0
):
def
apply_classifier
(
x
,
model
,
img
,
im0
):
# applies a second stage classifier to yolo outputs
# applies a second stage classifier to yolo outputs
...
@@ -1146,23 +1154,26 @@ def plot_labels(labels, save_dir=''):
...
@@ -1146,23 +1154,26 @@ def plot_labels(labels, save_dir=''):
plt
.
close
()
plt
.
close
()
def
plot_evolution_results
(
hyp
):
# from utils.utils import *; plot_evolution_results(hyp
)
def
plot_evolution_results
(
yaml_file
=
'hyp_evolved.yaml'
):
# from utils.utils import *; plot_evolution_results(
)
# Plot hyperparameter evolution results in evolve.txt
# Plot hyperparameter evolution results in evolve.txt
with
open
(
yaml_file
)
as
f
:
hyp
=
yaml
.
load
(
f
,
Loader
=
yaml
.
FullLoader
)
x
=
np
.
loadtxt
(
'evolve.txt'
,
ndmin
=
2
)
x
=
np
.
loadtxt
(
'evolve.txt'
,
ndmin
=
2
)
f
=
fitness
(
x
)
f
=
fitness
(
x
)
# weights = (f - f.min()) ** 2 # for weighted results
# weights = (f - f.min()) ** 2 # for weighted results
plt
.
figure
(
figsize
=
(
1
2
,
10
),
tight_layout
=
True
)
plt
.
figure
(
figsize
=
(
1
4
,
10
),
tight_layout
=
True
)
matplotlib
.
rc
(
'font'
,
**
{
'size'
:
8
})
matplotlib
.
rc
(
'font'
,
**
{
'size'
:
8
})
for
i
,
(
k
,
v
)
in
enumerate
(
hyp
.
items
()):
for
i
,
(
k
,
v
)
in
enumerate
(
hyp
.
items
()):
y
=
x
[:,
i
+
7
]
y
=
x
[:,
i
+
7
]
# mu = (y * weights).sum() / weights.sum() # best weighted result
# mu = (y * weights).sum() / weights.sum() # best weighted result
mu
=
y
[
f
.
argmax
()]
# best single result
mu
=
y
[
f
.
argmax
()]
# best single result
plt
.
subplot
(
4
,
5
,
i
+
1
)
plt
.
subplot
(
4
,
6
,
i
+
1
)
plt
.
plot
(
mu
,
f
.
max
(),
'o'
,
markersize
=
10
)
plt
.
plot
(
mu
,
f
.
max
(),
'o'
,
markersize
=
10
)
plt
.
plot
(
y
,
f
,
'.'
)
plt
.
plot
(
y
,
f
,
'.'
)
plt
.
title
(
'
%
s =
%.3
g'
%
(
k
,
mu
),
fontdict
=
{
'size'
:
9
})
# limit to 40 characters
plt
.
title
(
'
%
s =
%.3
g'
%
(
k
,
mu
),
fontdict
=
{
'size'
:
9
})
# limit to 40 characters
print
(
'
%15
s:
%.3
g'
%
(
k
,
mu
))
print
(
'
%15
s:
%.3
g'
%
(
k
,
mu
))
plt
.
savefig
(
'evolve.png'
,
dpi
=
200
)
plt
.
savefig
(
'evolve.png'
,
dpi
=
200
)
print
(
'
\n
Plot saved as evolve.png'
)
def
plot_results_overlay
(
start
=
0
,
stop
=
0
):
# from utils.utils import *; plot_results_overlay()
def
plot_results_overlay
(
start
=
0
,
stop
=
0
):
# from utils.utils import *; plot_results_overlay()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论