未验证 提交 85ca658e 编写于 作者: B Bin Lu 提交者: GitHub

Update __init__.py

上级 9c395ac8
...@@ -18,11 +18,14 @@ import importlib ...@@ -18,11 +18,14 @@ import importlib
import paddle.nn as nn import paddle.nn as nn
from . import backbone from . import backbone
from . import head
from .backbone import * from .backbone import *
from ppcls.arch.loss_metrics.loss import * from .head import *
from .utils import * from .utils import *
__all__ = ["build_model", "RecModel"]
def build_model(config): def build_model(config):
config = copy.deepcopy(config) config = copy.deepcopy(config)
...@@ -35,31 +38,31 @@ def build_model(config): ...@@ -35,31 +38,31 @@ def build_model(config):
class RecModel(nn.Layer): class RecModel(nn.Layer):
def __init__(self, **config): def __init__(self, **config):
super().__init__() super().__init__()
backbone_config = config["Backbone"] backbone_config = config["Backbone"]
backbone_name = backbone_config.pop("name") backbone_name = backbone_config.pop("name")
self.backbone = getattr(backbone_name)(**backbone_config) self.backbone = eval(backbone_name)(**backbone_config)
if "backbone_stop_layer" in config:
backbone_stop_layer = config["backbone_stop_layer"]
self.backbone.stop_layer(backbone_stop_layer)
if "Neck" in config: assert "Stoplayer" in config, "Stoplayer should be specified in retrieval task \
neck_config = config["Neck"] please specified a Stoplayer config"
neck_name = neck_config.pop("name") stop_layer_config = config["Stoplayer"]
self.neck = getattr(neck_name)(**neck_config) self.backbone.stop_after(stop_layer_config["name"])
if stop_layer_config.get("embedding_size", 0) > 0:
self.neck = nn.Linear(stop_layer_config["output_dim"], stop_layer_config["embedding_size"])
embedding_size = stop_layer_config["embedding_size"]
else: else:
self.neck = None self.neck = None
embedding_size = stop_layer_config["output_dim"]
assert "Head" in config, "Head should be specified in retrieval task \
please specify a Head config"
config["Head"]["embedding_size"] = embedding_size
self.head = build_head(config["Head"])
if "Head" in config: def forward(self, x, label):
head_config = config["Head"] x = self.backbone(x)
head_name = head_config.pop("name")
self.head = getattr(head_name)(**head_config)
else:
self.head = None
def forward(self, x):
y = self.backbone(x)
if self.neck is not None: if self.neck is not None:
y = self.neck(y) x = self.neck(x)
if self.head is not None: y = self.head(x, label)
y = self.head(y) return {"features":x, "logits":y}
return y
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册