1
votes

I am using tensorflow to do a multi-class classification

I load the training dataset and validation dataset in the following way

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  shuffle=True,
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  shuffle=True,
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

Then when I train the model using model.fit()

history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs,
  shuffle=True
)

I get validation accuracy around 95%.

But when I load the same validation set and use model.evaluate()

model.evaluate(val_ds)

I get very low accuracy (around 10%).

Why am I getting such different results? Am I using the model.evaluate function incorrectly?

Note : In the model.compile() I am specifying the following, Optimizer - Adam, Loss - SparseCategoricalCrossentropy, Metric - Accuracy

Model.evaluate() output

41/41 [==============================] - 5s 118ms/step - loss: 0.3037 - accuracy: 0.1032
Test Loss -  0.3036555051803589
Test Acc -  0.10315627604722977

Model.fit() output for last three epochs

Epoch 8/10
41/41 [==============================] - 3s 80ms/step - loss: 0.6094 - accuracy: 0.8861 - val_loss: 0.4489 - val_accuracy: 0.9483
Epoch 9/10
41/41 [==============================] - 3s 80ms/step - loss: 0.5377 - accuracy: 0.8953 - val_loss: 0.3868 - val_accuracy: 0.9554
Epoch 10/10
41/41 [==============================] - 3s 80ms/step - loss: 0.4663 - accuracy: 0.9092 - val_loss: 0.3404 - val_accuracy: 0.9590
1

1 Answers

0
votes

Try saving your model as .h5 instead of .tf - this worked for me in Keras 2.4.0 and tensorflow 2.4.0