I know that keras provides a .class_indicies dictionary containing the mapping from class names to class as part of .flow_from_directory() from its ImageDataGenerator class (https://keras.io/preprocessing/image/).

HOWEVER, is there a way to access the corresponding class labels from an existing saved model (a model saved in as an .h5 file)? This seems important when putting my model into production and serving predictions since the classes are not known upfront and therefore the images are not separated in pre-labeled directories.

Similarly, are there any good examples of how a keras model should be deployed into production (especially for mobile)? Thanks!


1 Answer 1


Don't know if you solved already. I faced this problem yesterday, and the only solution that came to my mind is saving a dictionary to a numpy file after a train_generator is set; something like:

np.save('FILENAME', self.train_generator.class_indices)

when you finish your training. Then, when you do your single prediction:

single_pred = np.squeeze(self.model.predict(image))

if isfile('FILENAME.npy'):
    class_indices = np.load(self.indices_file + '.npy').item()

results_dict = dict(zip(class_indices.keys(), single_pred))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.