diff --git a/README.md b/README.md index d7192697de6eacc5e22ac704adf55fe38f48f60f..228334c500f5a9ec6eab87dc3306cfa5a5c444d6 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ paddlepaddle >= 1.5.0 **以下依赖只需对应安装自己需要的即可** 转换tensorflow模型 : tensorflow == 1.14.0 转换caffe模型 : caffe == 1.0.0 - +转换onnx模型 : onnx == 1.5.0 pytorch == 1.1.0 ## 安装 ``` pip install x2paddle @@ -32,8 +32,9 @@ x2paddle --framework=tensorflow --model=tf_model.pb --save_dir=pd_model x2paddle --framework=caffe --prototxt=deploy.proto --weight=deploy.caffemodel --save_dir=pd_model ``` ### ONNX -即将release,目前仍可使用[onnx2fluid](https://github.com/PaddlePaddle/X2Paddle/tree/release-0.3/onnx2fluid) - +``` +x2paddle --framework=onnx --model=onnx_model.onnx --save_dir=pd_model +``` ### 参数选项 | 参数 | | |----------|--------------| diff --git a/x2paddle/op_mapper/onnx_directly_map.py b/x2paddle/op_mapper/onnx_directly_map.py index ab0551545baff19beadf553addf3a3ce4757f6da..75c2a842f8074761b6a1a4cbacd3248fb8254526 100644 --- a/x2paddle/op_mapper/onnx_directly_map.py +++ b/x2paddle/op_mapper/onnx_directly_map.py @@ -13,6 +13,7 @@ # limitations under the License. from collections import OrderedDict as _dict +import numpy as _np default_op_mapping_field_values = _dict() default_op_mapping_field_values['FLUID_OP'] = '' @@ -30,6 +31,21 @@ default_op_mapping = { 'Mul': ['elementwise_mul', ['X', 'Y'], ['Out'], dict(), dict(axis=-1)], + 'Clip': [ + 'clip', ['X'], ['Out'], + dict(), + dict( + min=(_np.asarray([255, 255, 127, 255], + dtype=_np.uint8).view(_np.float32)), + max=(_np.asarray([255, 255, 127, 127], + dtype=_np.uint8).view(_np.float32)), + ) + ], + 'ReduceMean': [ + 'reduce_mean', ['X'], ['Out'], + dict(axes='dim', keepdims='keep_dim'), + dict(keep_dim=1) + ] } default_ioa_constraint = { diff --git a/x2paddle/op_mapper/onnx_op_mapper.py b/x2paddle/op_mapper/onnx_op_mapper.py index 2fc4ff15c3dee0e87c48de53a4f1f2529494d360..e15ec2191b28d9fce7b3e65965ae16a3561de73d 100644 --- a/x2paddle/op_mapper/onnx_op_mapper.py +++ b/x2paddle/op_mapper/onnx_op_mapper.py @@ -62,7 +62,7 @@ class ONNXOpMapper(OpMapper): func = getattr(self, op) func(node) elif op in default_op_mapping: - self._default(node) + self.directly_map(node) def op_checker(self): unsupported_ops = set() @@ -80,7 +80,7 @@ class ONNXOpMapper(OpMapper): print(op) return False - def _default(self, node, *args, name='', **kwargs): + def directly_map(self, node, *args, name='', **kwargs): inputs = node.layer.input outputs = node.layer.output op_type = node.layer_type @@ -544,7 +544,7 @@ class ONNXOpMapper(OpMapper): "momentum": momentum, "epsilon": epsilon, "data_layout": string('NCHW'), - "is_test": 'True', + "is_test": True, "param_attr": string(val_scale.layer_name), "bias_attr": string(val_b.layer_name), "moving_mean_name": string(val_mean.layer_name), diff --git a/x2paddle_model_zoo.md b/x2paddle_model_zoo.md index 80d12d9fdc91ead399bc6c9da26d8be5929becfd..6aaf7499762dca1d44f1050b0b07767de7ca85d9 100644 --- a/x2paddle_model_zoo.md +++ b/x2paddle_model_zoo.md @@ -47,8 +47,12 @@ ``` import torch import torchvision -dummy_input = torch.randn(1, 3, 224, 224) #根据不同模型调整shape + +#根据不同模型调整输入的shape +dummy_input = torch.randn(1, 3, 224, 224) resnet18 = torchvision.models.resnet18(pretrained=True) -torch.onnx.export(resnet18, dummy_input, "resnet18.onnx",verbose=True)#"resnet18.onnx"为onnx model的存储路径 + +#"resnet18.onnx"为onnx model的存储路径 +torch.onnx.export(resnet18, dummy_input, "resnet18.onnx",verbose=True) ```