【问题标题】:Tensorflow object_detection correct way to save and load fine tune modelTensorFlow object_detection 保存和加载微调模型的正确方法
【发布时间】:2021-04-04 09:01:51
【问题描述】:

我正在使用 colabs 教程中的 this 示例来微调模型,训练后我想保存模型并使用以下命令加载到我的本地计算机上:

ckpt_manager = tf.train.CheckpointManager(ckpt, directory="test_data/checkpoint/", max_to_keep=5)
...
...
print('Done fine-tuning!')

ckpt_manager.save()
print('Checkpoint saved!')

但是在我的本地计算机上使用检查点文件还原后没有检测到任何对象(分数太低)

我也试过

tf.saved_model.save(detection_model, '/content/new_model/')

然后加载:

detection_model = tf.saved_model.load('/saved_model_20201226/')

input_tensor = tf.convert_to_tensor(image, dtype=tf.float32)
detections = detection_model(input_tensor)

给我这个错误: TypeError: '_UserObject' 对象不可调用

保存和加载微调模型的正确方法是什么?

编辑 1: 等待保存新的管道配置,之后终于成功了! 这是我的答案:

# Save new pipeline config
new_pipeline_proto = config_util.create_pipeline_proto_from_configs(configs)
config_util.save_pipeline_config(new_pipeline_proto, '/content/new_config')
exported_ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt_manager = tf.train.CheckpointManager(
exported_ckpt, directory="test_data/checkpoint/", max_to_keep=5)
...
...
print('Done fine-tuning!')

ckpt_manager.save()
print('Checkpoint saved!')

【问题讨论】:

  • 我总是使用model.save(saving_dir) 并使用model = keras.models.load_model(saving_dir) 加载(如果您有自定义对象,则应该传递它们)。或者只是通过model.save_weights(saving_path) 保存权重并通过model.load_weights(saving_path) 加载它
  • Tks @amin 我也试过了,但是在恢复模型之后,我有一个来自 object_detection API 的 SSDMetaArch 对象,而不是 Keras 模型对象,我不能使用那个方法。无论如何,我终于设法解决了。

标签: tensorflow machine-learning deep-learning object-detection


【解决方案1】:

等待保存新的管道配置,之后终于成功了!这是我的答案:

# Save new pipeline config
new_pipeline_proto = config_util.create_pipeline_proto_from_configs(configs)
config_util.save_pipeline_config(new_pipeline_proto, '/content/new_config')

exported_ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt_manager = tf.train.CheckpointManager(
exported_ckpt, directory="test_data/checkpoint/", max_to_keep=5)
...
...
print('Done fine-tuning!')

ckpt_manager.save()
print('Checkpoint saved!')

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    相关资源
    最近更新 更多