Skip to content

Results with ONNX and opencv are different for reduceMean #25103

@LaurentBerger

Description

@LaurentBerger

System Information

opencv 4.9.0 up to date

Detailed description

Onnx model is created in code with only one node.

image

Results with onnx and opencv are differents :

input DATA
[[[[[  0.   1.   2.   3.   4.]
    [  5.   6.   7.   8.   9.]
    [ 10.  11.  12.  13.  14.]
    [ 15.  16.  17.  18.  19.]]

   [[ 20.  21.  22.  23.  24.]
    [ 25.  26.  27.  28.  29.]
    [ 30.  31.  32.  33.  34.]
    [ 35.  36.  37.  38.  39.]]

   [[ 40.  41.  42.  43.  44.]
    [ 45.  46.  47.  48.  49.]
    [ 50.  51.  52.  53.  54.]
    [ 55.  56.  57.  58.  59.]]]


  [[[ 60.  61.  62.  63.  64.]
    [ 65.  66.  67.  68.  69.]
    [ 70.  71.  72.  73.  74.]
    [ 75.  76.  77.  78.  79.]]

   [[ 80.  81.  82.  83.  84.]
    [ 85.  86.  87.  88.  89.]
    [ 90.  91.  92.  93.  94.]
    [ 95.  96.  97.  98.  99.]]

   [[100. 101. 102. 103. 104.]
    [105. 106. 107. 108. 109.]
    [110. 111. 112. 113. 114.]
    [115. 116. 117. 118. 119.]]]]]
ONNX result
[[[[30. 31. 32. 33. 34.]
   [35. 36. 37. 38. 39.]
   [40. 41. 42. 43. 44.]
   [45. 46. 47. 48. 49.]]

  [[50. 51. 52. 53. 54.]
   [55. 56. 57. 58. 59.]
   [60. 61. 62. 63. 64.]
   [65. 66. 67. 68. 69.]]

  [[70. 71. 72. 73. 74.]
   [75. 76. 77. 78. 79.]
   [80. 81. 82. 83. 84.]
   [85. 86. 87. 88. 89.]]]]
Writting model
Set opencv input DATA
Opencv result
[[[[30. 31. 32. 33. 34.]
   [35. 36. 37. 38. 39.]
   [40. 41. 42. 43. 44.]
   [45. 46. 47. 48. 49.]]

  [[50. 51. 52. 53. 54.]
   [55. 56. 57. 58. 59.]
   [60. 61. 62. 63. 64.]
   [65. 66. 67. 68. 69.]]

  [[50. 51. 52. 53. 54.]
   [55. 56. 57. 58. 59.]
   [60. 61. 62. 63. 64.]
   [65. 66. 67. 68. 69.]]]]
OPENCV result
Quadratic error for node  :  133.33333
Max error for  node :  400.0

Steps to reproduce

import numpy as np
import onnx
import onnxsim
import onnx.reference
import cv2 as cv

shape_ini = (4, 5)
N = 2
data = np.arange(0, 1 * N * 3 *shape_ini[0]* shape_ini[1]).reshape((1, N, 3, shape_ini[0], shape_ini[1])).astype(dtype=np.float32)    
shape_ini = data.shape 
select_axes = np.array([1], dtype=np.int64)
keepdims = 0
print("input DATA ")
print(data)
onnx_name = "testReduceMean"
out1 = onnx.helper.make_tensor_value_info('out1', onnx.TensorProto.FLOAT, [None, None, None, None, None])
inp0 = onnx.helper.make_tensor_value_info('inp0', onnx.TensorProto.FLOAT, [None, None, None, None, None])
node1 = onnx.helper.make_node( "ReduceMean", inputs=["inp0"], outputs=["out1"], keepdims=keepdims , axes=select_axes)
graph = onnx.helper.make_graph([node1], onnx_name,  [inp0], [out1])
onnx_model = onnx.helper.make_model(graph)

feeds = {'inp0': data}
sess = onnx.reference.ReferenceEvaluator(onnx_model)
res_onnx = sess.run(["out1"], feeds)
print("ONNX result")
print(res_onnx[0])
print("Writting model")
with open(onnx_name + ".onnx", "wb") as f:
    f.write(onnx_model.SerializeToString())

net = cv.dnn.readNet(onnx_name+'.onnx')
net.enableWinograd(False)
print("Set opencv input DATA ")

net.setInput(data)
res_ocv = net.forward()
print("Opencv result")
print(res_ocv)
print("Quadratic error for node  : ", np.mean((res_ocv -  res_onnx[0])**2))
print("Max error for  node : ", np.max((res_ocv -  res_onnx[0])**2))

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions