Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
last_winter
yolov4-pytorch
提交
cb78b709
Y
yolov4-pytorch
项目概览
last_winter
/
yolov4-pytorch
与 Fork 源项目一致
从无法访问的项目Fork
通知
7
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
yolov4-pytorch
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
cb78b709
编写于
9月 28, 2020
作者:
B
Bubbliiiing
提交者:
GitHub
9月 28, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update yolo_training.py
上级
9c6099ba
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
38 addition
and
30 deletion
+38
-30
nets/yolo_training.py
nets/yolo_training.py
+38
-30
未找到文件。
nets/yolo_training.py
浏览文件 @
cb78b709
...
@@ -197,32 +197,39 @@ class YOLOLoss(nn.Module):
...
@@ -197,32 +197,39 @@ class YOLOLoss(nn.Module):
box_loss_scale_x
=
torch
.
zeros
(
bs
,
int
(
self
.
num_anchors
/
3
),
in_h
,
in_w
,
requires_grad
=
False
)
box_loss_scale_x
=
torch
.
zeros
(
bs
,
int
(
self
.
num_anchors
/
3
),
in_h
,
in_w
,
requires_grad
=
False
)
box_loss_scale_y
=
torch
.
zeros
(
bs
,
int
(
self
.
num_anchors
/
3
),
in_h
,
in_w
,
requires_grad
=
False
)
box_loss_scale_y
=
torch
.
zeros
(
bs
,
int
(
self
.
num_anchors
/
3
),
in_h
,
in_w
,
requires_grad
=
False
)
for
b
in
range
(
bs
):
for
b
in
range
(
bs
):
for
t
in
range
(
target
[
b
].
shape
[
0
]):
if
len
(
target
[
b
])
==
0
:
# 计算出在特征层上的点位
continue
gx
=
target
[
b
][
t
,
0
]
*
in_w
# 计算出在特征层上的点位
gy
=
target
[
b
][
t
,
1
]
*
in_h
gxs
=
target
[
b
][:,
0
:
1
]
*
in_w
gys
=
target
[
b
][:,
1
:
2
]
*
in_h
gw
=
target
[
b
][
t
,
2
]
*
in_w
gh
=
target
[
b
][
t
,
3
]
*
in_h
gws
=
target
[
b
][:,
2
:
3
]
*
in_w
ghs
=
target
[
b
][:,
3
:
4
]
*
in_h
# 计算出属于哪个网格
gi
=
int
(
gx
)
# 计算出属于哪个网格
gj
=
int
(
gy
)
gis
=
torch
.
floor
(
gxs
)
gjs
=
torch
.
floor
(
gys
)
# 计算真实框的位置
gt_box
=
torch
.
FloatTensor
(
np
.
array
([
0
,
0
,
gw
,
gh
])).
unsqueeze
(
0
)
# 计算真实框的位置
gt_box
=
torch
.
FloatTensor
(
torch
.
cat
([
torch
.
zeros_like
(
gws
),
torch
.
zeros_like
(
ghs
),
gws
,
ghs
],
1
))
# 计算出所有先验框的位置
anchor_shapes
=
torch
.
FloatTensor
(
np
.
concatenate
((
np
.
zeros
((
self
.
num_anchors
,
2
)),
# 计算出所有先验框的位置
np
.
array
(
anchors
)),
1
))
anchor_shapes
=
torch
.
FloatTensor
(
torch
.
cat
((
torch
.
zeros
((
self
.
num_anchors
,
2
)),
torch
.
FloatTensor
(
anchors
)),
1
))
# 计算重合程度
# 计算重合程度
anch_ious
=
bbox_iou
(
gt_box
,
anchor_shapes
)
anch_ious
=
jaccard
(
gt_box
,
anchor_shapes
)
# Find the best matching anchor box
# Find the best matching anchor box
best_n
=
np
.
argmax
(
anch_ious
)
best_ns
=
torch
.
argmax
(
anch_ious
,
dim
=-
1
)
for
i
,
best_n
in
enumerate
(
best_ns
):
if
best_n
not
in
anchor_index
:
if
best_n
not
in
anchor_index
:
continue
continue
# Masks
# Masks
gi
=
gis
[
i
].
long
()
gj
=
gjs
[
i
].
long
()
gx
=
gxs
[
i
]
gy
=
gys
[
i
]
gw
=
gws
[
i
]
gh
=
ghs
[
i
]
if
(
gj
<
in_h
)
and
(
gi
<
in_w
):
if
(
gj
<
in_h
)
and
(
gi
<
in_w
):
best_n
=
best_n
-
subtract_index
best_n
=
best_n
-
subtract_index
# 判定哪些先验框内部真实的存在物体
# 判定哪些先验框内部真实的存在物体
...
@@ -235,12 +242,12 @@ class YOLOLoss(nn.Module):
...
@@ -235,12 +242,12 @@ class YOLOLoss(nn.Module):
tw
[
b
,
best_n
,
gj
,
gi
]
=
gw
tw
[
b
,
best_n
,
gj
,
gi
]
=
gw
th
[
b
,
best_n
,
gj
,
gi
]
=
gh
th
[
b
,
best_n
,
gj
,
gi
]
=
gh
# 用于获得xywh的比例
# 用于获得xywh的比例
box_loss_scale_x
[
b
,
best_n
,
gj
,
gi
]
=
target
[
b
][
t
,
2
]
box_loss_scale_x
[
b
,
best_n
,
gj
,
gi
]
=
target
[
b
][
i
,
2
]
box_loss_scale_y
[
b
,
best_n
,
gj
,
gi
]
=
target
[
b
][
t
,
3
]
box_loss_scale_y
[
b
,
best_n
,
gj
,
gi
]
=
target
[
b
][
i
,
3
]
# 物体置信度
# 物体置信度
tconf
[
b
,
best_n
,
gj
,
gi
]
=
1
tconf
[
b
,
best_n
,
gj
,
gi
]
=
1
# 种类
# 种类
tcls
[
b
,
best_n
,
gj
,
gi
,
int
(
target
[
b
][
t
,
4
]
)]
=
1
tcls
[
b
,
best_n
,
gj
,
gi
,
target
[
b
][
i
,
4
].
long
(
)]
=
1
else
:
else
:
print
(
'Step {0} out of bound'
.
format
(
b
))
print
(
'Step {0} out of bound'
.
format
(
b
))
print
(
'gj: {0}, height: {1} | gi: {2}, width: {3}'
.
format
(
gj
,
in_h
,
gi
,
in_w
))
print
(
'gj: {0}, height: {1} | gi: {2}, width: {3}'
.
format
(
gj
,
in_h
,
gi
,
in_w
))
...
@@ -251,6 +258,7 @@ class YOLOLoss(nn.Module):
...
@@ -251,6 +258,7 @@ class YOLOLoss(nn.Module):
t_box
[...,
3
]
=
th
t_box
[...,
3
]
=
th
return
mask
,
noobj_mask
,
t_box
,
tconf
,
tcls
,
box_loss_scale_x
,
box_loss_scale_y
return
mask
,
noobj_mask
,
t_box
,
tconf
,
tcls
,
box_loss_scale_x
,
box_loss_scale_y
def
get_ignore
(
self
,
prediction
,
target
,
scaled_anchors
,
in_w
,
in_h
,
noobj_mask
):
def
get_ignore
(
self
,
prediction
,
target
,
scaled_anchors
,
in_w
,
in_h
,
noobj_mask
):
bs
=
len
(
target
)
bs
=
len
(
target
)
anchor_index
=
[[
0
,
1
,
2
],[
3
,
4
,
5
],[
6
,
7
,
8
]][
self
.
feature_length
.
index
(
in_w
)]
anchor_index
=
[[
0
,
1
,
2
],[
3
,
4
,
5
],[
6
,
7
,
8
]][
self
.
feature_length
.
index
(
in_w
)]
...
@@ -292,12 +300,12 @@ class YOLOLoss(nn.Module):
...
@@ -292,12 +300,12 @@ class YOLOLoss(nn.Module):
gy
=
target
[
i
][:,
1
:
2
]
*
in_h
gy
=
target
[
i
][:,
1
:
2
]
*
in_h
gw
=
target
[
i
][:,
2
:
3
]
*
in_w
gw
=
target
[
i
][:,
2
:
3
]
*
in_w
gh
=
target
[
i
][:,
3
:
4
]
*
in_h
gh
=
target
[
i
][:,
3
:
4
]
*
in_h
gt_box
=
torch
.
FloatTensor
(
np
.
concatenate
([
gx
,
gy
,
gw
,
gh
],
-
1
)).
type
(
FloatTensor
)
gt_box
=
torch
.
FloatTensor
(
torch
.
cat
([
gx
,
gy
,
gw
,
gh
],
-
1
)).
type
(
FloatTensor
)
anch_ious
=
jaccard
(
gt_box
,
pred_boxes_for_ignore
)
anch_ious
=
jaccard
(
gt_box
,
pred_boxes_for_ignore
)
for
t
in
range
(
target
[
i
].
shape
[
0
]):
anch_ious_max
,
_
=
torch
.
max
(
anch_ious
,
dim
=
0
)
anch_iou
=
anch_ious
[
t
]
.
view
(
pred_boxes
[
i
].
size
()[:
3
])
anch_ious_max
=
anch_ious_max
.
view
(
pred_boxes
[
i
].
size
()[:
3
])
noobj_mask
[
i
][
anch_iou
>
self
.
ignore_threshold
]
=
0
noobj_mask
[
i
][
anch_ious_max
>
self
.
ignore_threshold
]
=
0
return
noobj_mask
,
pred_boxes
return
noobj_mask
,
pred_boxes
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录