TensorFlow2预构建FRCNN模型训练牙齿数据集时Checkpoint报错求助
TensorFlow2 FRCNN预训练模型加载Checkpoint时AssertionError解决方法
问题概述
使用TensorFlow2基于全景X光图像进行牙齿分类,已将图像缩至640x640并对应调整标注,测试运行正常,但加载预构建FRCNN模型恢复Checkpoint执行训练时,触发AssertionError,提示发现265个未与Checkpoint值绑定的Python对象。
环境依赖版本
- hdfs-2.7.0
- keras-2.11.0
- pyyaml-5.4.1
- tensorboard-2.11.2
- tensorflow-2.11.0
核心问题分析
从代码和错误信息来看,存在两个关键问题:
- 预训练模型下载与解压不匹配:代码指定使用resnet50架构的FRCNN,但下载链接指向resnet152模型,解压包却对应resnet50,导致模型文件与架构不匹配。
- fine_tune_checkpoint_type配置错误:将预训练检测模型的checkpoint类型设为
classification,而预训练FRCNN是检测模型,类型不匹配导致大量变量无法与checkpoint绑定。
解决方案
1. 修正预训练模型下载与解压命令
确保下载的模型与使用的架构一致,若使用resnet50的配置文件,需下载对应模型:
!python object_detection/builders/model_builder_tf2_test.py !python generate_tfrecord.py --csv_input=images/train/annotation.txt --image_dir=images/train --output_path=train.record !python generate_tfrecord.py --csv_input=images/test/annotation.txt --image_dir=images/test --output_path=test.record model_name = 'faster_rcnn_resnet50_v1_640x640_coco17_tpu-8' !wget http://download.tensorflow.org/models/object_detection/tf2/20200711/{model_name}.tar.gz !tar -xf {model_name}.tar.gz
2. 修正配置文件中的checkpoint类型
将fine_tune_checkpoint_type设为detection,匹配预训练检测模型的类型:
# 注释错误的classification设置,改为detection # config = re.sub('fine_tune_checkpoint_type: "detection"', # 'fine_tune_checkpoint_type: "{}"'.format('classification'), config) config = re.sub('fine_tune_checkpoint_type: "classification"', 'fine_tune_checkpoint_type: "{}"'.format('detection'), config)
3. 确保模型路径正确
修正fine_tune_checkpoint路径,指向解压后的正确checkpoint文件:
fine_tune_checkpoint = '/content/' + model_name + '/checkpoint/ckpt-0'
验证步骤
- 删除之前生成的
model_config.config和training/目录下的旧文件 - 重新运行配置文件生成代码
- 执行训练命令:
model_dir = 'training/' pipeline_config_path = 'model_config.config' !python /content/tensorflow-models/research/object_detection/model_main_tf2.py \ --pipeline_config_path={pipeline_config_path} \ --model_dir={model_dir} \ --alsologtostderr \ --num_train_steps={num_steps} \ --sample_1_of_n_eval_examples=1 \ --num_eval_steps={num_eval_steps}
内容的提问来源于stack exchange,提问作者seekme_94
相关产品推荐
相关产品推荐

