mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-06 00:57:33 +08:00
[Other] Integrate paddle2coreml tool into FastDeploy (#1460)
Integration of paddle2coreml tool
This commit is contained in:
@@ -6,7 +6,7 @@ import uvicorn
|
||||
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',
|
||||
@@ -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
|
||||
|
||||
@@ -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__':
|
||||
|
Reference in New Issue
Block a user