From 1e2f9b2afceb87495972599416b818cdc957d42a Mon Sep 17 00:00:00 2001 From: sulisindriyani Date: Thu, 14 Nov 2024 15:53:47 +0700 Subject: [PATCH] Update C2_W4_Assignment.py --- .../assignment_optional/C2_W4_Assignment.py | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/C2_Device-based-TF-lite/W4/assignment_optional/C2_W4_Assignment.py b/C2_Device-based-TF-lite/W4/assignment_optional/C2_W4_Assignment.py index 4ee911ae..a909efff 100755 --- a/C2_Device-based-TF-lite/W4/assignment_optional/C2_W4_Assignment.py +++ b/C2_Device-based-TF-lite/W4/assignment_optional/C2_W4_Assignment.py @@ -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') @@ -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)