Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions C2_Device-based-TF-lite/W4/assignment_optional/C2_W4_Assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
labels = ['Rock', 'Paper', 'Scissors']

# Load TFLite model and allocate tensors
interpreter = # YOUR CODE HERE
interpreter = Interpreter(model_path=model_path)
#Allocate tensors to the interpreter
# YOUR CODE HERE
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = # YOUR CODE HERE
output_details = # YOUR CODE HERE
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Read image with Pillow
img = Image.open(filename).convert('RGB')
Expand All @@ -47,21 +47,22 @@

# Preprocess image
# Resize the image
img = # YOUR CODE HERE
img = mg.resize(size)
# Convert to Numpy with float32 as the datatype
img = # YOUR CODE HERE
img = np.array(img, dtype=np.float32)
# Normalize the image
img = # YOUR CODE HERE
img = img / 255.0

# Add a batch dimension
input_data = # YOUR CODE HERE
input_data =np.expand_dims(img, axis=0)

# Point the data to be used for testing and run the interpreter
# YOUR CODE HERE
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()

# Obtain results and print the predicted category
predictions = # YOUR CODE HERE
predictions = interpreter.get_tensor(output_details[0]['index'])[0]
# Get the label with highest probability
predicted_label = # YOUR CODE HERE
predicted_label = labels[np.argmax(predictions)]
# Print the predicted category
# YOUR CODE HERE
print("Predicted category:", predicted_label)