|
|
|
@ -71,9 +71,15 @@ import collections
|
|
|
|
|
import paddle.trainer_config_helpers as conf_helps
|
|
|
|
|
from paddle.trainer_config_helpers.config_parser_utils import \
|
|
|
|
|
parse_network_config as __parse__
|
|
|
|
|
|
|
|
|
|
from paddle.trainer_config_helpers.default_decorators import wrap_name_default
|
|
|
|
|
from paddle.trainer_config_helpers.default_decorators import wrap_act_default
|
|
|
|
|
from paddle.trainer_config_helpers.default_decorators import wrap_bias_attr_default
|
|
|
|
|
from paddle.trainer_config_helpers.layers import layer_support
|
|
|
|
|
|
|
|
|
|
import data_type
|
|
|
|
|
import activation
|
|
|
|
|
import attr
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
'parse_network', 'data', 'fc', 'conv_shift', 'img_conv', 'img_pool', 'spp',
|
|
|
|
@ -89,6 +95,13 @@ __all__ = [
|
|
|
|
|
'hsigmoid', 'eos'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
__projection_names__ = filter(lambda x: x.endswith('_projection'),
|
|
|
|
|
dir(conf_helps))
|
|
|
|
|
__all__ += __projection_names__
|
|
|
|
|
|
|
|
|
|
__operator_names__ = filter(lambda x: x.endswith('_operator'), dir(conf_helps))
|
|
|
|
|
__all__ += __operator_names__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_network(*outputs):
|
|
|
|
|
"""
|
|
|
|
@ -106,9 +119,8 @@ def parse_network(*outputs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Layer(object):
|
|
|
|
|
def __init__(self, name, parent_layers):
|
|
|
|
|
def __init__(self, name=None, parent_layers=None):
|
|
|
|
|
assert isinstance(parent_layers, dict)
|
|
|
|
|
assert isinstance(name, basestring)
|
|
|
|
|
self.name = name
|
|
|
|
|
self.__parent_layers__ = parent_layers
|
|
|
|
|
|
|
|
|
@ -127,19 +139,25 @@ class Layer(object):
|
|
|
|
|
self.__parent_layers__[layer_name])
|
|
|
|
|
kwargs[layer_name] = v1_layer
|
|
|
|
|
|
|
|
|
|
if self.name not in context:
|
|
|
|
|
if self.name is None:
|
|
|
|
|
return self.to_proto_impl(**kwargs)
|
|
|
|
|
elif self.name not in context:
|
|
|
|
|
context[self.name] = self.to_proto_impl(**kwargs)
|
|
|
|
|
|
|
|
|
|
return context[self.name]
|
|
|
|
|
|
|
|
|
|
def to_proto_impl(self, **kwargs):
|
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __convert_to_v2__(method_name, parent_names):
|
|
|
|
|
wrapper = wrap_name_default(name_prefix=method_name)
|
|
|
|
|
def __convert_to_v2__(method_name, parent_names, is_default_name=True):
|
|
|
|
|
if is_default_name:
|
|
|
|
|
wrapper = wrap_name_default(name_prefix=method_name)
|
|
|
|
|
else:
|
|
|
|
|
wrapper = None
|
|
|
|
|
|
|
|
|
|
class V2LayerImpl(Layer):
|
|
|
|
|
def __init__(self, name=None, **kwargs):
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
parent_layers = dict()
|
|
|
|
|
other_kwargs = dict()
|
|
|
|
|
for pname in parent_names:
|
|
|
|
@ -150,6 +168,7 @@ def __convert_to_v2__(method_name, parent_names):
|
|
|
|
|
if key not in parent_names:
|
|
|
|
|
other_kwargs[key] = kwargs[key]
|
|
|
|
|
|
|
|
|
|
name = kwargs.get('name', None)
|
|
|
|
|
super(V2LayerImpl, self).__init__(name, parent_layers)
|
|
|
|
|
self.__other_kwargs__ = other_kwargs
|
|
|
|
|
|
|
|
|
@ -162,7 +181,7 @@ def __convert_to_v2__(method_name, parent_names):
|
|
|
|
|
args[each] = kwargs[each]
|
|
|
|
|
for each in self.__other_kwargs__:
|
|
|
|
|
args[each] = self.__other_kwargs__[each]
|
|
|
|
|
return getattr(conf_helps, method_name)(name=self.name, **args)
|
|
|
|
|
return getattr(conf_helps, method_name)(**args)
|
|
|
|
|
|
|
|
|
|
return V2LayerImpl
|
|
|
|
|
|
|
|
|
@ -193,6 +212,78 @@ class DataLayerV2(Layer):
|
|
|
|
|
return getattr(conf_helps, self.__method_name__)(name=self.name, **args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MixedLayerV2(Layer):
|
|
|
|
|
"""
|
|
|
|
|
This class is use to support `with` grammar. If not, the following code
|
|
|
|
|
could convert mixed_layer simply.
|
|
|
|
|
|
|
|
|
|
mixed = __convert_to_v2__(
|
|
|
|
|
'mixed_layer', name_prefix='mixed', parent_names=['input'])
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
class AddToSealedMixedLayerExceptionV2(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
|
size=0,
|
|
|
|
|
input=None,
|
|
|
|
|
name=None,
|
|
|
|
|
act=None,
|
|
|
|
|
bias_attr=None,
|
|
|
|
|
layer_attr=None):
|
|
|
|
|
self.__method_name__ = 'mixed_layer'
|
|
|
|
|
self.finalized = False
|
|
|
|
|
self.__inputs__ = []
|
|
|
|
|
if input is not None:
|
|
|
|
|
self.__inputs__ = input
|
|
|
|
|
|
|
|
|
|
other_kwargs = dict()
|
|
|
|
|
other_kwargs['name'] = name
|
|
|
|
|
other_kwargs['size'] = size
|
|
|
|
|
other_kwargs['act'] = act
|
|
|
|
|
other_kwargs['bias_attr'] = bias_attr
|
|
|
|
|
other_kwargs['layer_attr'] = layer_attr
|
|
|
|
|
|
|
|
|
|
parent_layers = {"input": self.__inputs__}
|
|
|
|
|
super(MixedLayerV2, self).__init__(name, parent_layers)
|
|
|
|
|
self.__other_kwargs__ = other_kwargs
|
|
|
|
|
|
|
|
|
|
def __iadd__(self, other):
|
|
|
|
|
if not self.finalized:
|
|
|
|
|
self.__inputs__.append(other)
|
|
|
|
|
return self
|
|
|
|
|
else:
|
|
|
|
|
raise MixedLayerTypeV2.AddToSealedMixedLayerExceptionV2()
|
|
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
|
assert len(self.__inputs__) == 0
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def __exit__(self, *args, **kwargs):
|
|
|
|
|
self.finalized = True
|
|
|
|
|
|
|
|
|
|
def to_proto_impl(self, **kwargs):
|
|
|
|
|
args = dict()
|
|
|
|
|
for each in kwargs:
|
|
|
|
|
args[each] = kwargs[each]
|
|
|
|
|
for each in self.__other_kwargs__:
|
|
|
|
|
args[each] = self.__other_kwargs__[each]
|
|
|
|
|
return getattr(conf_helps, self.__method_name__)(**args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@wrap_name_default("mixed")
|
|
|
|
|
@wrap_act_default(act=activation.Linear())
|
|
|
|
|
@wrap_bias_attr_default(has_bias=False)
|
|
|
|
|
@layer_support(conf_helps.layers.ERROR_CLIPPING, conf_helps.layers.DROPOUT)
|
|
|
|
|
def mixed(size=0,
|
|
|
|
|
name=None,
|
|
|
|
|
input=None,
|
|
|
|
|
act=None,
|
|
|
|
|
bias_attr=False,
|
|
|
|
|
layer_attr=None):
|
|
|
|
|
return MixedLayerV2(size, input, name, act, bias_attr, layer_attr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = DataLayerV2
|
|
|
|
|
AggregateLevel = conf_helps.layers.AggregateLevel
|
|
|
|
|
ExpandLevel = conf_helps.layers.ExpandLevel
|
|
|
|
@ -274,3 +365,18 @@ layer_list = [
|
|
|
|
|
]
|
|
|
|
|
for l in layer_list:
|
|
|
|
|
globals()[l[0]] = __convert_to_v2__(l[1], l[2])
|
|
|
|
|
|
|
|
|
|
# convert projection
|
|
|
|
|
for prj in __projection_names__:
|
|
|
|
|
globals()[prj] = __convert_to_v2__(
|
|
|
|
|
prj, parent_names=['input'], is_default_name=False)
|
|
|
|
|
|
|
|
|
|
# convert operator
|
|
|
|
|
operator_list = [
|
|
|
|
|
# [V1_method_name, parent_names],
|
|
|
|
|
['dotmul_operator', ['a', 'b']],
|
|
|
|
|
['conv_operator', ['img', 'filter']]
|
|
|
|
|
]
|
|
|
|
|
for op in operator_list:
|
|
|
|
|
globals()[op[0]] = __convert_to_v2__(
|
|
|
|
|
op[0], parent_names=op[1], is_default_name=False)
|
|
|
|
|