Tensorflow load saved model, Predict and Evaluate. Too low accuracy on test?

Viewed 340

I have trained my model on TPU and result seems good for testing. Dataset has 5 classes and result shows that:

accuracy: 0.9867 - sparse_categorical_accuracy: 0.9867 - loss: 0.0412 - val_accuracy: 0.9859 - val_sparse_categorical_accuracy: 0.9859 - val_loss: 0.0564 - lr: 1.8013e-07

My Sparse Categorical Accuracy Graph

I saved model as .h5 file as described Tensorflow Docs

I loaded TPU_model.h5 using reconstructed_model = keras.models.load_model("TPU_model.h5"). When I use reconstructed_model.predict_classes(tensored_image), result is incorrect. Tried reconstructed_model.evaluate, same result. I think about overfitting, tested prediction with image that used on training, result is incorrect. My latest load and predict code.

reconstructed_model= load_model('TPU_Model_Final.h5')
reconstructed_model= tf.keras.Sequential([reconstructed_model,tf.keras.layers.Softmax()])
reconstructed_model.compile(loss=loss,
              optimizer='adam',
              metrics=['accuracy','sparse_categorical_accuracy'])

classes = reconstructed_model.predict_classes(images, batch_size=10)

I follwed this tutorial(Tensorflor - Basic classification: Classify images of clothing)

1 Answers

I tried replicating the same model with sample dataset with the same good accuracy while model training and evaluation and found that it's predicting the images correctly.

Epoch 10/10
92/92 [==============================] - 39s 423ms/step - loss: 0.0750 - accuracy: 0.9734 - sparse_categorical_accuracy: 0.9734 - val_loss: 0.9160 - val_accuracy: 0.8692 - val_sparse_categorical_accuracy: 0.8692

Verified the same accuracy after loading back the model:

reconstructed_model = tf.keras.models.load_model("TPU_Model_Final.h5")

reconstructed_model.evaluate(val_ds)

Output:

23/23 [==============================] - 5s 106ms/step - loss: 0.9160 - accuracy: 0.8692 - sparse_categorical_accuracy: 0.8692
[0.9159510135650635, 0.8692098259925842, 0.8692098259925842]

The prediction part:
(predict_classes() is deprecated, hence using predict() and argmax() to show the prediction)

import matplotlib.pyplot as plt

plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
  for i in range(9):
    ax = plt.subplot(3, 3, i + 1)
    plt.imshow(images[i].numpy().astype("uint8"))
    plt.title(class_names[labels[i]])
    plt.axis("off")

Output:

enter image description here

Predicting the second image on the above 9 images:

classes = reconstructed_model.predict(images, batch_size=10)
pred = np.argmax(classes[1])
class_names[pred]

Output:

sunflowers

Verify with the actual label:

actual_labels=labels[1]
class_names[actual_labels]

Output:

sunflowers
Related