tensorflow-onnx icon indicating copy to clipboard operation
tensorflow-onnx copied to clipboard

[Predict] Prediction from ONNX is same for all images

Open HoaNguyen55 opened this issue 2 years ago • 1 comments

Describe the issue All image which imported to prediction phase, even image of training phase have only one result, they result 4th class. Here is the prediction result, they should be class #0 (for example):

Image: none_hash_marks_46.png
Predicted Class: 4
Prediction Scores: [[-2.1449246  -4.203819    0.95531905 -1.5842476  10.773621   -3.190796
  -1.4259771 ]]
---
Image: none_hash_marks_38.png
Predicted Class: 4
Prediction Scores: [[-2.1837478 -4.2522173  0.959147  -1.5910885 10.895861  -3.213889
  -1.4876479]]
---
Image: none_hash_marks_164.png
Predicted Class: 4
Prediction Scores: [[-2.1521454 -4.217044   0.9689145 -1.5811151 10.766655  -3.1886518
  -1.4249626]]
---
Image: none_hash_marks_161.png
Predicted Class: 4
Prediction Scores: [[-2.144948  -4.1984224  0.953305  -1.5931376 10.76777   -3.1868176
  -1.4167397]]
---
Image: none_hash_marks_41.png
Predicted Class: 4
Prediction Scores: [[-2.1507223 -4.2076254  0.9528841 -1.5993671 10.778776  -3.1894715
  -1.4074483]]
---
Image: none_hash_marks_86.png
Predicted Class: 4
Prediction Scores: [[-2.143237   -4.194382    0.96171093 -1.588678   10.740727   -3.1816995
  -1.4191349 ]]
---
Image: none_hash_marks_89.png
Predicted Class: 4
Prediction Scores: [[-2.14335    -4.192027    0.96441483 -1.5825499  10.715067   -3.1794114
  -1.408412  ]]
---
Image: none_hash_marks_172.png
Predicted Class: 4
Prediction Scores: [[-2.1474411  -4.1931567   0.97177017 -1.5898392  10.722211   -3.1753037
  -1.4145494 ]]
---
Image: none_hash_marks_309.png
Predicted Class: 4
Prediction Scores: [[-2.154678  -4.214909   0.9894781 -1.5827848 10.721918  -3.1820467
  -1.4003942]]
---
Image: none_hash_marks_317.png
Predicted Class: 4
Prediction Scores: [[-2.1523755 -4.210281   0.9955939 -1.5875626 10.722953  -3.1860068
  -1.4124641]]
---
Image: none_hash_marks_310.png
Predicted Class: 4
Prediction Scores: [[-2.1789439  -4.198061    0.96620387 -1.5875412  10.755175   -3.1888392
  -1.4128104 ]]
---
Image: none_hash_marks_314.png
Predicted Class: 4
Prediction Scores: [[-2.163153  -4.2003064  0.9966894 -1.5794321 10.694052  -3.17596
  -1.4182464]]
---
Image: none_hash_marks_470.png
Predicted Class: 4
Prediction Scores: [[-2.1628435  -4.2121544   0.95464015 -1.587901   10.777224   -3.1786203
  -1.4414166 ]]
---
Image: none_hash_marks_483.png
Predicted Class: 4
Prediction Scores: [[-2.1538308 -4.2089605  0.9529837 -1.5880953 10.777311  -3.1835651
  -1.4362395]]
---
Image: none_hash_marks_473.png
Predicted Class: 4
Prediction Scores: [[-2.144536  -4.1854153  0.9559608 -1.5870082 10.714812  -3.1720605
  -1.4002267]]
---
Image: none_hash_marks_651.png
Predicted Class: 4
Prediction Scores: [[-2.163309   -4.1983047   0.95716214 -1.5911573  10.739629   -3.171879
  -1.4147897 ]]
---
Image: none_hash_marks_654.png
Predicted Class: 4
Prediction Scores: [[-2.153453   -4.1837397   0.97166634 -1.5800877  10.684534   -3.1687734
  -1.3992469 ]]
---
Image: none_hash_marks_655.png
Predicted Class: 4
Prediction Scores: [[-2.1455472 -4.184617   0.9616484 -1.5884068 10.697898  -3.166878
  -1.4030488]]
