Ejemplo n.º 1
0
 def from_config(cls, config):
     embedding_layer = layer_module.deserialize(
         config.pop('embedding_layer_config'), custom_objects=globals())
     output_layer = layer_module.deserialize(
         config.pop('output_layer_config'), custom_objects=globals())
     return cls(embedding_layer=embedding_layer,
                output_layer=output_layer,
                **config)
Ejemplo n.º 2
0
 def from_config(cls, config, custom_objects=None):
     linear_config = config.pop('linear_model')
     linear_model = layer_module.deserialize(linear_config, custom_objects)
     dnn_config = config.pop('dnn_model')
     dnn_model = layer_module.deserialize(dnn_config, custom_objects)
     activation = activations.deserialize(config.pop('activation', None),
                                          custom_objects=custom_objects)
     return cls(linear_model=linear_model,
                dnn_model=dnn_model,
                activation=activation,
                **config)
Ejemplo n.º 3
0
 def to_layer(self):
     if self.classname == "CustomConnected":
         return layers.deserialize({
             'class_name': "Dense",
             'config': self.config
         })
     if self.classname == "CustomConv":
         return layers.deserialize({
             'class_name': "Conv1D",
             'config': self.config
         })
     return layers.deserialize({
         'class_name': self.classname,
         'config': self.config
     })
Ejemplo n.º 4
0
def model_from_yaml(yaml_string, custom_objects=None):
    """Parses a yaml model configuration file and returns a model instance.

  Usage:

  >>> model = tf.keras.Sequential([
  ...     tf.keras.layers.Dense(5, input_shape=(3,)),
  ...     tf.keras.layers.Softmax()])
  >>> try:
  ...   import yaml
  ...   config = model.to_yaml()
  ...   loaded_model = tf.keras.models.model_from_yaml(config)
  ... except ImportError:
  ...   pass

  Arguments:
      yaml_string: YAML string or open file encoding a model configuration.
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.

  Returns:
      A Keras model instance (uncompiled).

  Raises:
      ImportError: if yaml module is not found.
  """
    if yaml is None:
        raise ImportError(
            'Requires yaml module installed (`pip install pyyaml`).')
    config = yaml.load(yaml_string)
    from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
    return deserialize(config, custom_objects=custom_objects)
Ejemplo n.º 5
0
def model_from_config(config, custom_objects=None):
    """Instantiates a Keras model from its config.

  Usage:
  ```
  # for a Functional API model
  tf.keras.Model().from_config(model.get_config())

  # for a Sequential model
  tf.keras.Sequential().from_config(model.get_config())
  ```

  Args:
      config: Configuration dictionary.
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.

  Returns:
      A Keras model instance (uncompiled).

  Raises:
      TypeError: if `config` is not a dictionary.
  """
    if isinstance(config, list):
        raise TypeError(
            '`model_from_config` expects a dictionary, not a list. '
            'Maybe you meant to use '
            '`Sequential.from_config(config)`?')
    from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
    return deserialize(config, custom_objects=custom_objects)
def unpack(model, training_config, weights):
    restored_model = deserialize(model)
    if training_config is not None:
        restored_model.compile(
            **saving_utils.compile_args_from_training_config(training_config))
    restored_model.set_weights(weights)
    return restored_model
Ejemplo n.º 7
0
 def from_config(cls, config, custom_objects=None):
     model = cls()
     for conf in config:
         layer = layer_module.deserialize(conf,
                                          custom_objects=custom_objects)
         model.add(layer)
     return model
Ejemplo n.º 8
0
  def from_config(cls, config, custom_objects=None):
    """Creates a RNNModel from its config.

    Args:
      config: A Python dictionary, typically the output of `get_config`.
      custom_objects: Optional dictionary mapping names (strings) to custom
        classes or functions to be considered during deserialization.

    Returns:
      A RNNModel.
    """
    rnn_layer = keras_layers.deserialize(
        config.pop('rnn_layer'), custom_objects=custom_objects)
    sequence_feature_columns = fc.deserialize_feature_columns(
        config.pop('sequence_feature_columns'), custom_objects=custom_objects)
    context_feature_columns = config.pop('context_feature_columns', None)
    if context_feature_columns:
      context_feature_columns = fc.deserialize_feature_columns(
          context_feature_columns, custom_objects=custom_objects)
    activation = activations.deserialize(
        config.pop('activation', None), custom_objects=custom_objects)
    return cls(
        rnn_layer=rnn_layer, sequence_feature_columns=sequence_feature_columns,
        context_feature_columns=context_feature_columns, activation=activation,
        **config)
