Skip to content

add converted conformance gemm tests#1073

Merged
vpisarev merged 5 commits intoopencv:4.xfrom
fengyuentau:refactor_fc
Sep 19, 2023
Merged

add converted conformance gemm tests#1073
vpisarev merged 5 commits intoopencv:4.xfrom
fengyuentau:refactor_fc

Conversation

@fengyuentau
Copy link
Copy Markdown
Member

@fengyuentau fengyuentau commented Jun 30, 2023

Merge with opencv/opencv#23897.

These tests are converted from onnx gemm conformance tests. They have const C (bias) instead of input C.


googlenet.onnx is updated because it has the Reshape operator which hard-writes the batch size to be 1, and this goes against the test which has batch 2:

image

Replacing Reshape with Flatten solves the problem:

image

Also the current implementation of Softmax should be upgraded as well, since the output of Gemm is with batch and theoratically the output shape of Softmax should have batch as well (2, 1, 1000) instead of current output shape (2, 1000).

Scripts for model and data generation with:

  • PyTorch 2.0.1
  • TorchVision 0.15.2
  • NumPy 1.24.2
  • ONNX 1.13.1
  • ONNXRuntime 1.14.1
  • OpenCV 4.8.0

gen_googlenet.py:

from torchvision.models import googlenet, GoogLeNet_Weights

model = googlenet(weights=GoogLeNet_Weights.IMAGENET1K_V1, transform_input=False)

import torch
import torch.nn as nn

x = torch.randn(1, 3, 224, 224, dtype=torch.float)
print(x.shape)

class gnet(nn.Module):
    def __init__(self, model):
        super().__init__()
        self.m = model
        self.softmax = nn.Softmax(-1)

    def forward(self, x):
        return self.softmax(self.m(x))

m = gnet(model)
torch.onnx.export(m, x, "googlenet_2023.onnx", export_params=True, opset_version=13, do_constant_folding=True)

import onnx

model = onnx.load("googlenet_2023.onnx")
inferred_model = onnx.shape_inference.infer_shapes(model)
onnx.save(inferred_model, "googlenet_2023.onnx")

verify_outputs.py:

import onnxruntime as ort
import numpy as np
import cv2 as cv

def ort_inference(model_path, x):
    net = ort.InferenceSession(model_path)
    y = net.run([], {"input.1": x})
    return y[0]

def ocv_inference(model_path, x):
    net = cv.dnn.readNet(model_path)
    net.setInput(x)
    y = net.forward()
    return y

x = np.random.randn(1, 3, 224, 224).astype(np.float32)
ort_y = ort_inference("googlenet_2023.onnx", x)
ocv_y = ocv_inference("googlenet_2023.onnx", x)

#print(ort_y)
#print()
#print(ocv_y)

print(ort_y.shape)
print(ocv_y.shape)
print("all_close=", np.allclose(ort_y, ocv_y, atol=1e-05))

gen_outs.py:

import onnxruntime as ort
import numpy as np
import cv2 as cv

def ort_inference(model_path, x):
    net = ort.InferenceSession(model_path)
    y = net.run([], {"input.1": x})
    return y[0]

def ocv_inference(model_path, x):
    net = cv.dnn.readNet(model_path)
    net.setInput(x)
    y = net.forward()
    return y

x = np.random.randn(1, 3, 224, 224).astype(np.float32)
ort_y = ort_inference("googlenet_2023.onnx", x)
ocv_y = ocv_inference("googlenet_2023.onnx", x)

#print(ort_y)
#print()
#print(ocv_y)

print(ort_y.shape)
print(ocv_y.shape)
print("all_close=", np.allclose(ort_y, ocv_y, atol=1e-05))

(base) Airnew cat gen_outs.py
import onnxruntime as ort
import numpy as np
import cv2 as cv

x1 = cv.imread("../../opencv_extra/testdata/dnn/googlenet_0.png")
x1 = cv.dnn.blobFromImage(x1)
x2 = cv.imread("../../opencv_extra/testdata/dnn/googlenet_1.png")
x2 = cv.dnn.blobFromImage(x2)

net = ort.InferenceSession("modern_googlenet.onnx")
y1 = net.run([], {"input.1": x1})[0]
print(y1.shape)
y2 = net.run([], {"input.1": x2})[0]
print(y2.shape)

#y = np.concatenate([y1[np.newaxis, :, :], y2[np.newaxis, :, :]], axis=0)
y = np.concatenate([y1, y2], axis=0)
print(y.shape)
np.save("googlenet_prob_2023.npy", y)

@yifan2

This comment was marked as spam.

@vpisarev vpisarev merged commit c1be747 into opencv:4.x Sep 19, 2023
@asmorkalov asmorkalov mentioned this pull request Sep 28, 2023
@fengyuentau fengyuentau deleted the refactor_fc branch December 3, 2024 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants