提交 50900443 编写于 作者: Z zhiboniu

remove strongbaseline_attr, etc...

上级 26d5b7d1
......@@ -70,7 +70,6 @@ from ppcls.arch.backbone.model_zoo.van import VAN_tiny
from ppcls.arch.backbone.variant_models.resnet_variant import ResNet50_last_stage_stride1
from ppcls.arch.backbone.variant_models.vgg_variant import VGG19Sigmoid
from ppcls.arch.backbone.variant_models.pp_lcnet_variant import PPLCNet_x2_5_Tanh
from ppcls.arch.backbone.model_zoo.strongbaseline_attr import StrongBaselineAttr
# help whl get all the models' api (class type) and components' api (func type)
......
......@@ -114,10 +114,10 @@ class ConvBNLayer(TheseusLayer):
filter_size,
stride=1,
groups=1,
norm_decay=0.0005,
is_vd_mode=False,
act=None,
lr_mult=1.0,
norm_decay=0.,
data_format="NCHW"):
super().__init__()
self.is_vd_mode = is_vd_mode
......@@ -135,7 +135,7 @@ class ConvBNLayer(TheseusLayer):
bias_attr=False,
data_format=data_format)
param_attr = ParamAttr(
weight_attr = ParamAttr(
learning_rate=lr_mult,
regularizer=L2Decay(norm_decay),
trainable=True)
......@@ -145,7 +145,7 @@ class ConvBNLayer(TheseusLayer):
trainable=True)
self.bn = BatchNorm2D(
num_filters, weight_attr=param_attr, bias_attr=bias_attr)
num_filters, weight_attr=weight_attr, bias_attr=bias_attr)
self.relu = nn.ReLU()
def forward(self, x):
......@@ -166,6 +166,7 @@ class BottleneckBlock(TheseusLayer):
shortcut=True,
if_first=False,
lr_mult=1.0,
norm_decay=0.,
data_format="NCHW"):
super().__init__()
......@@ -175,6 +176,7 @@ class BottleneckBlock(TheseusLayer):
filter_size=1,
act="relu",
lr_mult=lr_mult,
norm_decay=norm_decay,
data_format=data_format)
self.conv1 = ConvBNLayer(
num_channels=num_filters,
......@@ -183,6 +185,7 @@ class BottleneckBlock(TheseusLayer):
stride=stride,
act="relu",
lr_mult=lr_mult,
norm_decay=norm_decay,
data_format=data_format)
self.conv2 = ConvBNLayer(
num_channels=num_filters,
......@@ -190,6 +193,7 @@ class BottleneckBlock(TheseusLayer):
filter_size=1,
act=None,
lr_mult=lr_mult,
norm_decay=norm_decay,
data_format=data_format)
if not shortcut:
......@@ -200,6 +204,7 @@ class BottleneckBlock(TheseusLayer):
stride=stride if if_first else 1,
is_vd_mode=False if if_first else True,
lr_mult=lr_mult,
norm_decay=norm_decay,
data_format=data_format)
self.relu = nn.ReLU()
......@@ -228,6 +233,7 @@ class BasicBlock(TheseusLayer):
shortcut=True,
if_first=False,
lr_mult=1.0,
norm_decay=0.,
data_format="NCHW"):
super().__init__()
......@@ -239,6 +245,7 @@ class BasicBlock(TheseusLayer):
stride=stride,
act="relu",
lr_mult=lr_mult,
norm_decay=norm_decay,
data_format=data_format)
self.conv1 = ConvBNLayer(
num_channels=num_filters,
......@@ -246,6 +253,7 @@ class BasicBlock(TheseusLayer):
filter_size=3,
act=None,
lr_mult=lr_mult,
norm_decay=norm_decay,
data_format=data_format)
if not shortcut:
self.short = ConvBNLayer(
......@@ -255,6 +263,7 @@ class BasicBlock(TheseusLayer):
stride=stride if if_first else 1,
is_vd_mode=False if if_first else True,
lr_mult=lr_mult,
norm_decay=norm_decay,
data_format=data_format)
self.shortcut = shortcut
self.relu = nn.ReLU()
......@@ -291,6 +300,7 @@ class ResNet(TheseusLayer):
stem_act="relu",
class_num=1000,
lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0],
norm_decay=0.,
data_format="NCHW",
input_image_channel=3,
return_patterns=None,
......@@ -330,6 +340,7 @@ class ResNet(TheseusLayer):
stride=s,
act=stem_act,
lr_mult=self.lr_mult_list[0],
norm_decay=norm_decay,
data_format=data_format)
for in_c, out_c, k, s in self.stem_cfg[version]
])
......@@ -348,6 +359,7 @@ class ResNet(TheseusLayer):
shortcut=shortcut,
if_first=block_idx == i == 0 if version == "vd" else True,
lr_mult=self.lr_mult_list[block_idx + 1],
norm_decay=norm_decay,
data_format=data_format))
shortcut = True
self.blocks = nn.Sequential(*block_list)
......
# copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
#
# 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
from __future__ import division
from __future__ import print_function
import numpy as np
import paddle
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
import math
from ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url, get_weights_path_from_url
from ..legendary_models.resnet import ResNet50
MODEL_URLS = {"StrongBaselineAttr": "strongbaseline_attr_clas", }
__all__ = list(MODEL_URLS.keys())
class StrongBaselinePAR(nn.Layer):
def __init__(
self,
**config, ):
"""
A strong baseline for Pedestrian Attribute Recognition, see https://arxiv.org/abs/2107.03576
Args:
backbone (object): backbone instance
classifier (object): classifier instance
loss (object): loss instance
"""
super(StrongBaselinePAR, self).__init__()
backbone_config = config["Backbone"]
backbone_name = backbone_config.pop("name")
self.backbone = eval(backbone_name)(**backbone_config)
def forward(self, x):
fc_feat = self.backbone(x)
output = F.sigmoid(fc_feat)
return fc_feat
def _load_pretrained(pretrained, model, model_url, use_ssld):
if pretrained is False:
pass
elif pretrained is True:
load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
elif isinstance(pretrained, str):
load_dygraph_pretrain(model, pretrained)
else:
raise RuntimeError(
"pretrained type is not available. Please use `string` or `boolean` type."
)
def load_pretrained(model, local_weight_path):
# local_weight_path = get_weights_path_from_url(model_url).replace(
# ".pdparams", "")
param_state_dict = paddle.load(local_weight_path + ".pdparams")
model_dict = model.state_dict()
model_dict_keys = list(model_dict.keys())
param_state_dict_keys = list(param_state_dict.keys())
# assert(len(model_dict_keys) == len(param_state_dict_keys)), "{} == {}".format(len(model_dict_keys), len(param_state_dict_keys))
for idx in range(len(model_dict.keys())):
model_key = model_dict_keys[idx]
param_key = param_state_dict_keys[idx]
if model_dict[model_key].shape == param_state_dict[param_key].shape:
model_dict[model_key] = param_state_dict[param_key]
else:
print("miss match idx: {} weights: {} vs {}; {} vs {}".format(
idx, model_key, param_key, model_dict[
model_key].shape, param_state_dict[param_key].shape))
model.set_dict(model_dict)
def StrongBaselineAttr(pretrained=True, use_ssld=False, **kwargs):
model = StrongBaselinePAR(**kwargs)
_load_pretrained(MODEL_URLS["StrongBaselineAttr"], model, None, None)
# load_pretrained(model, MODEL_URLS["StrongBaselineAttr"])
return model
......@@ -17,10 +17,10 @@ Global:
# model architecture
Arch:
name: "StrongBaselineAttr"
Backbone:
name: "ResNet50"
class_num: 26
name: "ResNet50"
pretrained: True
class_num: 26
norm_decay: 0.0005
# loss function config for traing/eval process
Loss:
......@@ -60,7 +60,7 @@ DataLoader:
channel_first: False
- ResizeImage:
size: [192, 256]
- Pad:
- Padv2:
size: [212, 276]
pad_mode: 1
fill_value: 0
......
......@@ -34,6 +34,7 @@ from ppcls.data.preprocess.ops.operators import Pad
from ppcls.data.preprocess.ops.operators import ToTensor
from ppcls.data.preprocess.ops.operators import Normalize
from ppcls.data.preprocess.ops.operators import RandomCropImage
from ppcls.data.preprocess.ops.operators import Padv2
from ppcls.data.preprocess.batch_ops.batch_operators import MixupOperator, CutmixOperator, OpSampler, FmixOperator
......
......@@ -190,7 +190,7 @@ class CropImage(object):
return img[h_start:h_end, w_start:w_end, :]
class Pad(object):
class Padv2(object):
def __init__(self,
size=None,
size_divisor=32,
......
......@@ -32,8 +32,8 @@ def classification_eval(engine, epoch_id=0):
}
print_batch_step = engine.config["Global"]["print_batch_step"]
if engine.eval_metric_func is not None and engine.config["Arch"][
"name"] == "StrongBaselineAttr":
if engine.eval_metric_func is not None and "ATTRMetric" in engine.config[
"Metric"]["Eval"][0]:
output_info["attr"] = AttrMeter(threshold=0.5)
metric_key = None
......@@ -128,7 +128,7 @@ def classification_eval(engine, epoch_id=0):
# calc metric
if engine.eval_metric_func is not None:
if engine.config["Arch"]["name"] == "StrongBaselineAttr":
if "ATTRMetric" in engine.config["Metric"]["Eval"][0]:
metric_dict = engine.eval_metric_func(preds, labels)
metric_key = "attr"
output_info["attr"].update(metric_dict)
......@@ -153,7 +153,7 @@ def classification_eval(engine, epoch_id=0):
ips_msg = "ips: {:.5f} images/sec".format(
batch_size / time_info["batch_cost"].avg)
if engine.config["Arch"]["name"] == "StrongBaselineAttr":
if "ATTRMetric" in engine.config["Metric"]["Eval"][0]:
metric_msg = ""
else:
metric_msg = ", ".join([
......@@ -168,7 +168,7 @@ def classification_eval(engine, epoch_id=0):
if engine.use_dali:
engine.eval_dataloader.reset()
if engine.config["Arch"]["name"] == "StrongBaselineAttr":
if "ATTRMetric" in engine.config["Metric"]["Eval"][0]:
metric_msg = ", ".join([
"evalres: ma: {:.5f} label_f1: {:.5f} label_pos_recall: {:.5f} label_neg_recall: {:.5f} instance_f1: {:.5f} instance_acc: {:.5f} instance_prec: {:.5f} instance_recall: {:.5f}".
format(*output_info["attr"].res())
......
......@@ -362,7 +362,7 @@ class ATTRMetric(nn.Layer):
super().__init__()
self.threshold = threshold
def __call__(self, output, target):
def forward(self, output, target):
metric_dict = get_attr_metrics(target[0].numpy(),
output.numpy(), self.threshold)
return metric_dict
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册