-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PaddlePaddle конвертер #561
Changes from 1 commit
0b9f1f4
33770be
cd95f2a
13c9a95
36b1d33
49d269a
9cc12ab
7de463f
1da33b0
b76f80c
7a4b88a
bfa7839
34494c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Conversion to PaddlePaddle | ||
|
||
PaddlePaddle converter supports conversion to Paddle format from Pytorch and ONNX formats. | ||
|
||
## PaddlePaddle converter usage | ||
|
||
Usage of the script: | ||
|
||
```sh | ||
python converter.py -m <path/to/input/model> -f <source_framework> -p <Pytorch/module/name> -d <output_directory> | ||
``` | ||
|
||
This will convert model from `<source_framework>` to Paddle format. | ||
|
||
### Paddle converter parameters | ||
|
||
- `--model_path` Path to an .onnx or .pth file with the original model. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a path |
||
- `--framework` is a source framework for convertion to PaddlePaddle format. | ||
- `--pytorch_module_name` Module name for PyTorch model (necessary if source framework is Pytorch). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a module name |
||
- `--save_dir` Directory for converted model to be saved to. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a directory |
||
|
||
### Examples of usage | ||
|
||
```sh | ||
python converter.py -m .\public\googlenet-v3-pytorch\inception_v3_google-1a9a5a14.pth -f pytorch -p InceptionV3 -d pd | ||
``` | ||
|
||
```sh | ||
python converter.py -m .\public\ctdet_coco_dlav0_512\ctdet_coco_dlav0_512.onnx -f onnx -d pd | ||
``` | ||
|
||
# Conversion from PaddlePaddle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Директория называется x2paddle, но откуда-то возник обратный конвертер?! Надо либо вынести, либо переименовать директорию. |
||
|
||
paddle2onnx converter supports conversion to ONNX format from Paddle format. | ||
|
||
## PaddlePaddle converter usage | ||
|
||
Usage of the script: | ||
|
||
```sh | ||
python paddle2onnx.py -d .\pd_pth\inference_model -f model.pdmodel -p model.pdiparams -m inference.onnx -o 11 | ||
``` | ||
|
||
This will convert model from Paddle to ONNX format. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This script will... |
||
|
||
### Converter parameters | ||
|
||
- `--model_dir` Path to the directory with the original model. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a path |
||
- `--model_filename` Name of the model file name. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a name |
||
- `--params_filename` Name of the parameters file name. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a name |
||
- `--model_path` Path to the resulting .onnx file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a path |
||
- `--opset_version` Desired opset version of the resulting ONNX model. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is a desired... |
||
|
||
### Examples of usage | ||
|
||
```sh | ||
python paddle2onnx.py -d .\pd_pth\inference_model -f model.pdmodel -p model.pdiparams -m inference.onnx -o 11 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import torch | ||
import numpy as np | ||
from torchvision.models import * | ||
from x2paddle.convert import pytorch2paddle | ||
import argparse | ||
import logging as log | ||
import sys | ||
from pathlib import Path | ||
import os | ||
|
||
sys.path.append(str(Path(__file__).parent.parent.parent.parent)) | ||
|
||
|
||
def cli_argument_parser(): | ||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument('-m', '--model_path', | ||
help='Path to an .onnx or .pth file.', | ||
required=True, | ||
type=str, | ||
dest='model_path') | ||
parser.add_argument('-f', '--framework', | ||
help='Original model framework (ONNX or Pytorch)', | ||
required=True, | ||
type=str, | ||
choices=['onnx', 'pytorch'], | ||
dest='framework') | ||
parser.add_argument('-p', '--pytorch_module_name', | ||
help='Module name for PyTorch model.', | ||
required=False, | ||
type=str, | ||
choices=['AlexNet', 'VGG', 'ResNet', 'SqueezeNet', 'DenseNet', 'InceptionV3', 'GoogLeNet', | ||
'ShuffleNetV2', 'MobileNetV2', 'MobileNetV3', 'MNASNet'], | ||
dest='module_name') | ||
parser.add_argument('-d', '--save_dir', | ||
help='Directory for converted model to be saved to.', | ||
required=True, | ||
type=str, | ||
dest='save_dir') | ||
args = parser.parse_args() | ||
|
||
return args | ||
|
||
def convert_pytorch_to_paddle(model_path: str, module_name, save_dir: str): | ||
|
||
modules = {'AlexNet': AlexNet, | ||
'VGG': VGG, | ||
'ResNet': ResNet, | ||
'SqueezeNet': SqueezeNet, | ||
'DenseNet': DenseNet, | ||
'InceptionV3': Inception3, | ||
'GoogLeNet': GoogLeNet, | ||
'ShuffleNetV2': ShuffleNetV2, | ||
'MobileNetV2': MobileNetV2, | ||
'MobileNetV3': MobileNetV3, | ||
'MNASNet': MNASNet} | ||
|
||
torch_module = modules[module_name]() | ||
torch_module.load_state_dict(torch.load(model_path)) | ||
torch_module.eval() | ||
|
||
input_data = np.random.rand(1, 3, 224, 224).astype("float32") | ||
pytorch2paddle(torch_module, | ||
save_dir=save_dir, | ||
jit_type="trace", | ||
input_examples=[torch.tensor(input_data)]) | ||
|
||
def convert_onnx_to_paddle(model_path: str, save_dir: str): | ||
print(f'x2paddle --framework=onnx --model={model_path} --save_dir={save_dir}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нигде в ридми нет информации, что требуется какой-то внешний инструмент. Надо где-то это написать. |
||
os.system(f'x2paddle --framework=onnx --model={model_path} --save_dir={save_dir}') | ||
|
||
|
||
def main(): | ||
log.basicConfig(format='[ %(levelname)s ] %(message)s', | ||
level=log.INFO, stream=sys.stdout) | ||
args = cli_argument_parser() | ||
if args.framework == 'pytorch' and not args.module_name: | ||
raise ValueError('Module name for pytorch is not specified') | ||
elif args.framework == 'pytorch' and args.module_name: | ||
convert_pytorch_to_paddle(model_path=args.model_path, | ||
module_name=args.module_name, save_dir=args.save_dir) | ||
elif args.framework == 'onnx': | ||
convert_onnx_to_paddle(model_path=args.model_path, save_dir=args.save_dir) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import argparse | ||
import logging as log | ||
import sys | ||
from pathlib import Path | ||
import os | ||
|
||
sys.path.append(str(Path(__file__).parent.parent.parent.parent)) | ||
|
||
|
||
def cli_argument_parser(): | ||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument('-d', '--model_dir', | ||
help='Directory to save model in.', | ||
required=True, | ||
type=str, | ||
dest='model_dir') | ||
parser.add_argument('-f', '--model_filename', | ||
help='Name of the model file name.', | ||
required=True, | ||
type=str, | ||
dest='model_filename') | ||
parser.add_argument('-p', '--params_filename', | ||
help='Name of the parameters file name.', | ||
required=True, | ||
type=str, | ||
dest='params_filename') | ||
parser.add_argument('-m', '--model_path', | ||
help='Path to an .onnx file.', | ||
required=True, | ||
type=str, | ||
dest='model_path') | ||
parser.add_argument('-o', '--opset_version', | ||
help='', | ||
required=True, | ||
type=str, | ||
dest='opset_version') | ||
args = parser.parse_args() | ||
|
||
return args | ||
|
||
|
||
def convert_paddle_to_onnx(model_dir: str, model_filename: str, params_filename: str, | ||
model_path: str, opset_version: str): | ||
os.system(f'paddle2onnx --model_dir {model_dir} \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Аналогично, нигде в ридми нет информации, что требуется какой-то внешний инструмент. Надо где-то это написать. |
||
--model_filename {model_filename} \ | ||
--params_filename {params_filename} \ | ||
--save_file {model_path} \ | ||
--opset_version {opset_version} \ | ||
--enable_onnx_checker True') | ||
|
||
|
||
def main(): | ||
log.basicConfig(format='[ %(levelname)s ] %(message)s', | ||
level=log.INFO, stream=sys.stdout) | ||
args = cli_argument_parser() | ||
convert_paddle_to_onnx(model_dir=args.model_dir, model_filename=args.model_filename, | ||
params_filename=args.params_filename, model_path=args.model_path, | ||
opset_version=args.opset_version) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pytorch? Либо все мелкими буквами (предпочтительно) или в соответствии с названием библиотеки.