Deepali
Deepali

Reputation: 41

TFLearn - Evaluate a model

I am using TFLearn Alexnet sample with my own dataset.

Next I want to perform classification on test data and to determine the accuracy of the model.

  1. TFLearn API provides methods model.predict() and model.evaluate().
  2. model.predict() gives prediction result for each image in the test data set. How can I use the result to get the accuracy?
  3. model.evaluate() gives the accuracy score on the test data. Is there a way to get the accuracy for each batch as well?

Upvotes: 4

Views: 4053

Answers (3)

sbharti
sbharti

Reputation: 969

Accuracy from Prediction results

As stated by @Martin, the maximum value in the predictions array is the class predicted by model. you compare that class to the actual value: a match increases accuracy while mismatch decreases.

#METHOD 1
accuracy = model.evaluate(x_test, y_test)

#METHOD 2
predictions = model.predict(x_test)
accuracy = 0
for prediction, actual in zip(predictions, y_test):
    predicted_class = numpy.argmax(prediction)
    actual_class = numpy.argmax(actual)
    if(predicted_class == actual_class):
        accuracy+=1

accuracy = accuracy / len(y_test)

Upvotes: 1

Martin Thoma
Martin Thoma

Reputation: 136177

# Evaluate model
score = model.evaluate(test_x, test_y)
print('Test accuarcy: %0.4f%%' % (score[0] * 100))

# Run the model on one example
prediction = model.predict([test_x[0]])
print("Prediction: %s" % str(prediction[0][:3]))  # only show first 3 probas

How can I use the result to get the accuracy?

  1. Take the argmax of each prediction to get the predicted class
  2. Probably also take the argmax of the labels to get indices if you have one-hot encoded labels
  3. accuracy = sum(predicted label == target label) / len(predicted labels)

Is there a way to get the accuracy for each batch?

batch_index = 42
batch_size = 128
batch_x = test_x[batch_index * batch_size : (batch_index + 1) * batch_size]
batch_y = test_y[batch_index * batch_size : (batch_index + 1) * batch_size]
score = model.evaluate(batch_x, batch_y)
print('Batch accuarcy: %0.4f%%' % (score[0] * 100))

Upvotes: 2

sashimi
sashimi

Reputation: 1304

Below the responses:

  1. You can calculate the accuracy by comparing predicted classes against effective ones when using model.predict()
  2. No, that I am aware of. I am not sure would be useful as a use case either: usually you are interested on the overall accuracy for the dataset/partition you are evaluating.

Upvotes: 0

Related Questions