本地运行TensorFlow 2目标检测脚本遇TypeError错误求解决
我在Windows系统上成功安装了带目标检测模块的TensorFlow 2,运行以下命令启动训练:
python model_main_tf2.py --pipeline_config_path=ssd_efficientdet_d0_512x512_coco17_tpu-8.config --model.dir=training --alsologtostderr
出现如下报错信息:
W0724 22:39:24.257511 12060 deprecation.py:350] From C:\Users\malde\anaconda3\envs\tf2\lib\site-packages\tensorflow\python\util\dispatch.py:1176: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Usetf.castinstead.
Traceback (most recent call last):
File "C:\Users\malde\Documents\TF2\models\research\object_detection\model_main_tf2.py", line 114, in
tf.compat.v1.app.run()
File "C:\Users\malde\anaconda3\envs\tf2\lib\site-packages\tensorflow\python\platform\app.py", line 36, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "C:\Users\malde\anaconda3\envs\tf2\lib\site-packages\absl\app.py", line 308, in run
_run_main(main, args)
File "C:\Users\malde\anaconda3\envs\tf2\lib\site-packages\absl\app.py", line 254, in _run_main
sys.exit(main(argv))
File "C:\Users\malde\Documents\TF2\models\research\object_detection\model_main_tf2.py", line 105, in main
model_lib_v2.train_loop(
File "C:\Users\malde\anaconda3\envs\tf2\lib\site-packages\object_detection\model_lib_v2.py", line 591, in train_loop
os.path.join(model_dir, 'train'))
File "C:\Users\malde\anaconda3\envs\tf2\lib\ntpath.py", line 78, in join
path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not NoneType
我尝试过重建csv文件,但未能解决问题,求可行的解决方案。
解决方案
- 修正命令参数拼写:命令中的
--model.dir是错误写法,正确参数应为--model_dir(使用下划线而非点)。TensorFlow目标检测训练脚本的模型保存路径参数是--model_dir,错误参数会导致脚本无法识别,进而使model_dir变量为None,触发路径拼接的TypeError。 - 确认目录存在:确保当前工作目录下的
training文件夹已提前创建,避免因路径不存在引发其他问题。 - 忽略弃用警告:报错中的
to_float弃用提示属于TensorFlow版本兼容信息,不影响训练流程,无需处理。
内容的提问来源于stack exchange,提问作者Timcho

