def clone_model(model, custom_objects={}): config = { 'class_name': model.__class__.__name__, 'config': model.get_config(), } clone = model_from_config(config, custom_objects=custom_objects) clone.set_weights(model.get_weights()) return clone
def clone_model(model, custom_objects={}): # Requires Keras 1.0.7 since get_config has breaking changes. config = { 'class_name': model.__class__.__name__, 'config': model.get_config(), } clone = model_from_config(config, custom_objects=custom_objects) clone.set_weights(model.get_weights()) return clone
def load_model(path, custom_objects={}, verbose=0): from tensorflow.keras.models import model_from_config import json path = splitext(path)[0] with open('%s.json' % path, 'r') as json_file: model_json = json_file.read() config = json.loads(model_json) model = model_from_config(config, custom_objects=custom_objects) model.load_weights('%s.h5' % path) if verbose: print('Loaded from %s' % path) return model
def layer_from_config(config): if 'class_name' not in config: raise ValueError('class_name is needed to define layer') if 'config' not in config: # auto add empty config for layer with only class_name config['config'] = {} if 'name' not in config['config'] and 'name' in config: config['config']['name'] = config['name'] return model_from_config(config, custom_objects={ **Layers().layers, **Activations().activations })
def __init__(self, model, instance_state, tuner_state, metrics_config, cloudservice): self.instance_state = instance_state self.tuner_state = tuner_state self.cloudservice = cloudservice self.state = ExecutionState(tuner_state.max_epochs, metrics_config) # Model rereation config = serialize_keras_object(model) self.model = model_from_config(config) optimizer_config = tf.keras.optimizers.serialize(model.optimizer) optimizer = tf.keras.optimizers.deserialize(optimizer_config) self.model.compile(optimizer=optimizer, metrics=model.metrics, loss=model.loss, loss_weights=model.loss_weights)