Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added testdata/dnn/onnx/data/input_PReLU_slope.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_expand.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_expand_identity.npy
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_slice_neg_starts.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_split_neg_axis.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_PReLU_slope.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_expand.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_expand_identity.npy
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_slice_neg_starts.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_split_neg_axis.npy
Binary file not shown.
99 changes: 99 additions & 0 deletions testdata/dnn/onnx/generate_onnx_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy as np
import os.path
import onnx
import onnxsim
import google.protobuf.text_format
import io

Expand Down Expand Up @@ -74,6 +75,14 @@ def save_onnx_data_and_model(input, output, name, operation, *args, **kwargs):
model = onnx.helper.make_model(graph, producer_name=name)
onnx.save(model, models_files)

def simplify(name, rename=False, **kwargs):
model, check = onnxsim.simplify(name, **kwargs)
assert check, "couldn't valide"
name = name[:-5]
if rename:
name += '_optimized'
onnx.save(model, name + '.onnx')

torch.manual_seed(0)
np.random.seed(0)

Expand Down Expand Up @@ -129,6 +138,18 @@ def save_onnx_data_and_model(input, output, name, operation, *args, **kwargs):
relu = nn.ReLU(inplace=True)
save_data_and_model("ReLU", input, relu)

class PReLU_slope(nn.Module):
def __init__(self, *args, **kwargs):
super(PReLU_slope, self).__init__()

def forward(self, x):
return nn.PReLU()(x)

model = PReLU_slope()
input_ = Variable(torch.randn(1, 1, 5, 5, dtype=torch.float32))
save_data_and_model("PReLU_slope", input_, model, export_params=True)
simplify('models/PReLU_slope.onnx', False)


input = Variable(torch.randn(2, 3))
dropout = nn.Dropout()
Expand Down Expand Up @@ -414,6 +435,17 @@ def forward(self, x):
save_data_and_model("slice", input, model)
save_data_and_model("slice_opset_11", input, model, version=11)

class SliceStarts(nn.Module):
def __init__(self, *args, **kwargs):
super(SliceStarts, self).__init__()

def forward(self, x):
return x[-1:]

model = SliceStarts()
input_ = Variable(torch.randn(1, 10, dtype=torch.float32))
save_data_and_model("slice_neg_starts", input_, model)

