Skip to content

generate gemm sampel for 3.4 branch #983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2022
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_gemm_no_transB.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/input_gemm_transB_0.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_gemm_no_transB.npy
Binary file not shown.
Binary file added testdata/dnn/onnx/data/output_gemm_transB_0.npy
Binary file not shown.
64 changes: 62 additions & 2 deletions testdata/dnn/onnx/generate_onnx_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import onnxsim
import google.protobuf.text_format
import io

from typing import Optional

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the master branch there is only one empty line here

Copy link
Member Author

@zihaomu zihaomu Jun 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Now I understand, we need to make sure that the bits of PR of the two branches are exactly the same.
Short question, if we encounter the same thing in the future, can we do this via git? rather than manual copying.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forwarding this question to @alalek

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4.6.0 - 2 empty lines: https://github.com/opencv/opencv_extra/blob/4.6.0/testdata/dnn/onnx/generate_onnx_models.py

In the master branch there is only one empty line here

one line is here for the last few hours only: #982


Git works well in one-way. Two-way merging is a mess in general (some changes are just lost).
Proposed patches propagation is 3.4 -> 4.x -> 5.x: https://github.com/opencv/opencv/wiki/Branches
So this is why we need to put code into 3.4 branch first instead of 4.x if it is applicable and needed.
Manual backporting 4.x -> 3.4 is possible, but it is still manual (through git cherry-pick and resolving conflicts).

def assertExpected(s):
if not (isinstance(s, str) or (sys.version_info[0] == 2 and isinstance(s, unicode))):
Expand Down Expand Up @@ -73,6 +73,24 @@ 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 save_data_and_onnx_model(name, input_np, output_np, onnx_model):
print(name + " input has sizes", input_np.shape)
input_files = os.path.join("data", "input_" + name)
np.save(input_files, input_np.data)

print(name + " output has sizes", output_np.shape)
print()
output_files = os.path.join("data", "output_" + name)
np.save(output_files, np.ascontiguousarray(output_np.data))

models_files = os.path.join("models", name + ".onnx")

onnx_model_pb = onnx._serialize(onnx_model)
model_def = assertONNXExpected(onnx_model_pb)
with open(models_files, 'wb') as file:
file.write(model_def.SerializeToString())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and two empty lines instead of one here


def simplify(name, rename=False, **kwargs):
model, check = onnxsim.simplify(name, **kwargs)
assert check, "couldn't valide"
Expand Down Expand Up @@ -1665,4 +1683,46 @@ def forward(self, a, b):
save_data_and_model_multy_inputs('output_registration', model, a, b)
model = onnx.load('models/output_registration.onnx')
model.graph.node[0].name = model.graph.output[0].name
onnx.save(model, 'models/output_registration.onnx')
onnx.save(model, 'models/output_registration.onnx')

# ########################## GEMM ##########################
# The original code is : https://github.com/onnx/onnx/blob/main/onnx/backend/test/case/node/gemm.py
def gemm_reference_implementation(A: np.ndarray, B: np.ndarray, C: Optional[np.ndarray] = None, alpha: float = 1., beta: float = 1., transA: int = 0,
transB: int = 0) -> np.ndarray:
A = A if transA == 0 else A.T
B = B if transB == 0 else B.T
C = C if C is not None else np.array(0)

Y = alpha * np.dot(A, B) + beta * C

return Y

## gemm without transB
input_np = np.random.rand(2, 10).astype("float32")
inputs = [onnx.helper.make_tensor_value_info("input1", onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[input_np.dtype], shape=input_np.shape)]

weight_np = np.random.rand(10, 3).astype("float32")
weight_tensor = onnx.helper.make_tensor('weight_tensor', data_type=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[weight_np.dtype], dims=weight_np.shape, vals=weight_np)

outputs = [onnx.helper.make_tensor_value_info("output", onnx.TensorProto.FLOAT, shape=(2, 3))]

nodes = [onnx.helper.make_node("Gemm", ["input1", "weight_tensor"], ["output"])]

graph = onnx.helper.make_graph(nodes,
"gemm_test",
inputs,
outputs, initializer=[weight_tensor])
gemm_model = onnx.helper.make_model(graph)
output_np = gemm_reference_implementation(input_np, weight_np)
save_data_and_onnx_model("gemm_no_transB", input_np, output_np, gemm_model)

## gemm with transB = 0

nodes2 = [onnx.helper.make_node("Gemm", ["input1", "weight_tensor"], ["output"], transB=0)]
graph2 = onnx.helper.make_graph(nodes2,
"gemm_test",
inputs,
outputs, initializer=[weight_tensor])
gemm_model2 = onnx.helper.make_model(graph2)
output_np = gemm_reference_implementation(input_np, weight_np)
save_data_and_onnx_model("gemm_transB_0", input_np, output_np, gemm_model2)
16 changes: 16 additions & 0 deletions testdata/dnn/onnx/models/gemm_no_transB.onnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
:�
�weight_node_outinput22"Constant*�
value*�
"x��z?��L?G�>��G?�9�=��#?4�>��q?ڗ?�N�>�s�>.4F?���>�?�<\?N�?c�?y�q?Ƌ.?k�>���>��2?��v=9�*?�+?�nW>A>��>L8�>Bweight_tensor�
'
input1
weight_node_outoutput"Gemm gemm_testZ
input1



b
output


B
Expand Down
Binary file added testdata/dnn/onnx/models/gemm_transB_0.onnx
Binary file not shown.