未验证 提交 2228423e 编写于 作者: J Jason 提交者: GitHub

Merge pull request #16 from MacroBull/master

Rename onnx2paddle to onnx2fluid
...@@ -57,3 +57,4 @@ coverage.xml ...@@ -57,3 +57,4 @@ coverage.xml
/examples/*.aria2 /examples/*.aria2
/examples/*.onnx /examples/*.onnx
/examples/*.np? /examples/*.np?
**/.*
Onnx2paddle Onnx2Fluid
=== ===
Inference model conversion from ONNX/PyTorch to Paddle Inference model conversion from ONNX/PyTorch to Paddle fluid
快速开始 快速开始
--- ---
......
...@@ -22,4 +22,4 @@ output_data = data['outputs'] ...@@ -22,4 +22,4 @@ output_data = data['outputs']
inputs = Dict(zip(input_names, [input_data])) inputs = Dict(zip(input_names, [input_data]))
outputs = Dict(zip(output_name, [output_data])) outputs = Dict(zip(output_name, [output_data]))
np.savez(fn, inputs=inputs, outputs=outputs) # overwrite np.savez(fn, inputs=inputs, outputs=outputs) # overwrite
...@@ -6,7 +6,7 @@ Created on Fri Mar 22 11:19:45 2019 ...@@ -6,7 +6,7 @@ Created on Fri Mar 22 11:19:45 2019
@author: Macrobull @author: Macrobull
Not all ops in this file are supported by both Pytorch and ONNX Not all ops in this file are supported by both Pytorch and ONNX
This only demostrates the conversion/validation workflow from Pytorch to ONNX to Paddle This only demostrates the conversion/validation workflow from Pytorch to ONNX to Paddle fluid
""" """
...@@ -16,12 +16,10 @@ import torch ...@@ -16,12 +16,10 @@ import torch
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
from onnx2paddle.torch_export_helper import export_onnx_with_validation from onnx2fluid.torch_export_helper import export_onnx_with_validation
idx = 0 idx = 0
######### example: RNN ######## ######### example: RNN ########
# #
#class Model(nn.Module): #class Model(nn.Module):
...@@ -44,7 +42,6 @@ idx = 0 ...@@ -44,7 +42,6 @@ idx = 0
# ['x'], ['y'], # ['x'], ['y'],
# verbose=True, training=False) # verbose=True, training=False)
######### example: random ######## ######### example: random ########
# #
#class Model(nn.Module): #class Model(nn.Module):
...@@ -66,9 +63,9 @@ idx = 0 ...@@ -66,9 +63,9 @@ idx = 0
# ['x'], ['y'], # ['x'], ['y'],
# verbose=True, training=False) # verbose=True, training=False)
######## example: fc ######## ######## example: fc ########
class Model(nn.Module): class Model(nn.Module):
def __init__(self): def __init__(self):
super(Model, self).__init__() super(Model, self).__init__()
...@@ -85,13 +82,12 @@ xb = torch.rand((2, 3)) ...@@ -85,13 +82,12 @@ xb = torch.rand((2, 3))
yp = model(xb) yp = model(xb)
idx += 1 idx += 1
print('index: ', idx) print('index: ', idx)
export_onnx_with_validation(model, (xb, ), 't' + str(idx), export_onnx_with_validation(
['x'], ['y'], model, (xb, ), 't' + str(idx), ['x'], ['y'], verbose=True, training=False)
verbose=True, training=False)
######## example: compare ######## ######## example: compare ########
class Model(nn.Module): class Model(nn.Module):
def __init__(self): def __init__(self):
super(Model, self).__init__() super(Model, self).__init__()
...@@ -110,12 +106,15 @@ xb1 = torch.rand((2, 3)) ...@@ -110,12 +106,15 @@ xb1 = torch.rand((2, 3))
ya, yb, yc = model(xb0, xb1) ya, yb, yc = model(xb0, xb1)
idx += 1 idx += 1
print('index: ', idx) print('index: ', idx)
export_onnx_with_validation(model, (xb0, xb1), 't' + str(idx), export_onnx_with_validation(
['x0', 'x1'], ['ya', 'yb', 'yc'], model, (xb0, xb1),
verbose=True, training=False) 't' + str(idx), ['x0', 'x1'], ['ya', 'yb', 'yc'],
verbose=True,
training=False)
######## example: affine_grid ######## ######## example: affine_grid ########
class Model(nn.Module): class Model(nn.Module):
def __init__(self): def __init__(self):
super(Model, self).__init__() super(Model, self).__init__()
...@@ -130,13 +129,15 @@ theta = torch.rand((2, 2, 3)) ...@@ -130,13 +129,15 @@ theta = torch.rand((2, 2, 3))
grid = model(theta) grid = model(theta)
idx += 1 idx += 1
print('index: ', idx) print('index: ', idx)
export_onnx_with_validation(model, (theta, ), 't' + str(idx), export_onnx_with_validation(
['theta'], ['grid'], model, (theta, ),
verbose=True, training=False) 't' + str(idx), ['theta'], ['grid'],
verbose=True,
training=False)
######## example: conv2d_transpose ######## ######## example: conv2d_transpose ########
class Model(nn.Module): class Model(nn.Module):
def __init__(self): def __init__(self):
super(Model, self).__init__() super(Model, self).__init__()
...@@ -155,12 +156,12 @@ xb = torch.rand((2, 3, 4, 5)) ...@@ -155,12 +156,12 @@ xb = torch.rand((2, 3, 4, 5))
yp = model(xb) yp = model(xb)
idx += 1 idx += 1
print('index: ', idx) print('index: ', idx)
export_onnx_with_validation(model, (xb, ), 't' + str(idx), export_onnx_with_validation(
['x'], ['y'], model, (xb, ), 't' + str(idx), ['x'], ['y'], verbose=True, training=False)
verbose=True, training=False)
######## example: conv2d ######## ######## example: conv2d ########
class Model(nn.Module): class Model(nn.Module):
def __init__(self): def __init__(self):
super(Model, self).__init__() super(Model, self).__init__()
...@@ -181,10 +182,8 @@ xb = torch.rand((2, 3, 4, 5)) ...@@ -181,10 +182,8 @@ xb = torch.rand((2, 3, 4, 5))
yp = model(xb) yp = model(xb)
idx += 1 idx += 1
print('index: ', idx) print('index: ', idx)
export_onnx_with_validation(model, (xb, ), 't' + str(idx), export_onnx_with_validation(
['x'], ['y'], model, (xb, ), 't' + str(idx), ['x'], ['y'], verbose=True, training=False)
verbose=True, training=False)
######### example: conv1d ######## ######### example: conv1d ########
# #
...@@ -210,6 +209,7 @@ export_onnx_with_validation(model, (xb, ), 't' + str(idx), ...@@ -210,6 +209,7 @@ export_onnx_with_validation(model, (xb, ), 't' + str(idx),
######## example: empty ######## ######## example: empty ########
class Model(nn.Module): class Model(nn.Module):
def __init__(self): def __init__(self):
super(Model, self).__init__() super(Model, self).__init__()
...@@ -223,6 +223,5 @@ xb = torch.rand((2, 3)) ...@@ -223,6 +223,5 @@ xb = torch.rand((2, 3))
yp = model(xb) yp = model(xb)
idx += 1 idx += 1
print('index: ', idx) print('index: ', idx)
export_onnx_with_validation(model, (xb, ), 't' + str(idx), export_onnx_with_validation(
['y'], ['y'], model, (xb, ), 't' + str(idx), ['y'], ['y'], verbose=True, training=False)
verbose=True, training=False)
#! /usr/bin/env sh #! /usr/bin/env sh
get_url="proxychains4 aria2c -c -s8 -x8" get_url="aria2c -c -s8 -x8"
base_url="https://s3.amazonaws.com/download.onnx/models/opset_9/" base_url="https://s3.amazonaws.com/download.onnx/models/opset_9/"
flags="-de -o /tmp/export/" flags="-e -o /tmp/export/"
bvlc_alexnet() bvlc_alexnet()
{ {
...@@ -18,13 +18,13 @@ bvlc_alexnet() ...@@ -18,13 +18,13 @@ bvlc_alexnet()
do do
echo "converting $npz ..." echo "converting $npz ..."
python convert_data_npz_0.py "$npz" "data_0" "prob_1" python convert_data_npz_0.py "$npz" "data_0" "prob_1"
python -m onnx2paddle $flags "$fn_model" -t $npz python -m onnx2fluid $flags "$fn_model" -t $npz
done done
for pb_dir in $bn_tar/*/ for pb_dir in $bn_tar/*/
do do
echo "converting $pb_dir ..." echo "converting $pb_dir ..."
python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1" python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1"
python -m onnx2paddle $flags "$fn_model" -t echo $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -42,7 +42,7 @@ bvlc_googlenet() ...@@ -42,7 +42,7 @@ bvlc_googlenet()
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1" python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -60,7 +60,7 @@ bvlc_reference_caffenet() ...@@ -60,7 +60,7 @@ bvlc_reference_caffenet()
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1" python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -69,7 +69,7 @@ bvlc_reference_rcnn_ilsvrc13() ...@@ -69,7 +69,7 @@ bvlc_reference_rcnn_ilsvrc13()
bn_tar="bvlc_reference_rcnn_ilsvrc13" bn_tar="bvlc_reference_rcnn_ilsvrc13"
fn_tar="$bn_tar.tar.gz" fn_tar="$bn_tar.tar.gz"
fn_model="$bn_tar/model.onnx" fn_model="$bn_tar/model.onnx"
$get_url "$base_url$fn_tar" $get_url "$base_url$fn_tar"
echo "extracting ..." echo "extracting ..."
tar xf "$fn_tar" tar xf "$fn_tar"
...@@ -77,8 +77,8 @@ bvlc_reference_rcnn_ilsvrc13() ...@@ -77,8 +77,8 @@ bvlc_reference_rcnn_ilsvrc13()
for pb_dir in $bn_tar/*/ for pb_dir in $bn_tar/*/
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "data_0" "softmaxout_1" python convert_data_pb_0.py "$pb_dir" "data_0" "fc_rcnn_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -87,7 +87,7 @@ inception_v1() ...@@ -87,7 +87,7 @@ inception_v1()
bn_tar="inception_v1" bn_tar="inception_v1"
fn_tar="$bn_tar.tar.gz" fn_tar="$bn_tar.tar.gz"
fn_model="$bn_tar/model.onnx" fn_model="$bn_tar/model.onnx"
$get_url "$base_url$fn_tar" $get_url "$base_url$fn_tar"
echo "extracting ..." echo "extracting ..."
tar xf "$fn_tar" tar xf "$fn_tar"
...@@ -96,14 +96,14 @@ inception_v1() ...@@ -96,14 +96,14 @@ inception_v1()
do do
echo "converting $npz ..." echo "converting $npz ..."
python convert_data_npz_0.py "$npz" "data_0" "prob_1" python convert_data_npz_0.py "$npz" "data_0" "prob_1"
python -m onnx2paddle $flags "$fn_model" -t $npz python -m onnx2fluid $flags "$fn_model" -t $npz
done done
for pb_dir in $bn_tar/*/ for pb_dir in $bn_tar/*/
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1" python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -112,7 +112,7 @@ inception_v2() ...@@ -112,7 +112,7 @@ inception_v2()
bn_tar="inception_v2" bn_tar="inception_v2"
fn_tar="$bn_tar.tar.gz" fn_tar="$bn_tar.tar.gz"
fn_model="$bn_tar/model.onnx" fn_model="$bn_tar/model.onnx"
$get_url "$base_url$fn_tar" $get_url "$base_url$fn_tar"
echo "extracting ..." echo "extracting ..."
tar xf "$fn_tar" tar xf "$fn_tar"
...@@ -121,14 +121,14 @@ inception_v2() ...@@ -121,14 +121,14 @@ inception_v2()
do do
echo "converting $npz ..." echo "converting $npz ..."
python convert_data_npz_0.py "$npz" "data_0" "prob_1" python convert_data_npz_0.py "$npz" "data_0" "prob_1"
python -m onnx2paddle $flags "$fn_model" -t $npz python -m onnx2fluid $flags "$fn_model" -t $npz
done done
for pb_dir in $bn_tar/*/ for pb_dir in $bn_tar/*/
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1" python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -137,7 +137,7 @@ resnet50() ...@@ -137,7 +137,7 @@ resnet50()
bn_tar="resnet50" bn_tar="resnet50"
fn_tar="$bn_tar.tar.gz" fn_tar="$bn_tar.tar.gz"
fn_model="$bn_tar/model.onnx" fn_model="$bn_tar/model.onnx"
$get_url "$base_url$fn_tar" $get_url "$base_url$fn_tar"
echo "extracting ..." echo "extracting ..."
tar xf "$fn_tar" tar xf "$fn_tar"
...@@ -146,14 +146,14 @@ resnet50() ...@@ -146,14 +146,14 @@ resnet50()
do do
echo "converting $npz ..." echo "converting $npz ..."
python convert_data_npz_0.py "$npz" "gpu_0/data_0" "gpu_0/softmaxout_1" python convert_data_npz_0.py "$npz" "gpu_0/data_0" "gpu_0/softmaxout_1"
python -m onnx2paddle $flags "$fn_model" -t $npz python -m onnx2fluid $flags "$fn_model" -t $npz
done done
for pb_dir in $bn_tar/*/ for pb_dir in $bn_tar/*/
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "gpu_0/data_0" "gpu_0/softmaxout_1" python convert_data_pb_0.py "$pb_dir" "gpu_0/data_0" "gpu_0/softmaxout_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -162,7 +162,7 @@ shufflenet() ...@@ -162,7 +162,7 @@ shufflenet()
bn_tar="shufflenet" bn_tar="shufflenet"
fn_tar="$bn_tar.tar.gz" fn_tar="$bn_tar.tar.gz"
fn_model="$bn_tar/model.onnx" fn_model="$bn_tar/model.onnx"
$get_url "$base_url$fn_tar" $get_url "$base_url$fn_tar"
echo "extracting ..." echo "extracting ..."
tar xf "$fn_tar" tar xf "$fn_tar"
...@@ -171,7 +171,7 @@ shufflenet() ...@@ -171,7 +171,7 @@ shufflenet()
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "gpu_0/data_0" "gpu_0/softmaxout_1" python convert_data_pb_0.py "$pb_dir" "gpu_0/data_0" "gpu_0/softmaxout_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -180,7 +180,7 @@ squeezenet() ...@@ -180,7 +180,7 @@ squeezenet()
bn_tar="squeezenet" bn_tar="squeezenet"
fn_tar="$bn_tar.tar.gz" fn_tar="$bn_tar.tar.gz"
fn_model="$bn_tar/model.onnx" fn_model="$bn_tar/model.onnx"
$get_url "$base_url$fn_tar" $get_url "$base_url$fn_tar"
echo "extracting ..." echo "extracting ..."
tar xf "$fn_tar" tar xf "$fn_tar"
...@@ -189,7 +189,7 @@ squeezenet() ...@@ -189,7 +189,7 @@ squeezenet()
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "data_0" "softmaxout_1" python convert_data_pb_0.py "$pb_dir" "data_0" "softmaxout_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -198,7 +198,7 @@ tiny_yolov2() ...@@ -198,7 +198,7 @@ tiny_yolov2()
bn_tar="tiny_yolov2" bn_tar="tiny_yolov2"
fn_tar="$bn_tar.tar.gz" fn_tar="$bn_tar.tar.gz"
fn_model="$bn_tar/model.onnx" fn_model="$bn_tar/model.onnx"
$get_url "https://onnxzoo.blob.core.windows.net/models/opset_8/tiny_yolov2/$fn_tar" $get_url "https://onnxzoo.blob.core.windows.net/models/opset_8/tiny_yolov2/$fn_tar"
echo "extracting ..." echo "extracting ..."
tar xf "$fn_tar" tar xf "$fn_tar"
...@@ -207,7 +207,7 @@ tiny_yolov2() ...@@ -207,7 +207,7 @@ tiny_yolov2()
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "image" "grid" python convert_data_pb_0.py "$pb_dir" "image" "grid"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz -x python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz -x
done done
} }
...@@ -216,7 +216,7 @@ vgg19() ...@@ -216,7 +216,7 @@ vgg19()
bn_tar="vgg19" bn_tar="vgg19"
fn_tar="$bn_tar.tar.gz" fn_tar="$bn_tar.tar.gz"
fn_model="$bn_tar/model.onnx" fn_model="$bn_tar/model.onnx"
$get_url "$base_url$fn_tar" $get_url "$base_url$fn_tar"
echo "extracting ..." echo "extracting ..."
tar xf "$fn_tar" tar xf "$fn_tar"
...@@ -225,7 +225,7 @@ vgg19() ...@@ -225,7 +225,7 @@ vgg19()
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1" python convert_data_pb_0.py "$pb_dir" "data_0" "prob_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
...@@ -234,7 +234,7 @@ zfnet512() ...@@ -234,7 +234,7 @@ zfnet512()
bn_tar="zfnet512" bn_tar="zfnet512"
fn_tar="$bn_tar.tar.gz" fn_tar="$bn_tar.tar.gz"
fn_model="$bn_tar/model.onnx" fn_model="$bn_tar/model.onnx"
$get_url "$base_url$fn_tar" $get_url "$base_url$fn_tar"
echo "extracting ..." echo "extracting ..."
tar xf "$fn_tar" tar xf "$fn_tar"
...@@ -243,20 +243,20 @@ zfnet512() ...@@ -243,20 +243,20 @@ zfnet512()
do do
echo "converting $pb_dir" echo "converting $pb_dir"
python convert_data_pb_0.py "$pb_dir" "gpu_0/data_0" "gpu_0/softmax_1" python convert_data_pb_0.py "$pb_dir" "gpu_0/data_0" "gpu_0/softmax_1"
python -m onnx2paddle $flags "$fn_model" -t $(dirname "$pb_dir/x").npz python -m onnx2fluid $flags "$fn_model" -t $(dirname "$pb_dir/x").npz
done done
} }
bvlc_alexnet # data error bvlc_alexnet
bvlc_googlenet # desc error bvlc_googlenet
bvlc_reference_caffenet bvlc_reference_caffenet
bvlc_reference_rcnn_ilsvrc13 bvlc_reference_rcnn_ilsvrc13
inception_v1 ### inception_v1
inception_v2 ### inception_v2
resnet50 # data error resnet50
shufflenet ### shufflenet
squeezenet squeezenet
tiny_yolov2 # not supported tiny_yolov2 # not supported
vgg19 vgg19
zfnet512 # data error zfnet512
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# #
################################################################################ ################################################################################
""" """
本文件允许模块包以python -m onnx2paddle方式直接执行。 本文件允许模块包以python -m onnx2fluid方式直接执行。
Authors: Macrobull Authors: Macrobull
Date: 2019/02/22 10:25:46 Date: 2019/02/22 10:25:46
...@@ -21,43 +21,67 @@ import argparse ...@@ -21,43 +21,67 @@ import argparse
import logging import logging
import sys import sys
parser = argparse.ArgumentParser(
parser = argparse.ArgumentParser(description='onnx2paddle', description='onnx2fluid',
formatter_class=argparse.ArgumentDefaultsHelpFormatter, formatter_class=argparse.ArgumentDefaultsHelpFormatter,
) )
parser.add_argument('model', nargs=1, parser.add_argument(
help='path to model.onnx', 'model',
) nargs=1,
parser.add_argument('--debug', '-d', action='store_true', help='path to model.onnx',
help='enable debug logging and checking', )
) parser.add_argument(
parser.add_argument('--output-dir', '-o', type=str, default='', '--debug',
help='output directory', '-d',
) action='store_true',
parser.add_argument('--test_data', '-t', type=str, default='', help='enable debug logging and checking',
help='I/O golden data for validation, e.g. test.npy, test.npz', )
) parser.add_argument(
parser.add_argument('--embed_params', '-e', action='store_true', '--output_dir',
help='try to embed parameters for trainable Paddle layers', '-o',
) type=str,
parser.add_argument('--pedantic', action='store_true', default=True, default='',
help='accept and convert only standard ONNX opset', help='output directory',
) )
parser.add_argument('--no-pedantic', '-x', action='store_false', parser.add_argument(
dest='pedantic', '--test_data',
help='process non-standard ONNX ops, this may lead to fails', '-t',
) type=str,
parser.add_argument('--precision', '-p', type=int, default=4, default='',
help='assertion decimal for validation', help='I/O golden data for validation, e.g. test.npy, test.npz',
) )
parser.add_argument(
'--embed_params',
'-e',
action='store_true',
help='try to embed parameters for trainable Paddle fluid layers',
)
parser.add_argument(
'--pedantic',
action='store_true',
default=True,
help='accept and convert only standard ONNX opset',
)
parser.add_argument(
'--no-pedantic',
'-x',
action='store_false',
dest='pedantic',
help='process non-standard ONNX ops, this may lead to fails',
)
parser.add_argument(
'--precision',
'-p',
type=int,
default=4,
help='assertion decimal for validation',
)
args = parser.parse_args() args = parser.parse_args()
logging_format = '[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s' logging_format = '[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s'
logging_level = logging.DEBUG if args.debug else logging.INFO logging_level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(format=logging_format, level=logging_level) logging.basicConfig(format=logging_format, level=logging_level)
try: try:
from . import cmdline from . import cmdline
except ImportError: except ImportError:
...@@ -66,5 +90,4 @@ except ImportError: ...@@ -66,5 +90,4 @@ except ImportError:
# imports # imports
main = cmdline.main main = cmdline.main
sys.exit(main(**args.__dict__)) sys.exit(main(**args.__dict__))
...@@ -21,7 +21,6 @@ import logging ...@@ -21,7 +21,6 @@ import logging
import shutil import shutil
import zipfile import zipfile
__all__ = [ __all__ = [
'main', 'main',
] ]
...@@ -42,7 +41,7 @@ def main(**kwargs): ...@@ -42,7 +41,7 @@ def main(**kwargs):
# imports # imports
convert = conversion.convert convert = conversion.convert
logger = logging.getLogger('onnx2paddle') logger = logging.getLogger('onnx2fluid')
debug = kwargs.get('debug', False) debug = kwargs.get('debug', False)
# prepare arguments # prepare arguments
...@@ -58,13 +57,15 @@ def main(**kwargs): ...@@ -58,13 +57,15 @@ def main(**kwargs):
onnx_opset_pedantic = kwargs.get('pedantic', True) onnx_opset_pedantic = kwargs.get('pedantic', True)
# convert # convert
convert(filename, save_dir, convert(
model_basename=model_basename, filename,
model_func_name=model_func_name, save_dir,
embed_params=embed_params, model_basename=model_basename,
onnx_opset_version=onnx_opset_version, model_func_name=model_func_name,
onnx_opset_pedantic=onnx_opset_pedantic, embed_params=embed_params,
debug=debug) onnx_opset_version=onnx_opset_version,
onnx_opset_pedantic=onnx_opset_pedantic,
debug=debug)
# validate # validate
passed = True passed = True
...@@ -80,21 +81,23 @@ def main(**kwargs): ...@@ -80,21 +81,23 @@ def main(**kwargs):
# in fact fluid can not fully clear the context # in fact fluid can not fully clear the context
# continuous validation may be inaccurate # continuous validation may be inaccurate
precision = 10 ** -kwargs.get('precision', 4) precision = 10**-kwargs.get('precision', 4)
logger.info('starting validation on desc ...') logger.info('starting validation on desc ...')
passed &= validate(shutil.os.path.join(save_dir, '__model__'), passed &= validate(
golden_data_filename, shutil.os.path.join(save_dir, '__model__'),
precision=precision, golden_data_filename,
) precision=precision,
)
logger.info('starting validation on code ...') logger.info('starting validation on code ...')
passed &= validate(shutil.os.path.join(save_dir, model_basename), passed &= validate(
golden_data_filename, shutil.os.path.join(save_dir, model_basename),
model_func_name=model_func_name, golden_data_filename,
precision=precision, model_func_name=model_func_name,
save_inference_model=debug, # this overwrite desc file for test precision=precision,
) save_inference_model=debug, # this overwrite desc file for test
)
if not passed: if not passed:
logger.error('validation failed, exit') logger.error('validation failed, exit')
...@@ -112,20 +115,22 @@ def main(**kwargs): ...@@ -112,20 +115,22 @@ def main(**kwargs):
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig( logging.basicConfig(
format='[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s', format=
level=logging.DEBUG, '[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s',
) level=logging.DEBUG,
)
# main(model=['../examples/t5.onnx'],
# output_dir='/tmp/export/', # main(model=['../examples/t5.onnx'],
# embed_params=False, # output_dir='/tmp/export/',
# pedantic=False, # embed_params=False,
# test_data='../examples/t5.npz', # pedantic=False,
# debug=True) # test_data='../examples/t5.npz',
# debug=True)
main(model=['../examples/shufflenet/model.onnx'],
output_dir='/tmp/export/', main(
embed_params=True, model=['../examples/inception_v2/model.onnx'],
pedantic=False, output_dir='/tmp/export/',
test_data='../examples/shufflenet/test_data_set_0.npz', embed_params=True,
debug=True) pedantic=False,
test_data='../examples/inception_v2/test_data_set_2.npz',
debug=True)
...@@ -12,19 +12,21 @@ from __future__ import division ...@@ -12,19 +12,21 @@ from __future__ import division
import logging import logging
import shutil import shutil
__all__ = [ __all__ = [
'convert', 'convert',
] ]
def convert(onnx_model_filename, save_dir, def convert(onnx_model_filename,
model_basename='model.py', model_func_name='inference', save_dir,
model_basename='model.py',
model_func_name='inference',
embed_params=False, embed_params=False,
onnx_opset_version=9, onnx_opset_pedantic=True, onnx_opset_version=9,
onnx_opset_pedantic=True,
debug=False): debug=False):
""" """
convert an ONNX model to Paddle Python code and desc pb convert an ONNX model to Paddle fluid Python code and desc pb
""" """
import onnx import onnx
...@@ -59,10 +61,11 @@ def convert(onnx_model_filename, save_dir, ...@@ -59,10 +61,11 @@ def convert(onnx_model_filename, save_dir,
logger.info('checking model ...') logger.info('checking model ...')
check_model(onnx_model) check_model(onnx_model)
logger.debug('using opset version: %d', onnx_opset_version) logger.debug('using opset version: %d', onnx_opset_version)
if onnx_opset_pedantic: # WORKAROUND: RuntimeError: No Adapter For OP if onnx_opset_pedantic: # WORKAROUND: RuntimeError: No Adapter For OP
onnx_model = convert_version(onnx_model, onnx_opset_version) onnx_model = convert_version(onnx_model, onnx_opset_version)
else: # TODO: add new argument for this option else: # TODO: add new argument for this option
logger.warning('opset conversion skipped for onnx_opset_pedantic is OFF') logger.warning(
'opset conversion skipped for onnx_opset_pedantic is OFF')
onnx_model = polish_model(onnx_model) onnx_model = polish_model(onnx_model)
except ValidationError as e: except ValidationError as e:
if onnx_opset_pedantic: if onnx_opset_pedantic:
...@@ -90,13 +93,13 @@ def convert(onnx_model_filename, save_dir, ...@@ -90,13 +93,13 @@ def convert(onnx_model_filename, save_dir,
onnx.save(model, debug_model_filename + '.optimized_and_inffered.onnx') onnx.save(model, debug_model_filename + '.optimized_and_inffered.onnx')
# onnx.save(model, '/tmp/export/optimized_and_inffered.onnx') # onnx.save(model, '/tmp/export/optimized_and_inffered.onnx')
# I/O instances # I/O instances
onnx_graph = onnx_model.graph onnx_graph = onnx_model.graph
paddle_program = Program() fluid_program = Program()
paddle_writer = Writer() fluid_writer = Writer()
# model components # model components
# graph_name = onnx_graph.name # graph_name = onnx_graph.name
graph_inputs = [value.name for value in onnx_graph.input] graph_inputs = [value.name for value in onnx_graph.input]
graph_outputs = [value.name for value in onnx_graph.output] graph_outputs = [value.name for value in onnx_graph.output]
graph_params = [] graph_params = []
...@@ -107,29 +110,37 @@ def convert(onnx_model_filename, save_dir, ...@@ -107,29 +110,37 @@ def convert(onnx_model_filename, save_dir,
for name, weight in graph_weights(onnx_graph): for name, weight in graph_weights(onnx_graph):
value_info = graph_value_infos[name] value_info = graph_value_infos[name]
value_info['embeded_as'] = [] value_info['embeded_as'] = []
value_info['get_weight'] = lambda: weight.tolist() # lazy getter value_info['get_weight'] = (lambda w: lambda: w.tolist())(
weight) # lazy getter
logger.info('conversion started') logger.info('conversion started')
# op set conversion # op set conversion
# topo = 'backward' if embed_params else 'forward' # topo = 'backward' if embed_params else 'forward'
topo = 'forward' topo = 'forward'
for name, domain, op_type, inputs, outputs, attrs in graph_ops(onnx_graph, topo=topo): for name, domain, op_type, inputs, outputs, attrs in graph_ops(
onnx_graph, topo=topo):
logger.debug('translating op %s %s::%s ...', name, domain, op_type) logger.debug('translating op %s %s::%s ...', name, domain, op_type)
if domain == DEFAULT_OP_DOMAIN: if domain == DEFAULT_OP_DOMAIN:
domain = '' domain = ''
try: try:
paddle_writer.emit_op(paddle_program, name, domain, op_type, fluid_writer.emit_op(
inputs, outputs, attrs, fluid_program,
graph_value_infos, name,
embed_params=embed_params, domain,
) op_type,
inputs,
outputs,
attrs,
graph_value_infos,
embed_params=embed_params,
)
except BaseException as e: except BaseException as e:
logger.fatal('conversion failed for:\n\t%s -> %s::%s -> %s', logger.fatal('conversion failed for:\n\t%s -> %s::%s -> %s', inputs,
inputs, domain, op_type, outputs) domain, op_type, outputs)
raise e raise e
op_codes = paddle_program.codes op_codes = fluid_program.codes
paddle_program.codes = [] fluid_program.codes = []
logger.info('%d ops converted', len(paddle_program.op_descs)) logger.info('%d ops converted', len(fluid_program.op_descs))
# weight writer # weight writer
for name, weight in graph_weights(onnx_graph): for name, weight in graph_weights(onnx_graph):
...@@ -138,18 +149,24 @@ def convert(onnx_model_filename, save_dir, ...@@ -138,18 +149,24 @@ def convert(onnx_model_filename, save_dir,
var_names = value_info.get('embeded_as', []) var_names = value_info.get('embeded_as', [])
if var_names: if var_names:
if len(var_names) > 1: if len(var_names) > 1:
logger.info('weight %s is shared between ops, more disk space will be consumed', name) logger.info(
logger.debug('saving weight %s with size of %d, in %d bytes, as %s ...', 'weight %s is shared between ops, more disk space will be consumed',
name, weight.size, weight.nbytes, var_names) name)
for var_name in var_names: # multiple references logger.debug(
paddle_writer.write_weight(weight, shutil.os.path.join(save_dir, var_name)) 'saving weight %s with size of %d, in %d bytes, as %s ...',
name, weight.size, weight.nbytes, var_names)
for var_name in var_names: # multiple references
fluid_writer.write_weight(
weight, shutil.os.path.join(save_dir, var_name))
else: else:
logger.debug('saving weight %s with size of %d, in %d bytes, to %s ...', logger.debug(
name, weight.size, weight.nbytes, make_var_name(name)) 'saving weight %s with size of %d, in %d bytes, to %s ...',
paddle_writer.write_weight(weight, shutil.os.path.join(save_dir, make_var_name(name))) name, weight.size, weight.nbytes, make_var_name(name))
paddle_writer.emit_param(paddle_program, name, value_info) fluid_writer.write_weight(
param_codes = paddle_program.codes weight, shutil.os.path.join(save_dir, make_var_name(name)))
paddle_program.codes = [] fluid_writer.emit_param(fluid_program, name, value_info)
param_codes = fluid_program.codes
fluid_program.codes = []
logger.info('%d weights converted', len(graph_params)) logger.info('%d weights converted', len(graph_params))
# input writer # input writer
...@@ -159,9 +176,11 @@ def convert(onnx_model_filename, save_dir, ...@@ -159,9 +176,11 @@ def convert(onnx_model_filename, save_dir,
value_info = graph_value_infos[name] value_info = graph_value_infos[name]
assert value_info['external'] assert value_info['external']
external_inputs.append(name) external_inputs.append(name)
paddle_writer.emit_inputs(paddle_program, external_inputs, graph_value_infos, remove_batch=False) # TODO: fluid_writer.emit_inputs(
input_codes = paddle_program.codes fluid_program, external_inputs, graph_value_infos,
paddle_program.codes = [] remove_batch=False) # TODO:
input_codes = fluid_program.codes
fluid_program.codes = []
logger.info('%d inputs converted', len(external_inputs)) logger.info('%d inputs converted', len(external_inputs))
# output writer # output writer
...@@ -171,49 +190,93 @@ def convert(onnx_model_filename, save_dir, ...@@ -171,49 +190,93 @@ def convert(onnx_model_filename, save_dir,
value_info = graph_value_infos[name] value_info = graph_value_infos[name]
assert value_info['external'] assert value_info['external']
external_outputs.append(name) external_outputs.append(name)
paddle_writer.emit_outputs(paddle_program, external_outputs) fluid_writer.emit_outputs(fluid_program, external_outputs)
output_codes = [''] + paddle_program.codes # add an empty line output_codes = [''] + fluid_program.codes # add an empty line
paddle_program.codes = [] fluid_program.codes = []
logger.info('%d outputs converted', len(external_outputs)) logger.info('%d outputs converted', len(external_outputs))
# code generation # code generation
header_codes = fluid_writer.header_code(
model_func_name, 'From: {}'.format(onnx_model_filename))
code_filename = shutil.os.path.join(save_dir, model_basename) code_filename = shutil.os.path.join(save_dir, model_basename)
paddle_writer.write_code_file(code_filename, paddle_writer.header_code(model_func_name), fluid_writer.write_code_file(code_filename, header_codes, input_codes,
input_codes, param_codes, op_codes, output_codes) param_codes, op_codes, output_codes)
logger.info('code saved to %s, factory function: %s', code_filename, model_func_name) logger.info('code saved to %s, factory function: %s', code_filename,
model_func_name)
# desc generation # desc generation
desc_filename = shutil.os.path.join(save_dir, '__model__') desc_filename = shutil.os.path.join(save_dir, '__model__')
paddle_writer.write_desc_file(desc_filename, fluid_writer.write_desc_file(
op_descs=paddle_program.op_descs, desc_filename,
var_descs=paddle_program.var_descs, op_descs=fluid_program.op_descs,
) var_descs=fluid_program.var_descs,
)
logger.info('program saved to %s', desc_filename) logger.info('program saved to %s', desc_filename)
logger.info('conversion finished') logger.info('conversion finished')
# globals().update(locals())
# globals().update(locals())
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig( import argparse
format='[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s',
level=logging.DEBUG, parser = argparse.ArgumentParser(
) description='onnx2fluid.convert',
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
'model',
nargs=1,
help='path to model.onnx',
)
parser.add_argument(
'--debug',
'-d',
action='store_true',
help='enable debug logging and checking',
)
parser.add_argument(
'--output_dir',
'-o',
type=str,
default='',
help='output directory',
)
parser.add_argument(
'--embed_params',
'-e',
action='store_true',
help='try to embed parameters for trainable Paddle fluid layers',
)
parser.add_argument(
'--pedantic',
action='store_true',
default=True,
help='accept and convert only standard ONNX opset',
)
parser.add_argument(
'--no-pedantic',
'-x',
action='store_false',
dest='pedantic',
help='process non-standard ONNX ops, this may lead to fails',
)
args = parser.parse_args()
logging_format = '[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s'
logging_level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(format=logging_format, level=logging_level)
debug = args.debug
model_filename = args.model[0]
save_dir = args.output_dir
embed_params = args.embed_params
pedantic = args.pedantic
model_list = [ convert(
'../examples/t1.onnx', model_filename,
'../examples/t2.onnx', save_dir,
'../examples/t3.onnx', embed_params=embed_params,
'../examples/t4.onnx', onnx_opset_pedantic=pedantic,
'../examples/t5.onnx', debug=debug)
'../examples/t6.onnx',
# '../examples/t7.onnx',
# '../examples/t8.onnx',
]
for model in model_list:
pathname, _ = shutil.os.path.splitext(model)
convert(model, pathname,
onnx_opset_pedantic=False, debug=True)
convert(model, pathname + '.embeded',
embed_params=True, onnx_opset_pedantic=False, debug=True)
此差异已折叠。
...@@ -12,34 +12,36 @@ import logging ...@@ -12,34 +12,36 @@ import logging
import numpy as np import numpy as np
import onnx import onnx
from collections import OrderedDict as Dict # as default dict from collections import OrderedDict as Dict # as default dict
from onnx.helper import get_attribute_value, make_attribute from onnx.helper import get_attribute_value, make_attribute
from onnx.mapping import TENSOR_TYPE_TO_NP_TYPE from onnx.mapping import TENSOR_TYPE_TO_NP_TYPE
from onnx.numpy_helper import to_array from onnx.numpy_helper import to_array
from onnx.shape_inference import infer_shapes from onnx.shape_inference import infer_shapes
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
__all__ = [ __all__ = [
'print_pb_structure', 'print_pb_structure',
'build_value_refs', 'build_value_refs',
'node_attrs', 'node_topo', 'node_iter', 'node_attrs',
'node_topo',
'node_iter',
'tensor_shape', 'tensor_shape',
'graph_ops', 'graph_weights', 'graph_ops',
'graph_weights',
'inferred_model_value_info', 'inferred_model_value_info',
'optimize_model_skip_op_for_inference', 'optimize_model_skip_op_for_inference',
'optimize_model_strip_initializer', 'optimize_model_strip_initializer',
'optimize_model_cast', 'optimize_model_slice', 'optimize_model_cast',
'optimize_model_slice',
] ]
ONNX_INT_MAX = 2 ** 63 - 1 ONNX_INT_MAX = 2**63 - 1
DEFAULT_OP_DOMAIN = 'ai.onnx' DEFAULT_OP_DOMAIN = 'ai.onnx'
def print_pb_structure(message, def print_pb_structure(message, loop_iterative=False, depth=0):
loop_iterative=False, depth=0):
""" """
print pb fields in its structure print pb fields in its structure
""" """
...@@ -47,14 +49,17 @@ def print_pb_structure(message, ...@@ -47,14 +49,17 @@ def print_pb_structure(message,
if hasattr(message, 'DESCRIPTOR') and hasattr(message.DESCRIPTOR, 'fields'): if hasattr(message, 'DESCRIPTOR') and hasattr(message.DESCRIPTOR, 'fields'):
for field in message.DESCRIPTOR.fields: for field in message.DESCRIPTOR.fields:
print('\t' * depth + '-', field.name) print('\t' * depth + '-', field.name)
print_pb_structure(getattr(message, field.name), print_pb_structure(
loop_iterative=loop_iterative, depth=(depth + 1)) getattr(message, field.name),
loop_iterative=loop_iterative,
depth=(depth + 1))
if loop_iterative and hasattr(message, 'MergeFrom') and hasattr(message, '__len__'): if loop_iterative and hasattr(message, 'MergeFrom') and hasattr(
message, '__len__'):
for idx, item in enumerate(message): for idx, item in enumerate(message):
print('\t' * depth + '-', idx) print('\t' * depth + '-', idx)
print_pb_structure(item, print_pb_structure(
loop_iterative=loop_iterative, depth=(depth + 1)) item, loop_iterative=loop_iterative, depth=(depth + 1))
def build_value_refs(nodes): def build_value_refs(nodes):
...@@ -80,7 +85,8 @@ def get_attribute_value2(attr): ...@@ -80,7 +85,8 @@ def get_attribute_value2(attr):
if attr.type == onnx.AttributeProto.TENSOR: if attr.type == onnx.AttributeProto.TENSOR:
dtype = np.dtype(TENSOR_TYPE_TO_NP_TYPE[attr.t.data_type]) dtype = np.dtype(TENSOR_TYPE_TO_NP_TYPE[attr.t.data_type])
data = attr.t.raw_data data = attr.t.raw_data
value = np.frombuffer(data, dtype=dtype, count=(len(data) // dtype.itemsize)) value = np.frombuffer(
data, dtype=dtype, count=(len(data) // dtype.itemsize))
else: else:
value = get_attribute_value(attr) value = get_attribute_value(attr)
return value return value
...@@ -91,7 +97,8 @@ def node_attrs(node): ...@@ -91,7 +97,8 @@ def node_attrs(node):
convert ONNX node attributes to dict convert ONNX node attributes to dict
""" """
return {attr.name: get_attribute_value2(attr) for attr in node.attribute} # dict return {attr.name: get_attribute_value2(attr)
for attr in node.attribute} # dict
def tensor_shape(tensor): def tensor_shape(tensor):
...@@ -137,7 +144,7 @@ def node_topo(nodes, topo='default'): ...@@ -137,7 +144,7 @@ def node_topo(nodes, topo='default'):
for next_idx in input_refs[val_name]: for next_idx in input_refs[val_name]:
node_in_degrees[next_idx] -= 1 node_in_degrees[next_idx] -= 1
if node_in_degrees[next_idx] == 0: if node_in_degrees[next_idx] == 0:
queue.insert(0, next_idx) # make it lazy queue.insert(0, next_idx) # make it lazy
return node_topo return node_topo
if topo == 'backward': if topo == 'backward':
...@@ -162,14 +169,13 @@ def node_topo(nodes, topo='default'): ...@@ -162,14 +169,13 @@ def node_topo(nodes, topo='default'):
for next_idx in output_refs[val_name]: for next_idx in output_refs[val_name]:
node_out_degrees[next_idx] -= 1 node_out_degrees[next_idx] -= 1
if node_out_degrees[next_idx] == 0: if node_out_degrees[next_idx] == 0:
queue.insert(0, next_idx) # make it lazy queue.insert(0, next_idx) # make it lazy
return node_topo return node_topo
raise ValueError('unkown given topo: {}'.format(topo)) raise ValueError('unkown given topo: {}'.format(topo))
def node_iter(nodes, def node_iter(nodes, indices=None):
indices=None):
""" """
generator for ONNX node graph with given indices generator for ONNX node graph with given indices
""" """
...@@ -194,8 +200,7 @@ def node_iter(nodes, ...@@ -194,8 +200,7 @@ def node_iter(nodes,
yield name, domain, op_type, inputs, outputs, attrs yield name, domain, op_type, inputs, outputs, attrs
def graph_ops(graph, def graph_ops(graph, topo='default'):
topo='default'):
""" """
generator for ONNX node graph with given topology generator for ONNX node graph with given topology
""" """
...@@ -232,24 +237,24 @@ def inferred_model_value_info(model): ...@@ -232,24 +237,24 @@ def inferred_model_value_info(model):
value_info = Dict() value_info = Dict()
for item in graph.value_info: for item in graph.value_info:
value_info[item.name] = dict( value_info[item.name] = dict(
dtype=TENSOR_TYPE_TO_NP_TYPE[item.type.tensor_type.elem_type], dtype=TENSOR_TYPE_TO_NP_TYPE[item.type.tensor_type.elem_type],
shape=tensor_shape(item), shape=tensor_shape(item),
external=False, external=False,
) )
for item in graph.input: for item in graph.input:
assert item.name not in value_info assert item.name not in value_info
value_info[item.name] = dict( value_info[item.name] = dict(
dtype=TENSOR_TYPE_TO_NP_TYPE[item.type.tensor_type.elem_type], dtype=TENSOR_TYPE_TO_NP_TYPE[item.type.tensor_type.elem_type],
shape=tensor_shape(item), shape=tensor_shape(item),
external=True, external=True,
) )
for item in graph.output: for item in graph.output:
# assert item.name not in value_info, 'bypass-model not supported' # assert item.name not in value_info, 'bypass-model not supported'
value_info[item.name] = dict( value_info[item.name] = dict(
dtype=TENSOR_TYPE_TO_NP_TYPE[item.type.tensor_type.elem_type], dtype=TENSOR_TYPE_TO_NP_TYPE[item.type.tensor_type.elem_type],
shape=tensor_shape(item), shape=tensor_shape(item),
external=True, external=True,
) )
return value_info return value_info
...@@ -283,9 +288,7 @@ def skip_node_backward(nodes, src_input_name, dst_output_name, output_refs): ...@@ -283,9 +288,7 @@ def skip_node_backward(nodes, src_input_name, dst_output_name, output_refs):
return processed return processed
def optimize_model_skip_op_for_inference( def optimize_model_skip_op_for_inference(model, op_list=None):
model,
op_list=None):
""" """
skip ops can be bypassed for inference skip ops can be bypassed for inference
""" """
...@@ -297,38 +300,42 @@ def optimize_model_skip_op_for_inference( ...@@ -297,38 +300,42 @@ def optimize_model_skip_op_for_inference(
ret = type(model)() ret = type(model)()
ret.CopyFrom(model) ret.CopyFrom(model)
ret.graph.ClearField('value_info') # WORKAROUND: onnx do not drop old value_info ret.graph.ClearField(
'value_info') # WORKAROUND: onnx do not drop old value_info
ret_nodes = ret.graph.node ret_nodes = ret.graph.node
nodes_to_remove = [] nodes_to_remove = []
for node_idx, node in enumerate(nodes): for node_idx, node in enumerate(nodes):
if not(node.domain == DEFAULT_OP_DOMAIN or node.domain == ''): if not (node.domain == DEFAULT_OP_DOMAIN or node.domain == ''):
continue continue
op_type = node.op_type op_type = node.op_type
if not(op_type in op_list): if not (op_type in op_list):
continue continue
if op_type in ['Dropout']: if op_type in ['Dropout']:
input_name = node.input[0] input_name = node.input[0]
output_name = node.output[0] output_name = node.output[0]
elif not(len(node.input) == 1 and len(node.output) == 1): elif not (len(node.input) == 1 and len(node.output) == 1):
logger.warning('currently only 1-input-1-output op supported, skip required %d: %s', logger.warning(
node_idx, node.op_type) 'currently only 1-input-1-output op supported, skip required %d: %s',
node_idx, node.op_type)
continue continue
else: else:
input_name = node.input[0] input_name = node.input[0]
output_name = node.output[0] output_name = node.output[0]
if output_name in input_refs: if output_name in input_refs:
processed = skip_node_forward(ret_nodes, output_name, input_name, input_refs) processed = skip_node_forward(ret_nodes, output_name, input_name,
input_refs)
elif input_name in output_refs: elif input_name in output_refs:
processed = skip_node_backward(ret_nodes, input_name, output_name, output_refs) processed = skip_node_backward(ret_nodes, input_name, output_name,
output_refs)
else: else:
processed = -1 processed = -1
if processed > 0: if processed > 0:
nodes_to_remove.append(node_idx) nodes_to_remove.append(node_idx)
logger.debug('skip op %d: %s -> %s -> %s', logger.debug('skip op %d: %s -> %s -> %s', node_idx, input_name,
node_idx, input_name, node.op_type, output_name) node.op_type, output_name)
elif processed == 0: elif processed == 0:
logger.warning('weird, no node processed') logger.warning('weird, no node processed')
else: else:
...@@ -342,8 +349,7 @@ def optimize_model_skip_op_for_inference( ...@@ -342,8 +349,7 @@ def optimize_model_skip_op_for_inference(
return ret return ret
def optimize_model_strip_initializer(model, def optimize_model_strip_initializer(model, keep_input_only=True):
keep_input_only=True):
""" """
strip weights for inference strip weights for inference
""" """
...@@ -354,7 +360,8 @@ def optimize_model_strip_initializer(model, ...@@ -354,7 +360,8 @@ def optimize_model_strip_initializer(model,
ret = type(model)() ret = type(model)()
ret.CopyFrom(model) ret.CopyFrom(model)
ret.graph.ClearField('value_info') # WORKAROUND: onnx do not drop old value_info ret.graph.ClearField(
'value_info') # WORKAROUND: onnx do not drop old value_info
# strip initializers # strip initializers
ret.graph.ClearField('initializer') ret.graph.ClearField('initializer')
...@@ -366,8 +373,7 @@ def optimize_model_strip_initializer(model, ...@@ -366,8 +373,7 @@ def optimize_model_strip_initializer(model,
elif not keep_input_only and name in output_refs: elif not keep_input_only and name in output_refs:
ret_initializers.add().CopyFrom(initializer) ret_initializers.add().CopyFrom(initializer)
else: else:
logger.debug('initializer %s(%s[%d]) stripped', logger.debug('initializer %s(%s[%d]) stripped', name,
name,
TENSOR_TYPE_TO_NP_TYPE[initializer.data_type], TENSOR_TYPE_TO_NP_TYPE[initializer.data_type],
len(initializer.raw_data)) len(initializer.raw_data))
...@@ -379,10 +385,10 @@ def optimize_model_strip_initializer(model, ...@@ -379,10 +385,10 @@ def optimize_model_strip_initializer(model,
if name in input_refs or name in out_names: if name in input_refs or name in out_names:
ret_inputs.add().CopyFrom(item) ret_inputs.add().CopyFrom(item)
else: else:
logger.debug('input %s(%s%s) stripped', logger.debug(
name, 'input %s(%s%s) stripped', name,
TENSOR_TYPE_TO_NP_TYPE[item.type.tensor_type.elem_type], TENSOR_TYPE_TO_NP_TYPE[item.type.tensor_type.elem_type],
tensor_shape(item)) tensor_shape(item))
return ret return ret
...@@ -397,18 +403,19 @@ def optimize_model_cast(model): ...@@ -397,18 +403,19 @@ def optimize_model_cast(model):
ret = type(model)() ret = type(model)()
ret.CopyFrom(model) ret.CopyFrom(model)
ret.graph.ClearField('value_info') # WORKAROUND: onnx do not drop old value_info ret.graph.ClearField(
'value_info') # WORKAROUND: onnx do not drop old value_info
ret_nodes = ret.graph.node ret_nodes = ret.graph.node
nodes_to_remove = [] nodes_to_remove = []
for node_idx, node in enumerate(nodes): for node_idx, node in enumerate(nodes):
if not(node.domain == DEFAULT_OP_DOMAIN or node.domain == ''): if not (node.domain == DEFAULT_OP_DOMAIN or node.domain == ''):
continue continue
if not(node.op_type == 'Cast'): if not (node.op_type == 'Cast'):
continue continue
attrs = node_attrs(node) attrs = node_attrs(node)
output_dtype = TENSOR_TYPE_TO_NP_TYPE[attrs['to']] output_dtype = TENSOR_TYPE_TO_NP_TYPE[attrs['to']]
input_name = node.input[0] input_name = node.input[0]
info = value_info.get('input_name', None) # relax for un-inferrable info = value_info.get('input_name', None) # relax for un-inferrable
if info is None: if info is None:
continue continue
input_dtype = info.get('dtype', None) input_dtype = info.get('dtype', None)
...@@ -417,21 +424,23 @@ def optimize_model_cast(model): ...@@ -417,21 +424,23 @@ def optimize_model_cast(model):
output_name = node.output[0] output_name = node.output[0]
if output_name in input_refs: if output_name in input_refs:
processed = skip_node_forward(ret_nodes, output_name, input_name, input_refs) processed = skip_node_forward(ret_nodes, output_name, input_name,
input_refs)
elif input_name in output_refs: elif input_name in output_refs:
processed = skip_node_backward(ret_nodes, input_name, output_name, output_refs) processed = skip_node_backward(ret_nodes, input_name, output_name,
output_refs)
else: else:
processed = -1 processed = -1
if processed > 0: if processed > 0:
nodes_to_remove.append(node_idx) nodes_to_remove.append(node_idx)
logger.debug('skip %s: %s -> %s Cast op', logger.debug('skip %s: %s -> %s Cast op', node.name, input_dtype,
node.name, input_dtype, output_dtype) output_dtype)
elif processed == 0: elif processed == 0:
logger.warning('weird, no node processed') logger.warning('weird, no node processed')
else: else:
logger.debug('keep standalone %s: %s -> %s Cast op', logger.debug('keep standalone %s: %s -> %s Cast op', node.name,
node.name, input_dtype, output_dtype) input_dtype, output_dtype)
nodes_to_remove.sort(reverse=True) nodes_to_remove.sort(reverse=True)
for node_idx in nodes_to_remove: for node_idx in nodes_to_remove:
...@@ -452,13 +461,14 @@ def optimize_model_slice(model): ...@@ -452,13 +461,14 @@ def optimize_model_slice(model):
chain = [] chain = []
while True: while True:
node = nodes[node_idx] node = nodes[node_idx]
if not(node.domain == DEFAULT_OP_DOMAIN or node.domain == ''): if not (node.domain == DEFAULT_OP_DOMAIN or node.domain == ''):
return chain return chain
if not node.op_type == 'Slice': if not node.op_type == 'Slice':
return chain return chain
chain.append(node_idx) chain.append(node_idx)
output_name = node.output[0] output_name = node.output[0]
if output_name not in input_refs or len(input_refs[output_name]) != 1: if output_name not in input_refs or len(
input_refs[output_name]) != 1:
return chain return chain
node_idx = list(input_refs[output_name])[0] node_idx = list(input_refs[output_name])[0]
...@@ -468,7 +478,8 @@ def optimize_model_slice(model): ...@@ -468,7 +478,8 @@ def optimize_model_slice(model):
for slice_node_idx in slice_chain: for slice_node_idx in slice_chain:
node = nodes[slice_node_idx] node = nodes[slice_node_idx]
attrs = node_attrs(node) attrs = node_attrs(node)
for axis, start, end in zip(attrs['axes'], attrs['starts'], attrs['ends']): for axis, start, end in zip(attrs['axes'], attrs['starts'],
attrs['ends']):
if start == 0 and end == ONNX_INT_MAX: if start == 0 and end == ONNX_INT_MAX:
continue continue
if axis in merged_slice: if axis in merged_slice:
...@@ -480,7 +491,8 @@ def optimize_model_slice(model): ...@@ -480,7 +491,8 @@ def optimize_model_slice(model):
ret = type(model)() ret = type(model)()
ret.CopyFrom(model) ret.CopyFrom(model)
ret.graph.ClearField('value_info') # WORKAROUND: onnx do not drop old value_info ret.graph.ClearField(
'value_info') # WORKAROUND: onnx do not drop old value_info
ret_nodes = ret.graph.node ret_nodes = ret.graph.node
nodes_to_remove = [] nodes_to_remove = []
for node_idx in range(len(nodes)): for node_idx in range(len(nodes)):
...@@ -488,7 +500,7 @@ def optimize_model_slice(model): ...@@ -488,7 +500,7 @@ def optimize_model_slice(model):
if len(slice_chain) == 0: if len(slice_chain) == 0:
continue continue
merged_slice = _merge_slice(slice_chain) merged_slice = _merge_slice(slice_chain)
if len(merged_slice) > 0 and len(slice_chain) == 1: # no need to merge if len(merged_slice) > 0 and len(slice_chain) == 1: # no need to merge
continue continue
attrs = dict(axes=[], starts=[], ends=[]) attrs = dict(axes=[], starts=[], ends=[])
...@@ -501,42 +513,50 @@ def optimize_model_slice(model): ...@@ -501,42 +513,50 @@ def optimize_model_slice(model):
input_name = first_node.input[0] input_name = first_node.input[0]
output_name = last_node.output[0] output_name = last_node.output[0]
processed = -1 processed = -1
if output_name in input_refs: # 0, [1...] if output_name in input_refs: # 0, [1...]
new_input_name = first_node.output[0] if len(merged_slice) > 0 else input_name new_input_name = first_node.output[0] if len(
processed = skip_node_forward(ret_nodes, output_name, new_input_name, input_refs) merged_slice) > 0 else input_name
processed = skip_node_forward(ret_nodes, output_name,
new_input_name, input_refs)
if processed > 0: if processed > 0:
if len(merged_slice) > 0: if len(merged_slice) > 0:
remain_idx = slice_chain[0] remain_idx = slice_chain[0]
remove_chain = slice_chain[1:] remove_chain = slice_chain[1:]
slice_node = ret_nodes[remain_idx] slice_node = ret_nodes[remain_idx]
for attr in slice_node.attribute: for attr in slice_node.attribute:
attr.CopyFrom(make_attribute(attr.name, attrs[attr.name])) attr.CopyFrom(
make_attribute(attr.name, attrs[attr.name]))
logger.debug('merged slice chain %s -> %s%s -> %s', logger.debug('merged slice chain %s -> %s%s -> %s',
input_name, remain_idx, remove_chain, output_name) input_name, remain_idx, remove_chain,
output_name)
else: else:
remove_chain = slice_chain remove_chain = slice_chain
if processed < 0 and input_name in output_refs: if processed < 0 and input_name in output_refs:
new_output_name = last_node.input[0] if len(merged_slice) > 0 else output_name new_output_name = last_node.input[0] if len(
processed = skip_node_backward(ret_nodes, input_name, new_output_name, output_refs) merged_slice) > 0 else output_name
processed = skip_node_backward(ret_nodes, input_name,
new_output_name, output_refs)
if processed > 0: if processed > 0:
if len(merged_slice) > 0: if len(merged_slice) > 0:
remain_idx = slice_chain[-1] remain_idx = slice_chain[-1]
remove_chain = slice_chain[:-1] remove_chain = slice_chain[:-1]
slice_node = ret_nodes[remain_idx] slice_node = ret_nodes[remain_idx]
for attr in slice_node.attribute: for attr in slice_node.attribute:
attr.CopyFrom(make_attribute(attr.name, attrs[attr.name])) attr.CopyFrom(
make_attribute(attr.name, attrs[attr.name]))
logger.debug('merged slice chain %s -> %s%s -> %s', logger.debug('merged slice chain %s -> %s%s -> %s',
input_name, remove_chain, remain_idx, output_name) input_name, remove_chain, remain_idx,
output_name)
else: else:
remove_chain = slice_chain remove_chain = slice_chain
if processed > 0: if processed > 0:
nodes_to_remove.extend(remove_chain) nodes_to_remove.extend(remove_chain)
if len(merged_slice) == 0: if len(merged_slice) == 0:
logger.debug('skip slice chain %s -> %s -> %s', logger.debug('skip slice chain %s -> %s -> %s', input_name,
input_name, slice_chain, output_name) slice_chain, output_name)
elif processed < 0: # NEVERFIX: not merge standalone slice chain elif processed < 0: # NEVERFIX: not merge standalone slice chain
logger.debug('keep standalone slice chain %s -> %s -> %s', logger.debug('keep standalone slice chain %s -> %s -> %s',
input_name, slice_chain, output_name) input_name, slice_chain, output_name)
...@@ -549,9 +569,10 @@ def optimize_model_slice(model): ...@@ -549,9 +569,10 @@ def optimize_model_slice(model):
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig( logging.basicConfig(
format='[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s', format=
level=logging.DEBUG, '[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s',
) level=logging.DEBUG,
)
from onnx.checker import check_model from onnx.checker import check_model
from onnx.utils import polish_model from onnx.utils import polish_model
......
...@@ -24,8 +24,7 @@ def _ensure_tuple(obj): ...@@ -24,8 +24,7 @@ def _ensure_tuple(obj):
return (obj, ) return (obj, )
def _flatten_list(obj, def _flatten_list(obj, out=None):
out=None):
assert isinstance(obj, list) assert isinstance(obj, list)
if out is None: if out is None:
out = type(obj)() out = type(obj)()
...@@ -37,8 +36,7 @@ def _flatten_list(obj, ...@@ -37,8 +36,7 @@ def _flatten_list(obj,
return out return out
def export_data(state_dict, def export_data(state_dict, prefix=''):
prefix=''):
""" """
export binary data with meta text for raw C++ inference engines export binary data with meta text for raw C++ inference engines
""" """
...@@ -65,10 +63,14 @@ def export_data(state_dict, ...@@ -65,10 +63,14 @@ def export_data(state_dict,
fp.close() fp.close()
def export_onnx_with_validation(model, inputs, export_basepath, def export_onnx_with_validation(model,
input_names=None, output_names=None, inputs,
export_basepath,
input_names=None,
output_names=None,
use_npz=True, use_npz=True,
*args, **kwargs): *args,
**kwargs):
""" """
export PyTorch model to ONNX model and export sample inputs and outputs in a Numpy file export PyTorch model to ONNX model and export sample inputs and outputs in a Numpy file
""" """
...@@ -95,12 +97,16 @@ def export_onnx_with_validation(model, inputs, export_basepath, ...@@ -95,12 +97,16 @@ def export_onnx_with_validation(model, inputs, export_basepath,
ret[key] = value ret[key] = value
return ret return ret
torch_inputs = _ensure_tuple(inputs) # WORKAROUND: for torch.onnx torch_inputs = _ensure_tuple(inputs) # WORKAROUND: for torch.onnx
outputs = torch.onnx.export(model, torch_inputs, export_basepath + '.onnx', outputs = torch.onnx.export(
input_names=_flatten_list(input_names), model,
output_names=_flatten_list(output_names), torch_inputs,
*args, **kwargs) export_basepath + '.onnx',
if outputs is None: # WORKAROUND: for torch.onnx input_names=_flatten_list(input_names),
output_names=_flatten_list(output_names),
*args,
**kwargs)
if outputs is None: # WORKAROUND: for torch.onnx
outputs = model(*inputs) outputs = model(*inputs)
torch_outputs = _ensure_tuple(outputs) torch_outputs = _ensure_tuple(outputs)
......
...@@ -13,8 +13,7 @@ import os ...@@ -13,8 +13,7 @@ import os
import sys import sys
def _flatten_dict(obj, def _flatten_dict(obj, out=None):
out=None):
assert isinstance(obj, dict) assert isinstance(obj, dict)
if out is None: if out is None:
out = type(obj)() out = type(obj)()
...@@ -34,12 +33,13 @@ def _ensure_list(obj): ...@@ -34,12 +33,13 @@ def _ensure_list(obj):
return [obj] return [obj]
def validate(paddle_model_filename, golden_data_filename, def validate(fluid_model_filename,
golden_data_filename,
model_func_name='inference', model_func_name='inference',
precision=1e-4, precision=1e-4,
save_inference_model=False): save_inference_model=False):
""" """
inferece the converted Paddle model, validate with given golden data inferece the converted Paddle fluid model, validate with given golden data
""" """
import numpy as np import numpy as np
...@@ -52,17 +52,17 @@ def validate(paddle_model_filename, golden_data_filename, ...@@ -52,17 +52,17 @@ def validate(paddle_model_filename, golden_data_filename,
exe.run(fluid.default_startup_program()) exe.run(fluid.default_startup_program())
# load model # load model
paddle_model_dir, basename = os.path.split(paddle_model_filename) fluid_model_dir, basename = os.path.split(fluid_model_filename)
if basename == '__model__': # is desc model if basename == '__model__': # is desc model
logger.debug('using desc file %s', basename) logger.debug('using desc file %s', basename)
prog, in_names, var_outs = fluid.io.load_inference_model(paddle_model_dir, exe) prog, _, var_outs = fluid.io.load_inference_model(fluid_model_dir, exe)
out_names = var_outs # HINT: pass var if fetch ops already created out_names = var_outs # HINT: pass var if fetch ops already created
logger.info('model load passed') logger.info('model load passed')
elif basename.endswith('.py'): # is python code elif basename.endswith('.py'): # is python code
logger.debug('using python code file %s', basename) logger.debug('using python code file %s', basename)
module_name, _ = os.path.splitext(basename) module_name, _ = os.path.splitext(basename)
sys_path = sys.path.copy() sys_path = sys.path.copy()
sys.path.append(paddle_model_dir) sys.path.append(fluid_model_dir)
try: try:
module = importlib.import_module(module_name) module = importlib.import_module(module_name)
func = getattr(module, model_func_name) func = getattr(module, model_func_name)
...@@ -71,18 +71,21 @@ def validate(paddle_model_filename, golden_data_filename, ...@@ -71,18 +71,21 @@ def validate(paddle_model_filename, golden_data_filename,
module = importlib.import_module(module_name) module = importlib.import_module(module_name)
func = getattr(module, model_func_name) func = getattr(module, model_func_name)
sys.path = sys_path sys.path = sys_path
logger.debug('from %s imported %s: %s', module_name, model_func_name, func) logger.debug('from %s imported %s: %s', module_name, model_func_name,
func)
var_outs = func() var_outs = func()
var_outs = _ensure_list(var_outs) var_outs = _ensure_list(var_outs)
out_names = [var.name for var in var_outs] # HINT: pass string to create fetch ops out_names = [var.name for var in var_outs
] # HINT: pass string to create fetch ops
logger.info('import passed') logger.info('import passed')
prog = fluid.default_main_program() prog = fluid.default_main_program()
fluid.io.load_persistables(executor=exe, dirname=paddle_model_dir, main_program=prog) fluid.io.load_persistables(
executor=exe, dirname=fluid_model_dir, main_program=prog)
logger.info('weight load passed') logger.info('weight load passed')
else: else:
raise ValueError('unsupported Paddle model') raise ValueError('unsupported Paddle fluid model')
# load data # load data
logger.info('using golden data %s', golden_data_filename) logger.info('using golden data %s', golden_data_filename)
...@@ -100,10 +103,15 @@ def validate(paddle_model_filename, golden_data_filename, ...@@ -100,10 +103,15 @@ def validate(paddle_model_filename, golden_data_filename,
# DEBUG: reload test for python code # DEBUG: reload test for python code
if basename.endswith('.py') and save_inference_model: if basename.endswith('.py') and save_inference_model:
fluid.io.save_inference_model(paddle_model_dir, input_data.keys(), var_outs, exe, fluid.io.save_inference_model(
main_program=prog, export_for_deployment=True) fluid_model_dir,
input_data.keys(),
var_outs,
exe,
main_program=prog,
export_for_deployment=True)
logger.info('model re-save passed') logger.info('model re-save passed')
fluid.io.load_inference_model(paddle_model_dir, exe) fluid.io.load_inference_model(fluid_model_dir, exe)
logger.info('model re-load passed') logger.info('model re-load passed')
# execute # execute
...@@ -124,49 +132,54 @@ def validate(paddle_model_filename, golden_data_filename, ...@@ -124,49 +132,54 @@ def validate(paddle_model_filename, golden_data_filename,
else: else:
logger.info('accuracy not passed') logger.info('accuracy not passed')
# globals().update(locals()) # globals().update(locals())
return passed return passed
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig( import argparse
format='[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s',
level=logging.DEBUG, parser = argparse.ArgumentParser(
) description='onnx2fluid.validate',
logger = logging.getLogger('validation_test') formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
model_rc_list = [ parser.add_argument(
'../examples/t{}/model.py', 'model',
'../examples/t{}/__model__', nargs=1,
'../examples/t{}.embeded/model.py', help='path to model.py or __model__',
'../examples/t{}.embeded/__model__', )
] parser.add_argument(
'--debug',
import numpy as np '-d',
action='store_true',
idx_model = np.random.randint(1, 7) help='enable debug logging and checking',
model = np.random.choice(model_rc_list).format(idx_model) )
precision = 10 ** (np.random.rand() * -4 - 2) parser.add_argument(
debug = False '--test_data',
'-t',
model = '/tmp/export/model.py' type=str,
# model = '../examples/t1/__model__' help='I/O golden data for validation, e.g. test.npy, test.npz',
# model = '../examples/t1.embeded/model.py' )
# model = '../examples/t1.embeded/__model__' parser.add_argument(
debug = True '--precision',
'-p',
logger.info('args: %s %.6f', model, precision) type=int,
default=4,
data_dir, dir_name = os.path.split(os.path.split(model)[0]) help='assertion decimal for validation',
data_pathname = os.path.splitext(dir_name)[0] )
args = parser.parse_args()
# proto debug test
from framework_pb2 import ProgramDesc logging_format = '[%(levelname)8s]%(name)s::%(funcName)s:%(lineno)04d: %(message)s'
pd = ProgramDesc() logging_level = logging.DEBUG if args.debug else logging.INFO
pd.ParseFromString(open(os.path.join(data_dir, dir_name, '__model__'), 'rb').read()) logging.basicConfig(format=logging_format, level=logging_level)
# validate debug = args.debug
# validate(model, os.path.join(data_dir, data_pathname + '.npz'), fluid_model_filename = args.model[0]
# precision=precision, save_inference_model=debug) golden_data_filename = args.test_data
validate(model, '../examples/bvlc_alexnet/test_data_0.npz', precision = args.precision
precision=precision, save_inference_model=debug)
validate(
fluid_model_filename,
golden_data_filename,
precision=precision,
save_inference_model=debug)
...@@ -34,15 +34,13 @@ except ImportError: ...@@ -34,15 +34,13 @@ except ImportError:
logger.warning('importing paddle.fluid.proto.framework_pb2d failed,' logger.warning('importing paddle.fluid.proto.framework_pb2d failed,'
'using fallback framework_pb2') 'using fallback framework_pb2')
__all__ = [ __all__ = [
'Program', 'Program',
'Writer', 'Writer',
] ]
def _irepr(obj, def _irepr(obj, to='_'):
to='_'):
"""inline repr""" """inline repr"""
s = repr(obj) s = repr(obj)
...@@ -53,8 +51,7 @@ def _irepr(obj, ...@@ -53,8 +51,7 @@ def _irepr(obj,
return s return s
def _flatten_list(obj, def _flatten_list(obj, out=None):
out=None):
if out is None: if out is None:
out = type(obj)() out = type(obj)()
for item in obj: for item in obj:
...@@ -72,7 +69,7 @@ def make_attr_name(name): ...@@ -72,7 +69,7 @@ def make_attr_name(name):
if name == '': if name == '':
raise ValueError('name should not be empty') raise ValueError('name should not be empty')
for s in ' *?\/-:': # for s in ' *?\\/-:': #
name = name.replace(s, '_') name = name.replace(s, '_')
if not name.startswith('_'): if not name.startswith('_'):
name = '_' + name name = '_' + name
...@@ -85,15 +82,15 @@ class Program(object): ...@@ -85,15 +82,15 @@ class Program(object):
""" """
DTYPE_TO_FRAMEWORK_DTYPE = { DTYPE_TO_FRAMEWORK_DTYPE = {
'bool': framework_pb2.VarType.BOOL, 'bool': framework_pb2.VarType.BOOL,
'int8': framework_pb2.VarType.INT8, 'int8': framework_pb2.VarType.INT8,
'uint8': framework_pb2.VarType.UINT8, 'uint8': framework_pb2.VarType.UINT8,
'int16': framework_pb2.VarType.INT16, 'int16': framework_pb2.VarType.INT16,
'int32': framework_pb2.VarType.INT32, 'int32': framework_pb2.VarType.INT32,
'int64': framework_pb2.VarType.INT64, 'int64': framework_pb2.VarType.INT64,
'float16': framework_pb2.VarType.FP16, 'float16': framework_pb2.VarType.FP16,
'float32': framework_pb2.VarType.FP32, 'float32': framework_pb2.VarType.FP32,
'float64': framework_pb2.VarType.FP64 'float64': framework_pb2.VarType.FP64
} }
@staticmethod @staticmethod
...@@ -116,7 +113,7 @@ class Program(object): ...@@ -116,7 +113,7 @@ class Program(object):
od_var = framework_pb2.OpDesc.Var() od_var = framework_pb2.OpDesc.Var()
od_var.parameter = key od_var.parameter = key
if idx < len(vals): if idx < len(vals):
od_var.arguments.append(vals[idx]) # od_var.arguments.append(vals[idx]) #
od_vars.append(od_var) od_vars.append(od_var)
return od_vars return od_vars
...@@ -130,10 +127,10 @@ class Program(object): ...@@ -130,10 +127,10 @@ class Program(object):
for key, value in attrs.items(): for key, value in attrs.items():
od_attr = framework_pb2.OpDesc.Attr() od_attr = framework_pb2.OpDesc.Attr()
od_attr.name = key od_attr.name = key
if isinstance(value, bool): # bool.mro() = [bool, int, object] if isinstance(value, bool): # bool.mro() = [bool, int, object]
od_attr.type = framework_pb2.BOOLEAN od_attr.type = framework_pb2.BOOLEAN
od_attr.b = value od_attr.b = value
elif isinstance(value, int): # only cast to int32 elif isinstance(value, int): # only cast to int32
od_attr.type = framework_pb2.INT od_attr.type = framework_pb2.INT
od_attr.i = value od_attr.i = value
elif isinstance(value, float): elif isinstance(value, float):
...@@ -143,10 +140,10 @@ class Program(object): ...@@ -143,10 +140,10 @@ class Program(object):
od_attr.type = framework_pb2.STRING od_attr.type = framework_pb2.STRING
od_attr.s = value od_attr.s = value
elif isinstance(value, list) and len(value) > 0: elif isinstance(value, list) and len(value) > 0:
if isinstance(value, bool): # bool.mro() = [bool, int, object] if isinstance(value, bool): # bool.mro() = [bool, int, object]
od_attr.type = framework_pb2.BOOLEANS od_attr.type = framework_pb2.BOOLEANS
od_attr.bools.extend(value) od_attr.bools.extend(value)
elif isinstance(value[0], int): # only cast to int32 list elif isinstance(value[0], int): # only cast to int32 list
od_attr.type = framework_pb2.INTS od_attr.type = framework_pb2.INTS
od_attr.ints.extend(value) od_attr.ints.extend(value)
elif isinstance(value[0], float): elif isinstance(value[0], float):
...@@ -168,11 +165,8 @@ class Program(object): ...@@ -168,11 +165,8 @@ class Program(object):
return ('Program(code mutable: {}) with:\n' return ('Program(code mutable: {}) with:\n'
'codes: {}\n' 'codes: {}\n'
'op_descs: {}\n' 'op_descs: {}\n'
'var_descs: {}\n').format( 'var_descs: {}\n').format(self.code_mutable, self.codes,
self.code_mutable, self.op_descs, self.var_descs)
self.codes,
self.op_descs,
self.var_descs)
def __repr__(self): def __repr__(self):
return self.__str__() return self.__str__()
...@@ -185,8 +179,11 @@ class Program(object): ...@@ -185,8 +179,11 @@ class Program(object):
if self.code_mutable: if self.code_mutable:
self.codes.append(code) self.codes.append(code)
def OpDesc(self, name, def OpDesc(self,
input_val_keys=None, output_val_keys=None, attrs=None): name,
input_val_keys=None,
output_val_keys=None,
attrs=None):
""" """
add OpDesc add OpDesc
""" """
...@@ -202,10 +199,15 @@ class Program(object): ...@@ -202,10 +199,15 @@ class Program(object):
self.op_descs.append(desc) self.op_descs.append(desc)
return desc return desc
def VarDesc(self, name, def VarDesc(self,
persistable=False, value_info=None, remove_batch=None): name,
persistable=False,
value_info=None,
remove_batch=None,
dummy_dtype='float32'):
""" """
add VarDesc add VarDesc,
dummy_dtype: WORKAROUND for Netron viewer
""" """
var_desc = framework_pb2.VarDesc() var_desc = framework_pb2.VarDesc()
...@@ -213,14 +215,19 @@ class Program(object): ...@@ -213,14 +215,19 @@ class Program(object):
var_desc.persistable = persistable var_desc.persistable = persistable
var_desc.type.type = framework_pb2.VarType.LOD_TENSOR var_desc.type.type = framework_pb2.VarType.LOD_TENSOR
# REMOVEIT: WORKAROUND: Netron: null.tensor error
tensor_desc = var_desc.type.lod_tensor.tensor
tensor_desc.data_type = self.Dtype(dummy_dtype) # required
if value_info and 'dtype' in value_info: if value_info and 'dtype' in value_info:
tensor_desc = var_desc.type.lod_tensor.tensor tensor_desc = var_desc.type.lod_tensor.tensor
tensor_desc.data_type = self.Dtype(value_info['dtype']) # required tensor_desc.data_type = self.Dtype(value_info['dtype']) # required
if 'shape' in value_info: if 'shape' in value_info:
tensor_desc.dims.extend(value_info['shape']) tensor_desc.dims.extend(value_info['shape'])
if len(value_info['shape']) > 0: # skip scalars if len(value_info['shape']) > 0: # skip scalars
if remove_batch is None: if remove_batch is None:
remove_batch = value_info.get('remove_batch', not persistable) remove_batch = value_info.get('remove_batch',
not persistable)
if remove_batch: if remove_batch:
tensor_desc.dims[0] = -1 tensor_desc.dims[0] = -1
...@@ -231,7 +238,7 @@ class Program(object): ...@@ -231,7 +238,7 @@ class Program(object):
convert an ONNX op and add it to program convert an ONNX op and add it to program
""" """
if domain != '': # TODO: symbolic file routing by domain if domain != '': # TODO: symbolic file routing by domain
raise ValueError('only default domain supported') raise ValueError('only default domain supported')
if op_type in symbolic.DEFAULT_OP_MAPPING: if op_type in symbolic.DEFAULT_OP_MAPPING:
...@@ -240,8 +247,8 @@ class Program(object): ...@@ -240,8 +247,8 @@ class Program(object):
fn = getattr(symbolic, op_type) fn = getattr(symbolic, op_type)
fn(self, *args, **kwargs) fn(self, *args, **kwargs)
else: else:
raise ValueError('conversion for {}::{} not supported' raise ValueError('conversion for {}::{} not supported'.format(
.format(domain, op_type)) domain, op_type))
def IntermediateOp(self, domain, op_type, *args, **kwargs): def IntermediateOp(self, domain, op_type, *args, **kwargs):
""" """
...@@ -267,14 +274,15 @@ class Writer(object): ...@@ -267,14 +274,15 @@ class Writer(object):
CODE_INDENT = ' ' * 4 CODE_INDENT = ' ' * 4
@staticmethod @staticmethod
def header_code(func_name): def header_code(func_name, info=''):
""" """
Python header codes Python header codes
""" """
codes = list() codes = list()
codes.append('"""') codes.append('"""')
codes.append('This code is generated by onnx2paddle.') codes.append('This code is generated by onnx2fluid.')
codes.append('{}'.format(info))
codes.append('"""') codes.append('"""')
codes.append('') codes.append('')
codes.append('from __future__ import division') codes.append('from __future__ import division')
...@@ -287,16 +295,25 @@ class Writer(object): ...@@ -287,16 +295,25 @@ class Writer(object):
return codes return codes
@staticmethod @staticmethod
def emit_op(prog, name, domain, op_type, inputs, outputs, attrs, value_infos, *args, **kwargs): def emit_op(prog, name, domain, op_type, inputs, outputs, attrs,
value_infos, *args, **kwargs):
""" """
emit an ONNX op into program emit an ONNX op into program
""" """
prog.Code('# {}, {}::{}: {} -> {}, {}' prog.Code('# {}, {}::{}: {} -> {}, {}'.format(name, domain, op_type,
.format(name, domain, op_type, inputs, outputs, _irepr(attrs, to=', '))) inputs, outputs,
prog.Op(domain, op_type, inputs, outputs, attrs, _irepr(attrs, to=', ')))
value_infos=value_infos, name=name, prog.Op(
*args, **kwargs) domain,
op_type,
inputs,
outputs,
attrs,
value_infos=value_infos,
name=name,
*args,
**kwargs)
@staticmethod @staticmethod
def emit_param(prog, name, value_info): def emit_param(prog, name, value_info):
...@@ -313,18 +330,18 @@ class Writer(object): ...@@ -313,18 +330,18 @@ class Writer(object):
var_name = make_var_name(name) var_name = make_var_name(name)
attr_name = make_attr_name(name) attr_name = make_attr_name(name)
prog.Code('# parameter: {}'.format(name)) prog.Code('# parameter: {}'.format(name))
prog.Code('{} = ParamAttr(name={})' # , trainable=True prog.Code('{} = ParamAttr(name={})' # , trainable=True
.format(attr_name, repr(var_name))) .format(attr_name, repr(var_name)))
prog.Code('{} = layers.create_parameter(shape={}, dtype={}, name={}, attr={}' prog.Code(
', default_initializer=initializer.Constant(0))' #, is_bias={} '{} = layers.create_parameter(shape={}, dtype={}, name={}, attr={}'
.format(var_name, ', default_initializer=initializer.Constant(0))' #, is_bias={}
value_info['shape'], repr(value_info['dtype'].name), .format(var_name, value_info['shape'],
repr(name), attr_name)) #, value_info.get('is_bias', False))) repr(value_info['dtype'].name), repr(name),
attr_name)) #, value_info.get('is_bias', False)))
prog.VarDesc(var_name, persistable=True, value_info=value_info) prog.VarDesc(var_name, persistable=True, value_info=value_info)
@staticmethod @staticmethod
def emit_inputs(prog, names, value_infos, def emit_inputs(prog, names, value_infos, remove_batch=None):
remove_batch=None):
""" """
emit ONNX inputs into program emit ONNX inputs into program
""" """
...@@ -334,27 +351,33 @@ class Writer(object): ...@@ -334,27 +351,33 @@ class Writer(object):
value_info = value_infos[name] value_info = value_infos[name]
shape = value_info['shape'] shape = value_info['shape']
if remove_batch is None: if remove_batch is None:
remove_batch = value_info.get('remove_batch', True) # HINT: True by default ? remove_batch = value_info.get('remove_batch',
True) # HINT: True by default ?
if remove_batch: if remove_batch:
shape = shape[1:] shape = shape[1:]
prog.Code('# input: {}'.format(name)) prog.Code('# input: {}'.format(name))
prog.Code(('{} = layers.data(name={}, shape={}, dtype={}, ' prog.Code((
'append_batch_size={})' # , stop_gradient=True '{} = layers.data(name={}, shape={}, dtype={}, '
).format(var_name, repr(name), 'append_batch_size={})' # , stop_gradient=True
shape, ).format(
repr(value_info['dtype'].name), var_name,
remove_batch, repr(name),
)) shape,
prog.OpDesc('feed', repr(value_info['dtype'].name),
(['feed'], 'X'), remove_batch,
([var_name], 'Out'), ))
dict(col=idx), prog.OpDesc(
) 'feed',
prog.VarDesc(var_name, value_info=value_info, remove_batch=remove_batch) (['feed'], 'X'),
([var_name], 'Out'),
dict(col=idx),
)
prog.VarDesc(
var_name, value_info=value_info, remove_batch=remove_batch)
@staticmethod @staticmethod
def emit_outputs(prog, names): #, value_infos def emit_outputs(prog, names): #, value_infos
""" """
emit ONNX outputs into program emit ONNX outputs into program
""" """
...@@ -364,11 +387,12 @@ class Writer(object): ...@@ -364,11 +387,12 @@ class Writer(object):
var_name = make_var_name(name) var_name = make_var_name(name)
code += var_name + ', ' code += var_name + ', '
prog.OpDesc('fetch', prog.OpDesc(
([var_name], 'X'), 'fetch',
(['fetch'], 'Out'), ([var_name], 'X'),
dict(col=idx), (['fetch'], 'Out'),
) dict(col=idx),
)
# var is emitted over ops # var is emitted over ops
prog.Code(code) prog.Code(code)
...@@ -396,9 +420,9 @@ class Writer(object): ...@@ -396,9 +420,9 @@ class Writer(object):
tensor_desc.dims.extend(weight.shape) tensor_desc.dims.extend(weight.shape)
fp = open(filename, 'wb') fp = open(filename, 'wb')
np.array([0], dtype=np.int32).tofile(fp) # version np.array([0], dtype=np.int32).tofile(fp) # version
np.array([0], dtype=np.int64).tofile(fp) # LOD level np.array([0], dtype=np.int64).tofile(fp) # LOD level
np.array([0], dtype=np.int32).tofile(fp) # tensor version np.array([0], dtype=np.int32).tofile(fp) # tensor version
np.array([tensor_desc.ByteSize()], dtype=np.int32).tofile(fp) np.array([tensor_desc.ByteSize()], dtype=np.int32).tofile(fp)
fp.write(tensor_desc.SerializeToString()) fp.write(tensor_desc.SerializeToString())
weight.tofile(fp) weight.tofile(fp)
...@@ -463,4 +487,4 @@ class Writer(object): ...@@ -463,4 +487,4 @@ class Writer(object):
fp = open(filename, 'wb') fp = open(filename, 'wb')
fp.write(prog_desc.SerializeToString()) fp.write(prog_desc.SerializeToString())
fp.close() fp.close()
logger.debug('saved descs to %s', filename) logger.debug('saved descs to %s', filename)
\ No newline at end of file
-e . -e .
onnx>=1.4.0 onnx>=1.4.0
paddlepaddle paddlepaddle
\ No newline at end of file
...@@ -2,14 +2,14 @@ ...@@ -2,14 +2,14 @@
# https://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files # https://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files
[metadata] [metadata]
# 项目名称,发布、安装时以此作为包名 # 项目名称,发布、安装时以此作为包名
name = onnx2paddle name = onnx2fluid
# 作者姓名和邮箱地址 # 作者姓名和邮箱地址
author = Macrobull author = Macrobull
# author_email = .Github@github.com # author_email = .Github@github.com
# 项目版本号,1.0以上版本才视为正式版 # 项目版本号,1.0以上版本才视为正式版
version = 0.1.0 version = 0.1.0
# 项目概要描述信息,一句话让用户明白项目概要,不支持中文 # 项目概要描述信息,一句话让用户明白项目概要,不支持中文
description = Inference model conversion from ONNX/PyTorch to Paddle description = Inference model conversion from ONNX/PyTorch to Paddle fluid
# 项目的详细描述内容和格式,包括readme和changelog等,通常使用md或rst等格式 # 项目的详细描述内容和格式,包括readme和changelog等,通常使用md或rst等格式
long_description = file: README.md, CHANGELOG.md long_description = file: README.md, CHANGELOG.md
long_description_content_type = text/markdown long_description_content_type = text/markdown
...@@ -25,7 +25,7 @@ classifier = ...@@ -25,7 +25,7 @@ classifier =
Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.5
# 关键字,用于检索,方便用户搜索到你的项目 # 关键字,用于检索,方便用户搜索到你的项目
keywords = keywords =
onnx paddle onnx paddlepaddle
[options] [options]
# 包名称,find:表示自动寻找,可在options.packages.find中进行详细配置 # 包名称,find:表示自动寻找,可在options.packages.find中进行详细配置
...@@ -44,21 +44,21 @@ install_requires = ...@@ -44,21 +44,21 @@ install_requires =
# mock # mock
# 单测代码目录 # 单测代码目录
#test_suite = onnx2paddle.tests #test_suite = onnx2fluid.tests
# 自动添加被版本控制的数据文件 # 自动添加被版本控制的数据文件
include_package_data = True include_package_data = True
# 项目是纯py项目,可以直接执行zip源码包 # 项目是纯py项目,可以直接执行zip源码包
zip_safe = False zip_safe = False
# 可以通过以下配置将指定的函数变成命令行工具,允许用户直接执行 # 可以通过以下配置将指定的函数变成命令行工具,允许用户直接执行
#[options.entry_points] [options.entry_points]
#console_scripts = console_scripts =
# onnx2paddle = onnx2paddle.cmdline:main onnx2fluid = onnx2fluid.cmdline:main
# 可以通过以下配置向包中添加conf或data等非py文件,安装时会一同安装到site-packages目录下 # 可以通过以下配置向包中添加conf或data等非py文件,安装时会一同安装到site-packages目录下
# 仅支持文件,不支持目录,但可以使用通配 # 仅支持文件,不支持目录,但可以使用通配
#[options.package_data] #[options.package_data]
#onnx2paddle = #onnx2fluid =
# conf/* # conf/*
# data/* # data/*
......
...@@ -15,4 +15,3 @@ Date: 2019/02/22 10:25:46 ...@@ -15,4 +15,3 @@ Date: 2019/02/22 10:25:46
import setuptools import setuptools
setuptools.setup() setuptools.setup()
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册