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
29 changes: 27 additions & 2 deletions testdata/dnn/download_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def verify(self):
break
sha.update(buf)
print(' actual {}'.format(sha.hexdigest()))
return self.sha == sha.hexdigest()
self.sha_actual = sha.hexdigest()
return self.sha == self.sha_actual
except Exception as e:
print(' catch {}'.format(e))

Expand Down Expand Up @@ -82,7 +83,10 @@ def get(self):

print(' done')
print(' file {}'.format(self.filename))
return self.verify()
candidate_verify = self.verify()
if not candidate_verify:
self.handle_bad_download()
return candidate_verify

def download(self):
try:
Expand Down Expand Up @@ -112,6 +116,27 @@ def save(self, r):
print('>', end='')
sys.stdout.flush()

def handle_bad_download(self):
if os.path.exists(self.filename):
# rename file for further investigation
try:
# NB: using `self.sha_actual` may create unbounded number of files
rename_target = self.filename + '.invalid'
# TODO: use os.replace (Python 3.3+)
try:
if os.path.exists(rename_target): # avoid FileExistsError on Windows from os.rename()
os.remove(rename_target)
finally:
os.rename(self.filename, rename_target)
print(' renaming invalid file to ' + rename_target)
except:
import traceback
traceback.print_exc()
finally:
if os.path.exists(self.filename):
print(' deleting invalid file')
os.remove(self.filename)


def GDrive(gid):
def download_gdrive(dst):
Expand Down
Binary file added testdata/dnn/onnx/data/input_dynamic_batch.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_resize_humanseg.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_scale_broadcast_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_scale_broadcast_1.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_scale_broadcast_2.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_dynamic_batch.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_resize_humanseg.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_scale_broadcast.npy
Binary file not shown.
52 changes: 51 additions & 1 deletion testdata/dnn/onnx/generate_onnx_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import torch.nn.functional as F
import tensorflow as tf # version 2.5.0
import tf2onnx # version 1.9.1
import paddle # version 2.1.1
import numpy as np
import os.path
import onnx
Expand Down Expand Up @@ -1159,6 +1160,19 @@ def forward(self, x):
model = Scale()
save_data_and_model("scale", x, model)

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

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

model = ScaleBroadcast()
input_0 = Variable(torch.ones(2, 1, 4, 5, dtype=torch.float32))
input_1 = Variable(torch.ones(1, 4, 1, dtype=torch.float32))
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)

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 Expand Up @@ -1310,6 +1324,19 @@ def forward(self, x):
save_data_and_model("average_pooling_dynamic_axes", input, ave_pool)
postprocess_model("models/average_pooling_dynamic_axes.onnx", [[1, 3, 'height', 'width']])

class DynamicBatch(nn.Module):
def __init__(self, *args, **kwargs):
super(DynamicBatch, self).__init__()
self.pool = nn.MaxPool2d(2, stride=2)

def forward(self, x):
return torch.cat((self.pool(x), torch.ones(2, 3, 1, 2)))

model = DynamicBatch()
input_ = Variable(torch.ones(2, 3, 3, 4, dtype=torch.float32))
save_data_and_model("dynamic_batch", input_, model, export_params=True)
postprocess_model("models/dynamic_batch.onnx", [['batch_size', 3, 3, 4]])

x = Variable(torch.randn(1, 3, 10))
max_pool = nn.MaxPool1d(kernel_size=(5), stride=1, padding=2, dilation=1)
save_data_and_model("maxpooling_1d", x, max_pool)
Expand Down Expand Up @@ -1443,4 +1470,27 @@ def cumsum_reverse(x):
def cumsum_exclusive_1d_reverse(x):
return tf.cumsum(x, exclusive=True, reverse=True)

save_data_and_tf_function(cumsum_exclusive_1d_reverse, "cumsum_1d_exclusive_1_reverse", x)
save_data_and_tf_function(cumsum_exclusive_1d_reverse, "cumsum_1d_exclusive_1_reverse", x)

#paddle2onnx model
class Resize_HumanSeg(paddle.nn.Layer):
def __init__(self, ):
super(Resize_HumanSeg, self).__init__()

def forward(self, x0):
x1 = paddle.nn.functional.interpolate(x0,size=[6,8],mode='bilinear',align_corners=False)
return x1

def save_data_and_paddle_model(model, name, input_data):
model.eval()
np.save(os.path.join("data", "input_" + name + ".npy"), input_data.numpy())
output = model(input_data)
np.save(os.path.join("data", "output_" + name + ".npy"), output.numpy())
inputs = [paddle.static.InputSpec(shape=input_data.shape, dtype="float32")]
paddle.onnx.export(model, "models/" + name,
input_spec=inputs,
opset_version=11)

input_shape = [1, 2, 3, 4]
x = paddle.rand(input_shape, dtype="float32")
save_data_and_paddle_model(Resize_HumanSeg(), "resize_humanseg", x)
Binary file added testdata/dnn/onnx/models/dynamic_batch.onnx
Binary file not shown.
Binary file added testdata/dnn/onnx/models/resize_humanseg.onnx
Binary file not shown.
30 changes: 30 additions & 0 deletions testdata/dnn/onnx/models/scale_broadcast.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pytorch1.9:�

0
13Mul_0"Mul

3
24Mul_1"Multorch-jit-exportZ
0




Z
1



Z
2




b
4




B