使用model_main_tf2.py训练TensorFlow模型时提前终止求助
问题描述
运行TensorFlow目标检测库的model_main_tf2.py训练模型时,出现两个异常情况:
- 弹出TensorFlow Addons(TFA)已停止开发、进入维护期至2024年5月的警告
- 训练启动后不到10分钟就提前终止
执行的训练命令:
# Run the command below from the content/models/research/object_detection directory """ PIPELINE_CONFIG_PATH=path/to/pipeline.config MODEL_DIR=path to training checkpoints directory NUM_TRAIN_STEPS=50000 SAMPLE_1_OF_N_EVAL_EXAMPLES=1 python model_main_tf2.py -- \ --model_dir=$MODEL_DIR --num_train_steps=$NUM_TRAIN_STEPS \ --sample_1_of_n_eval_examples=$SAMPLE_1_OF_N_EVAL_EXAMPLES \ --pipeline_config_path=$PIPELINE_CONFIG_PATH \ --alsologtostderr """ !python model_main_tf2.py --pipeline_config_path=/mydrive/customTF2/data/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config --model_dir=/mydrive/customTF2/training --alsologtostderr
训练输出日志(关键片段):
2023-05-14 22:08:38.006027: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT /usr/local/lib/python3.10/dist-packages/tensorflow_addons/utils/tfa_eol_msg.py:23: UserWarning: TensorFlow Addons (TFA) has ended development and introduction of new features. TFA has entered a minimal maintenance and release mode until a planned end of life in May 2024. Please modify downstream libraries to take dependencies from other repositories in our TensorFlow community (e.g. Keras, Keras-CV, and Keras-NLP). ... I0514 22:09:10.429069 140178526795520 api.py:459] feature_map_spatial_dims: [(40, 40), (20, 20), (10, 10), (5, 5), (3, 3)] ^C
解决方案
1. 解决训练提前终止问题
从日志末尾的^C可以判断,训练是被中断信号终止的,对应原因和解决方法:
- 手动误操作中断:如果是不小心按下了
Ctrl+C,直接重新执行训练命令即可 - 云端环境会话断开:如果在Colab、AWS SageMaker等云端环境运行,可能是会话超时或浏览器断开导致:
- 保持浏览器标签页处于活跃状态,避免触发自动断开机制
- 使用后台运行命令,脱离终端会话限制:
nohup python model_main_tf2.py --pipeline_config_path=/mydrive/customTF2/data/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config --model_dir=/mydrive/customTF2/training --num_train_steps=50000 --alsologtostderr > training.log 2>&1 & - 查看实时训练日志:
tail -f training.log
- 未指定训练总步数:日志显示
Maybe overwriting train_steps: None,说明训练没有设置终止步数,可能在跑完一轮数据后自动停止。需要在命令中添加--num_train_steps=50000(根据需求调整步数),确保训练持续到指定轮次
2. 处理TensorFlow Addons警告
这个警告只是通知TFA进入维护期,不会影响训练正常运行,可以暂时忽略。如果要彻底消除警告:
- 升级TensorFlow目标检测库到最新版本,部分新版本已替换TFA依赖到Keras-CV等替代库
- 手动修改目标检测库中使用TFA的代码,替换为Keras生态的等价实现(此操作需要一定代码修改能力,非必要不推荐)
内容的提问来源于stack exchange,提问作者hammale mourad
相关产品推荐
相关产品推荐

