From f406100091188a38e312f957989b638d2ca92d0a Mon Sep 17 00:00:00 2001 From: Riccardo <36033044+gasgallo@users.noreply.github.com> Date: Tue, 21 Apr 2020 08:40:31 +0700 Subject: [PATCH] Add support for ReLU6 op in CAFFE (#621) Add support for Clip op in CAFFE Co-authored-by: riccardo --- third_party/caffe/Dockerfile | 2 ++ third_party/caffe/caffe.proto | 9 ++++++++- tools/python/transform/caffe_converter.py | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/third_party/caffe/Dockerfile b/third_party/caffe/Dockerfile index 92f67f23..d3df2cf6 100644 --- a/third_party/caffe/Dockerfile +++ b/third_party/caffe/Dockerfile @@ -30,6 +30,8 @@ WORKDIR $CAFFE_ROOT # FIXME: use ARG instead of ENV once DockerHub supports this # https://github.com/docker/hub-feedback/issues/460 ENV CLONE_TAG=1.0 +# To enable Clip operation in CAFFE during validation, it's needed to set CLONE_TAG=master +# ENV CLONE_TAG=master # ImportError in system pip wrappers after an upgrade. # https://github.com/pypa/pip/issues/5599 diff --git a/third_party/caffe/caffe.proto b/third_party/caffe/caffe.proto index 8be551bd..baf38801 100644 --- a/third_party/caffe/caffe.proto +++ b/third_party/caffe/caffe.proto @@ -437,7 +437,7 @@ message ParamSpec { // NOTE // Update the next available ID when you add a new LayerParameter field. // -// LayerParameter next available layer-specific ID: 147 (last added: recurrent_param) +// LayerParameter next available layer-specific ID: 149 (last added: clip_param) message LayerParameter { optional string name = 1; // the layer name optional string type = 2; // the layer type @@ -494,6 +494,7 @@ message LayerParameter { optional ArgMaxParameter argmax_param = 103; optional BatchNormParameter batch_norm_param = 139; optional BiasParameter bias_param = 141; + optional ClipParameter clip_param = 148; optional ConcatParameter concat_param = 104; optional ContrastiveLossParameter contrastive_loss_param = 105; optional ConvolutionParameter convolution_param = 106; @@ -778,6 +779,12 @@ message ArgMaxParameter { optional int32 axis = 3; } +// Message that stores parameters used by ClipLayer +message ClipParameter { + required float min = 1; + required float max = 2; +} + message ConcatParameter { // The axis along which to concatenate -- may be negative to index from the // end (e.g., -1 for the last axis). Other axes must have the diff --git a/tools/python/transform/caffe_converter.py b/tools/python/transform/caffe_converter.py index 3eb6e429..a2482a90 100644 --- a/tools/python/transform/caffe_converter.py +++ b/tools/python/transform/caffe_converter.py @@ -164,6 +164,7 @@ class CaffeConverter(base_converter.ConverterInterface): 'PReLU': ActivationType.PRELU, 'TanH': ActivationType.TANH, 'Sigmoid': ActivationType.SIGMOID, + 'Clip': ActivationType.RELUX, } def __init__(self, option, src_model_file, src_weight_file): @@ -177,6 +178,7 @@ class CaffeConverter(base_converter.ConverterInterface): 'TanH': self.convert_activation, 'Sigmoid': self.convert_activation, 'PReLU': self.convert_activation, + 'Clip': self.convert_activation, 'Pooling': self.convert_pooling, 'Concat': self.convert_concat, 'Slice': self.convert_slice, @@ -506,6 +508,13 @@ class CaffeConverter(base_converter.ConverterInterface): type_arg.s = six.b(ActivationType.LEAKYRELU.name) + if caffe_op.type == 'Clip': + mace_check(caffe_op.layer.clip_param.min == 0, + "Mace only supports min == 0 Clip op") + limit_arg = op.arg.add() + limit_arg.name = MaceKeyword.mace_activation_max_limit_str + limit_arg.f = caffe_op.layer.clip_param.max + def convert_folded_batchnorm(self, caffe_op): op = self.convert_general_op(caffe_op) op.type = MaceOp.BatchNorm.name -- GitLab