【问题标题】:How to set the checkpoint for fine-tuning如何设置微调的检查点
【发布时间】:2019-10-10 22:12:25
【问题描述】:

我发现在训练开始时从model_zoo重新训练模型(ssd_mobilenetv2)时的损失非常大,而validation_set的准确性很好。训练日志如下:

日志不能来自经过训练的模型。我怀疑它不会加载检查点来进行微调。请帮助我如何对同一数据集上的训练模型进行微调。我根本没有修改网络结构。

我在 pipeline.config 中设置检查点路径如下: fine_tune_checkpoint:"//ssd_mobilenet_v2_coco_2018_03_29/model.ckpt" 如果我将 model_dir 设置为我下载的目录,它不会训练,因为 global_train_step 大于 max_step。然后我放大max_step,可以看到checkpoint恢复参数的日志。但它会遇到无法恢复某些参数的错误。 所以我将 model_dir 设置为一个空目录。可以正常训练,但是step0的损失会很大。而且验证结果很差

在 pipeline.config 中

fine_tune_checkpoint: "/ssd_mobilenet_v2_coco_2018_03_29/model.ckpt"
num_steps: 200000
fine_tune_checkpoint_type: "detection"

训练脚本

model_dir = '/ssd_mobilenet_v2_coco_2018_03_29/retrain0524

pipeline_config_path = '/ssd_mobilenet_v2_coco_2018_03_29/pipeline.config'

checkpoint_dir = '/ssd_mobilenet_v2_coco_2018_03_29/model.ckpt'

num_train_steps = 300000
config = tf.estimator.RunConfig(model_dir=model_dir)
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,    
    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))
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)

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

INFO:tensorflow:loss = 356.25497, step = 0 信息:张量流:global_step/sec:1.89768 信息:张量流:损失 = 11.221423,步长 = 100(52.700 秒) 信息:张量流:global_step/sec:2.21685 信息:张量流:损失 = 10.329516,步长 = 200(45.109 秒)

【问题讨论】:

  • 最好把完整的配置文件贴在这里。比如from_checkpoint_type设置为classification,部分参数不会恢复到checkpoint,所以一开始loss可能会很高。
  • 感谢您的回复,我只是把pipeline.config的内容填满了,pipeline文件从model zoo下载后除了dataset和labels的路径没有修改。我还有什么地方需要修改吗?我怎么知道模型的参数已经加载了?
  • 可以不使用checkpoint,直接从零开始训练,然后在设置checkpoint的时候对比一下结果是否有差异。我预计这次的训练损失会大得多(相差几个订单)
  • 感谢您的建议。我把checkpoint去掉,发现第一个loss大于400,还有没有找到checkpoint的警告。尽管我已经正确加载了检查点,但为什么第一次损失如此之大,我感到很困惑。
  • 我只是将 val_step 设置为 1 并且学习率足够小。加载检查点后。每次迭代都会对其进行验证。但是我发现AP都是零。这表明预训练的模型没有正确加载。

标签: tensorflow object-detection-api


【解决方案1】:

如果初始训练损失为 400,则模型很可能从检查点成功恢复,只是与检查点不完全相同。

Here是ssd模型的restore_map函数,注意即使你设置了fine_tune_checkpoint_type : detection,甚至提供了完全相同的模型检查点,仍然只有feature_extractor范围内的变量被恢复。要从检查点恢复尽可能多的变量,您必须在配置文件中设置 load_all_detection_checkpoint_vars: true。

def restore_map(self,
              fine_tune_checkpoint_type='detection',
              load_all_detection_checkpoint_vars=False):

if fine_tune_checkpoint_type not in ['detection', 'classification']:
  raise ValueError('Not supported fine_tune_checkpoint_type: {}'.format(
      fine_tune_checkpoint_type))

if fine_tune_checkpoint_type == 'classification':
  return self._feature_extractor.restore_from_classification_checkpoint_fn(
      self._extract_features_scope)

if fine_tune_checkpoint_type == 'detection':
  variables_to_restore = {}
  for variable in tf.global_variables():
    var_name = variable.op.name
    if load_all_detection_checkpoint_vars:
      variables_to_restore[var_name] = variable
    else:
      if var_name.startswith(self._extract_features_scope):
        variables_to_restore[var_name] = variable

return variables_to_restore

【讨论】:

  • 非常感谢。我现在可以得到正确的损失。顺便说一句,你能告诉我你附加的文件的位置在哪里吗?我想用 morphNet 修改网络。 .如果你有什么建议请告诉我。谢谢
猜你喜欢
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多