文章目录

  • 1. 配置Debug
  • 方法1: 直接代码中设置参数
  • 方法2: 在launch.json中配置参数
  • 2. 使用debugpy工具调试
  • (1) debugpy 工具安装
  • (2) 配置launch.json文件
  • (3) 在程序中增加debugpy的代码
  • (4) 启动调试
  • 3. 调试快捷键
  • 3.1 删除所有断点快捷键设置


1. 配置Debug

我们训练yolov5代码时,一般会配置一些参数,比如模型权重文件--weights, 模型的配置文件--cfg, 以及训练的数据--data,
对应的训练脚本为:

训练train

CUDA_VISIBLE_DEVICES=0,1  python train.py   -- weights './yolov5s.pt' --cfg 'models\yolov5s.yaml' --data './data/coco128.yaml'
方法1: 直接代码中设置参数

那么对train.py 的代码进行Debug,如果不进行参数设置,直接Debug是会报错的。一种方法是手动在parse_opt函数中修改

--weights , --cfg , --data这三个参数,然后设置断点,按F5进行调试。很显然这种方式需要手动去改代码,不是很方便,由于测试改动了参数下次重新改回来,很容易忘记原来的参数设置。

vscode lua 编辑 vscode lua debug_vscode lua 编辑

方法2: 在launch.json中配置参数

点击右边Debug按钮, 选择创建launch.json文件。

vscode lua 编辑 vscode lua debug_python_02


此时显示launch.json的代码,如下所示:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",                          
    "configurations": [
        {
            "name": "Python: Current File",    
            "type": "python",                   
            "request": "launch",                
            "program": "${file}",               
            "console": "integratedTerminal",   
            "justMyCode": true                 
        }
    ]
}

launch.json中,配置调试需要的参数, 新增一个args变量,配置--weights, --data, --cfg等需要配置的参数.

{
   
    "version": "0.2.0",                               //指定了配置文件的版本
    "configurations": [
        {
            "name": "Python: Current File",           //指定了vscode中Debug的名称
            "type": "python",                         //指定了调试器的类型,这里是python调试器
            "request": "launch",					  //指定了调试请求的类型,这里是"launch",表示启动调试
            "program": "${file}",                     //指定了要调试程序的入口文件,这里使用了${file}变量,表示当前打开的文件
            "args": [                                 //终端传入传入的参数
            	"--weights", "./yolov5s.pt",
            	"--data", "./data/coco128.yaml",
            	"--cfg", "models\yolov5s.yaml",
            ],
            #"env":{"CUDA_VISIBLE_DEVICES":"0,1"}     //指定环境变量:比如"CUDA_VISIBLE_DEVICES":"0,1",表示使用0,1这两块GPU
            # "cmd":  "/home/yws/project/yolov5"      //指定项目的工作目录
            "console": "integratedTerminal",          //指定了调试器输出控制台的类型,在集成终端中输出
            "justMyCode": false                       //指定了是否只调试用户自己的代码,不调试第三方库的代码, 这里设置false,表示需要调试第三方库
        }
    ]
}

这样就完成了训练参数的配置,就可以打断点,按F5进行调试了,这个方式会比较方便点。

  • name: 运行和调试显示的名称,可以自行修改,比如修改为train yolov8
  • program: 调试的文件,默认为${file}(当前的文件)可以不改;也可以指定具体的调试文件比如tools/train.py, 路径相对于项目根目录
  • args: 指定运行文件,需要的参数
  • env: 可以通过CUDA_VISIBLE_DEVICES,指定训练使用的GPU,等价于: CUDA_VISIBLE_DEVICES="0,1" train.py, 支持DP训练模式,但DDP多GPU 并行训练调试不支持
  • justMyCode: 设为true的话,只调试项目中的代码,如果设为false的话可以调试python库文件中的代码。

2. 使用debugpy工具调试

大多数情况下,使用VSCode调试Python的体验已经非常好了, 有的Python程序并不是通过python直接启动的, 比如DDP 训练时使用用python -m torch.distributed.launch启动, 以及MMDet3D框架中使用torchpack dist-run -np 1 python tools/test.py启动,一般非常难调试, 此时就需要借助debugpy进行调试

#1. DDP 训练或测试
CUDA_VISIBLE_DEVICES=6,7 python -m torch.distributed.launch --nproc_per_node=2 train.py  
#2. MMDet3d中 DDP训练或测试
CUDA_VISIBLE_DEVICES=2 torchpack dist-run -np 1 python tools/test.py    train_result/configs.yaml      train_result/epoch_3.pth    --eval bbox    --out box.pkl

(1) debugpy 工具安装

pip install debugpy

(2) 配置launch.json文件

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach",
            "type": "python",
            "request": "attach",
           "connect": {
            "host": "localhost",
            "port":12361
           },
           "justMyCode": true
        }
    ]
}
  • 只需要配置下端口号port, 一般任意选择4-5位的数字均可

(3) 在程序中增加debugpy的代码

在调试的入口脚本的开头,增加几行代码

import debugpy
#保证host和端口一致,listen可以只设置端口。则为localhost,否则设置成(host,port)
debugpy.listen(12361)
print('wait debugger')
debugpy.wait_for_client()
print("Debugger Attached")

(4) 启动调试

  • 以MMDet3D中,假设我们需要调试test.py ,在终端执行如下脚本:
CUDA_VISIBLE_DEVICES=2 torchpack dist-run -np 1 python tools/test.py    train_result/configs.yaml      train_result/epoch_3.pth    --eval bbox    --out box.pkl

vscode lua 编辑 vscode lua debug_vscode_03


当出现出现wait debugger时,点击vscode工具栏中的debug工具,此时就可以进行调试了

vscode lua 编辑 vscode lua debug_python_04

3. 调试快捷键

3.1 删除所有断点快捷键设置

在调试的时候,通过设置删除所有断点快捷键,可以提高调试效率

vscode lua 编辑 vscode lua debug_vscode lua 编辑_05

  • 找到左下角的设置图标,选择Keyboard Shortcuts, 然后输入breakpoint, 查找所有断点的快捷键
  • 找到Remove All Breakpoints, 然后设置快捷键,最后通过回车enter确认, 我这里设置win+delete删除所有断点