当前位置: 首页 > article >正文

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]


http://www.kler.cn/news/283231.html

相关文章:

  • 安卓-广播LocalBroadcastManager
  • 标准c++---2
  • 什么是Socks5代理协议?揭秘其优势与应用
  • UDP英译汉网络词典
  • 在VB.net中,LINQ有什么查询表达式,举例说明
  • 掌握 Rust 中的 YAML 魔法:Serde_yaml 使用指南
  • QT在控件graphicsView中绘制箭头
  • Native开发与逆向第三篇 - hook JNI函数NewStringUTF
  • 构建视频生态技术基石:EasyCVR平台如何打破视频流媒体协议壁垒
  • kubeadm部署k8s1.25.3一主二从集群(Containerd)
  • 【GIT】说一说 Git 的常见命令和实践
  • uniapp中使用弹出框控制Tab栏区域显示与隐藏
  • Python编程实战营:四款实用小项目助你快速入门,从零开始打造你的个人项目集!
  • 【大模型理论篇】RoPE旋转位置编码底层数学原理分析
  • 深入理解Spring Boot的开箱即用与自动装配特性
  • 【爬虫软件】YouTube关键词搜索采集工具
  • 2024如何开始进入美业?美业创业步骤分享|博弈美业系统管理系统源码
  • Spark-第八周
  • 浅谈【数据结构】树与二叉树之哈夫曼树
  • 【Java设计模式】集合管道模式:简化数据操作
  • 买对不买贵,宠物空气净化器应该怎么选才能选到好的产品
  • 大数据技术之Flume 企业开发案例——负载均衡和故障转移(6)
  • SIGFPE (Arithmetic exception)
  • [Meachines] [Medium] Bastard Drupal 7 Module Services-RCE+MS15-051权限提升
  • 参数高效的模型微调
  • 【学习笔记】技术分析-华为智驾控制器MDC Pro 610分析
  • 怎么自定义spring security对用户信息进行校验及密码的加密校验
  • 关于springboot的异常处理以及源码分析(二)
  • 【面试04】ARM架构问题
  • 从 MLOps 到 LMOps 的关键技术嬗变