input_2 = Variable(torch.randn(6, 6))
custom_slice_list = [
slice(1, 3, 1),
Expand Down Expand Up @@ -575,6 +607,18 @@ def forward(self, x):
input_ = Variable(torch.tensor(list(range(20)), dtype=torch.float32))
save_data_and_model("split_sizes", input_, model)

class SplitAxis(nn.Module):
def __init__(self, *args, **kwargs):
super(SplitAxis, self).__init__()

def forward(self, x):
tup = torch.split(x, 2, -1)
return torch.cat(tup, 1)

model = SplitAxis()
input_ = Variable(torch.randn(1, 10, dtype=torch.float32))
save_data_and_model("split_neg_axis", input_, model)

class SplitMax(nn.Module):

def __init__(self):
Expand Down Expand Up @@ -865,6 +909,32 @@ def forward(self, x):
output = np.mean(x, axis=2, keepdims=True)
save_onnx_data_and_model(x, output, 'reduce_mean_axis2', 'ReduceMean', axes=(2), keepdims=True)

class Expand(nn.Module):
def __init__(self):
super(Expand, self).__init__()

def forward(self, x):
return x.expand(1, 3, -1, -1, -1)

input = Variable(torch.randn(1, 3, 2, 4))
model = Expand()
model.eval()
save_data_and_model("expand", input, model, export_params=True, version=12)
simplify('models/expand.onnx', False)

class ExpandIdentity(nn.Module):
def __init__(self):
super(ExpandIdentity, self).__init__()

def forward(self, x):
return x.expand(1, 3, -1, -1)

input = Variable(torch.randn(1, 3, 2, 4))
model = ExpandIdentity()
model.eval()
save_data_and_model("expand_identity", input, model, export_params=True, version=12)
simplify('models/expand_identity.onnx', False)

class Expand(nn.Module):
def __init__(self, shape):
super(Expand, self).__init__()
Expand Down Expand Up @@ -933,6 +1003,23 @@ def forward(self, x):
x = Variable(torch.randn(1, 2, 3, 4))
save_data_and_model("reduceL2_subgraph_2", x, model)

class reduceL2_subgraph2_2(nn.Module):
def __init__(self):
super(reduceL2_subgraph2_2, self).__init__()
self.size = torch.Size([1, 3, 2, 4])

def forward(self, x):
norm = torch.norm(x, p=2, dim=1, keepdim=True)
clip = torch.clamp(norm, min=0)
expand = clip.expand([1, 3, 2, 4])
return x / expand

input = Variable(torch.randn(1, 3, 2, 4))
model = reduceL2_subgraph2_2()
model.eval()
save_data_and_model("reduceL2_subgraph2_2", input, model, export_params=True, version=12)
simplify('models/reduceL2_subgraph2_2.onnx', False)

from torchvision.ops.misc import *
n = 3
model = FrozenBatchNorm2d(n)
Expand Down Expand Up @@ -1173,6 +1260,18 @@ def forward(self, x0, x1, x2):
input_2 = Variable(torch.ones(2, 1, 4, 1, dtype=torch.float32))
save_data_and_model_multy_inputs("scale_broadcast", model, input_0, input_1, input_2)

class ScaleBroadcastMid(nn.Module):
def __init__(self, *args, **kwargs):
super(ScaleBroadcastMid, self).__init__()

def forward(self, x0, x1):
return torch.mul(x0, x1)

model = ScaleBroadcastMid()
input_0 = Variable(torch.ones(2, 1, 4, dtype=torch.float32))
input_1 = Variable(torch.ones(2, 5, 4, dtype=torch.float32))
save_data_and_model_multy_inputs("scale_broadcast_mid", model, input_0, input_1)

x = Variable(torch.randn(1, 3, 25))
conv1d = nn.Conv1d(3, 2, kernel_size=3, padding=2, stride=2, dilation=2, bias=False)
save_data_and_model("conv1d", x, conv1d)
Expand Down
Binary file added testdata/dnn/onnx/models/PReLU_slope.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/expand.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/expand_identity.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/reduceL2_subgraph2_2.onnx
Binary file not shown.
19 changes: 19 additions & 0 deletions testdata/dnn/onnx/models/scale_broadcast_mid.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pytorch1.9:t

0
12Mul_0"Multorch-jit-exportZ
0



Z
1



b
2



B
Binary file added testdata/dnn/onnx/models/slice_neg_starts.onnx
Binary file not shown.
22 changes: 22 additions & 0 deletions testdata/dnn/onnx/models/split_neg_axis.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pytorch1.9:�
S
tensor12345Split_0"Split*
axis����������*
split@@@@@�
1
1
2
3
4
56Concat_1"Concat*
axis�torch-jit-exportZ
tensor



b
6



B
Binary file added testdata/dnn/tensorflow/bias_add_1_in.npy
Binary file not shown.
Binary file added testdata/dnn/tensorflow/bias_add_1_net.pb
Binary file not shown.
Binary file added testdata/dnn/tensorflow/bias_add_1_out.npy
Binary file not shown.
13 changes: 13 additions & 0 deletions testdata/dnn/tensorflow/generate_tf_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,19 @@ def pad_depth(x, desired_channels):
input_down = tf.image.resize(conv, size=[hi, wi], method=0, name='resize_down')
save(inp, input_down, 'resize_bilinear_down')
################################################################################
inp = tf.placeholder(tf.float32, [1, None, None, 3], 'input')
biased = tf.nn.bias_add(inp, [1, 2, 3], data_format='NHWC')
resized1 = tf.image.resize(biased, [5, 6])
concat = tf.concat([resized1, biased], 3)
# blob = np.random.standard_normal([1, 5, 6, 3]).astype(tf.float32.as_numpy_dtype())
# writeBlob(blob, 'resize_concat_optimization_in')
save(inp, concat, 'resize_concat_optimization', optimize=False, is_gen_data=False)
################################################################################
inp = tf.placeholder(tf.float32, [1, 2, 3, 4], 'input')
sub = inp - 3.0
sub = 4.0 + sub
save(inp, sub, prefix + 'bias_add_1', optimize=False)
################################################################################

# Uncomment to print the final graph.
# with tf.gfile.FastGFile('fused_batch_norm_net.pb', 'rb') as f:
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.