所以我使用了此链接中的预训练权重:http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz 然后我将 pipeline.config 从下载链接复制到我的文件夹,因为我想将优化器更改为 adam 以在我自己的数据集上进行训练(链接中的管道正在使用momentum_optimizer)
这是我要训练的 pipeline.config 的代码:
optimizer {
adam_optimizer {
learning_rate {
cosine_decay_learning_rate {
learning_rate_base: 0.01
total_steps: 50000
warmup_learning_rate: 0.026666
warmup_steps: 1000
}
}
epsilon: 1e-8
}
use_moving_average: false
}
但是 cmd 提示给了我这个:
WARNING:tensorflow:Value in checkpoint could not be found in the restored object: (root).optimizer.iter
W0419 23:47:07.776149 17436 util.py:194] Value in checkpoint could not be found in the restored object: (root).optimizer.iter
WARNING:tensorflow:Value in checkpoint could not be found in the restored object: (root).optimizer.decay
W0419 23:47:07.777309 17436 util.py:194] Value in checkpoint could not be found in the restored object: (root).optimizer.decay
WARNING:tensorflow:Value in checkpoint could not be found in the restored object: (root).optimizer.momentum
W0419 23:47:07.779311 17436 util.py:194] Value in checkpoint could not be found in the restored object: (root).optimizer.momentum
model.load_weights(MY_CHECKPOINT_FILE).expect_partial()
一直在为我工作
我在执行以下几行时遇到了同样的错误:
python Tensorflow\models\research\object_detection\model_main_tf2.py --model_dir=Tensorflow\workspace\models\my_ssd_mobnet --pipeline_config_path=Tensorflow\workspace\models\my_ssd_mobnet\pipeline.config --num_train_steps=2000
更改步数对我有用!
对我有用的是进入模型检查点文件夹并删除所有以前的检查点,只留下管道配置文件。
对我有用的是进入模型检查点文件夹并删除所有以前的检查点,只留下管道配置文件。
我的功能如下:my_ssd_mobnet 上的地毯和消除的检查点以及单独的存档 pipline.config 和相关命令:python Tensorflow\models esearch\object_detection\model_main_tf2.py --model_dir=Tensorflow\workspace\models\my_ssd_mobnet --pipeline_config_path=Tensorflow\workspace\models\my_ssd_mobnet\pipeline.config --num_train_steps=2000
我删除了所有早期的检查点,这对我来说效果很好。您也可以更改检查点的路径。
正如this答案中提到的,我们需要增加
train_config.num_steps
和train_config.optimizer.momentum_optimizer.learning_rate.cosine_decay_learning_rate.total_steps
以使优化器正常工作。如果之前的检查点是使用 num_steps=30000
进行训练的,则应将 num_steps
增加到 50000
(任何大于 30000
的数字)。
train_config: {
...
num_steps: 50000 <--- Increment the steps here
...
optimizer {
momentum_optimizer: {
learning_rate: {
cosine_decay_learning_rate {
...
total_steps: 50000 <--- Should be equal to `num_steps`
...
}
}
发生这种情况是因为当您加载权重时,优化器的状态并未加载,如 [this][1] 线程中所示。正如它所说 - 只需在加载语句末尾添加
.except_partial()
,例如:
model.load_weights(checkpoint_file).expect_partial()
干杯 [1]:https://github.com/tensorflow/tensorflow/issues/43554#issuecomment-748820652
来自 Tensorflow 文档:
从 SavedModel 检查点加载
要从 SavedModel 加载值,只需传递 SavedModel 目录 到检查点。恢复:
model = tf.keras.Model(...) tf.saved_model.save(model, path) # or model.save(path, save_format='tf') checkpoint = tf.train.Checkpoint(model) checkpoint.restore(path).expect_partial()
这个例子在加载状态上调用expect_partial(),因为 从 Keras 保存的 SavedModel 通常会在 检查站。否则,程序会打印很多警告 退出时未使用的密钥。
来源:https://www.tensorflow.org/api_docs/python/tf/train/Checkpoint#restore