load.py 758 B

123456789101112131415161718192021222324252627
  1. from typing import Tuple
  2. from keras.models import model_from_json
  3. import tensorflow as tf
  4. def init_model(model: str, weights: str) -> Tuple:
  5. """Initialize the keras model
  6. :param model, str: path to model.json
  7. :param weights, str: path to weights for model
  8. :return keras model and graph
  9. """
  10. with open(model, 'r') as json_file:
  11. loaded_model_json = json_file.read()
  12. loaded_model = model_from_json(loaded_model_json)
  13. # load woeights into new model
  14. loaded_model.load_weights(weights)
  15. # compile and evaluate loaded model
  16. loaded_model.compile(
  17. loss='categorical_crossentropy',
  18. optimizer='adam',
  19. metrics=['accuracy'])
  20. graph = tf.get_default_graph()
  21. return loaded_model, graph