[Python] Is it possible to save the class/label mapping directly inside a keras model.h5 file?

Discussão em 'Python' iniciado por Stack, Setembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    Using model.save() I am able to save the trained model. However, upon using the model for predictions, I still need to recover the respective class/label mappings (0: 'cat', 1: 'dog' ... etc.). Currently I am saving the .class_indices from my train_generator and reload it to prepare my test-data to accomplish this. However this is quite inconvenient since it forces me to keep the mapping file somewhere save for future use of my saved model.h5 file.

    Hence, I wonder if there is a simpler way of saving the class information i.e. saving it inside the model file. I can't find any information on this in the keras docs, only this post Attaching class labels to a Keras model where someone tried to come up with a 'workaround' but I assume there must be a better way.

    Continue reading...

Compartilhe esta Página