diff --git a/src/operators/math/depthwise_conv_3x3.cpp b/src/operators/math/depthwise_conv_3x3.cpp index 91e11fa8ff0184e5321269167b5f4693de2245ac..f745bdd85c0b281dc959455df2e6f5becd266ace 100644 --- a/src/operators/math/depthwise_conv_3x3.cpp +++ b/src/operators/math/depthwise_conv_3x3.cpp @@ -1667,7 +1667,7 @@ void DepthwiseConvAddBNRelu3x3s2p1v2(const Tensor *input, const Tensor *filter, const int w_times = (out_w - 2) / 3; float32x4_t zero = vdupq_n_f32(0.0); for (int b = batch_size; b > 0; --b) { - #pragma omp parallel for +#pragma omp parallel for for (int j = 0; j < c; j++) { const float *input_row_ptr; float *output_row_ptr; @@ -1901,7 +1901,9 @@ void DepthwiseConv3x3s2p0(const Tensor *input, const Tensor *filter, for (int c = 0; c < input_channel; c++) { const float *filter_data = filter->data() + c * 9; const float *input_data = input->data() + c * inhxw; - const float *bias_data = bias.data() + c; + if (if_bias) { + const float *bias_data = bias.data() + c; + } float *output_data = output->data() + c * outhxw; float w00 = filter_data[0]; float w01 = filter_data[1]; @@ -1912,9 +1914,9 @@ void DepthwiseConv3x3s2p0(const Tensor *input, const Tensor *filter, float w20 = filter_data[6]; float w21 = filter_data[7]; float w22 = filter_data[8]; - - float32x4_t biasv = vld1q_dup_f32(bias_data); - + if (if_bias) { + float32x4_t biasv = vld1q_dup_f32(bias_data); + } for (int i = 0; i < output_height; i += 1) { for (int m = 0; m < output_width - 2; m += 3) { float *output_ptr = output_data + i * output_width + m; @@ -1949,8 +1951,9 @@ void DepthwiseConv3x3s2p0(const Tensor *input, const Tensor *filter, out0 = vmlaq_n_f32(out0, in4, w20); out0 = vmlaq_n_f32(out0, tmp4, w21); out0 = vmlaq_n_f32(out0, tmp5, w22); - out0 = vaddq_f32(out0, biasv); - + if (if_bias) { + out0 = vaddq_f32(out0, biasv); + } vst1q_lane_f32(output_ptr, out0, 0); vst1q_lane_f32(output_ptr + 1, out0, 1); vst1q_lane_f32(output_ptr + 2, out0, 2); @@ -1960,16 +1963,18 @@ void DepthwiseConv3x3s2p0(const Tensor *input, const Tensor *filter, } for (int j = m; j < output_width; j++) { output_data[i * output_width + j] = - input_data[(2 * i - 1) * input_width + 2 * j - 1] * w00 + - input_data[(2 * i - 1) * input_width + 2 * j] * w01 + - input_data[(2 * i - 1) * input_width + 2 * j + 1] * w02 + - input_data[(2 * i) * input_width + 2 * j - 1] * w10 + - input_data[(2 * i) * input_width + 2 * j] * w11 + - input_data[(2 * i) * input_width + 2 * j + 1] * w12 + - input_data[(2 * i + 1) * input_width + 2 * j - 1] * w20 + - input_data[(2 * i + 1) * input_width + 2 * j] * w21 + - input_data[(2 * i + 1) * input_width + 2 * j + 1] * w22; - output_data[i * output_width + j] += *bias_data; + input_data[(2 * i) * input_width + 2 * j] * w00 + + input_data[(2 * i) * input_width + 2 * j + 1] * w01 + + input_data[(2 * i) * input_width + 2 * j + 2] * w02 + + input_data[(2 * i + 1) * input_width + 2 * j] * w10 + + input_data[(2 * i + 1) * input_width + 2 * j + 1] * w11 + + input_data[(2 * i + 1) * input_width + 2 * j + 2] * w12 + + input_data[(2 * i + 2) * input_width + 2 * j] * w20 + + input_data[(2 * i + 2) * input_width + 2 * j + 1] * w21 + + input_data[(2 * i + 2) * input_width + 2 * j + 2] * w22; + if (if_bias) { + output_data[i * output_width + j] += *bias_data; + } } } } diff --git a/tools/op.cmake b/tools/op.cmake index 5d5567a524ae69bfb4668ff6078621eb4cb5920d..4795568b8e64549d3d21fd5546ff2eec15a05012 100644 --- a/tools/op.cmake +++ b/tools/op.cmake @@ -33,6 +33,7 @@ if (CON GREATER -1) set(POOL_OP ON) set(RESHAPE_OP ON) set(FUSION_CONVADDBNRELU_OP ON) + set(FUSION_CONVADDRELU_OP ON) set(FUSION_CONVADD_OP ON) set(FOUND_MATCH ON) diff --git a/tools/python/imagetools/img2nchw.py b/tools/python/imagetools/img2nchw.py index 70ca456a1b1b5d20b92d0aaa51b01abb352c1d54..b38c9808059e08b089303208063184bb956667c1 100644 --- a/tools/python/imagetools/img2nchw.py +++ b/tools/python/imagetools/img2nchw.py @@ -45,13 +45,13 @@ def combine_bgrs_nchw(bgrs, means_b_g_r, scale, channel_type=ChannelType.BGR): print '------------------' print bgrs_float_array[0] - print bgrs_float_array[416 * 416 * 2 + 416 * 2 + 2] + print bgrs_float_array[224 * 224 * 2 + 224 * 2 + 2] # for i in range(0, 9): # print'bs %d' % i # print bs[i] / 255. - print bs[416 * 2 + 2] / 255. + print bs[224 * 2 + 2] / 255. print '--------------combine_bgrs_nchw-----------------end' return bgrs_float_array @@ -64,6 +64,6 @@ def combine_bgrs_nchw(bgrs, means_b_g_r, scale, channel_type=ChannelType.BGR): # cv2.waitKey(0) -bgrs = tools.resize_take_rgbs('datas/newyolo.jpg', (416, 416, 3)) +bgrs = tools.resize_take_rgbs('datas/jpgs/0000_0.9834-148196_82452-0ad4b83ec6bc0f9c5f28101539267054.jpg_p0_0.126571263346.jpg', (224, 224, 3)) array = combine_bgrs_nchw(bgrs, (0, 0, 0), 1. / 255, ChannelType.RGB) -tools.save_to_file('datas/desktop_1_3_416_416_nchw_float', array) +tools.save_to_file('datas/desktop_1_3_224_224_nchw_float', array) diff --git a/tools/python/imagetools/numpy2binary.py b/tools/python/imagetools/numpy2binary.py index dd4bc6e10074183b8dcee4122860c4140ff54229..87f0fda76666225256e7a80ddf3a5b0cda8ad12f 100644 --- a/tools/python/imagetools/numpy2binary.py +++ b/tools/python/imagetools/numpy2binary.py @@ -15,11 +15,11 @@ from array import array # image.resize(shape_h_w) -data = np.fromfile('datas/img.res') +data = np.fromfile('/Users/xiebaiyuan/PaddleProject/paddle-mobile/tools/python/imagetools/datas/jpgs2/0000_0.9834-148196_82452-0ad4b83ec6bc0f9c5f28101539267054.jpg_p0_0.126571263346.jpg.input.npfile','f') print data.size -print data[0] +print data -data.reshape(1, 3, 416, 416) +data.reshape(1, 3, 224, 224) out_array = array('f') print'--------------------' print data.size @@ -27,12 +27,12 @@ print data[0] print '如果是nhwc --------' # rgb rgb rgb rgb rgb -print data[416 * 3 * 2 + 3 * 2 + 2] +print data[224 * 3 * 2 + 3 * 2 + 2] # print data[2] print '如果是nchw --------' # rgb rgb rgb rgb rgb -print data[416 * 416 * 2 + 416 * 2 + 2] +print data[224 * 224 * 2 + 224 * 2 + 2] # print data[2] # 明明是nchw @@ -42,6 +42,8 @@ for i in range(0, data.size): print len(out_array) -print out_array[416 * 416 * 2 + 416 * 2 + 2] +print out_array[224 * 224 * 2 + 224 * 2 + 2] + +# print out_array -tools.save_to_file('datas/in_put_1_3_416_416_2', out_array) +tools.save_to_file('datas/in_put_1_3_224_224_nchw', out_array) diff --git a/tools/python/modeltools/core/op_types.py b/tools/python/modeltools/core/op_types.py index 89f6ee47dd5e46ae952c574f246c09f859b8a5db..550f87339c9a048a3732daa7707dd6427965029a 100644 --- a/tools/python/modeltools/core/op_types.py +++ b/tools/python/modeltools/core/op_types.py @@ -77,6 +77,14 @@ fusion_conv_add_attrs_dict = { 'strides': 'stride', 'groups': 'group' } + +# fluid attr key --- mdl params key +pool2d_attrs_dict = { + 'global_pooling': 'global_pooling', + 'pooling_type': 'type' +} + + # fluid attr key --- mdl params key fluid_attrs_type_dict = { 'paddings': 0, diff --git a/tools/python/modeltools/mobilenet/converter_mobilenet.py b/tools/python/modeltools/mobilenet/converter_mobilenet.py index da95c24212f48e3ed03dcf0481c14f3ffa881986..ca1e1f7f4d83cf219e1e74603bb23a15c34cfb36 100644 --- a/tools/python/modeltools/mobilenet/converter_mobilenet.py +++ b/tools/python/modeltools/mobilenet/converter_mobilenet.py @@ -1,3 +1,4 @@ +# coding=utf-8 import json import os @@ -12,13 +13,25 @@ def load_mdl(mdl_json_path): return json.load(f) +def create_if_not_exit(target_dir): + if os.path.exists(target_dir): + shutil.rmtree(target_dir) + os.makedirs(target_dir, 0777) + + class Converter: 'convert mdlmodel to fluidmodel' def __init__(self, base_dir, mdl_json_path): + print 'base_dir: ' + base_dir self.mdl_json_path = base_dir + mdl_json_path self.base_dir = base_dir print mdl_json_path + self.source_weights_dir = self.base_dir + 'datas/sourcemodels/source_weights/' + self.target_weight_dir = self.base_dir + 'datas/target/target_weights/' + + create_if_not_exit(self.target_weight_dir) + self.mdl_json = load_mdl(self.mdl_json_path) self.program_desc = framework_pb2.ProgramDesc() self.weight_list_ = [] @@ -41,16 +54,18 @@ class Converter: print 'convert end.....' desc_serialize_to_string = self.program_desc.SerializeToString() - 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/') + outputmodel_dir = self.base_dir + 'datas/target/mobilenet_classfication/' + if os.path.exists(outputmodel_dir): + shutil.rmtree(outputmodel_dir) + os.makedirs(outputmodel_dir, 0777) - f = open(outputmodel_ + "__model__", "wb") + if os.path.exists(outputmodel_dir): + shutil.rmtree(outputmodel_dir) + # create_if_not_exit(outputmodel_dir) + + shutil.copytree(self.target_weight_dir, outputmodel_dir) + + f = open(outputmodel_dir + "__model__", "wb") f.write(desc_serialize_to_string) f.close() @@ -63,26 +78,30 @@ class Converter: layers_ = self.mdl_json['layer'] for layer in layers_: - desc_ops_add = block_desc.ops.add() - # print layer - # for i in layer: - # print i - if 'name' in layer: - l_name = layer['name'] - if 'type' in layer: - self.package_ops_type(desc_ops_add, layer) + if layer['type'] == 'SoftmaxLayer': + pass + else: + desc_ops_add = block_desc.ops.add() + + # print layer + # for i in layer: + # print i + if 'name' in layer: + l_name = layer['name'] + if 'type' in layer: + self.package_ops_type(desc_ops_add, layer) - if 'weight' in layer: - self.package_ops_weight2inputs(desc_ops_add, layer) + if 'weight' in layer: + self.package_ops_weight2inputs(desc_ops_add, layer) - if 'output' in layer: - self.package_ops_outputs(desc_ops_add, layer) + if 'output' in layer: + self.package_ops_outputs(desc_ops_add, layer) - if 'input' in layer: - self.package_ops_inputs(desc_ops_add, layer) + if 'input' in layer: + self.package_ops_inputs(desc_ops_add, layer) - self.package_ops_attrs(desc_ops_add, layer) + self.package_ops_attrs(desc_ops_add, layer) self.add_op_fetch(block_desc) @@ -105,7 +124,8 @@ class Converter: 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') + # todo pick last layer --> op output + inputs_add.arguments.append('fc7') desc_ops_add.type = 'fetch' outputs_add = desc_ops_add.outputs.add() outputs_add.parameter = 'Out' @@ -129,6 +149,128 @@ class Converter: # boolean attrs_add.type = 6 attrs_add.b = 0 + elif desc_ops_add.type == types.op_fluid_pooling: + Converter.pack_pooling_attr(desc_ops_add, layer) + pass + elif desc_ops_add.type == types.op_fluid_softmax: + pass + + @staticmethod + def pack_pooling_attr(desc_ops_add, layer): + print layer + l_params = layer['param'] + + 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 = 'paddings' + # ints + attrs_add.type = 3 + attrs_add.ints.append(0) + attrs_add.ints.append(0) + + attrs_add = desc_ops_add.attrs.add() + attrs_add.name = 'strides' + # ints + attrs_add.type = 3 + attrs_add.ints.append(1) + attrs_add.ints.append(1) + + attrs_add = desc_ops_add.attrs.add() + attrs_add.name = 'global_pooling' + # boolean + attrs_add.type = 6 + attrs_add.b = (l_params[types.pool2d_attrs_dict.get('global_pooling')]) + + attrs_add = desc_ops_add.attrs.add() + attrs_add.name = 'pooling_type' + # 2-->STRING + attrs_add.type = 2 + # 注意这里 avg but mdl is ave + attrs_add.s = l_params[types.pool2d_attrs_dict.get('pooling_type')] + + attrs_add = desc_ops_add.attrs.add() + attrs_add.name = 'ceil_mode' + # boolean + attrs_add.type = 6 + attrs_add.b = 1 + + attrs_add = desc_ops_add.attrs.add() + attrs_add.name = 'ksize' + # ints + attrs_add.type = 3 + attrs_add.ints.append(7) + attrs_add.ints.append(7) + + # type: "pool2d" + # attrs + # { + # name: "use_mkldnn" + # type: BOOLEAN + # b: false + # } + # attrs + # { + # name: "ceil_mode" + # type: BOOLEAN + # b: true + # } + # attrs + # { + # name: "use_cudnn" + # type: BOOLEAN + # b: true + # } + # attrs + # { + # name: "paddings" + # type: INTS + # ints: 0 + # ints: 0 + # } + # attrs + # { + # name: "strides" + # type: INTS + # ints: 1 + # ints: 1 + # } + # attrs + # { + # name: "global_pooling" + # type: BOOLEAN + # b: false + # } + # attrs + # { + # name: "data_format" + # type: STRING + # s: "AnyLayout" + # } + # attrs + # { + # name: "ksize" + # type: INTS + # ints: 7 + # ints: 7 + # } + # attrs + # { + # name: "pooling_type" + # type: STRING + # s: "avg" + # } + # is_target: false @staticmethod def pack_fusion_conv_add_attr(desc_ops_add, layer): @@ -181,6 +323,13 @@ class Converter: 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 = 'paddings' + # # ints + # attrs_add.type = 3 + # attrs_add.ints.append(0) + # attrs_add.ints.append(0) + attrs_add = desc_ops_add.attrs.add() attrs_add.name = 'strides' # ints @@ -188,6 +337,13 @@ class Converter: 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 = 'strides' + # # ints + # attrs_add.type = 3 + # attrs_add.ints.append(6) + # attrs_add.ints.append(6) + attrs_add = desc_ops_add.attrs.add() attrs_add.name = 'groups' # int @@ -232,8 +388,8 @@ class Converter: # print o outputs_add = desc_ops_add.outputs.add() dict = types.op_io_dict.get(desc_ops_add.type) - print 'desc_ops_add.type: ' + desc_ops_add.type - print dict + # print 'desc_ops_add.type: ' + desc_ops_add.type + # print dict outputs_add.parameter = dict.get(types.mdl_outputs_key) outputs_add.arguments.append(o) @@ -305,7 +461,7 @@ class Converter: # issues in mdl model filter swich n and c if j in self.deepwise_weight_list_ and len(dims_of_matrix) == 4: - print j + print "deep wise issue fit: " + j tensor.dims.append(dims_of_matrix[1]) tensor.dims.append(dims_of_matrix[0]) tensor.dims.append(dims_of_matrix[2]) @@ -320,6 +476,12 @@ class Converter: vars_add.persistable = 1 dims_size = len(dims_of_matrix) # print dims_size + # print 'weight name : ' + j + Swichter().copy_add_head( + self.source_weights_dir + j + '.bin', + self.target_weight_dir + j + ) + # if dims_size == 4: # # convert weight from nhwc to nchw # Swichter().nhwc2nchw_one_slice_add_head( @@ -341,7 +503,7 @@ class Converter: vars_add.persistable = 0 -mdl_path = "datas/sourcemodels/cls231_0802/mobileNetModel.json" +mdl_path = "datas/sourcemodels/source_profile/mobileNetModel.json" base_dir = "/Users/xiebaiyuan/PaddleProject/paddle-mobile/tools/python/modeltools/mobilenet/" converter = Converter(base_dir, mdl_path) converter.convert() diff --git a/tools/python/modeltools/mobilenet/swicher.py b/tools/python/modeltools/mobilenet/swicher.py index 04c10806029c562f429da583dcff7212b94cb162..90bc6d26f600624b14c5912cddfe6e156865d196 100644 --- a/tools/python/modeltools/mobilenet/swicher.py +++ b/tools/python/modeltools/mobilenet/swicher.py @@ -1,3 +1,5 @@ +import os +import shutil from array import array @@ -58,7 +60,7 @@ class Swichter: to_file = open(to_file_name, "wb") tmp = tmp_file.read() - head = self.read_head('yolo/datas/yolo/conv1_biases') + head = self.read_head('yolo/datas/yolo/head') to_file.write(head) to_file.write(tmp) tmp_file.close() @@ -72,12 +74,14 @@ class Swichter: # print read return read - def copy_add_head(self, from_file_name, to_file_name, tmp_file_name): + def copy_add_head(self, from_file_name, to_file_name): + from_file = open(from_file_name, "rb") to_file = open(to_file_name, "wb") # tmp_file = open(tmp_file_name, "wb") - head = self.read_head('yolo/datas/yolo/conv1_biases') + head = self.read_head( + '/Users/xiebaiyuan/PaddleProject/paddle-mobile/tools/python/modeltools/mobilenet/datas/sourcemodels/head/head') to_file.write(head) to_file.write(from_file.read()) from_file.close() @@ -96,7 +100,7 @@ class Swichter: to_file = open(to_file_name, "wb") # tmp_file = open(tmp_file_name, "wb") - head = self.read_head('yolo/datas/yolo/conv1_biases') + head = self.read_head('yolo/datas/yolo/head') to_file.write(head) to_file.write(read) from_file.close() @@ -110,6 +114,6 @@ class Swichter: # 32, # 3, 3, 3) -# Swichter().read_head('/Users/xiebaiyuan/PaddleProject/paddle-mobile/python/tools/modeltools/yolo/conv1_biases') +# Swichter().read_head('/Users/xiebaiyuan/PaddleProject/paddle-mobile/python/tools/modeltools/yolo/head') # Swichter().copy_add_head('datas/model.0.0.weight', 'datas/conv1_0', '') diff --git a/tools/python/modeltools/yolo/swicher.py b/tools/python/modeltools/yolo/swicher.py index 04c10806029c562f429da583dcff7212b94cb162..713ce93985957fe7f3c99d6bc6a9c436faea59a4 100644 --- a/tools/python/modeltools/yolo/swicher.py +++ b/tools/python/modeltools/yolo/swicher.py @@ -58,7 +58,7 @@ class Swichter: to_file = open(to_file_name, "wb") tmp = tmp_file.read() - head = self.read_head('yolo/datas/yolo/conv1_biases') + head = self.read_head('yolo/datas/yolo/head') to_file.write(head) to_file.write(tmp) tmp_file.close() @@ -77,7 +77,7 @@ class Swichter: to_file = open(to_file_name, "wb") # tmp_file = open(tmp_file_name, "wb") - head = self.read_head('yolo/datas/yolo/conv1_biases') + head = self.read_head('yolo/datas/yolo/head') to_file.write(head) to_file.write(from_file.read()) from_file.close() @@ -96,7 +96,7 @@ class Swichter: to_file = open(to_file_name, "wb") # tmp_file = open(tmp_file_name, "wb") - head = self.read_head('yolo/datas/yolo/conv1_biases') + head = self.read_head('yolo/datas/yolo/head') to_file.write(head) to_file.write(read) from_file.close() @@ -110,6 +110,6 @@ class Swichter: # 32, # 3, 3, 3) -# Swichter().read_head('/Users/xiebaiyuan/PaddleProject/paddle-mobile/python/tools/modeltools/yolo/conv1_biases') +# Swichter().read_head('/Users/xiebaiyuan/PaddleProject/paddle-mobile/python/tools/modeltools/yolo/head') # Swichter().copy_add_head('datas/model.0.0.weight', 'datas/conv1_0', '')