Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
ac466b68
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
1 年多 前同步成功
通知
116
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ac466b68
编写于
2月 08, 2023
作者:
T
tianyi1997
提交者:
HydrogenSulfate
2月 28, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Create MetaBNNeck
上级
1be9c16a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
130 addition
and
5 deletion
+130
-5
ppcls/arch/gears/__init__.py
ppcls/arch/gears/__init__.py
+2
-1
ppcls/arch/gears/metabnneck.py
ppcls/arch/gears/metabnneck.py
+122
-0
ppcls/configs/reid/MetaBIN_ResNet50_single_source.yaml
ppcls/configs/reid/MetaBIN_ResNet50_single_source.yaml
+1
-1
ppcls/engine/train/train_metabin.py
ppcls/engine/train/train_metabin.py
+5
-3
未找到文件。
ppcls/arch/gears/__init__.py
浏览文件 @
ac466b68
...
...
@@ -21,6 +21,7 @@ from paddle.nn import Tanh
from
.bnneck
import
BNNeck
from
.adamargin
import
AdaMargin
from
.frfn_neck
import
FRFNNeck
from
.metabnneck
import
MetaBNNeck
__all__
=
[
'build_gear'
]
...
...
@@ -28,7 +29,7 @@ __all__ = ['build_gear']
def
build_gear
(
config
):
support_dict
=
[
'ArcMargin'
,
'CosMargin'
,
'CircleMargin'
,
'FC'
,
'VehicleNeck'
,
'Tanh'
,
'BNNeck'
,
'AdaMargin'
,
'FRFNNeck'
'BNNeck'
,
'AdaMargin'
,
'FRFNNeck'
,
'MetaBNNeck'
]
module_name
=
config
.
pop
(
'name'
)
assert
module_name
in
support_dict
,
Exception
(
...
...
ppcls/arch/gears/metabnneck.py
0 → 100644
浏览文件 @
ac466b68
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
absolute_import
,
division
,
print_function
from
collections
import
defaultdict
import
copy
import
paddle
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
from
..utils
import
get_param_attr_dict
class
MetaBN1D
(
nn
.
BatchNorm2D
):
def
forward
(
self
,
inputs
,
opt
=
{}):
mode
=
opt
.
get
(
"bn_mode"
,
"general"
)
if
self
.
training
else
"eval"
if
mode
==
"general"
:
# update, but not apply running_mean/var
result
=
F
.
batch_norm
(
inputs
,
self
.
_mean
,
self
.
_variance
,
self
.
weight
,
self
.
bias
,
self
.
training
,
self
.
_momentum
,
self
.
_epsilon
)
elif
mode
==
"hold"
:
# not update, not apply running_mean/var
result
=
F
.
batch_norm
(
inputs
,
paddle
.
mean
(
inputs
,
axis
=
0
),
paddle
.
var
(
inputs
,
axis
=
0
),
self
.
weight
,
self
.
bias
,
self
.
training
,
self
.
_momentum
,
self
.
_epsilon
)
elif
mode
==
"eval"
:
# fix and apply running_mean/var,
if
self
.
_mean
is
None
:
result
=
F
.
batch_norm
(
inputs
,
paddle
.
mean
(
inputs
,
axis
=
0
),
paddle
.
var
(
inputs
,
axis
=
0
),
self
.
weight
,
self
.
bias
,
True
,
self
.
_momentum
,
self
.
_epsilon
)
else
:
result
=
F
.
batch_norm
(
inputs
,
self
.
_mean
,
self
.
_variance
,
self
.
weight
,
self
.
bias
,
False
,
self
.
_momentum
,
self
.
_epsilon
)
return
result
class
MetaBNNeck
(
nn
.
Layer
):
def
__init__
(
self
,
num_features
,
**
kwargs
):
super
(
MetaBNNeck
,
self
).
__init__
()
weight_attr
=
paddle
.
ParamAttr
(
initializer
=
paddle
.
nn
.
initializer
.
Constant
(
value
=
1.0
))
bias_attr
=
paddle
.
ParamAttr
(
initializer
=
paddle
.
nn
.
initializer
.
Constant
(
value
=
0.0
),
trainable
=
False
)
if
'weight_attr'
in
kwargs
:
weight_attr
=
get_param_attr_dict
(
kwargs
[
'weight_attr'
])
bias_attr
=
None
if
'bias_attr'
in
kwargs
:
bias_attr
=
get_param_attr_dict
(
kwargs
[
'bias_attr'
])
use_global_stats
=
None
if
'use_global_stats'
in
kwargs
:
use_global_stats
=
get_param_attr_dict
(
kwargs
[
'use_global_stats'
])
self
.
feat_bn
=
MetaBN1D
(
num_features
,
momentum
=
0.9
,
epsilon
=
1e-05
,
weight_attr
=
weight_attr
,
bias_attr
=
bias_attr
,
use_global_stats
=
use_global_stats
)
self
.
flatten
=
nn
.
Flatten
()
self
.
opt
=
{}
def
forward
(
self
,
x
):
x
=
self
.
flatten
(
x
)
x
=
self
.
feat_bn
(
x
,
self
.
opt
)
return
x
def
reset_opt
(
self
):
self
.
opt
=
defaultdict
()
def
setup_opt
(
self
,
opt
):
"""
enable_inside_update: enable inside updating for `gate` in MetaBIN
lr_gate: learning rate of `gate` during meta-train phase
bn_mode: control the running stats & updating of BN
"""
self
.
check_opt
(
opt
)
self
.
opt
=
copy
.
deepcopy
(
opt
)
@
classmethod
def
check_opt
(
cls
,
opt
):
assert
isinstance
(
opt
,
dict
),
\
TypeError
(
'Got the wrong type of `opt`. Please use `dict` type.'
)
if
opt
.
get
(
'enable_inside_update'
,
False
)
and
'lr_gate'
not
in
opt
:
raise
RuntimeError
(
'Missing `lr_gate` in opt.'
)
assert
isinstance
(
opt
.
get
(
'lr_gate'
,
1.0
),
float
),
\
TypeError
(
'Got the wrong type of `lr_gate`. Please use `float` type.'
)
assert
isinstance
(
opt
.
get
(
'enable_inside_update'
,
True
),
bool
),
\
TypeError
(
'Got the wrong type of `enable_inside_update`. Please use `bool` type.'
)
assert
opt
.
get
(
'bn_mode'
,
"general"
)
in
[
"general"
,
"hold"
,
"eval"
],
\
TypeError
(
'Got the wrong value of `bn_mode`.'
)
ppcls/configs/reid/MetaBIN_ResNet50_single_source.yaml
浏览文件 @
ac466b68
...
...
@@ -34,7 +34,7 @@ Arch:
BackboneStopLayer
:
name
:
"
flatten"
Neck
:
name
:
BNNeck
name
:
Meta
BNNeck
num_features
:
&feat_dim
2048
use_global_stats
:
True
Head
:
...
...
ppcls/engine/train/train_metabin.py
浏览文件 @
ac466b68
...
...
@@ -99,7 +99,7 @@ def train_epoch_metabin(engine, epoch_id, print_batch_step):
}
# step lr (by iter)
# the last lr_sch is cyclic_lr
for
i
in
range
(
len
(
engine
.
lr_sch
)
-
1
):
for
i
in
range
(
len
(
engine
.
lr_sch
)):
if
not
getattr
(
engine
.
lr_sch
[
i
],
"by_epoch"
,
False
):
engine
.
lr_sch
[
i
].
step
()
# update ema
...
...
@@ -118,7 +118,7 @@ def train_epoch_metabin(engine, epoch_id, print_batch_step):
# step lr(by epoch)
# the last lr_sch is cyclic_lr
for
i
in
range
(
len
(
engine
.
lr_sch
)
-
1
):
for
i
in
range
(
len
(
engine
.
lr_sch
)):
if
getattr
(
engine
.
lr_sch
[
i
],
"by_epoch"
,
False
)
and
\
type_name
(
engine
.
lr_sch
[
i
])
!=
"ReduceOnPlateau"
:
engine
.
lr_sch
[
i
].
step
()
...
...
@@ -138,19 +138,20 @@ def setup_opt(engine, stage):
elif
stage
==
"mtest"
:
norm_lr
=
engine
.
lr_sch
[
1
].
last_lr
cyclic_lr
=
engine
.
lr_sch
[
2
].
get_lr
()
engine
.
lr_sch
[
2
].
step
()
# update cyclic learning rate
opt
[
"bn_mode"
]
=
"hold"
opt
[
"enable_inside_update"
]
=
True
opt
[
"lr_gate"
]
=
norm_lr
*
cyclic_lr
for
name
,
layer
in
engine
.
model
.
backbone
.
named_sublayers
():
if
"bn"
==
name
.
split
(
'.'
)[
-
1
]:
layer
.
setup_opt
(
opt
)
engine
.
model
.
neck
.
setup_opt
(
opt
)
def
reset_opt
(
model
):
for
name
,
layer
in
model
.
backbone
.
named_sublayers
():
if
"bn"
==
name
.
split
(
'.'
)[
-
1
]:
layer
.
reset_opt
()
model
.
neck
.
reset_opt
()
def
get_meta_data
(
meta_dataloader_iter
,
num_domain
):
...
...
@@ -200,6 +201,7 @@ def forward(engine, batch, loss_func):
def
backward
(
engine
,
loss
,
optimizer
):
optimizer
.
clear_grad
()
scaled
=
engine
.
scaler
.
scale
(
loss
)
scaled
.
backward
()
engine
.
scaler
.
minimize
(
optimizer
,
scaled
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录