Skip to content

Instantly share code, notes, and snippets.

@tkeyo
Created July 6, 2021 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkeyo/6777893acbed14386c455d441714ba84 to your computer and use it in GitHub Desktop.
Save tkeyo/6777893acbed14386c455d441714ba84 to your computer and use it in GitHub Desktop.
import numpy as np
import onnxruntime as rt
labels = ['hot_dog', 'not_hot_dog']
# get image as tensor - 1 x 3 x 256 x 256 dimensions
hot_dog_tensor_onnx = image_transform_onnx('test_images/hot_dog_114.jpg', 256)
# initialize onnx runtime inference session
sess = rt.InferenceSession('models/hot_dog_model_resnet18_256_256.onnx')
# input & output names
input_name = sess.get_inputs()[0].name
output_name = sess.get_outputs()[0].name
# input dimensions (important for debugging)
input_dims = sess.get_inputs()[0].shape
print(input_name, output_name, input_dims)
results = sess.run([output_name], {input_name: not_hot_dog_tensor_onnx})[0]
print(labels[np.argmax(results)], results)
# prints
# ('hot_dog',array([[0.9986059 , 0.00139411]], dtype=float32))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment