debug模式中调好,正常执行不生效

demo.py文件的关键内容如下:

    if args.input:
        if len(args.input) == 1:
            args.input = glob.glob(os.path.expanduser(args.input[0]))[:200]
            assert args.input, "The input path(s) was not found"

1.我用debug模式执行demo.py文件,用了下面这个json配置文件。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [
                "--config-file", "configs/sparse_inst_r50_base.yaml", "--input","datasets/coco/val2017/*", "--output" ,"image_results", "--opt" ,"MODEL.WEIGHTS","sparse_inst_r50_base_ff9809.pth" ,"INPUT.MIN_SIZE_TEST", "512"
            ]
            
        }
    ]
}

[:200]生效, args.input的值被改为了前两百个。

2.但是输入

python demo.py --config-file configs/sparse_inst_r50_base.yaml --input datasets/coco/val2017/* --output image_results --opt MODEL.WEIGHTS sparse_inst_r50_base_ff9809.pth INPUT.MIN_SIZE_TEST 512

直接执行,发现demo.py中的[:200]不生效,args.input的值没有被改为前两百个,为什么会这样?请给出解决方法

出现这种情况的原因是:在命令行中运行脚本时,参数的解析可能与在 VSCode 的 debug 配置中不同。具体来说,当你直接在命令行运行 python demo.py ... 时,args.input 并不是一个包含单个路径模式的列表(如 ["datasets/coco/val2017/*"]),而是已经扩展后的完整文件路径列表。这是因为大多数命令行解析器会自动展开通配符(如 *)。

原因分析

出现这种情况的原因是:在命令行中运行脚本时,参数的解析可能与在 VSCode 的 debug 配置中不同。具体来说,当你直接在命令行运行 python demo.py ... 时,args.input 并不是一个包含单个路径模式的列表(如 ["datasets/coco/val2017/*"]),而是已经扩展后的完整文件路径列表。这是因为大多数命令行解析器会自动展开通配符(如 *)。

  1. 命令行解析行为差异

    • 在命令行中运行时,参数 --input datasets/coco/val2017/* 被 Shell 自动展开成一个完整的文件路径列表,比如 datasets/coco/val2017/file1.jpg datasets/coco/val2017/file2.jpg 等。
    • 这样,传入 demo.pyargs.input 参数已经是一个包含多个具体文件路径的列表,因此 len(args.input) > 1
  2. 在 VSCode Debug 模式中

    • 使用的 launch.json 配置中传递的参数 --input datasets/coco/val2017/* 被作为一个单独的字符串传递给 args.input,因此不会被 Shell 自动展开。在你的代码中,args.input 初始值是一个长度为 1 的列表(["datasets/coco/val2017/*"]),所以 len(args.input) == 1 条件成立,然后代码执行了 glob.glob(...),将路径扩展并限制为前 200 个文件。

解决方法:

if args.input:
    # 如果只有一个输入路径,并且它包含通配符(* 或 ?)
    if len(args.input) == 1 and ('*' in args.input[0] or '?' in args.input[0]):
        # 展开通配符路径,并限制为前200个
        args.input = glob.glob(os.path.expanduser(args.input[0]))[:200]
        assert args.input, "The input path(s) was not found"
    elif len(args.input) > 1:
        # 如果已经是文件列表,则限制为前200个
        args.input = args.input[:200]

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值