converter_mobilenet.py 12.0 KB
Newer Older
xiebaiyuan's avatar
xiebaiyuan 已提交
1
import json
xiebaiyuan's avatar
convert  
xiebaiyuan 已提交
2 3
import os

xiebaiyuan's avatar
xiebaiyuan 已提交
4 5
from core import framework_pb2 as framework_pb2, op_types as types
from mobilenet.swicher import Swichter
xiebaiyuan's avatar
convert  
xiebaiyuan 已提交
6
import shutil
xiebaiyuan's avatar
xiebaiyuan 已提交
7 8 9 10 11 12 13 14 15 16 17


def load_mdl(mdl_json_path):
    # print('mdl json path : ' + mdl_json_path)
    with open(mdl_json_path, 'r') as f:
        return json.load(f)


class Converter:
    'convert mdlmodel to fluidmodel'

xiebaiyuan's avatar
xiebaiyuan 已提交
18 19 20
    def __init__(self, base_dir, mdl_json_path):
        self.mdl_json_path = base_dir + mdl_json_path
        self.base_dir = base_dir
xiebaiyuan's avatar
xiebaiyuan 已提交
21
        print mdl_json_path
xiebaiyuan's avatar
xiebaiyuan 已提交
22 23
        self.mdl_json = load_mdl(self.mdl_json_path)
        self.program_desc = framework_pb2.ProgramDesc()
xiebaiyuan's avatar
xiebaiyuan 已提交
24
        self.weight_list_ = []
xiebaiyuan's avatar
xiebaiyuan 已提交
25
        self.deepwise_weight_list_ = []
xiebaiyuan's avatar
xiebaiyuan 已提交
26 27 28 29 30 31 32 33 34 35 36 37
        # print(json_dick)
        # layers = (json_dick['layer'])
        # for layer in layers:
        #     print(layer)

    def convert(self):
        print 'convert begin.....'
        # add block_desc
        block_desc = self.program_desc.blocks.add()
        block_desc.idx = 0
        block_desc.parent_idx = -1
        self.package_ops(block_desc)
xiebaiyuan's avatar
xiebaiyuan 已提交
38
        self.package_vars(block_desc)
xiebaiyuan's avatar
xiebaiyuan 已提交
39 40
        print 'blocks: '
        print self.program_desc.blocks
xiebaiyuan's avatar
xiebaiyuan 已提交
41 42 43
        print 'convert end.....'
        desc_serialize_to_string = self.program_desc.SerializeToString()

xiebaiyuan's avatar
xiebaiyuan 已提交
44 45 46 47 48 49 50 51 52 53
        outputmodel_ = self.base_dir + 'datas/target/outputmodel/'
        if os.path.exists(outputmodel_):
            shutil.rmtree(outputmodel_)
        os.makedirs(outputmodel_, 0777)
        # todo copy weight files
        # if os.path.exists(outputmodel_):
        #     shutil.rmtree(outputmodel_)
        # shutil.copytree('yolo/datas/multiobjects/float32s_nchw_with_head/', 'mobilenet/datas/target/outputmodel/')

        f = open(outputmodel_ + "__model__", "wb")
xiebaiyuan's avatar
xiebaiyuan 已提交
54 55
        f.write(desc_serialize_to_string)
        f.close()
xiebaiyuan's avatar
xiebaiyuan 已提交
56 57

    def package_ops(self, block_desc):
xiebaiyuan's avatar
xiebaiyuan 已提交
58 59 60

        self.add_op_feed(block_desc)

xiebaiyuan's avatar
xiebaiyuan 已提交
61 62 63 64 65
        # add ops with layer
        if 'layer' in self.mdl_json:

            layers_ = self.mdl_json['layer']
            for layer in layers_:
xiebaiyuan's avatar
xiebaiyuan 已提交
66
                desc_ops_add = block_desc.ops.add()
xiebaiyuan's avatar
xiebaiyuan 已提交
67 68 69 70 71 72

                # print layer
                # for i in layer:
                #     print i
                if 'name' in layer:
                    l_name = layer['name']
xiebaiyuan's avatar
xiebaiyuan 已提交
73
                if 'type' in layer:
xiebaiyuan's avatar
xiebaiyuan 已提交
74
                    self.package_ops_type(desc_ops_add, layer)
xiebaiyuan's avatar
xiebaiyuan 已提交
75

xiebaiyuan's avatar
xiebaiyuan 已提交
76
                if 'weight' in layer:
xiebaiyuan's avatar
xiebaiyuan 已提交
77
                    self.package_ops_weight2inputs(desc_ops_add, layer)
