0
0
Computer Visionml~10 mins

ONNX Runtime in Computer Vision - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load an ONNX model using ONNX Runtime.

Computer Vision
import onnxruntime as ort

session = ort.InferenceSession([1])
Drag options to blanks, or click blank then click option'
A"model.onnx"
Bmodel.onnx
Cload_model("model.onnx")
Donnx.load("model.onnx")
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the model filename without quotes causes a NameError.
Trying to use onnx.load instead of passing the file path.
2fill in blank
medium

Complete the code to prepare input data for ONNX Runtime inference.

Computer Vision
import numpy as np

input_name = session.get_inputs()[0].name
input_data = np.random.rand(1, 3, 224, 224).astype([1])
Drag options to blanks, or click blank then click option'
A"int32"
B"float64"
C"uint8"
D"float32"
Attempts:
3 left
💡 Hint
Common Mistakes
Using float64 causes type mismatch errors.
Using integer types when the model expects floats.
3fill in blank
hard

Fix the error in the code to run inference and get the output.

Computer Vision
outputs = session.run(None, [1])
print(outputs[0])
Drag options to blanks, or click blank then click option'
A(input_name, input_data)
B{input_name: input_data}
Cinput_data
D[input_name, input_data]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing inputs as a list or tuple causes runtime errors.
Passing only the input data without the input name.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps input names to numpy arrays.

Computer Vision
inputs = { [1]: np.array(value) for [2], value in input_data.items() }
Drag options to blanks, or click blank then click option'
Aname
Bkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the key and the iteration variable.
Not using a colon between key and value.
5fill in blank
hard

Fill all three blanks to extract output names, run inference, and print the first output.

Computer Vision
output_names = [output.name for output in session.get_outputs()]
results = session.run([1], [2])
print(results[3])
Drag options to blanks, or click blank then click option'
Aoutput_names
B{input_name: input_data}
C[0]
Dinput_data
Attempts:
3 left
💡 Hint
Common Mistakes
Passing inputs as a list instead of a dictionary.
Not specifying output names in the run method.
Trying to print results without indexing.