在TensorFlow对象检测API中检测时期结束

时间:2018-09-05 18:52:49

标签: tensorflow

如何在TF对象检测API中检测一个历元的结束(即完成对数据集的一次完整扫描)?这对于在自定义检测模型中进行某些簿记或某些内部处理(即重新设置权重)可能很有用

1 个答案:

答案 0 :(得分:0)

您可能想实现tf.estimator.SessionRunHook

为此,您需要通过添加hook参数在tf.estimator.TrainSpec处编辑model_lib.py,或者创建自己的训练文件并覆盖train_spec,然后再将其传递给tf.estimator.train_and_evaluate。

使用添加到Tensorflow对象检测API的ProfilerHook的示例: (对于SessionRunHook应该是类似的)

config = tf.estimator.RunConfig(model_dir=model_dir, save_checkpoints_steps=save_checkpoints_steps,
                            save_checkpoints_secs=save_checkpoints_secs, keep_checkpoint_max=keep_checkpoint_max,
                            log_step_count_steps=log_step_count_steps)

train_and_eval_dict = model_lib.create_estimator_and_inputs(
      run_config=config,
      hparams=model_hparams.create_hparams(hparams_overrides),
      pipeline_config_path=pipeline_config_path,
      config_override = cfg_override,
      train_steps=num_train_steps,
      sample_1_of_n_eval_examples=sample_1_of_n_eval_examples,
      sample_1_of_n_eval_on_train_examples=sample_1_of_n_eval_on_train_examples,
      save_final_config=save_final_config)

estimator = train_and_eval_dict['estimator']
train_input_fn = train_and_eval_dict['train_input_fn']
eval_input_fns = train_and_eval_dict['eval_input_fns']
eval_on_train_input_fn = train_and_eval_dict['eval_on_train_input_fn']
predict_input_fn = train_and_eval_dict['predict_input_fn']
train_steps = train_and_eval_dict['train_steps']

train_spec, eval_specs = model_lib.create_train_and_eval_specs(
  train_input_fn,
  eval_input_fns,
  eval_on_train_input_fn,
  predict_input_fn,
  train_steps,
  eval_on_train_data=False)

profile_hook = tf.train.ProfilerHook(save_steps=profiler_save_step, save_secs=None, output_dir=profiler_output_dir, 
                                     show_dataflow=True, show_memory=True)

train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn
                                    ,max_steps=train_steps
                                    ,hooks=[profile_hook])

tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])