Ran
Ran

Reputation: 57

prediction result - Tensorflow

I'm trying to print the prediction results and the labels, in addition to accuracy from a model. I'm not sure what I'm doing wrong here

for mfcc, label in test_data:
 prediction = tflite_inference(mfcc, tflite_path)
 predicted_indices.append(np.squeeze(tf.argmax(prediction, axis=1)))

strlabel="C:/tmp/speech_commands_train/conv_labels.txt"
labels_list= [line.rstrip() for line in tf.io.gfile.GFile(strlabel)]

top_k = prediction.argsort()[-5:][::-1]


for node_id in top_k:
 human_string = labels_list[node_id]
 score = predicted_indices[node_id]
print('%s (score = %.5f)' % (human_string, score))

test_accuracy = calculate_accuracy(predicted_indices, expected_indices)
confusion_matrix = tf.math.confusion_matrix(expected_indices, predicted_indices,
                                        num_classes=model_settings['label_count'])

` Error message

human_string = labels_list[node_id] TypeError: only integer scalar arrays can be converted to a scalar index

Thank you in advance for your help.

Upvotes: 0

Views: 234

Answers (1)

Abhishek Prajapat
Abhishek Prajapat

Reputation: 1888

EDITED ANSWER (after some clarification regarding the problem): Here I assume that the prediction variable is the output of your model for a single input. With this assumption, your top_k should contain top 5 indices with the highest probability. To do that you should do the following:

  1. Reshape your predictions variable:
predictions = predictions.reshape(-1) # this will make the predicitions a vector
  1. Get the top_k
# this step is same but this time the output will be a vector instead of a matrix
top_k = prediction.argsort()[-5:][::-1] 
  1. Use the loop
# This is also same but as the `top_k` is a vector instead of a matrix there 
# won't be any issues/errors.
for node_id in top_k:
 human_string = labels_list[node_id]
 score = predicted_indices[node_id]
 print('%s (score = %.5f)' % (human_string, score))

Upvotes: 1

Related Questions