-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_onnx_model.py
84 lines (65 loc) · 2.94 KB
/
predict_onnx_model.py
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
import os
from utilities.tools import suppress_tf_warnings
import onnxruntime as ort
import numpy as np
import tensorflow as tf
from utilities.export_helper import export
from utilities.class_names import get_classes_for_model
# Supress TF warnings
suppress_tf_warnings()
# Functions to load and run an ONNX model
def load_onnx_model(onnx_model_path):
session = ort.InferenceSession(onnx_model_path)
return session
def predict(session, input_data):
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
# Check if the input data is already a numpy array
if isinstance(input_data, np.ndarray):
input_feed = {input_name: input_data}
# If the input data is a list of dictionaries, directly pass it as input_feed
elif isinstance(input_data, list) and all(isinstance(item, dict) for item in input_data):
input_feed = input_data
else:
raise ValueError("Input data must be a list of dictionaries or a single numpy array")
predictions = session.run([output_name], input_feed)[0]
return predictions
def preprocess_image_keras(image_path, input_shape):
img = tf.keras.utils.load_img(image_path, target_size=(input_shape[1], input_shape[2]))
img_array = tf.keras.utils.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) # Add a new dimension for the batch size
return img_array
def get_top_class_and_percentage(predictions, class_labels):
top_class_idx = np.argmax(predictions)
percentages = tf.nn.softmax(predictions)
return class_labels[top_class_idx], percentages[0, top_class_idx]
# Prepare inference
img_height = 300
img_width = 300
# Set model Type to 'all_specific_model_variants' or 'car_type' or "specific_model_variants"
model_type = 'all_specific_model_variants'
model_path = '../models/onnx/all_model_variants/vgg16-pretrained-model-variants.onnx'
img_folder = 'test_images'
# Load model
session = load_onnx_model(model_path)
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
print("Input name and shape:", input_name, session.get_inputs()[0].shape)
print("Output name and shape:", output_name, session.get_outputs()[0].shape)
# Load images
images = []
img_names = []
for image in os.listdir('test_images'):
img_names.append(image)
img_array = input_data = preprocess_image_keras(f"{img_folder}/{image}", session.get_inputs()[0].shape)
images.append(img_array)
# Predict
all_predictions = {}
class_names = get_classes_for_model(model_type)
for img_array, name in zip(images, img_names):
predictions = predict(session, img_array)
top_class, top_percentage = get_top_class_and_percentage(predictions, class_names)
print(f"Ground truth: {name} | Predicted: {top_class} | Confidence: {100 * top_percentage: .2f}%")
all_predictions[name] = [top_class, 100 * top_percentage]
# Export predictions to CSV or text file
export(all_predictions, export_to_csv=False, export_folder='results/onnx/')