Skip to content

Commit

Permalink
[Other] Integrate paddle2coreml tool into FastDeploy (PaddlePaddle#1460)
Browse files Browse the repository at this point in the history
Integration of paddle2coreml tool
  • Loading branch information
lishicheng1996 authored Feb 28, 2023
1 parent fe9aff1 commit b324f36
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
27 changes: 27 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FastDeploy提供了一系列高效易用的工具优化部署体验, 提升推

- [1.自动压缩工具包](#1)
- [2.模型转换工具包](#2)
- [3.paddle2coreml工具包](#3)

<p id="1"></p>

Expand Down Expand Up @@ -72,3 +73,29 @@ fastdeploy convert --framework onnx --model yolov5s.onnx --save_dir pd_model
```

更多详细内容可参考[X2Paddle](https://github.com/PaddlePaddle/X2Paddle)

## paddle2coreml工具

FastDeploy 基于 paddle2coreml 为用户提供了模型转换的工具, 用户可以轻松地通过一行命令将飞桨模型快速迁移至苹果电脑和手机端。

### 环境准备

1. PaddlePaddle 安装,可参考如下文档快速安装
```
https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/linux-pip.html
```
2. paddle2coreml 安装

可通过pip方式安装paddle2coreml:
```shell
pip install paddle2coreml
```
3. 使用方式

按照以上步骤成功安装后,即可使用 FastDeploy paddle2coreml 一键转换工具, 示例如下:

```bash
fastdeploy paddle2coreml --p2c_paddle_model_dir path/to/paddle_model --p2c_coreml_model_dir path/to/coreml_model --p2c_input_names "input1 input2" --p2c_input_shapes "1,3,224,224 1,4,64,64" --p2c_input_dtypes "float32 int32" --p2c_output_names "output1 output2"
```
注意,--p2c_input_names 与 --p2c_output_names 两个参数须与paddle模型的输入输出名字一致。

24 changes: 24 additions & 0 deletions tools/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,27 @@ fastdeploy convert --framework onnx --model yolov5s.onnx --save_dir pd_model
```

For more details, please refer to[X2Paddle](https://github.com/PaddlePaddle/X2Paddle)

## paddle2coreml tool

FastDeploy provides users with a model conversion tool based on paddle2coreml, which allows users to easily migrate PaddlePaddle models to Apple computers and mobile devices with a single command.

### Environment Preparation

1. PaddlePaddle installation, please refer to the following document for quick installation:
```
https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/linux-pip.html
```
2. paddle2coreml installation

paddle2coreml can be installed using pip:
```shell
pip install paddle2coreml
```
3. Usage

After successfully installing as described above, you can use the FastDeploy paddle2coreml one-click conversion tool, as shown below:

```bash
fastdeploy paddle2coreml --p2c_paddle_model_dir path/to/paddle_model --p2c_coreml_model_dir path/to/coreml_model --p2c_input_names "input1 input2" --p2c_input_shapes "1,3,224,224 1,4,64,64" --p2c_input_dtypes "float32 int32" --p2c_output_names "output1 output2"
```
90 changes: 89 additions & 1 deletion tools/common_tools/common_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def argsparser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'tools', choices=['compress', 'convert', 'simple_serving'])
'tools', choices=['compress', 'convert', 'simple_serving', 'paddle2coreml'])
## argumentments for auto compression
parser.add_argument(
'--config_path',
Expand Down Expand Up @@ -84,6 +84,49 @@ def argsparser():
help="Simple serving host IP address")
parser.add_argument(
"--port", type=int, default=8000, help="Simple serving host port")
## arguments for paddle2coreml
parser.add_argument(
"--p2c_paddle_model_dir",
type=str,
default=None,
required=True,
help="define paddle model path")
parser.add_argument(
"--p2c_coreml_model_dir",
type=str,
default=None,
required=True,
help="define generated coreml model path")
parser.add_argument(
"--p2c_coreml_model_name",
type=str,
default="coreml_model",
required=False,
help="define generated coreml model name")
parser.add_argument(
"--p2c_input_names",
type=str,
default=None,
required=True,
help="define input names")
parser.add_argument(
"--p2c_input_dtypes",
type=str,
default="float32",
required=True,
help="define input dtypes")
parser.add_argument(
"--p2c_input_shapes",
type=str,
default=None,
required=True,
help="define input shapes")
parser.add_argument(
"--p2c_output_names",
type=str,
default=None,
required=True,
help="define output names")
## arguments for other tools
return parser

Expand Down Expand Up @@ -170,6 +213,51 @@ def main():
port=args.port,
app_dir='.',
log_config=custom_logging_config)
if args.tools == "paddle2coreml":
import coremltools as ct
import os
import numpy as np
def type_to_np_dtype(dtype):
if dtype == 'float32':
return np.float32
elif dtype == 'float64':
return np.float64
elif dtype == 'int32':
return np.int32
elif dtype == 'int64':
return np.int64
elif dtype == 'uint8':
return np.uint8
elif dtype == 'uint16':
return np.uint16
elif dtype == 'uint32':
return np.uint32
elif dtype == 'uint64':
return np.uint64
elif dtype == 'int8':
return np.int8
elif dtype == 'int16':
return np.int16
else:
raise Exception("Unsupported dtype: {}".format(dtype))
input_names = args.p2c_input_names.split(' ')
input_shapes = [[int(i) for i in shape.split(',')] for shape in args.p2c_input_shapes.split(' ')]
input_dtypes = map(type_to_np_dtype, args.p2c_input_dtypes.split(' '))
output_names = args.p2c_output_names.split(' ')
sample_input = [ct.TensorType(
name=k,
shape=s,
dtype=d,
) for k, s, d in zip(input_names, input_shapes, input_dtypes)]

coreml_model = ct.convert(
args.p2c_paddle_model_dir,
convert_to="mlprogram",
minimum_deployment_target=ct.target.macOS13,
inputs=sample_input,
outputs=[ct.TensorType(name=name) for name in output_names],
)
coreml_model.save(os.path.join(args.p2c_coreml_model_dir, args.p2c_coreml_model_name))


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion tools/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name="fastdeploy-tools", # name of package
version="0.0.3", #version of package
version="0.0.4", #version of package
description="A toolkit for FastDeploy.",
long_description=long_description,
long_description_content_type="text/plain",
Expand Down

0 comments on commit b324f36

Please sign in to comment.