Is the order of tf.data.Dataset determined?

Viewed 43

I was trying to get the y_pred from x_train, and realised that my predictions are always changing with the same model.

Then I digged a bit and found that the problem may be at tf.data.Dataset.

Following is how I construct the dataset:

test_data = tf.keras.utils.image_dataset_from_directory(
    dirpath + '/test',
    image_size=(128, 128),
    batch_size = 32
)

And then I did some steps to train the model. Before I actually use the model to predict the labels, I tried to test the consistency of the test_data.

y_test = np.concatenate([y_test for x, y_test in test_data], axis = 0)
y_test2 = np.concatenate([y_test for x, y_test in test_data], axis = 0)
print(y_test == y_test2)

I was expecting for an array with all elements being True because y_test and y_test2 should be the same, while it turned out to be

[False False False False False  True False False True False False False ...]

Which means y_test and y_test2 are reading two different Dataset? Or is it simply because the Dataset is unordered and each time it reads with no order?

The same happens when I predict the label with test_data.

y_prob = my_model.predict(test_data)
y_pred = np.argmax(y_prob, axis=1)

Each time, it returns different y_pred output.

Can anyone help explain and give some guidance on how to ensure the consistency?

1 Answers

tf.keras.utils.image_dataset_from_directory shuffles your data every time you iterate over the dataset, since the argument shuffle is set to True by default. That would explain the inconsistency (assuming that the rest is correct). Try setting shuffle=False

Related