Skip to content
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

[Bug]: OpenVINO CPU gave inconsistent results with TensorFlow Keras for the GlobalAveragePooling2D #20815

Closed
3 tasks done
jikechao opened this issue Nov 1, 2023 · 7 comments
Closed
3 tasks done
Assignees
Labels
bug Something isn't working category: CPU OpenVINO CPU plugin

Comments

@jikechao
Copy link

jikechao commented Nov 1, 2023

OpenVINO Version

2023.1.0-12185-9e6b00e51cd-releases/2023/1

Operating System

Ubuntu 18.04 (LTS)

Device used for inference

CPU

Framework

Keras (TensorFlow 2)

Model used

self defined model shown in the following script

Issue description

OpenVINO and Keras gave different inference results about the model containing GlobalAveragePooling2D operator.

Specifically, only OpenVINO using the CPU for inference gave different results with OpenVINO(GPU) and PyTorch(CPU/GPU).

Step-by-step reproduction

import tensorflow as tf
from tensorflow import keras as keras
from tensorflow.keras import layers, models
import numpy as np

import openvino as ov

layer = keras.layers.GlobalAveragePooling2D()
input_shape = [1, 11, 1, 7]
input_data = np.random.randint(2, size=input_shape)
x = layers.Input(shape=input_shape[1:], dtype="int8")
y = layer(x)
model = models.Model(x, y)
# model.summary()
res_keras = model(input_data)

tf2_model_path = f"_temp_model"
tf.saved_model.save(model, tf2_model_path)
ov_model = ov.convert_model(tf2_model_path,  input=input_shape)

ir_path = f"_temp_OVIR.xml"
ov.save_model(ov_model, ir_path, compress_to_fp16=False)
core = ov.Core()
model = core.read_model(ir_path)
compiled_model = core.compile_model(model=model, device_name="CPU")  # GPU: run well

output_key = compiled_model.output(0)

res_dlc = compiled_model(input_data)[output_key]
print(res_keras)
print(res_dlc)
np.testing.assert_allclose(res_keras, res_dlc, atol=1e-3, rtol=1e-3)

Relevant log output

AssertionError: 
Not equal to tolerance rtol=0.001, atol=0.001

Mismatched elements: 5 / 7 (71.4%)
Max absolute difference: 1
Max relative difference: 1.
 x: array([[0, 0, 0, 0, 0, 0, 0]], dtype=int8)
 y: array([[0, 1, 0, 1, 1, 1, 1]], dtype=int8)

Issue submission checklist

  • I'm reporting an issue. It's not a question.
  • I checked the problem with the documentation, FAQ, open issues, Stack Overflow, etc., and have not found a solution.
  • There is reproducer code and related data files such as images, videos, models, etc.
@jikechao jikechao added bug Something isn't working support_request labels Nov 1, 2023
@rkazants rkazants changed the title [Bug]: OpenVINO CPU gave inconsistent results with PyTorch for the GlobalAveragePooling2D [Bug]: OpenVINO CPU gave inconsistent results with TensorFlow Keras for the GlobalAveragePooling2D Nov 1, 2023
@rkazants rkazants self-assigned this Nov 1, 2023
@rkazants rkazants added the category: TF FE OpenVINO TensorFlow FrontEnd label Nov 1, 2023
@jikechao
Copy link
Author

jikechao commented Nov 6, 2023

BTW, GlobalAveragePooling1D, GlobalAveragePooling2D, and GlobalAveragePooling3D have the same issue.

@rkazants
Copy link
Contributor

rkazants commented Dec 3, 2023

PR: #21439

@jikechao jikechao closed this as completed Dec 4, 2023
@rkazants rkazants reopened this Dec 4, 2023
@rkazants
Copy link
Contributor

rkazants commented Dec 4, 2023

@jikechao, there is one more bug in CPU plugin. Once my PR is merged, I will move it to CPU plugin team. There is also problem with ReduceMean for int8 type implementation there.

Best regards,
Roman

@jikechao
Copy link
Author

jikechao commented Dec 4, 2023

@rkazants, I understood! Thank you for your explanation.

@rkazants rkazants added category: CPU OpenVINO CPU plugin and removed category: TF FE OpenVINO TensorFlow FrontEnd labels Dec 5, 2023
@rkazants
Copy link
Contributor

rkazants commented Dec 5, 2023

Hi @mg-intel,

I fixed the first part of the issue when TF FE was unable to handle int8 type. #21439 was merged.

Now it comes to the second issue on the CPU side. ReduceMean for input int8 type output incorrect results different from TF, example:

import numpy as np
import openvino.runtime.opset9 as ov
from openvino.runtime import Model, Core

param = ov.parameter([1,2,3,1], name="data", dtype=np.int8)
axes = ov.constant(np.array([1, 2], dtype=np.int32))
reduce_mean = ov.reduce_mean(param, axes, False)
model = Model([reduce_mean.output(0)], [param], "model")
core = Core()
compiled_model = core.compile_model(model, "CPU")
data = np.array([[[[1], [1], [1]], [[1], [1], [0]]]], dtype=np.int8)
output = compiled_model.infer_new_request({0: data})
print("ov output = ", output)

# TF part
output = tf.raw_ops.Mean(input=data,axis=[1,2])
print("tf output = ", output)

OV reports 1 and TF reports 0 in the result. Now I re-assign this to CPU to align it with TF.

Best regards,
Roman

@xuchen-intel
Copy link
Contributor

Thanks @jikechao @rkazants for the detailed information!
PR: #25217

github-merge-queue bot pushed a commit that referenced this issue Jun 26, 2024
### Details:
- *vdivps instruction rounds to the nearest value. Here we append
vroundps instruction to make the result round towards zero, to align
behavior with Plugins/Frameworks.*
 - *Add the test case that can reproduce the issue beforehand.*

### Tickets:
-
*[issue#20815](#20815
@jikechao
Copy link
Author

@xuchen-intel Thanks for your efforts!

allnes pushed a commit to allnes/openvino that referenced this issue Jun 27, 2024
### Details:
- *vdivps instruction rounds to the nearest value. Here we append
vroundps instruction to make the result round towards zero, to align
behavior with Plugins/Frameworks.*
 - *Add the test case that can reproduce the issue beforehand.*

### Tickets:
-
*[issue#20815](openvinotoolkit#20815
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working category: CPU OpenVINO CPU plugin
Projects
Status: No status
Development

No branches or pull requests

5 participants