xiebaiyuan's avatar
xiebaiyuan 已提交
78 79

                if 'output' in layer:
xiebaiyuan's avatar
xiebaiyuan 已提交
80
                    self.package_ops_outputs(desc_ops_add, layer)
xiebaiyuan's avatar
xiebaiyuan 已提交
81 82

                if 'input' in layer:
xiebaiyuan's avatar
xiebaiyuan 已提交
83 84 85
                    self.package_ops_inputs(desc_ops_add, layer)

                self.package_ops_attrs(desc_ops_add, layer)
xiebaiyuan's avatar
xiebaiyuan 已提交
86

xiebaiyuan's avatar
xiebaiyuan 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        self.add_op_fetch(block_desc)

    def add_op_feed(self, block_desc):
        desc_ops_add = block_desc.ops.add()
        inputs_add = desc_ops_add.inputs.add()
        inputs_add.parameter = 'X'
        inputs_add.arguments.append('feed')
        desc_ops_add.type = 'feed'
        outputs_add = desc_ops_add.outputs.add()
        outputs_add.parameter = 'Out'
        outputs_add.arguments.append('data')
        attrs_add = desc_ops_add.attrs.add()
        attrs_add.name = 'col'
        # boolean
        attrs_add.type = 0
        attrs_add.i = 0

    def add_op_fetch(self, block_desc):
        desc_ops_add = block_desc.ops.add()
        inputs_add = desc_ops_add.inputs.add()
        inputs_add.parameter = 'X'
        inputs_add.arguments.append('conv_pred_87')
        desc_ops_add.type = 'fetch'
        outputs_add = desc_ops_add.outputs.add()
        outputs_add.parameter = 'Out'
        outputs_add.arguments.append('fetch')
        attrs_add = desc_ops_add.attrs.add()
        attrs_add.name = 'col'
        # boolean
        attrs_add.type = 0
        attrs_add.i = 0
xiebaiyuan's avatar
xiebaiyuan 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    @staticmethod
    def package_ops_attrs(desc_ops_add, layer):
        # print l_params
        # print desc_ops_add.type
        if desc_ops_add.type == types.op_fluid_fusion_conv_add:
            Converter.pack_fusion_conv_add_attr(desc_ops_add, layer)
        elif desc_ops_add.type == types.op_fluid_relu:
            # fusion_conv_add : attrs
            attrs_add = desc_ops_add.attrs.add()
            attrs_add.name = 'use_mkldnn'
            # boolean
            attrs_add.type = 6
            attrs_add.b = 0

    @staticmethod
    def pack_fusion_conv_add_attr(desc_ops_add, layer):

        # fusion_conv_add : attrs
        attrs_add = desc_ops_add.attrs.add()
        attrs_add.name = 'workspace_size_MB'
        # 0-->INT
        attrs_add.type = 0
        attrs_add.i = 4096

        attrs_add = desc_ops_add.attrs.add()
        attrs_add.name = 'data_format'
        # 2-->STRING
        attrs_add.type = 2
        attrs_add.s = 'AnyLayout'

        attrs_add = desc_ops_add.attrs.add()
        attrs_add.name = 'use_mkldnn'
        # boolean
        attrs_add.type = 6
        attrs_add.b = 0

        attrs_add = desc_ops_add.attrs.add()
        attrs_add.name = 'use_cudnn'
        # boolean
        attrs_add.type = 6
        attrs_add.b = 1

        attrs_add = desc_ops_add.attrs.add()
        attrs_add.name = 'dilations'
        # ints
        attrs_add.type = 3
        attrs_add.ints.append(1)
        attrs_add.ints.append(1)

xiebaiyuan's avatar
xiebaiyuan 已提交
168 169 170 171 172 173
        attrs_add = desc_ops_add.attrs.add()
        attrs_add.name = 'axis'
        # int
        attrs_add.type = 0
        attrs_add.i = 1

xiebaiyuan's avatar
xiebaiyuan 已提交
174 175 176 177 178 179
        if 'param' in layer:
            l_params = layer['param']

            attrs_add = desc_ops_add.attrs.add()
            attrs_add.name = 'paddings'
            # ints
xiebaiyuan's avatar
xiebaiyuan 已提交
180
            attrs_add.type = 3
xiebaiyuan's avatar
xiebaiyuan 已提交
181 182 183 184 185 186
            attrs_add.ints.append(l_params[types.fusion_conv_add_attrs_dict.get('paddings')])
            attrs_add.ints.append(l_params[types.fusion_conv_add_attrs_dict.get('paddings')])

            attrs_add = desc_ops_add.attrs.add()
            attrs_add.name = 'strides'
            # ints
xiebaiyuan's avatar
xiebaiyuan 已提交
187
            attrs_add.type = 3
xiebaiyuan's avatar
xiebaiyuan 已提交
188 189 190 191 192 193 194 195
            attrs_add.ints.append(l_params[types.fusion_conv_add_attrs_dict.get('strides')])
            attrs_add.ints.append(l_params[types.fusion_conv_add_attrs_dict.get('strides')])

            attrs_add = desc_ops_add.attrs.add()
            attrs_add.name = 'groups'
            # int
            attrs_add.type = 0
            attrs_add.i = l_params[types.fusion_conv_add_attrs_dict.get('groups')]
xiebaiyuan's avatar
xiebaiyuan 已提交
196
            # attrs_add.i = 1
xiebaiyuan's avatar
xiebaiyuan 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

        #
        # op_attrs_tupl = types.op_io_dict.get(desc_ops_add.type) \
        #     .get(types.mdl_attrs_key)
        #
        #
        #
        #
        # # group stride padding
        # print '----------------------'
        # for i, val in enumerate(op_attrs_tupl):
        #     attrs_add = desc_ops_add.attrs.add()
        #     attr_name = op_attrs_tupl[i]
        #     print attr_name
        #     attrs_add.name = attr_name
        #     attrs_add.type = types.fluid_attrs_type_dict.get(attr_name)
        #     attrs_add.
        #     print l_params[types.fusion_conv_add_attrs_dict.get(attr_name)]

        # for p in l_params:
        #     attrs_add = desc_ops_add.attrs.add()

    @staticmethod
    def package_ops_inputs(desc_ops_add, layer):
        l_inputs = layer['input']
        for i in l_inputs:
            inputs_add = desc_ops_add.inputs.add()
            # print i
            inputs_add.parameter = types.op_io_dict.get(desc_ops_add.type).get(types.mdl_inputs_key)
            inputs_add.arguments.append(i)

    @staticmethod
    def package_ops_outputs(desc_ops_add, layer):
        l_outputs = layer['output']
        for o in l_outputs:
            # print o
            outputs_add = desc_ops_add.outputs.add()
xiebaiyuan's avatar
xiebaiyuan 已提交
234 235 236 237
            dict = types.op_io_dict.get(desc_ops_add.type)
            print 'desc_ops_add.type:  ' + desc_ops_add.type
            print dict
            outputs_add.parameter = dict.get(types.mdl_outputs_key)
xiebaiyuan's avatar
xiebaiyuan 已提交
238 239
            outputs_add.arguments.append(o)

xiebaiyuan's avatar
xiebaiyuan 已提交
240
    def package_ops_weight2inputs(self, desc_ops_add, layer):
xiebaiyuan's avatar
xiebaiyuan 已提交
241
        l_weights = layer['weight']
xiebaiyuan's avatar
xiebaiyuan 已提交
242 243
        for w in l_weights:
            self.weight_list_.append(w)
xiebaiyuan's avatar
xiebaiyuan 已提交
244

xiebaiyuan's avatar
xiebaiyuan 已提交
245
        if layer['type'] == types.layer_mdl_deepwise_conv:
xiebaiyuan's avatar
xiebaiyuan 已提交
246 247 248
            # print l_weights[0]
            self.deepwise_weight_list_.append(l_weights[0])

xiebaiyuan's avatar
xiebaiyuan 已提交
249
        op_weight_tup = types.op_io_dict.get(desc_ops_add.type).get(types.mdl_weight_key)
xiebaiyuan's avatar
xiebaiyuan 已提交
250 251 252 253 254 255 256 257
        if op_weight_tup is not None:
            # print len(op_weight_tup)
            for i, val in enumerate(op_weight_tup):
                # print i
                # print val
                inputs_add = desc_ops_add.inputs.add()
                inputs_add.parameter = op_weight_tup[i]
                inputs_add.arguments.append(l_weights[i])
xiebaiyuan's avatar
xiebaiyuan 已提交
258

xiebaiyuan's avatar
xiebaiyuan 已提交
259 260 261 262 263 264 265 266 267 268 269 270
        # for w in l_weights:
        #     inputs_add = desc_ops_add.inputs.add()
        #     # print w
        #     inputs_add.parameter = op_weight_tup[0]
        #     inputs_add.arguments.append(w)

    @staticmethod
    def package_ops_type(desc_ops_add, layer):
        l_type = layer['type']
        # print l_type
        # print mdl2fluid_op_layer_dict.get(l_type)
        desc_ops_add.type = types.mdl2fluid_op_layer_dict.get(l_type)
xiebaiyuan's avatar
xiebaiyuan 已提交
271

xiebaiyuan's avatar
xiebaiyuan 已提交
272
    def package_vars(self, block_desc):
xiebaiyuan's avatar
xiebaiyuan 已提交
273 274 275 276 277 278 279 280 281 282
        vars_add = block_desc.vars.add()
        vars_add.name = 'feed'
        vars_add.type.type = 9  # 9 is FEED_MINIBATCH
        vars_add.persistable = 1
        # fetch
        vars_add = block_desc.vars.add()
        vars_add.name = 'fetch'
        vars_add.type.type = 10  # 10 is fetch list
        vars_add.persistable = 1

xiebaiyuan's avatar
xiebaiyuan 已提交
283 284 285 286 287 288
        json_matrix_ = self.mdl_json['matrix']
        # print json_matrix_
        for j in json_matrix_:
            vars_add = block_desc.vars.add()
            vars_add.name = j
            vars_add.type.type = 7  # 7 is lodtensor
xiebaiyuan's avatar
xiebaiyuan 已提交
289
            # print j
xiebaiyuan's avatar
xiebaiyuan 已提交
290 291
            tensor = vars_add.type.lod_tensor.tensor
            tensor.data_type = 5  # 5 is FP32
xiebaiyuan's avatar
convert  
xiebaiyuan 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304

            # print json_matrix_

            dims_of_matrix = json_matrix_.get(j)
            # dims_size = len(dims_of_matrix)
            # print dims_size

            # if dims_size == 4:
            #     tensor.dims.append(dims_of_matrix[0])  # N
            #     tensor.dims.append(dims_of_matrix[3])  # C
            #     tensor.dims.append(dims_of_matrix[1])  # H
            #     tensor.dims.append(dims_of_matrix[2])  # W
            # else:
xiebaiyuan's avatar
xiebaiyuan 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317

            # issues in mdl model filter swich n and c
            if j in self.deepwise_weight_list_ and len(dims_of_matrix) == 4:
                print j
                tensor.dims.append(dims_of_matrix[1])
                tensor.dims.append(dims_of_matrix[0])
                tensor.dims.append(dims_of_matrix[2])
                tensor.dims.append(dims_of_matrix[3])
                print tensor.dims
            else:
                for dims in dims_of_matrix:
                    # print dims
                    tensor.dims.append(dims)
xiebaiyuan's avatar
convert  
xiebaiyuan 已提交
318

xiebaiyuan's avatar
xiebaiyuan 已提交
319 320
            if j in self.weight_list_:
                vars_add.persistable = 1
xiebaiyuan's avatar
convert  
xiebaiyuan 已提交
321 322
                dims_size = len(dims_of_matrix)
                # print dims_size
xiebaiyuan's avatar
xiebaiyuan 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
                # if dims_size == 4:
                #     # convert weight from nhwc to nchw
                #     Swichter().nhwc2nchw_one_slice_add_head(
                #         'yolo/datas/multiobjects/float32s_nhwc/' + j + '.bin',
                #         'yolo/datas/multiobjects/float32s_nchw_with_head/' + j,
                #         'yolo/datas/multiobjects/float32s_nchw/' + j + '.tmp',
                #         dims_of_matrix[0],
                #         dims_of_matrix[1],
                #         dims_of_matrix[2],
                #         dims_of_matrix[3]
                #     )
                # else:
                #     Swichter().copy_add_head(
                #         'yolo/datas/multiobjects/float32s_nhwc/' + j + '.bin',
                #         'yolo/datas/multiobjects/float32s_nchw_with_head/' + j,
                #         'yolo/datas/multiobjects/float32s_nchw/' + j + '.tmp'
                #     )
xiebaiyuan's avatar
xiebaiyuan 已提交
340 341
            else:
                vars_add.persistable = 0
xiebaiyuan's avatar
xiebaiyuan 已提交
342

xiebaiyuan's avatar
xiebaiyuan 已提交
343

xiebaiyuan's avatar
xiebaiyuan 已提交
344 345 346
mdl_path = "datas/sourcemodels/cls231_0802/mobileNetModel.json"
base_dir = "/Users/xiebaiyuan/PaddleProject/paddle-mobile/tools/python/modeltools/mobilenet/"
converter = Converter(base_dir, mdl_path)
xiebaiyuan's avatar
xiebaiyuan 已提交
347
converter.convert()