---
Image: none_hash_marks_664.png
Predicted Class: 4
Prediction Scores: [[-2.1534238  -4.2176313   0.98432064 -1.5883641  10.76013    -3.1847723
  -1.4208087 ]]
---
Image: none_hash_marks_988.png
Predicted Class: 4
Prediction Scores: [[-2.1480696  -4.1941886   0.96258307 -1.5828831  10.720475   -3.1688619
  -1.4236683 ]]
---
Image: none_hash_marks_1105.png
Predicted Class: 4
Prediction Scores: [[-2.1580806  -4.213261    0.96541476 -1.5851772  10.7587595  -3.177482
  -1.441757  ]]
---
Image: none_hash_marks_995.png
Predicted Class: 4
Prediction Scores: [[-2.1410506  -4.1829267   0.96017164 -1.5860388  10.703795   -3.1719055
  -1.4053478 ]]
---
Image: none_hash_marks_999.png
Predicted Class: 4
Prediction Scores: [[-2.1448271 -4.18862    0.958737  -1.5879238 10.722557  -3.1763961
  -1.4063876]]
---
Image: none_hash_marks_1114.png
Predicted Class: 4
Prediction Scores: [[-2.1531098 -4.2050314  0.9679245 -1.5904634 10.751493  -3.1853566
  -1.4082947]]
---
Image: none_hash_marks_1118.png
Predicted Class: 4
Prediction Scores: [[-2.154631   -4.201218    0.97594416 -1.5841115  10.718743   -3.1744828
  -1.4118793 ]]
---
Image: none_hash_marks_1131.png
Predicted Class: 4
Prediction Scores: [[-2.1563578  -4.1954975   0.96010935 -1.5823518  10.731467   -3.1774504
  -1.4134333 ]]
---
Image: none_hash_marks_1127.png
Predicted Class: 4
Prediction Scores: [[-2.1597047 -4.210616   0.9639685 -1.5870692 10.769783  -3.178876
  -1.440206 ]]
---

Could you please give me any ideas to debug this case ?

To reproduce Here is my code for load ONNX model file and predict the image.

import onnxruntime as ort
import numpy as np
import cv2
import os

# Path to the ONNX model file
onnx_model_path = "/content/drive/MyDrive/hinhhoc/model.onnx"

# Load the ONNX model
ort_session = ort.InferenceSession(onnx_model_path)

# Function to preprocess an image
def preprocess_image(image_path):
    # Read the image
    img = cv2.imread(image_path)

    # Convert BGR to RGB
    # img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # Resize the image
    img_resized = cv2.resize(img, (224, 224))

    # Normalize the image
    img_normalized = img_resized.astype('float32') / 255.0

    # Add batch dimension and transpose the tensor
    img_batch = np.expand_dims(img_normalized, axis=0)
    img_transposed = np.transpose(img_batch, (0, 3, 1, 2))

    return img_transposed

# Path to the folder containing input images

folder_path = "/content/drive/MyDrive/hinhhoc/test_kyhieu/none_hash_marks"
# Iterate over the images in the folder
for filename in os.listdir(folder_path):
    if filename.endswith(".png"):
        # Preprocess the image
        image_path = os.path.join(folder_path, filename)
        input_data = preprocess_image(image_path)

        # Run the prediction
        ort_inputs = {ort_session.get_inputs()[0].name: input_data.astype(np.float32)}
        ort_outputs = ort_session.get_outputs()[0].name
        ort_outs = ort_session.run([ort_outputs], ort_inputs)

        # Process the prediction results
        predictions = ort_outs[0]
        predicted_class = np.argmax(predictions)
        print("Image:", filename)
        print("Predicted Class:", predicted_class)
        print("Prediction Scores:", predictions)
        print("---")

Urgency This issue is urgent for my project.

Platform Linux

OS Version Ubuntu 20.04.5 LTS

ONNX Runtime Installation Built from Source

ONNX Runtime Version or Commit ID 1.14.1

ONNX Runtime API Python 3.11

Architecture X64

Execution Provider Default CPU

Execution Provider Library Version No response

Expect Behavior: The result of test image should be different and the same as defined name of image.

HoaNguyen55 avatar May 19 '23 02:05 HoaNguyen55

Could you please share if the inference results (predicted class) are same as the results of original tf model?

fatcat-z avatar May 25 '23 11:05 fatcat-z