Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
0a49f80c
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
0a49f80c
编写于
2月 08, 2023
作者:
H
huangjun12
提交者:
GitHub
2月 08, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add mainKD for ppyoloe distill (#7708)
* add mainkd for ppyoloe distill * refine code style
上级
3b15b2e7
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
33 addition
and
1 deletion
+33
-1
configs/slim/distill/ppyoloe_plus_distill_m_distill_s.yml
configs/slim/distill/ppyoloe_plus_distill_m_distill_s.yml
+2
-0
ppdet/modeling/heads/ppyoloe_head.py
ppdet/modeling/heads/ppyoloe_head.py
+1
-0
ppdet/slim/distill_loss.py
ppdet/slim/distill_loss.py
+30
-1
未找到文件。
configs/slim/distill/ppyoloe_plus_distill_m_distill_s.yml
浏览文件 @
0a49f80c
...
@@ -43,6 +43,8 @@ DistillPPYOLOELoss: # M -> S
...
@@ -43,6 +43,8 @@ DistillPPYOLOELoss: # M -> S
loss_weight
:
{
'
logits'
:
4.0
,
'
feat'
:
1.0
}
loss_weight
:
{
'
logits'
:
4.0
,
'
feat'
:
1.0
}
logits_distill
:
True
logits_distill
:
True
logits_loss_weight
:
{
'
class'
:
1.0
,
'
iou'
:
2.5
,
'
dfl'
:
0.5
}
logits_loss_weight
:
{
'
class'
:
1.0
,
'
iou'
:
2.5
,
'
dfl'
:
0.5
}
logits_ld_distill
:
True
logits_ld_params
:
{
'
weight'
:
20000
,
'
T'
:
10
}
feat_distill
:
True
feat_distill
:
True
feat_distiller
:
'
fgd'
# ['cwd', 'fgd', 'pkd', 'mgd', 'mimic']
feat_distiller
:
'
fgd'
# ['cwd', 'fgd', 'pkd', 'mgd', 'mimic']
feat_distill_place
:
'
neck_feats'
feat_distill_place
:
'
neck_feats'
...
...
ppdet/modeling/heads/ppyoloe_head.py
浏览文件 @
0a49f80c
...
@@ -323,6 +323,7 @@ class PPYOLOEHead(nn.Layer):
...
@@ -323,6 +323,7 @@ class PPYOLOEHead(nn.Layer):
assigned_bboxes
,
assigned_scores
,
assigned_scores_sum
):
assigned_bboxes
,
assigned_scores
,
assigned_scores_sum
):
# select positive samples mask
# select positive samples mask
mask_positive
=
(
assigned_labels
!=
self
.
num_classes
)
mask_positive
=
(
assigned_labels
!=
self
.
num_classes
)
self
.
distill_pairs
[
'mask_positive_select'
]
=
mask_positive
num_pos
=
mask_positive
.
sum
()
num_pos
=
mask_positive
.
sum
()
# pos/neg loss
# pos/neg loss
if
num_pos
>
0
:
if
num_pos
>
0
:
...
...
ppdet/slim/distill_loss.py
浏览文件 @
0a49f80c
...
@@ -212,6 +212,9 @@ class DistillPPYOLOELoss(nn.Layer):
...
@@ -212,6 +212,9 @@ class DistillPPYOLOELoss(nn.Layer):
logits_loss_weight
=
{
'class'
:
1.0
,
logits_loss_weight
=
{
'class'
:
1.0
,
'iou'
:
2.5
,
'iou'
:
2.5
,
'dfl'
:
0.5
},
'dfl'
:
0.5
},
logits_ld_distill
=
False
,
logits_ld_params
=
{
'weight'
:
20000
,
'T'
:
10
},
feat_distill
=
True
,
feat_distill
=
True
,
feat_distiller
=
'fgd'
,
feat_distiller
=
'fgd'
,
feat_distill_place
=
'neck_feats'
,
feat_distill_place
=
'neck_feats'
,
...
@@ -222,6 +225,7 @@ class DistillPPYOLOELoss(nn.Layer):
...
@@ -222,6 +225,7 @@ class DistillPPYOLOELoss(nn.Layer):
self
.
loss_weight_logits
=
loss_weight
[
'logits'
]
self
.
loss_weight_logits
=
loss_weight
[
'logits'
]
self
.
loss_weight_feat
=
loss_weight
[
'feat'
]
self
.
loss_weight_feat
=
loss_weight
[
'feat'
]
self
.
logits_distill
=
logits_distill
self
.
logits_distill
=
logits_distill
self
.
logits_ld_distill
=
logits_ld_distill
self
.
feat_distill
=
feat_distill
self
.
feat_distill
=
feat_distill
if
logits_distill
and
self
.
loss_weight_logits
>
0
:
if
logits_distill
and
self
.
loss_weight_logits
>
0
:
...
@@ -230,6 +234,10 @@ class DistillPPYOLOELoss(nn.Layer):
...
@@ -230,6 +234,10 @@ class DistillPPYOLOELoss(nn.Layer):
self
.
qfl_loss_weight
=
logits_loss_weight
[
'class'
]
self
.
qfl_loss_weight
=
logits_loss_weight
[
'class'
]
self
.
loss_bbox
=
GIoULoss
()
self
.
loss_bbox
=
GIoULoss
()
if
logits_ld_distill
:
self
.
loss_kd
=
KnowledgeDistillationKLDivLoss
(
loss_weight
=
logits_ld_params
[
'weight'
],
T
=
logits_ld_params
[
'T'
])
if
feat_distill
and
self
.
loss_weight_feat
>
0
:
if
feat_distill
and
self
.
loss_weight_feat
>
0
:
assert
feat_distiller
in
[
'cwd'
,
'fgd'
,
'pkd'
,
'mgd'
,
'mimic'
]
assert
feat_distiller
in
[
'cwd'
,
'fgd'
,
'pkd'
,
'mgd'
,
'mimic'
]
assert
feat_distill_place
in
[
'backbone_feats'
,
'neck_feats'
]
assert
feat_distill_place
in
[
'backbone_feats'
,
'neck_feats'
]
...
@@ -334,6 +342,20 @@ class DistillPPYOLOELoss(nn.Layer):
...
@@ -334,6 +342,20 @@ class DistillPPYOLOELoss(nn.Layer):
loss_dfl
=
loss_dfl
.
mean
(
-
1
)
loss_dfl
=
loss_dfl
.
mean
(
-
1
)
return
loss_dfl
/
4.0
# 4 direction
return
loss_dfl
/
4.0
# 4 direction
def
main_kd
(
self
,
mask_positive
,
pred_scores
,
soft_cls
,
num_classes
):
num_pos
=
mask_positive
.
sum
()
if
num_pos
>
0
:
cls_mask
=
mask_positive
.
unsqueeze
(
-
1
).
tile
([
1
,
1
,
num_classes
])
pred_scores_pos
=
paddle
.
masked_select
(
pred_scores
,
cls_mask
).
reshape
([
-
1
,
num_classes
])
soft_cls_pos
=
paddle
.
masked_select
(
soft_cls
,
cls_mask
).
reshape
([
-
1
,
num_classes
])
loss_kd
=
self
.
loss_kd
(
pred_scores_pos
,
soft_cls_pos
,
avg_factor
=
num_pos
)
else
:
loss_kd
=
paddle
.
zeros
([
1
])
return
loss_kd
def
forward
(
self
,
teacher_model
,
student_model
):
def
forward
(
self
,
teacher_model
,
student_model
):
teacher_distill_pairs
=
teacher_model
.
yolo_head
.
distill_pairs
teacher_distill_pairs
=
teacher_model
.
yolo_head
.
distill_pairs
student_distill_pairs
=
student_model
.
yolo_head
.
distill_pairs
student_distill_pairs
=
student_model
.
yolo_head
.
distill_pairs
...
@@ -373,8 +395,15 @@ class DistillPPYOLOELoss(nn.Layer):
...
@@ -373,8 +395,15 @@ class DistillPPYOLOELoss(nn.Layer):
distill_cls_loss
=
paddle
.
add_n
(
distill_cls_loss
)
distill_cls_loss
=
paddle
.
add_n
(
distill_cls_loss
)
distill_bbox_loss
=
paddle
.
add_n
(
distill_bbox_loss
)
distill_bbox_loss
=
paddle
.
add_n
(
distill_bbox_loss
)
distill_dfl_loss
=
paddle
.
add_n
(
distill_dfl_loss
)
distill_dfl_loss
=
paddle
.
add_n
(
distill_dfl_loss
)
logits_loss
=
distill_bbox_loss
*
self
.
bbox_loss_weight
+
distill_cls_loss
*
self
.
qfl_loss_weight
+
distill_dfl_loss
*
self
.
dfl_loss_weight
logits_loss
=
distill_bbox_loss
*
self
.
bbox_loss_weight
+
distill_cls_loss
*
self
.
qfl_loss_weight
+
distill_dfl_loss
*
self
.
dfl_loss_weight
if
self
.
logits_ld_distill
:
loss_kd
=
self
.
main_kd
(
student_distill_pairs
[
'mask_positive_select'
],
student_distill_pairs
[
'pred_cls_scores'
],
teacher_distill_pairs
[
'pred_cls_scores'
],
student_model
.
yolo_head
.
num_classes
,
)
logits_loss
+=
loss_kd
else
:
else
:
logits_loss
=
paddle
.
zeros
([
1
])
logits_loss
=
paddle
.
zeros
([
1
])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录