topology.py 3.2 KB
Newer Older
Q
qiaolongfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2016 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.

Q
qiaolongfei 已提交
15 16 17 18 19
import collections

from paddle.proto.ModelConfig_pb2 import ModelConfig

import layer as v2_layer
Q
qiaolongfei 已提交
20 21 22 23 24 25 26 27 28 29

__all__ = ['Topology']


class Topology(object):
    """
    Topology is used to store the information about all layers
    and network configs.
    """

Q
qiaolongfei 已提交
30 31
    def __init__(self, layers):
        if not isinstance(layers, collections.Sequence):
Q
qiaolongfei 已提交
32 33
            __check_layer_type__(layers)
            layers = [layers]
Q
qiaolongfei 已提交
34
        for layer in layers:
Q
qiaolongfei 已提交
35
            __check_layer_type__(layer)
Q
qiaolongfei 已提交
36 37 38
        self.layers = layers
        self.__model_config__ = v2_layer.parse_network(*layers)
        assert isinstance(self.__model_config__, ModelConfig)
Q
qiaolongfei 已提交
39

Q
qiaolongfei 已提交
40
    def proto(self):
Q
qiaolongfei 已提交
41 42 43
        return self.__model_config__

    def get_layer(self, name):
Q
qiaolongfei 已提交
44 45 46 47 48 49 50 51
        """
        get v2.Layer Class instance by layer name
        :param name:
        :return:
        """
        result_layer = []

        def find_layer_by_name(layer, layer_name):
Q
qiaolongfei 已提交
52 53 54
            if len(result_layer) == 1:
                return
            elif layer.name == layer_name:
Q
qiaolongfei 已提交
55
                result_layer.append(layer)
Q
qiaolongfei 已提交
56 57 58
            else:
                for parent_layer in layer.__parent_layers__.values():
                    find_layer_by_name(parent_layer, layer_name)
Q
qiaolongfei 已提交
59 60 61 62

        for layer in self.layers:
            find_layer_by_name(layer, name)

Q
qiaolongfei 已提交
63
        assert len(result_layer) == 1
Q
qiaolongfei 已提交
64 65
        return result_layer[0]

Q
qiaolongfei 已提交
66
    def data_layers(self):
Q
qiaolongfei 已提交
67 68 69 70
        """
        get all data layer
        :return:
        """
71
        data_layers = dict()
Q
qiaolongfei 已提交
72 73 74

        def find_data_layer(layer):
            if isinstance(layer, v2_layer.DataLayerV2):
75 76 77 78 79 80 81 82
                data_layers[layer.name] = layer
            if not isinstance(layer, collections.Sequence):
                for parent_layer in layer.__parent_layers__.values():
                    find_data_layer(parent_layer)
            else:
                for each_l in layer:
                    for parent_layer in each_l.__parent_layers__.values():
                        find_data_layer(parent_layer)
Q
qiaolongfei 已提交
83 84 85 86 87 88

        for layer in self.layers:
            find_data_layer(layer)

        return data_layers

Q
qiaolongfei 已提交
89 90
    def data_type(self):
        """
Q
qiaolongfei 已提交
91 92
        get data_type from proto, such as:
        [('image', dense_vector(768)), ('label', integer_value(10))]
Q
qiaolongfei 已提交
93
        """
94 95 96 97 98 99

        data_types_lists = []
        for each in self.__model_config__.input_layer_names:
            data_layers = self.data_layers()
            data_types_lists.append((each, data_layers[each].type))
        return data_types_lists
Q
qiaolongfei 已提交
100 101


Q
qiaolongfei 已提交
102 103 104
def __check_layer_type__(layer):
    if not isinstance(layer, v2_layer.LayerV2):
        raise ValueError('layer should have type paddle.layer.Layer')