0
$\begingroup$

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!

$\endgroup$
1
$\begingroup$

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))
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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