Ejemplo n.º 1
0
  def create_hooks(
      self, t2r_model, estimator,
      export_generator
  ):
    export_generator.set_specification_from_model(t2r_model)
    warmup_requests_file = export_generator.create_warmup_requests_numpy(
        batch_sizes=self._batch_sizes_for_export,
        export_dir=estimator.model_dir)

    def _export_fn(export_dir):
      res = estimator.export_saved_model(
          export_dir_base=export_dir,
          serving_input_receiver_fn=export_generator
          .create_serving_input_receiver_numpy_fn(),
          assets_extra={'tf_serving_warmup_requests': warmup_requests_file})
      return res

    return [
        tf.contrib.tpu.AsyncCheckpointSaverHook(
            save_secs=self._save_secs,
            checkpoint_dir=estimator.model_dir,
            listeners=[
                checkpoint_hooks.LaggedCheckpointListener(
                    export_fn=_export_fn,
                    num_versions=self._num_versions,
                    export_dir=self._export_dir,
                    lagged_export_dir=self._lagged_export_dir)
            ])
    ]
Ejemplo n.º 2
0
    def create_hooks(
        self,
        t2r_model,
        estimator,
    ):
        if not self._export_dir and not self._lagged_export_dir:
            return []
        self._export_generator.set_specification_from_model(t2r_model)
        warmup_requests_file = self._export_generator.create_warmup_requests_numpy(
            batch_sizes=self._batch_sizes_for_export,
            export_dir=estimator.model_dir)

        in_feature_spec = t2r_model.get_feature_specification_for_packing(
            mode=tf.estimator.ModeKeys.PREDICT)
        in_label_spec = t2r_model.get_label_specification_for_packing(
            mode=tf.estimator.ModeKeys.PREDICT)
        t2r_assets = t2r_pb2.T2RAssets()
        t2r_assets.feature_spec.CopyFrom(in_feature_spec.to_proto())
        t2r_assets.label_spec.CopyFrom(in_label_spec.to_proto())

        def _export_fn(export_dir, global_step):
            """The actual closure function creating the exported model and assets."""
            t2r_assets.global_step = global_step
            tmpdir = tempfile.mkdtemp()
            t2r_assets_filename = os.path.join(
                tmpdir, tensorspec_utils.T2R_ASSETS_FILENAME)
            tensorspec_utils.write_t2r_assets_to_file(t2r_assets,
                                                      t2r_assets_filename)
            res = estimator.export_saved_model(
                export_dir_base=export_dir,
                serving_input_receiver_fn=self._export_generator.
                create_serving_input_receiver_numpy_fn(),
                assets_extra={
                    'tf_serving_warmup_requests': warmup_requests_file,
                    tensorspec_utils.T2R_ASSETS_FILENAME: t2r_assets_filename
                })
            return res

        return [
            contrib_tpu.AsyncCheckpointSaverHook(
                save_secs=self._save_secs,
                checkpoint_dir=estimator.model_dir,
                listeners=[
                    checkpoint_hooks.LaggedCheckpointListener(
                        export_fn=_export_fn,
                        num_versions=self._num_versions,
                        export_dir=self._export_dir,
                        lagged_export_dir=self._lagged_export_dir)
                ])
        ]
Ejemplo n.º 3
0
  def create_hooks(
      self, t2r_model, estimator,
      export_generator
  ):
    export_generator.set_specification_from_model(t2r_model)
    warmup_requests_file = export_generator.create_warmup_requests_numpy(
        batch_sizes=self._batch_sizes_for_export,
        export_dir=estimator.model_dir)

    def _export_fn(export_dir, global_step):
      """The actual closure function creating the exported model and assets."""
      tmpdir = tempfile.mkdtemp()
      global_step_pkl_filename = os.path.join(tmpdir, 'global_step.pkl')
      tensorspec_utils.write_global_step_to_file(global_step,
                                                 global_step_pkl_filename)
      res = estimator.export_saved_model(
          export_dir_base=export_dir,
          serving_input_receiver_fn=export_generator
          .create_serving_input_receiver_numpy_fn(),
          assets_extra={
              'tf_serving_warmup_requests': warmup_requests_file,
              'global_step.pkl': global_step_pkl_filename
          })
      return res

    return [
        tf.contrib.tpu.AsyncCheckpointSaverHook(
            save_secs=self._save_secs,
            checkpoint_dir=estimator.model_dir,
            listeners=[
                checkpoint_hooks.LaggedCheckpointListener(
                    export_fn=_export_fn,
                    num_versions=self._num_versions,
                    export_dir=self._export_dir,
                    lagged_export_dir=self._lagged_export_dir)
            ])
    ]
Ejemplo n.º 4
0
 def DefaultLaggedCheckpointListener(self):
     return checkpoint_hooks.LaggedCheckpointListener(
         export_fn=self._ExportFn,
         export_dir=self._export_dir,
         lagged_export_dir=self._lagged_export_dir,
         num_versions=3)