Ejemplo n.º 9
0
def unpack(model, training_config, weights):
    """Make Keras Model exportable within pipeline ("picklable") 1/2"""
    restored_model = deserialize(model)
    if training_config is not None:
        restored_model.compile(
            **saving_utils.compile_args_from_training_config(training_config))
    restored_model.set_weights(weights)
    return restored_model
Ejemplo n.º 10
0
 def repeat_sequence(self, layers, repeats):
     for i in range(repeats):
         for lv in layers:
             layer = deserialize(lv)
             layer._name = layer.name + '_repeat_{}'.format(i + 1)
             self.layer_name = layer.name
             self.layer_num += 1
             self.model.add(layer)
Ejemplo n.º 11
0
 def repeat_networks(self, layers, repeats):
     for i in range(repeats):
         for lv in layers:
             layer = deserialize(lv)
             layer._name = layer.name + '_repeat_{}'.format(i + 1)
             self.layer_name = layer.name
             self.layer_num += 1
             output = layer(self.model.output)
             self.model = Model(inputs=self.model.inputs, outputs=output)
Ejemplo n.º 12
0
 def from_config(cls, config):
     ei = config.pop('embedding_initializer')
     if ei:
         ei = np.array(ei)
     if config['output_layer_config'] is not None:
         output_layer = layer_module.deserialize(
             config.pop('output_layer_config'), custom_objects=globals())
     else:
         config.pop('output_layer_config')
         output_layer = None
     return cls(embedding_initializer=ei,
                output_layer=output_layer,
                **config)
Ejemplo n.º 13
0
def model_from_json(json_string, custom_objects=None):
  """Parses a JSON model configuration file and returns a model instance.

  Arguments:
      json_string: JSON string encoding a model configuration.
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.

  Returns:
      A Keras model instance (uncompiled).
  """
  config = json.loads(json_string)
  from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
  return deserialize(config, custom_objects=custom_objects)
Ejemplo n.º 14
0
def model_from_json(json_string, custom_objects=None):
    """Parses a JSON model configuration file and returns a model instance.

  Arguments:
      json_string: JSON string encoding a model configuration.
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.

  Returns:
      A Keras model instance (uncompiled).
  """
    config = json.loads(json_string)
    from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
    return deserialize(config, custom_objects=custom_objects)
Ejemplo n.º 15
0
 def from_config(cls, config, custom_objects=None):
   if 'name' in config:
     name = config['name']
     build_input_shape = config.get('build_input_shape')
     layer_configs = config['layers']
   else:
     name = None
     build_input_shape = None
   model = cls(name=name)
   for layer_config in layer_configs:
     layer = layer_module.deserialize(layer_config,
                                      custom_objects=custom_objects)
     model.add(layer)
   if not model.inputs and build_input_shape:
     model.build(build_input_shape)
   return model
Ejemplo n.º 16
0
 def from_config(cls, config, custom_objects=None):
   if 'name' in config:
     name = config['name']
     build_input_shape = config.get('build_input_shape')
     layer_configs = config['layers']
   else:
     name = None
     build_input_shape = None
   model = cls(name=name)
   for layer_config in layer_configs:
     layer = layer_module.deserialize(layer_config,
                                      custom_objects=custom_objects)
     model.add(layer)
   if not model.inputs and build_input_shape:
     model.build(build_input_shape)
   return model
Ejemplo n.º 17
0
def load_model(path: str,
               custom_objects: Optional[Dict[str, object]] = None) -> Model:
    if not custom_objects:
        custom_objects = all_custom_objects
    else:
        custom_objects = {**all_custom_objects, **custom_objects}

    with open(path + "/assets/saved_model.json") as f:
        model_config = json.load(f)
    try:
        del model_config["config"]["layers"][0]["config"]["ragged"]
    except KeyError:
        pass

    model: Model = deserialize(model_config, custom_objects=custom_objects)
    model.load_weights(path + "/variables/variables")
    return model
