前提
使用vscode通过luanch.json文件对BEVFormer模型进行调试。
但是BEVFormer 测试模型的命令为:
./tools/dist_test.sh ./projects/configs/bevformer/bevformer_base.py ./ckpts/bevformer_r101_dcn_24ep.pth 1
难点
项目使用.sh文件启动,难以使用vscode进行调试
方法
将dist_test.sh脚本修改为python脚本。
注:你需要在tools文件下创建test_py.py,内容为:
下述代码为博主的环境,具体需要替换config,checkpoint等为实际地址。
import os
import subprocess
from os.path import dirname, join
# 设置环境变量和参数
config = "./projects/configs/bevformer/bevformer_base.py"
checkpoint = "./ckpts/bevformer_r101_dcn_24ep.pth"
gpus = "1"
port = "29503"
# 指定Python解释器路径
python_interpreter = "/home/nuna/.conda/envs/bev/bin/python"
# 获取当前脚本所在目录的父目录,并更新PYTHONPATH环境变量
current_script_dir = dirname(__file__)
parent_dir = join(current_script_dir, "..")
os.environ['PYTHONPATH'] = f"{parent_dir}:{os.environ.get('PYTHONPATH', '')}"
# 构建命令行命令
command = [
python_interpreter, '-m', 'torch.distributed.launch',
'--nproc_per_node=' + gpus,
'--master_port=' + port,
join(dirname(__file__), 'test.py'), config, checkpoint,
'--launcher', 'pytorch'
] + ['--eval', 'bbox']
# 执行命令
subprocess.run(command)
然后添加launch.json文件
由于所有运行所需的参数都以在test_py.py中进行赋值,那么在json文件无需设置args。
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Your Model",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/tools/test_py.py",
// "args": [
// "./projects/configs/bevformer/bevformer_base.py",
// "./ckpts/bevformer_r101_dcn_24ep.pth",
// "1"
// ],
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
}
],
}