Exemple #1
0
    def test_export_keras_estimator(self, checkpoint_format):
        keras_model, (x_train, y_train), (
            _, _), train_input_fn, _ = get_resource_for_simple_model(
                model_type='sequential', is_evaluate=False)

        keras_model.compile(loss='categorical_crossentropy',
                            optimizer='adam',
                            metrics=['accuracy'])
        keras_model.fit(x_train, y_train, epochs=1)
        bias_value = keras.backend.get_value(keras_model.layers[0].bias)

        est_keras = keras_lib.model_to_estimator(
            keras_model=keras_model,
            model_dir=tempfile.mkdtemp(dir=self._base_dir),
            checkpoint_format=checkpoint_format)

        def serving_input_receiver_fn():
            feature_spec = {
                'dense_input':
                parsing_ops.FixedLenFeature([1], dtype=dtypes.float32)
            }
            return export_lib.build_parsing_serving_input_receiver_fn(
                feature_spec)

        # Try immediately exporting, testing that (1) exported values are the same,
        # and (2) estimator can be exported without saving a checkpoint into the
        # model directory.
        saved_model_dir = est_keras.export_saved_model(
            tempfile.mkdtemp(dir=self._base_dir), serving_input_receiver_fn())
        variables_path = saved_model_utils.get_variables_path(saved_model_dir)

        variable_name = 'dense/bias'
        if checkpoint_format == 'checkpoint':
            names_to_keys = saver_lib.object_graph_key_mapping(variables_path)
            variable_name = names_to_keys[variable_name]

        self.assertAllClose(
            bias_value, training.load_variable(variables_path, variable_name))

        # Export the estimator after training a bit.
        est_keras.train(input_fn=train_input_fn, steps=_TRAIN_SIZE / 16)
        saved_model_dir = est_keras.export_saved_model(
            tempfile.mkdtemp(dir=self._base_dir), serving_input_receiver_fn())
        variables_path = saved_model_utils.get_variables_path(saved_model_dir)
        self.assertNotAllClose(
            bias_value, training.load_variable(variables_path, variable_name))
 def get_variable_value(self, name):
   """Returns value of the variable given by name.
   Args:
     name: string or a list of string, name of the tensor.
   Returns:
     Numpy array - value of the tensor.
   Raises:
     ValueError: If the Estimator has not produced a checkpoint yet.
   """
   _check_checkpoint_available(self.model_dir)
   return training.load_variable(self.model_dir, name)
  def get_variable_value(self, name):
    """Returns value of the variable given by name.

    Args:
      name: string or a list of string, name of the tensor.

    Returns:
      Numpy array - value of the tensor.

    Raises:
      ValueError: If the Estimator has not produced a checkpoint yet.
    """
    _check_checkpoint_available(self.model_dir)
    return training.load_variable(self.model_dir, name)