Ejemplo n.º 18
0
def unpack_keras_model(model, training_config, weights):
    """Creates a new Keras model object using the input
    parameters.

    Returns
    -------
    Model
        A copy of the input Keras Model,
        compiled if the original was compiled.
    """
    restored_model = deserialize(model)
    if training_config is not None:
        restored_model.compile(
            **saving_utils.compile_args_from_training_config(training_config))
    restored_model.set_weights(weights)
    restored_model.__reduce_ex__ = pack_keras_model.__get__(restored_model)
    return restored_model
Ejemplo n.º 19
0
 def add(self, branch_rdd, repeats=0):
     branch_model_config = json.loads(
         getattr(branch_rdd.first(), MODEL_CONFIG))
     model_config = json.loads(getattr(self.model_rdd.first(),
                                       MODEL_CONFIG))
     model = self.convert2model(model_config)
     x = model.output
     for i in range(repeats):
         for lv in branch_model_config['layers']:
             layer = deserialize(lv)
             if isinstance(layer, InputLayer):
                 continue
             layer._name = layer.name + '_repeat_{}'.format(i + 1)
             self.layer_name = layer.name
             self.layer_num += 1
             x = layer(x)
     self.model = Model(inputs=model.inputs, outputs=x)
     return self.model2df(self.model)
Ejemplo n.º 20
0
 def from_config(cls, config, custom_objects=None):
   if 'name' in config:
     name = config['name']
     build_input_shape = config.get('build_input_shape')
     layer_configs = config['layers']
   else:
     name = None
     build_input_shape = None
     layer_configs = config
   model = cls(name=name)
   for layer_config in layer_configs:
     layer = layer_module.deserialize(layer_config,
                                      custom_objects=custom_objects)
     model.add(layer)
   if not model.inputs and build_input_shape:
     model.build(build_input_shape)
   if not model._is_graph_network:
     # Still needs to be built when passed input data.
     model.built = False
   return model
Ejemplo n.º 21
0
def model_from_yaml(yaml_string, custom_objects=None):
    """Parses a yaml model configuration file and returns a model instance.

  Arguments:
      yaml_string: YAML string encoding a model configuration.
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.

  Returns:
      A Keras model instance (uncompiled).

  Raises:
      ImportError: if yaml module is not found.
  """
    if yaml is None:
        raise ImportError('Requires yaml module installed.')
    config = yaml.load(yaml_string)
    from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
    return deserialize(config, custom_objects=custom_objects)
Ejemplo n.º 22
0
def model_from_yaml(yaml_string, custom_objects=None):
  """Parses a yaml model configuration file and returns a model instance.

  Arguments:
      yaml_string: YAML string encoding a model configuration.
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.

  Returns:
      A Keras model instance (uncompiled).

  Raises:
      ImportError: if yaml module is not found.
  """
  if yaml is None:
    raise ImportError('Requires yaml module installed (`pip install pyyaml`).')
  config = yaml.load(yaml_string)
  from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
  return deserialize(config, custom_objects=custom_objects)
Ejemplo n.º 23
0
 def from_config(cls, config, custom_objects=None):
     if 'name' in config:
         name = config['name']
         build_input_shape = config.get('build_input_shape')
         layer_configs = config['layers']
     else:
         name = None
         build_input_shape = None
         layer_configs = config
     model = cls(name=name)
     for layer_config in layer_configs:
         layer = layer_module.deserialize(layer_config,
                                          custom_objects=custom_objects)
         model.add(layer)
     if not model.inputs and build_input_shape:
         model.build(build_input_shape)
     if not model._is_graph_network:
         # Still needs to be built when passed input data.
         model.built = False
     return model
