maskrcnn_benchmark 代码详解之 boxlist_ops.py

本文深入解析maskrcnn_benchmark库中boxlist_ops.py文件,涵盖边框列表的非极大抑制、去除小边框、计算IoU及合并等关键操作,为理解目标检测中的深度学习算法提供帮助。

前言:

  与Bounding Box有关的操作有很多,例如对边框列表进行非极大线性抑制、去除过小的边框、计算边框之间的Iou以及对两个边框列表进行合并等操作。在maskrcnn_benchmark中,这些操作都得以实现,其具体代码以及解释如下:

# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import torch

from .bounding_box import BoxList

from maskrcnn_benchmark.layers import nms as _box_nms


def boxlist_nms(boxlist, nms_thresh, max_proposals=-1, score_field="scores"):
    """
    Performs non-maximum suppression on a boxlist, with scores specified
    in a boxlist field via score_field.在边框列表上进行非极大线性抑制

    参数:
        boxlist(BoxList):含有边框目标得分,原始图像大小,预测边框列表等信息的边框列表
        nms_thresh (float):非极大线性抑制的阈值
        max_proposals (int): if > 0, then only the top max_proposals are kept
            after non-maximum suppression当大于0时,在非极大线性抑制后只保存前max_proposals个最大目标评分的的预测边框
        score_field (str):保存目标得分列表的键值
    """
    # 如果非极大线性抑制的阈值小于0,则返回所有边框列表
    if nms_thresh <= 0:
        return boxlist
    # 得到预测边框的格式
    m
Traceback (most recent call last): File "tools/extract_clip_feature.py", line 20, in <module> from maskrcnn_benchmark.data import make_data_loader File "/root/.cache/huggingface/forget/lab/shichong/cyy/RECODE/maskrcnn_benchmark/data/__init__.py", line 2, in <module> from .build import make_data_loader, get_dataset_statistics File "/root/.cache/huggingface/forget/lab/shichong/cyy/RECODE/maskrcnn_benchmark/data/build.py", line 12, in <module> from maskrcnn_benchmark.utils.miscellaneous import save_labels File "/root/.cache/huggingface/forget/lab/shichong/cyy/RECODE/maskrcnn_benchmark/utils/miscellaneous.py", line 10, in <module> from maskrcnn_benchmark.structures.boxlist_ops import boxlist_iou File "/root/.cache/huggingface/forget/lab/shichong/cyy/RECODE/maskrcnn_benchmark/structures/boxlist_ops.py", line 7, in <module> from maskrcnn_benchmark.layers import nms as _box_nms File "/root/.cache/huggingface/forget/lab/shichong/cyy/RECODE/maskrcnn_benchmark/layers/__init__.py", line 10, in <module> from .nms import nms File "/root/.cache/huggingface/forget/lab/shichong/cyy/RECODE/maskrcnn_benchmark/layers/nms.py", line 3, in <module> from ._utils import _C File "/root/.cache/huggingface/forget/lab/shichong/cyy/RECODE/maskrcnn_benchmark/layers/_utils.py", line 39, in <module> _C = _load_C_extensions() File "/root/.cache/huggingface/forget/lab/shichong/cyy/RECODE/maskrcnn_benchmark/layers/_utils.py", line 35, in _load_C_extensions extra_include_paths=extra_include_paths, File "/opt/conda/envs/recode/lib/python3.7/site-packages/torch/utils/cpp_extension.py", line 1296, in load keep_intermediates=keep_intermediates) File "/opt/conda/envs/recode/lib/python3.7/site-packages/torch/utils/cpp_extension.py", line 1534, in _jit_compile return _import_module_from_library(name, build_directory, is_python_module) File "/opt/conda/envs/recode/lib/python3.7/site-packages/torch/utils/cpp_extension.py", line 1936, in _import_module_from_library module = importlib.util.module_from_spec(spec) ImportError: /root/.cache/torch_extensions/py37_cu117/torchvision/torchvision.so: cannot open shared object file: No such file or directory
最新发布
06-24
### 解决方案 以下是关于解决 `ImportError: cannot import name '_C' from 'maskrcnn_benchmark'` 的详细分析与解决方案。 #### 问题背景 错误的核心是无法正确导入 `_C` 模块。此模块通常是通过编译 C++ 或 CUDA 扩展代码生成的共享对象文件(如 `_C.so`),存储在 `maskrcnn_benchmark/_C.*.so` 中。如果该文件缺失或未正确生成,则会导致上述错误[^1]。 #### 环境依赖检查 确保安装了所有必要的依赖项,包括正确的 PyTorch、CUDA 和其他相关库版本。可以通过以下命令验证 PyTorch 是否正确安装以及是否支持 CUDA: ```python import torch print(torch.__version__) print(torch.cuda.is_available()) ``` 如果 CUDA 不可用或 PyTorch 版本与 CUDA 版本不匹配,可能导致编译失败。确保 PyTorch 版本与 CUDA 版本兼容[^2]。 #### 编译扩展模块 如果 `_C.so` 文件不存在或损坏,需要重新编译扩展模块。进入 `maskrcnn-benchmark` 源码目录并运行以下命令以重新构建模块: ```bash python setup.py build develop ``` 此命令会尝试重新编译 `_C` 模块。如果出现类似 `Microsoft Visual C++ 14.0 is required` 的错误,说明缺少必要的编译工具链。可以通过安装 Microsoft Visual Studio 来解决这个问题[^3]。 #### 替换导入路径 根据引用内容,可以尝试将 `from maskrcnn_benchmark import _C` 替换为 `from ._utils import _C`。此方法适用于某些特定场景,但需确保 `_utils` 模块存在且包含 `_C` 定义[^4]。 #### 检查源码完整性 如果上述方法无效,可能是源码本身存在问题。建议从官方 GitHub 仓库重新下载最新版本的 `maskrcnn-benchmark` 源码,并按照官方文档重新配置和编译[^5]。 #### 单卡训练验证 使用单卡训练脚本验证环境是否正常工作。运行以下命令进行测试: ```bash python /path_to_maskrcnn_benchmark/tools/train_net.py --config-file "/path/to/config/file.yaml" ``` 如果训练脚本能够正常运行,则表明问题已解决[^6]。 #### 示例代码修正 以下是修正后的代码片段(假设使用了替代导入路径): ```python # 原始代码 from maskrcnn_benchmark import _C # 替代代码 from ._utils import _C # 替换为正确的导入路径 ``` 如果上述修改无效,建议检查 `_C` 模块的生成过程,确保其正确编译。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值