I've just finished training an Inception V3 CNN and I'm trying to measure accuracy on the training dataset, specifically, top-k accuracy. I invoke the function called top_k_categorical_accuracy from tensorflow.keras.metrics ordering my parameters properly (y_true, y_pred, k) but I receive an error saying my targets (y_true) should be 1-dimensional. However, when I print the shape of y_true (which are the targets, if I understand correctly) I get (9000,), which, to me, seems 1-dimensional.
Both arrays have a dtype = "float32" since I read in a thread that this caused problem, but this does not solve my problem.
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
from keras.applications import InceptionV3
from keras.applications.inception_v3 import preprocess_input
test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
test_generator = test_datagen.flow_from_directory(
"data/test",
target_size=(299, 299),
batch_size=16,
class_mode="categorical",
shuffle=True,
seed=42,
)
STEP_SIZE_TEST = test_generator.n // test_generator.batch_size
model = keras.models.load_model("inceptionv3.hdf5")
results = model.evaluate_generator(test_generator, STEP_SIZE_TEST, workers=8)
y_pred = model.predict_generator(test_generator)
print(y_pred.shape) # Prints (9000, 6)
y_true = test_generator.classes
y_true = y_true.astype("float32")
print(y_true.shape) #Prints (9000,)
top_k = tf.keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=2)
The exact error I get is this : tensorflow.python.framework.errors_impl.InvalidArgumentError: targets must be 1-dimensional [Op:InTopKV2]
If I resize y_pred to a 1D array, I get the following error : tensorflow.python.framework.errors_impl.InvalidArgumentError: predictions must be 2-dimensional [Op:InTopKV2]