Ejemplo n.º 24
0
        def _unpack_obj(obj):
            """Recursively unpacks objects.
            """
            if isinstance(obj, SavedKerasModel):
                restored_model = deserialize(obj.model)
                training_config = obj.training_config
                restored_model.compile(
                    **saving_utils.compile_args_from_training_config(
                        training_config))
                restored_model.set_weights(obj.weights)
                return restored_model
            if hasattr(obj, "__dict__"):
                for key, val in obj.__dict__.items():
                    obj.__dict__[key] = _unpack_obj(val)
                return obj
            if isinstance(obj, (list, tuple)):
                obj_type = type(obj)
                new_obj = obj_type([_unpack_obj(o) for o in obj])
                return new_obj

            return obj  # not much we can do at this point, cross fingers
Ejemplo n.º 25
0
def model_from_config(config, custom_objects=None):
  """Instantiates a Keras model from its config.

  Arguments:
      config: Configuration dictionary.
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.

  Returns:
      A Keras model instance (uncompiled).

  Raises:
      TypeError: if `config` is not a dictionary.
  """
  if isinstance(config, list):
    raise TypeError('`model_from_config` expects a dictionary, not a list. '
                    'Maybe you meant to use '
                    '`Sequential.from_config(config)`?')
  from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
  return deserialize(config, custom_objects=custom_objects)
Ejemplo n.º 26
0
def model_from_yaml(yaml_string, custom_objects=None):
    """Parses a yaml model configuration file and returns a model instance.

  Usage:

  >>> model = tf.keras.Sequential([
  ...     tf.keras.layers.Dense(5, input_shape=(3,)),
  ...     tf.keras.layers.Softmax()])
  >>> try:
  ...   import yaml
  ...   config = model.to_yaml()
  ...   loaded_model = tf.keras.models.model_from_yaml(config)
  ... except ImportError:
  ...   pass

  Args:
      yaml_string: YAML string or open file encoding a model configuration.
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.

  Returns:
      A Keras model instance (uncompiled).

  Raises:
      ImportError: if yaml module is not found.
  """
    if yaml is None:
        raise ImportError(
            'Requires yaml module installed (`pip install pyyaml`).')
    # The method unsafe_load only exists in PyYAML 5.x+, so which branch of the
    # try block is covered by tests depends on the installed version of PyYAML.
    #try:
    # PyYAML 5.x+
    #  config = yaml.safe_load(yaml_string)
    #except AttributeError:
    #  config = yaml.safe_load(yaml_string)
    config = yaml.safe_load(yaml_string)
    from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
    return deserialize(config, custom_objects=custom_objects)
Ejemplo n.º 27
0
def model_from_json(json_string, custom_objects=None):
    """Parses a JSON model configuration string and returns a model instance.

  Usage:

  >>> model = tf.keras.Sequential([
  ...     tf.keras.layers.Dense(5, input_shape=(3,)),
  ...     tf.keras.layers.Softmax()])
  >>> config = model.to_json()
  >>> loaded_model = tf.keras.models.model_from_json(config)

  Args:
      json_string: JSON string encoding a model configuration.
      custom_objects: Optional dictionary mapping names
          (strings) to custom classes or functions to be
          considered during deserialization.

  Returns:
      A Keras model instance (uncompiled).
  """
    config = json_utils.decode(json_string)
    from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
    return deserialize(config, custom_objects=custom_objects)
Ejemplo n.º 28
0
 def from_config(cls, config, custom_objects=None):
   model = cls()
   for conf in config:
     layer = layer_module.deserialize(conf, custom_objects=custom_objects)
     model.add(layer)
   return model
Ejemplo n.º 29
0
 def from_config(cls, config, custom_objects=None):
     cell = layers.deserialize(config.pop("cell"),
                               custom_objects=custom_objects)
     layer = cls(cell, **config)
     return layer
Ejemplo n.º 30
0
 def from_config(cls, config, custom_objects=None):
     internal_layer = layers.deserialize(config.pop('layer'),
                                         custom_objects=custom_objects)
     layer = cls(internal_layer, **config)
     return layer
Ejemplo n.º 31
0
 def from_config(cls, config, custom_objects=None):
     config['kernel_function'] = deserialize(
         config.pop('kernel_function'),
         custom_objects=custom_objects,
     )
     return cls(**config)