PaddleClas/ppcls/arch/__init__.py

172 lines
5.8 KiB
Python
Raw Normal View History

2021-06-04 16:44:24 +08:00
#copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
2020-04-09 02:16:30 +08:00
#
#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.
import copy
import importlib
import paddle
import paddle.nn as nn
2021-06-21 14:14:06 +08:00
from paddle.jit import to_static
from paddle.static import InputSpec
2021-06-04 16:44:24 +08:00
from . import backbone, gears
2021-05-24 11:42:24 +08:00
from .backbone import *
2021-06-04 14:54:34 +08:00
from .gears import build_gear
2020-04-13 18:53:03 +08:00
from .utils import *
2021-09-15 11:03:46 +08:00
from ppcls.arch.backbone.base.theseus_layer import TheseusLayer
2021-06-21 14:14:06 +08:00
from ppcls.utils import logger
from ppcls.utils.save_load import load_dygraph_pretrain
2021-12-09 20:08:57 +08:00
from ppcls.arch.slim import prune_model, quantize_model
2022-02-28 19:11:50 +08:00
from ppcls.arch.distill.afd_attention import LinearTransformStudent, LinearTransformTeacher
2021-12-09 14:51:40 +08:00
2022-02-28 19:11:50 +08:00
__all__ = ["build_model", "RecModel", "DistillationModel", "AttentionModel"]
2021-06-01 11:30:26 +08:00
def build_model(config, mode="train"):
2021-12-09 14:51:40 +08:00
arch_config = copy.deepcopy(config["Arch"])
model_type = arch_config.pop("name")
2022-05-14 18:03:55 +08:00
use_sync_bn = arch_config.pop("use_sync_bn", False)
mod = importlib.import_module(__name__)
2021-12-09 14:51:40 +08:00
arch = getattr(mod, model_type)(**arch_config)
2022-05-14 18:03:55 +08:00
if use_sync_bn:
arch = nn.SyncBatchNorm.convert_sync_batchnorm(arch)
2022-05-23 09:11:03 +08:00
2021-12-09 14:51:40 +08:00
if isinstance(arch, TheseusLayer):
prune_model(config, arch)
quantize_model(config, arch, mode)
logger.info("The FLOPs and Params of Arch:")
try:
flops = paddle.flops(arch, [1, *config["Global"]["image_shape"]])
except Exception as e:
logger.warning(
f"An error occurred when calculating FLOPs and Params of Arch. Please check the Global.image_shape in config. The details of error is: {e}"
)
return arch
2021-06-21 14:14:06 +08:00
def apply_to_static(config, model):
support_to_static = config['Global'].get('to_static', False)
if support_to_static:
specs = None
if 'image_shape' in config['Global']:
specs = [InputSpec([None] + config['Global']['image_shape'])]
specs[0].stop_gradient = True
2021-06-21 14:14:06 +08:00
model = to_static(model, input_spec=specs)
logger.info("Successfully to apply @to_static with specs: {}".format(
specs))
return model
2021-12-09 14:51:40 +08:00
class RecModel(TheseusLayer):
def __init__(self, **config):
super().__init__()
backbone_config = config["Backbone"]
backbone_name = backbone_config.pop("name")
2021-06-04 16:44:24 +08:00
self.backbone = eval(backbone_name)(**backbone_config)
2021-06-04 14:54:34 +08:00
if "BackboneStopLayer" in config:
2021-06-04 16:44:24 +08:00
backbone_stop_layer = config["BackboneStopLayer"]["name"]
self.backbone.stop_after(backbone_stop_layer)
2021-06-02 20:04:24 +08:00
2021-06-04 14:54:34 +08:00
if "Neck" in config:
self.neck = build_gear(config["Neck"])
else:
self.neck = None
2021-06-02 20:04:24 +08:00
2021-06-04 14:54:34 +08:00
if "Head" in config:
self.head = build_gear(config["Head"])
else:
self.head = None
2021-06-05 17:56:40 +08:00
def forward(self, x, label=None):
out = dict()
2021-06-04 16:44:24 +08:00
x = self.backbone(x)
out["backbone"] = x
if self.neck is not None:
2021-06-04 16:44:24 +08:00
x = self.neck(x)
out["neck"] = x
out["features"] = x
2021-06-04 14:54:34 +08:00
if self.head is not None:
2021-06-04 16:44:24 +08:00
y = self.head(x, label)
out["logits"] = y
return out
class DistillationModel(nn.Layer):
def __init__(self,
models=None,
pretrained_list=None,
freeze_params_list=None,
**kargs):
super().__init__()
assert isinstance(models, list)
self.model_list = []
self.model_name_list = []
if pretrained_list is not None:
assert len(pretrained_list) == len(models)
if freeze_params_list is None:
freeze_params_list = [False] * len(models)
assert len(freeze_params_list) == len(models)
for idx, model_config in enumerate(models):
assert len(model_config) == 1
key = list(model_config.keys())[0]
model_config = model_config[key]
model_name = model_config.pop("name")
model = eval(model_name)(**model_config)
if freeze_params_list[idx]:
for param in model.parameters():
param.trainable = False
self.model_list.append(self.add_sublayer(key, model))
self.model_name_list.append(key)
if pretrained_list is not None:
for idx, pretrained in enumerate(pretrained_list):
if pretrained is not None:
load_dygraph_pretrain(
self.model_name_list[idx], path=pretrained)
def forward(self, x, label=None):
result_dict = dict()
for idx, model_name in enumerate(self.model_name_list):
if label is None:
result_dict[model_name] = self.model_list[idx](x)
else:
result_dict[model_name] = self.model_list[idx](x, label)
return result_dict
2022-02-28 19:11:50 +08:00
class AttentionModel(DistillationModel):
def __init__(self,
models=None,
pretrained_list=None,
freeze_params_list=None,
**kargs):
super().__init__(models, pretrained_list, freeze_params_list, **kargs)
def forward(self, x, label=None):
result_dict = dict()
out = x
for idx, model_name in enumerate(self.model_name_list):
if label is None:
out = self.model_list[idx](out)
result_dict.update(out)
else:
out = self.model_list[idx](out, label)
result_dict.update(out)
return result_dict