Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
57a0ae33
提交
57a0ae33
authored
6月 16, 2020
作者:
Glenn Jocher
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
AutoAnchor implementation
上级
95c46f72
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
45 行增加
和
50 行删除
+45
-50
utils.py
utils/utils.py
+45
-50
没有找到文件。
utils/utils.py
浏览文件 @
57a0ae33
...
@@ -53,18 +53,23 @@ def check_img_size(img_size, s=32):
...
@@ -53,18 +53,23 @@ def check_img_size(img_size, s=32):
def
check_anchors
(
dataset
,
model
,
thr
=
4.0
,
imgsz
=
640
):
def
check_anchors
(
dataset
,
model
,
thr
=
4.0
,
imgsz
=
640
):
# Check best possible recall of dataset with current anchors
# Check anchor fit to data, recompute if necessary
print
(
'
\n
Analyzing anchors... '
,
end
=
''
)
anchors
=
model
.
module
.
model
[
-
1
]
.
anchor_grid
if
hasattr
(
model
,
'module'
)
else
model
.
model
[
-
1
]
.
anchor_grid
anchors
=
model
.
module
.
model
[
-
1
]
.
anchor_grid
if
hasattr
(
model
,
'module'
)
else
model
.
model
[
-
1
]
.
anchor_grid
shapes
=
imgsz
*
dataset
.
shapes
/
dataset
.
shapes
.
max
(
1
,
keepdims
=
True
)
shapes
=
imgsz
*
dataset
.
shapes
/
dataset
.
shapes
.
max
(
1
,
keepdims
=
True
)
wh
=
torch
.
tensor
(
np
.
concatenate
([
l
[:,
3
:
5
]
*
s
for
s
,
l
in
zip
(
shapes
,
dataset
.
labels
)]))
.
float
()
# wh
wh
=
torch
.
tensor
(
np
.
concatenate
([
l
[:,
3
:
5
]
*
s
for
s
,
l
in
zip
(
shapes
,
dataset
.
labels
)]))
.
float
()
# wh
ratio
=
wh
[:,
None
]
/
anchors
.
view
(
-
1
,
2
)
.
cpu
()[
None
]
# ratio
ratio
=
wh
[:,
None
]
/
anchors
.
view
(
-
1
,
2
)
.
cpu
()[
None
]
# ratio
m
=
torch
.
max
(
ratio
,
1.
/
ratio
)
.
max
(
2
)[
0
]
# max ratio
m
=
torch
.
max
(
ratio
,
1.
/
ratio
)
.
max
(
2
)[
0
]
# max ratio
bpr
=
(
m
.
min
(
1
)[
0
]
<
thr
)
.
float
()
.
mean
()
# best possible recall
bpr
=
(
m
.
min
(
1
)[
0
]
<
thr
)
.
float
()
.
mean
()
# best possible recall
mr
=
(
m
<
thr
)
.
float
()
.
mean
()
# match ratio
# mr = (m < thr).float().mean() # match ratio
print
((
'AutoAnchor labels:'
+
'
%10
s'
*
6
)
%
(
'n'
,
'mean'
,
'min'
,
'max'
,
'matching'
,
'recall'
))
print
((
' '
+
'
%10.4
g'
*
6
)
%
(
wh
.
shape
[
0
],
wh
.
mean
(),
wh
.
min
(),
wh
.
max
(),
mr
,
bpr
))
print
(
'Best Possible Recall (BPR) =
%.3
f'
%
bpr
,
end
=
''
)
assert
bpr
>
0.9
,
'Best possible recall
%.3
g (BPR) below 0.9 threshold. Training cancelled. '
\
if
bpr
<
0.99
:
# threshold to recompute
'Compute new anchors with utils.utils.kmeans_anchors() and update model before training.'
%
bpr
print
(
'. Generating new anchors for improved recall, please wait...'
%
bpr
)
new_anchors
=
kmean_anchors
(
dataset
,
n
=
9
,
img_size
=
640
,
thr
=
4.0
,
gen
=
1000
,
verbose
=
False
)
anchors
[:]
=
torch
.
tensor
(
new_anchors
)
.
view_as
(
anchors
)
.
type_as
(
anchors
)
print
(
'New anchors saved to model. Update model *.yaml to use these anchors in the future.'
)
print
(
''
)
# newline
def
check_file
(
file
):
def
check_file
(
file
):
...
@@ -689,14 +694,14 @@ def coco_single_class_labels(path='../coco/labels/train2014/', label_class=43):
...
@@ -689,14 +694,14 @@ def coco_single_class_labels(path='../coco/labels/train2014/', label_class=43):
shutil
.
copyfile
(
src
=
img_file
,
dst
=
'new/images/'
+
Path
(
file
)
.
name
.
replace
(
'txt'
,
'jpg'
))
# copy images
shutil
.
copyfile
(
src
=
img_file
,
dst
=
'new/images/'
+
Path
(
file
)
.
name
.
replace
(
'txt'
,
'jpg'
))
# copy images
def
kmean_anchors
(
path
=
'./data/coco128.yaml'
,
n
=
9
,
img_size
=
(
640
,
640
),
thr
=
0.20
,
gen
=
1000
):
def
kmean_anchors
(
path
=
'./data/coco128.yaml'
,
n
=
9
,
img_size
=
640
,
thr
=
4.0
,
gen
=
1000
,
verbose
=
True
):
""" Creates kmeans-evolved anchors from training dataset
""" Creates kmeans-evolved anchors from training dataset
Arguments:
Arguments:
path: path to dataset *.yaml
path: path to dataset *.yaml
, or a loaded dataset
n: number of anchors
n: number of anchors
img_size:
(min, max) image size used for multi-scale training (can be same values)
img_size:
image size used for training
thr:
IoU threshold hyperparameter used for training (0.0 - 1.0)
thr:
anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0
gen: generations to evolve anchors using genetic algorithm
gen: generations to evolve anchors using genetic algorithm
Return:
Return:
...
@@ -705,52 +710,41 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
...
@@ -705,52 +710,41 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
Usage:
Usage:
from utils.utils import *; _ = kmean_anchors()
from utils.utils import *; _ = kmean_anchors()
"""
"""
thr
=
1.
/
thr
def
metric
(
k
):
# compute metrics
r
=
wh
[:,
None
]
/
k
[
None
]
x
=
torch
.
min
(
r
,
1.
/
r
)
.
min
(
2
)[
0
]
# ratio metric
# x = wh_iou(wh, torch.tensor(k)) # iou metric
return
x
,
x
.
max
(
1
)[
0
]
# x, best_x
from
utils.datasets
import
LoadImagesAndLabels
def
fitness
(
k
):
# mutation fitness
_
,
best
=
metric
(
k
)
return
(
best
*
(
best
>
thr
)
.
float
())
.
mean
()
# fitness
def
print_results
(
k
):
def
print_results
(
k
):
k
=
k
[
np
.
argsort
(
k
.
prod
(
1
))]
# sort small to large
k
=
k
[
np
.
argsort
(
k
.
prod
(
1
))]
# sort small to large
iou
=
wh_iou
(
wh
,
torch
.
Tensor
(
k
))
x
,
best
=
metric
(
k
)
max_iou
=
iou
.
max
(
1
)[
0
]
bpr
,
aat
=
(
best
>
thr
)
.
float
()
.
mean
(),
(
x
>
thr
)
.
float
()
.
mean
()
*
n
# best possible recall, anch > thr
bpr
,
aat
=
(
max_iou
>
thr
)
.
float
()
.
mean
(),
(
iou
>
thr
)
.
float
()
.
mean
()
*
n
# best possible recall, anch > thr
print
(
'thr=
%.2
f:
%.3
f best possible recall,
%.2
f anchors past thr'
%
(
thr
,
bpr
,
aat
))
print
(
'n=
%
g, img_size=
%
s, metric_all=
%.3
f/
%.3
f-mean/best, past_thr=
%.3
f-mean: '
%
# thr = 5.0
(
n
,
img_size
,
x
.
mean
(),
best
.
mean
(),
x
[
x
>
thr
]
.
mean
()),
end
=
''
)
# r = wh[:, None] / k[None]
# ar = torch.max(r, 1. / r).max(2)[0]
# max_ar = ar.min(1)[0]
# bpr, aat = (max_ar < thr).float().mean(), (ar < thr).float().mean() * n # best possible recall, anch > thr
print
(
'
%.2
f iou_thr:
%.3
f best possible recall,
%.2
f anchors > thr'
%
(
thr
,
bpr
,
aat
))
print
(
'n=
%
g, img_size=
%
s, IoU_all=
%.3
f/
%.3
f-mean/best, IoU>thr=
%.3
f-mean: '
%
(
n
,
img_size
,
iou
.
mean
(),
max_iou
.
mean
(),
iou
[
iou
>
thr
]
.
mean
()),
end
=
''
)
for
i
,
x
in
enumerate
(
k
):
for
i
,
x
in
enumerate
(
k
):
print
(
'
%
i,
%
i'
%
(
round
(
x
[
0
]),
round
(
x
[
1
])),
end
=
', '
if
i
<
len
(
k
)
-
1
else
'
\n
'
)
# use in *.cfg
print
(
'
%
i,
%
i'
%
(
round
(
x
[
0
]),
round
(
x
[
1
])),
end
=
', '
if
i
<
len
(
k
)
-
1
else
'
\n
'
)
# use in *.cfg
return
k
return
k
def
fitness
(
k
):
# mutation fitness
if
isinstance
(
path
,
str
):
# *.yaml file
iou
=
wh_iou
(
wh
,
torch
.
Tensor
(
k
))
# iou
with
open
(
path
)
as
f
:
max_iou
=
iou
.
max
(
1
)[
0
]
data_dict
=
yaml
.
load
(
f
,
Loader
=
yaml
.
FullLoader
)
# model dict
return
(
max_iou
*
(
max_iou
>
thr
)
.
float
())
.
mean
()
# product
from
utils.datasets
import
LoadImagesAndLabels
dataset
=
LoadImagesAndLabels
(
data_dict
[
'train'
],
augment
=
True
,
rect
=
True
)
# def fitness_ratio(k): # mutation fitness
else
:
# # wh(5316,2), k(9,2)
dataset
=
path
# dataset
# r = wh[:, None] / k[None]
# x = torch.max(r, 1. / r).max(2)[0]
# m = x.min(1)[0]
# return 1. / (m * (m < 5).float()).mean() # product
# Get label wh
# Get label wh
wh
=
[]
shapes
=
img_size
*
dataset
.
shapes
/
dataset
.
shapes
.
max
(
1
,
keepdims
=
True
)
with
open
(
path
)
as
f
:
wh
=
torch
.
tensor
(
np
.
concatenate
([
l
[:,
3
:
5
]
*
s
for
s
,
l
in
zip
(
shapes
,
dataset
.
labels
)]))
.
float
()
# wh
data_dict
=
yaml
.
load
(
f
,
Loader
=
yaml
.
FullLoader
)
# model dict
wh
=
wh
[(
wh
>
2.0
)
.
all
(
1
)]
.
numpy
()
# filter > 2 pixels
dataset
=
LoadImagesAndLabels
(
data_dict
[
'train'
],
augment
=
True
,
rect
=
True
)
nr
=
1
if
img_size
[
0
]
==
img_size
[
1
]
else
3
# number augmentation repetitions
for
s
,
l
in
zip
(
dataset
.
shapes
,
dataset
.
labels
):
# wh.append(l[:, 3:5] * (s / s.max())) # image normalized to letterbox normalized wh
wh
.
append
(
l
[:,
3
:
5
]
*
s
)
# image normalized to pixels
wh
=
np
.
concatenate
(
wh
,
0
)
.
repeat
(
nr
,
axis
=
0
)
# augment 3x
# wh *= np.random.uniform(img_size[0], img_size[1], size=(wh.shape[0], 1)) # normalized to pixels (multi-scale)
wh
=
wh
[(
wh
>
2.0
)
.
all
(
1
)]
# remove below threshold boxes (< 2 pixels wh)
# Kmeans calculation
# Kmeans calculation
from
scipy.cluster.vq
import
kmeans
from
scipy.cluster.vq
import
kmeans
...
@@ -758,10 +752,10 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
...
@@ -758,10 +752,10 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
s
=
wh
.
std
(
0
)
# sigmas for whitening
s
=
wh
.
std
(
0
)
# sigmas for whitening
k
,
dist
=
kmeans
(
wh
/
s
,
n
,
iter
=
30
)
# points, mean distance
k
,
dist
=
kmeans
(
wh
/
s
,
n
,
iter
=
30
)
# points, mean distance
k
*=
s
k
*=
s
wh
=
torch
.
T
ensor
(
wh
)
wh
=
torch
.
t
ensor
(
wh
)
k
=
print_results
(
k
)
k
=
print_results
(
k
)
#
#
Plot
# Plot
# k, d = [None] * 20, [None] * 20
# k, d = [None] * 20, [None] * 20
# for i in tqdm(range(1, 21)):
# for i in tqdm(range(1, 21)):
# k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance
# k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance
...
@@ -777,7 +771,7 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
...
@@ -777,7 +771,7 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
# Evolve
# Evolve
npr
=
np
.
random
npr
=
np
.
random
f
,
sh
,
mp
,
s
=
fitness
(
k
),
k
.
shape
,
0.9
,
0.1
# fitness, generations, mutation prob, sigma
f
,
sh
,
mp
,
s
=
fitness
(
k
),
k
.
shape
,
0.9
,
0.1
# fitness, generations, mutation prob, sigma
for
_
in
tqdm
(
range
(
gen
),
desc
=
'Evolving anchors'
):
for
_
in
tqdm
(
range
(
gen
),
desc
=
'Evolving anchors
with Genetic Algorithm:
'
):
v
=
np
.
ones
(
sh
)
v
=
np
.
ones
(
sh
)
while
(
v
==
1
)
.
all
():
# mutate until a change occurs (prevent duplicates)
while
(
v
==
1
)
.
all
():
# mutate until a change occurs (prevent duplicates)
v
=
((
npr
.
random
(
sh
)
<
mp
)
*
npr
.
random
()
*
npr
.
randn
(
*
sh
)
*
s
+
1
)
.
clip
(
0.3
,
3.0
)
v
=
((
npr
.
random
(
sh
)
<
mp
)
*
npr
.
random
()
*
npr
.
randn
(
*
sh
)
*
s
+
1
)
.
clip
(
0.3
,
3.0
)
...
@@ -785,7 +779,8 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
...
@@ -785,7 +779,8 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=(640, 640), thr=0.20
fg
=
fitness
(
kg
)
fg
=
fitness
(
kg
)
if
fg
>
f
:
if
fg
>
f
:
f
,
k
=
fg
,
kg
.
copy
()
f
,
k
=
fg
,
kg
.
copy
()
print_results
(
k
)
if
verbose
:
print_results
(
k
)
k
=
print_results
(
k
)
k
=
print_results
(
k
)
return
k
return
k
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论