From 9a8cd2114a8fcd049baf8cb8b2f71845119aafc6 Mon Sep 17 00:00:00 2001 From: SunAhong1993 Date: Tue, 18 Jun 2019 21:03:22 +0800 Subject: [PATCH] fix the bug of load numpy and fix diff.sh, reshape.py --- caffe2fluid/examples/imagenet/infer.py | 2 +- caffe2fluid/examples/imagenet/tools/diff.sh | 2 +- caffe2fluid/kaffe/custom_layers/reshape.py | 45 +++------------------ caffe2fluid/kaffe/paddle/network.py | 4 +- caffe2fluid/kaffe/shapes.py | 3 ++ 5 files changed, 12 insertions(+), 44 deletions(-) diff --git a/caffe2fluid/examples/imagenet/infer.py b/caffe2fluid/examples/imagenet/infer.py index 3962d96..6fca82b 100644 --- a/caffe2fluid/examples/imagenet/infer.py +++ b/caffe2fluid/examples/imagenet/infer.py @@ -172,7 +172,7 @@ def load_model(exe, place, net_file, net_name, net_weight, debug): def get_shape(fluid, program, name): for var in program.list_vars(): - if var.name == 'data': + if var.type == 'Input': return list(var.shape[1:]) raise ValueError('not found shape for input layer[%s], ' diff --git a/caffe2fluid/examples/imagenet/tools/diff.sh b/caffe2fluid/examples/imagenet/tools/diff.sh index c1756e8..8a561a8 100755 --- a/caffe2fluid/examples/imagenet/tools/diff.sh +++ b/caffe2fluid/examples/imagenet/tools/diff.sh @@ -49,7 +49,7 @@ if [[ -z $PYTHON ]];then PYTHON=`which python` fi $PYTHON ../../convert.py \ - $proto_file \ + --npy_path $proto_file \ --caffemodel $caffemodel_file \ --data-output-path $weight_file\ --code-output-path $net_file diff --git a/caffe2fluid/kaffe/custom_layers/reshape.py b/caffe2fluid/kaffe/custom_layers/reshape.py index da82e4d..b4ec051 100644 --- a/caffe2fluid/kaffe/custom_layers/reshape.py +++ b/caffe2fluid/kaffe/custom_layers/reshape.py @@ -2,6 +2,7 @@ more info can be found here: http://caffe.berkeleyvision.org/tutorial/layers/reshape.html """ from .register import register +from functools import reduce def import_fluid(): @@ -61,43 +62,6 @@ def reshape_shape(input_sp, shape, axis=0, num_axes=-1): assert len(output_shape) == num_axes_retained + num_new_axes,\ "[Reshape]invalid dims of output shape[%s]" % (str(output_shape)) - inferred_axis = -1 - copy_axes = [] - constant_count = 1 - for i in range(num_new_axes): - top_dim = shape['dim'][i] - if top_dim == 0: - copy_axes.append(i) - copy_axis_index = start_axis + i - output_shape[copy_axis_index] = input_shape[copy_axis_index] - elif top_dim == -1: - assert inferred_axis == -1, "[Reshape]new shape contains multiple -1 dims" - inferred_axis = i - else: - constant_count *= top_dim - - if inferred_axis >= 0: - explicit_count = constant_count - l = input_shape[0:start_axis] - if len(l) > 0: - explicit_count *= count(l) - - l = input_shape[end_axis:] - if len(l) > 0: - explicit_count *= count(l) - - for i in range(len(copy_axes)): - explicit_count *= output_shape[start_axis + copy_axes[i]] - - assert input_count % explicit_count == 0, "[Reshape]botom count[%d] "\ - "must be divisible by product of the specified dimensions[%d] "\ - % (input_count, explicit_count) - output_shape[start_axis + inferred_axis] = input_count / explicit_count - - output_count = count(output_shape) - assert output_count == input_count, "[Reshape]output count[%d] must match input count[%d]" % ( - output_count, input_count) - return output_shape @@ -117,17 +81,18 @@ def reshape_layer(input, name, shape, axis=0, num_axes=-1): fluid = import_fluid() input_shape = list(input.shape) + print(input_shape) + print(shape) if input_shape[0] == -1: - input_shape[0] = 1 + input_shape[0] = 0 output_shape = reshape_shape(input_shape, shape, axis, num_axes) - output_shape[0] = -1 else: output_shape = reshape_shape(input_shape, shape, axis, num_axes) - output = fluid.layers.reshape(input, shape=output_shape, name=name) return output register(kind='Reshape', shape=reshape_shape, layer=reshape_layer) + diff --git a/caffe2fluid/kaffe/paddle/network.py b/caffe2fluid/kaffe/paddle/network.py index 87d1c27..88eba56 100644 --- a/caffe2fluid/kaffe/paddle/network.py +++ b/caffe2fluid/kaffe/paddle/network.py @@ -103,7 +103,7 @@ class Network(object): place = self.paddle_env['place'] exe = self.paddle_env['exe'] - data_dict = np.load(data_path).item() + data_dict = np.load(data_path, allow_pickle=True).item() for op_name in data_dict: if op_name == 'caffe2fluid_name_trace': self.name_trace = data_dict[op_name] @@ -441,7 +441,7 @@ class Network(object): need_transpose = True if need_transpose: - in_order = range(dims) + in_order = list(range(dims)) in_order.remove(axis) in_order.append(axis) input = fluid.layers.transpose( diff --git a/caffe2fluid/kaffe/shapes.py b/caffe2fluid/kaffe/shapes.py index 4bbdbde..e3e4e71 100644 --- a/caffe2fluid/kaffe/shapes.py +++ b/caffe2fluid/kaffe/shapes.py @@ -3,6 +3,9 @@ from collections import namedtuple from .errors import KaffeError +Tensor5DShape = namedtuple('Tensor5DShape', + ['batch_size', 'data1', 'daat2', 'data3', 'data4']) + Tensor4DShape = namedtuple('Tensor4DShape', ['batch_size', 'channels', 'height', 'width']) -- GitLab