diff --git a/fluid/image_classification/caffe2fluid/examples/imagenet/compare.py b/fluid/image_classification/caffe2fluid/examples/imagenet/compare.py index 05fbd6b85c2d70124817e7c5a2d5a90e78ba7847..45b1f5303ce77de7c7f5e3a232517c26e159b2fa 100644 --- a/fluid/image_classification/caffe2fluid/examples/imagenet/compare.py +++ b/fluid/image_classification/caffe2fluid/examples/imagenet/compare.py @@ -24,15 +24,10 @@ def calc_diff(f1, f2): #print d2.shape #print d1[0, 0, 0:10, 0:10] #print d2[0, 0, 0:10, 0:10] - #d1 = d1[:, :, 1:-2, 1:-2] - #d2 = d2[:, :, 1:-2, 1:-2] d1 = d1.flatten() d2 = d2.flatten() - #print d1[:10] - #print d2[:10] - d1_num = reduce(lambda x, y: x * y, d1.shape) d2_num = reduce(lambda x, y: x * y, d2.shape) if d1_num != d2_num: @@ -41,7 +36,11 @@ def calc_diff(f1, f2): assert (d1_num == d2_num), "their shape is not consistent" try: + mask = np.abs(d1) >= np.abs(d2) + mask = mask.astype('int32') + df = np.abs(d1 - d2) + df = df / (1.0e-10 + np.abs(d1) * mask + np.abs(d2) * (1 - mask)) max_df = np.max(df) sq_df = np.mean(df * df) return max_df, sq_df diff --git a/fluid/image_classification/caffe2fluid/kaffe/layers.py b/fluid/image_classification/caffe2fluid/kaffe/layers.py index 678bc1c70a68dc9e5d991d0b26e67fac66e4d13a..98ef6b65329dd7ba314efdd638f72313d796e39f 100644 --- a/fluid/image_classification/caffe2fluid/kaffe/layers.py +++ b/fluid/image_classification/caffe2fluid/kaffe/layers.py @@ -39,6 +39,7 @@ LAYER_DESCRIPTORS = { 'Pooling': shape_pool, 'Power': shape_identity, 'ReLU': shape_identity, + 'PReLU': shape_identity, 'Scale': shape_identity, 'Sigmoid': shape_identity, 'SigmoidCrossEntropyLoss': shape_scalar, diff --git a/fluid/image_classification/caffe2fluid/kaffe/paddle/network.py b/fluid/image_classification/caffe2fluid/kaffe/paddle/network.py index 51d43c0b32507091f0f113d7cce423cb13a5b76c..1fc98b057dbf16228c834674f5aee8c4bd123935 100644 --- a/fluid/image_classification/caffe2fluid/kaffe/paddle/network.py +++ b/fluid/image_classification/caffe2fluid/kaffe/paddle/network.py @@ -240,10 +240,16 @@ class Network(object): @layer def relu(self, input, name): fluid = import_fluid() - output = fluid.layers.relu( - name=self.get_unique_output_name(name, 'relu'), x=input) + output = fluid.layers.relu(input) return output + @layer + def prelu(self, input, channel_shared, name): + #fluid = import_fluid() + #output = fluid.layers.relu(input) + #return output + raise NotImplementedError('prelu not implemented') + def pool(self, pool_type, input, k_h, k_w, s_h, s_w, ceil_mode, padding, name): # Get the number of channels in the input @@ -382,7 +388,8 @@ class Network(object): name, scale_offset=True, eps=1e-5, - relu=False): + relu=False, + relu_negative_slope=0.0): # NOTE: Currently, only inference is supported fluid = import_fluid() prefix = name + '_' @@ -392,6 +399,15 @@ class Network(object): name=prefix + 'offset') mean_name = prefix + 'mean' variance_name = prefix + 'variance' + + leaky_relu = False + act = 'relu' + if relu is False: + act = None + elif relu_negative_slope != 0.0: + leaky_relu = True + act = None + output = fluid.layers.batch_norm( name=self.get_unique_output_name(name, 'batch_norm'), input=input, @@ -401,7 +417,10 @@ class Network(object): moving_mean_name=mean_name, moving_variance_name=variance_name, epsilon=eps, - act='relu' if relu is True else None) + act=act) + + if leaky_relu: + output = fluid.layers.leaky_relu(output, alpha=relu_negative_slope) return output diff --git a/fluid/image_classification/caffe2fluid/kaffe/paddle/transformer.py b/fluid/image_classification/caffe2fluid/kaffe/paddle/transformer.py index 2f272a732a575c1668515007a6929592de938809..76a318d68de2932c83d158f38a5619043c55f0a8 100644 --- a/fluid/image_classification/caffe2fluid/kaffe/paddle/transformer.py +++ b/fluid/image_classification/caffe2fluid/kaffe/paddle/transformer.py @@ -112,6 +112,13 @@ class PaddleMapper(NodeMapper): def map_relu(self, node): return PaddleNode('relu') + def map_prelu(self, node): + channel_shared = getattr(node.parameters, 'channel_shared', False) + return PaddleNode('prelu', channel_shared) + + def map_tanh(self, node): + return PaddleNode('tanh') + def map_pooling(self, node): pool_type = node.parameters.pool if pool_type == 0: