diff --git a/testdata/dnn/download_models.py b/testdata/dnn/download_models.py index 2eac3b150..c4fdb34c7 100755 --- a/testdata/dnn/download_models.py +++ b/testdata/dnn/download_models.py @@ -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)) @@ -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: @@ -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): diff --git a/testdata/dnn/onnx/data/input_dynamic_batch.npy b/testdata/dnn/onnx/data/input_dynamic_batch.npy new file mode 100644 index 000000000..6146df46f Binary files /dev/null and b/testdata/dnn/onnx/data/input_dynamic_batch.npy differ diff --git a/testdata/dnn/onnx/data/input_resize_humanseg.npy b/testdata/dnn/onnx/data/input_resize_humanseg.npy new file mode 100644 index 000000000..21953e023 Binary files /dev/null and b/testdata/dnn/onnx/data/input_resize_humanseg.npy differ diff --git a/testdata/dnn/onnx/data/input_scale_broadcast_0.npy b/testdata/dnn/onnx/data/input_scale_broadcast_0.npy new file mode 100644 index 000000000..84e55f5f6 Binary files /dev/null and b/testdata/dnn/onnx/data/input_scale_broadcast_0.npy differ diff --git a/testdata/dnn/onnx/data/input_scale_broadcast_1.npy b/testdata/dnn/onnx/data/input_scale_broadcast_1.npy new file mode 100644 index 000000000..b08129156 Binary files /dev/null and b/testdata/dnn/onnx/data/input_scale_broadcast_1.npy differ diff --git a/testdata/dnn/onnx/data/input_scale_broadcast_2.npy b/testdata/dnn/onnx/data/input_scale_broadcast_2.npy new file mode 100644 index 000000000..95938facb Binary files /dev/null and b/testdata/dnn/onnx/data/input_scale_broadcast_2.npy differ diff --git a/testdata/dnn/onnx/data/output_dynamic_batch.npy b/testdata/dnn/onnx/data/output_dynamic_batch.npy new file mode 100644 index 000000000..4a69fa177 Binary files /dev/null and b/testdata/dnn/onnx/data/output_dynamic_batch.npy differ diff --git a/testdata/dnn/onnx/data/output_resize_humanseg.npy b/testdata/dnn/onnx/data/output_resize_humanseg.npy new file mode 100644 index 000000000..3a5d3fdac Binary files /dev/null and b/testdata/dnn/onnx/data/output_resize_humanseg.npy differ diff --git a/testdata/dnn/onnx/data/output_scale_broadcast.npy b/testdata/dnn/onnx/data/output_scale_broadcast.npy new file mode 100644 index 000000000..84e55f5f6 Binary files /dev/null and b/testdata/dnn/onnx/data/output_scale_broadcast.npy differ diff --git a/testdata/dnn/onnx/generate_onnx_models.py b/testdata/dnn/onnx/generate_onnx_models.py index 84dba7c72..6a5515281 100644 --- a/testdata/dnn/onnx/generate_onnx_models.py +++ b/testdata/dnn/onnx/generate_onnx_models.py @@ -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 @@ -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) @@ -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) @@ -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) \ No newline at end of file +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) diff --git a/testdata/dnn/onnx/models/dynamic_batch.onnx b/testdata/dnn/onnx/models/dynamic_batch.onnx new file mode 100644 index 000000000..c2a3b49ae Binary files /dev/null and b/testdata/dnn/onnx/models/dynamic_batch.onnx differ diff --git a/testdata/dnn/onnx/models/resize_humanseg.onnx b/testdata/dnn/onnx/models/resize_humanseg.onnx new file mode 100644 index 000000000..061aa65fa Binary files /dev/null and b/testdata/dnn/onnx/models/resize_humanseg.onnx differ diff --git a/testdata/dnn/onnx/models/scale_broadcast.onnx b/testdata/dnn/onnx/models/scale_broadcast.onnx new file mode 100644 index 000000000..c45a4f5fa --- /dev/null +++ b/testdata/dnn/onnx/models/scale_broadcast.onnx @@ -0,0 +1,30 @@ +pytorch1.9:° + +0 +13Mul_0"Mul + +3 +24Mul_1"Multorch-jit-exportZ +0 + + + + +Z +1 + + + +Z +2 + + + + +b +4 + + + + +B \ No newline at end of file