Skip to content

Commit 9f9f4d2

Browse files
authored
Merge pull request #1259 from nklskyoy:onnx-multifile
Add test files for importing gemm with external data #1259 Test data for opencv/opencv#27449
1 parent a3200b4 commit 9f9f4d2

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed
256 Bytes
Binary file not shown.
Binary file not shown.

testdata/dnn/onnx/generate_onnx_models.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,6 +2977,60 @@ def generate_gemm_dynmaic_inputs(name, inputA, inputB, inputC, path_data, path_m
29772977
"models"
29782978
)
29792979

2980+
## gemm with external data.
2981+
2982+
import numpy as np
2983+
import onnx
2984+
from onnx import helper, TensorProto, numpy_helper
2985+
from onnx.external_data_helper import convert_model_to_external_data
2986+
import os
2987+
import onnxruntime as ort
2988+
2989+
input_dim = 8
2990+
output_dim = 32
2991+
batch_dim = 4
2992+
onnx_model_path = "gemm_external_data.onnx"
2993+
B_filename = "gemm_external_data_B"
2994+
C_filename = "gemm_external_data_C"
2995+
2996+
A_data = np.random.randn(batch_dim, input_dim).astype(np.float32) # Batch size = 1
2997+
2998+
A = helper.make_tensor_value_info("A", TensorProto.FLOAT, A_data.shape)
2999+
Y = helper.make_tensor_value_info("Y", TensorProto.FLOAT, [batch_dim, output_dim])
3000+
3001+
np.random.seed(0)
3002+
B_data = np.random.randn(input_dim, output_dim).astype(np.float32)
3003+
C_data = np.random.randn(output_dim).astype(np.float32)
3004+
3005+
B_tensor = numpy_helper.from_array(B_data, name="B")
3006+
C_tensor = numpy_helper.from_array(C_data, name="C")
3007+
3008+
gemm_node = helper.make_node("Gemm", ["A", "B", "C"], ["Y"], alpha=1.0, beta=1.0, transB=0)
3009+
graph = helper.make_graph([gemm_node], "GEMMGraph", [A], [Y], [B_tensor, C_tensor])
3010+
model = helper.make_model(graph)
3011+
onnx.checker.check_model(model)
3012+
3013+
convert_model_to_external_data(
3014+
model,
3015+
all_tensors_to_one_file=False,
3016+
size_threshold=0, # Force all tensors to external
3017+
convert_attribute=False
3018+
)
3019+
3020+
for initializer in model.graph.initializer:
3021+
if initializer.name == "B":
3022+
initializer.external_data[0].value = B_filename
3023+
elif initializer.name == "C":
3024+
initializer.external_data[0].value = C_filename
3025+
3026+
onnx.save_model(model, onnx_model_path)
3027+
3028+
session = ort.InferenceSession(onnx_model_path)
3029+
outputs = session.run(None, {"A": A_data})
3030+
Y_data = outputs[0]
3031+
3032+
np.save("input_test_gemm_external_data.npy", A_data)
3033+
np.save("output_test_gemm_external_data.npy", Y_data)
29803034

29813035
# ########################## ReduceSum with Dynamic Batch ##########################
29823036
input_np = np.random.rand(2, 4, 4, 4).astype("float32")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+

2+
:�
3+

4+
A
5+
B
6+
CY"Gemm GEMMGraph*M BBj
7+
locationgemm_external_data_Bj
8+
offset1024j
9+
length1024p*I BCj
10+
locationgemm_external_data_Cj
11+
offset128j
12+
length128pZ
13+
A
14+

15+

16+
b
17+
Y
18+

19+

20+
 B
Binary file not shown.

testdata/dnn/onnx/models/gemm_external_data_C

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�0?梦?Y� �0I��_s@����f6 ����?$�=t<?��̾�w�>F<���=�?���) .��*?w���Ȫ�=a���1?8g#�����?�;���;��žh;�=��,��Ⓘ�k|���۽�0?梦?Y� �0I��_s@����f6 ����?$�=t<?��̾�w�>F<���=�?���) .��*?w���Ȫ�=a���1?8g#�����?�;���;��žh;�=��,��Ⓘ�k|���۽

0 commit comments

Comments
 (0)