maskrcnn_benchmark 代码详解之 bounding_box.py

前言:

  在目标检测网络模型当中,最常用到的便是Bounding Box,在maskrcnn_benchmark当中,bounding_box.py 实现了这一功能,其代码为:

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

# transpose
FLIP_LEFT_RIGHT = 0
FLIP_TOP_BOTTOM = 1


class BoxList(object):
    """
    This class represents a set of bounding boxes.这个类用于保存bbox列表
    The bounding boxes are represented as a Nx4 Tensor.bbox是一个Nx4的张量
    In order to uniquely determine the bounding boxes with respect为了决定这个bbox列表是属于那一个图片的,本类还保存了一个图片的大小
    to an image, we also store the corresponding image dimensions.
    They can contain extra information that is specific to each bounding box, such as
    labels.本类还包含一个其他信息的map,用户可以用其来保存一些备用信息,例如标签,目标水平等
    """

    def __init__(self, bbox, image_size, mode="xyxy"):
        # 如果bbox是一个张量,得到bbox存在的设备,否则就指定设备为cpu
        device = bbox.device if isinstance(bbox, torch.Tensor) else torch.device("cpu")
        # 指定bbox为一个张量,并指定其设备和数据类型
        bbox = torch.as_tensor(bbox, dtype=torch.float32, device=device)
        # bbox的维度应为2:Nx4
        if bbox.ndimension() != 2:
            raise ValueError(
                "bbox should have 2 dimensions, got {}".format(bbox.ndimension())
            )
        # 如果bbox的倒数第一个维度不是4个边框信息,则说明有错误
        if bbox.size(-1) != 4:
            raise ValueError(
                "last dimension of bbox should have a "
                "size of 4, got {}".format(bbox.size(-1))
            )
        # 如果边框信息的格式不是"xyxy", "xywh"中的一种
        if mode not in ("xyxy", "xywh"):
            raise ValueError("mode should be 'xyxy' or 'xywh'")
        # 初始化Boxlist的各种属性
        self.bbox = bbox
        self.size = image_size  # (image_width, image_height)
        self.mode = mode
        self.extra_fields = {}

    # 增加额外的信息
    def add_field(self, field, field_data):
        self.extra_fields[field] = field_data

    # 从extra_fields中获取名为field的数据
    def get_field(self, field):
        return self.extra_fields[field]

    # 判断extra_fields是否有为field的数据
    def has_field(self, field):
        return field in self.extra_fields

    # 得到保存在extra_fields的所有数据的键值
    def fields(self):
        return list(self.extra_fields.keys(
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值