首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我得到了一个ValueError:所有边界框都应该有正的高度和宽度

我得到了一个ValueError:所有边界框都应该有正的高度和宽度
EN

Stack Overflow用户
提问于 2020-08-25 12:59:16
回答 2查看 1.5K关注 0票数 2

嘿,我得到了错误

代码语言:javascript
复制
ValueError: All bounding boxes should have positive height and width. Found invaid box [264.0, 632.0, 264.0, 633.3333740234375] for target at index 2.
Epoch 1/1
Mini-batch: 1/1220 Loss: 0.1509
Mini-batch: 101/1220 Loss: 0.1201
Mini-batch: 201/1220 Loss: 0.1103
Mini-batch: 301/1220 Loss: 0.1098
Mini-batch: 401/1220 Loss: 0.1076
Mini-batch: 501/1220 Loss: 0.1056
Mini-batch: 601/1220 Loss: 0.1044
Mini-batch: 701/1220 Loss: 0.1035
ValueError Traceback (most recent call last)
in ()
13
14 # Calculate losses
—> 15 loss_dict = model(images, targets)
16 batch_loss = sum(loss for loss in loss_dict.values()) / len(loss_dict)
17

1 frames
/usr/local/lib/python3.6/dist-packages/torchvision/models/detection/generalized_rcnn.py in forward(self, images, targets)
91 raise ValueError(“All bounding boxes should have positive height and width.”
92 " Found invaid box {} for target at index {}."
—> 93 .format(degen_bb, target_idx))
94
95 features = self.backbone(images.tensors)

ValueError: All bounding boxes should have positive height and width. Found invaid box [264.0, 632.0, 264.0, 633.3333740234375] for target at index 2.

我在标签csv文件中找不到具有这些坐标的边界框。有谁能帮我解决这个问题吗。

以下是我的数据集类

代码语言:javascript
复制
from torch.utils.data import Dataset, DataLoader

Inherit from pytorch Dataset for convenience
class DamageDataset(Dataset):
def __init__(self, dataframe):

    super().__init__()

    self.filename = dataframe['filename'].unique()

    self.df = dataframe

def __len__(self) -> int:

    return len(self.filename)



def __getitem__(self, index: int):

    filename = self.filename[index]

    image = read_image_from_train_folder(filename).astype(np.float32)

    # Scale to [0,1] range expected by the pre-trained model

    image /= 255.0

    # Convert the shape from [h,w,c] to [c,h,w] as expected by pytorch

    image = torch.from_numpy(image).permute(2,0,1)

    

    records = self.df[self.df['filename'] == filename]

    

    boxes = records[['xmin', 'ymin', 'xmax', 'ymax']].values

    classes= records['class'].values

    damage_labels=[]

    damage_dict={

        'D00': 1,

        'D10': 2,

        'D20': 3,

        'D40': 4,

                                                                    

    }

    for label in classes:

      damage_labels.append(damage_dict[label])

   

    boxes = torch.as_tensor(boxes, dtype=torch.float32)

    n_boxes = boxes.shape[0]

    

    # there is only one foreground class, WHEAT

    labels = torch.as_tensor(damage_labels, dtype=torch.float32)

            

    target = {}

    target['boxes'] = boxes

    target['labels'] = labels

    

    return image, target

这是我的火车代码:

代码语言:javascript
复制
num_epochs = 1

Prepare the model for training
model = model.to(device)

model.train()

for epoch in range(num_epochs):

print("Epoch %i/%i " % (epoch + 1, num_epochs) )

average_loss = 0

for batch_id, (images, targets) in enumerate(train_data_loader):

    # Prepare the batch data

    images, targets = move_batch_to_device(images, targets)

    # Calculate losses

    loss_dict = model(images, targets)

    batch_loss = sum(loss for loss in loss_dict.values()) / len(loss_dict)

    

    # Refresh accumulated optimiser state and minimise losses

    optimizer.zero_grad()

    batch_loss.backward()

有人能帮我找出这个边界框的索引吗?这样我就可以删除它了,我已经用下面的代码遍历了我的数据帧:

代码语言:javascript
复制
for idx, row in merge_labels.iterrows():
if(row[‘xmin’]==264 and row[‘ymin’]== 632 and row[‘xmax’]== 264 and row[‘ymax’]== 633.3333740234375 ):

print(idx)

但是它不打印任何索引。谢谢

EN

回答 2

Stack Overflow用户

发布于 2021-01-04 00:53:08

这是因为在检测模块的fasterRCNN中应用了调整大小转换。如果您显式地应用调整大小操作,则边界框生成的坐标将根据调整大小定义进行更改,但如果您尚未应用调整大小转换,并且图像的最小和最大大小为outsider (800,1333),则会应用默认的调整大小转换。

查看pytorch git repo中的以下代码片段。

generalized_rcnn.py :/torchvision/models/detection,在Module中产生异常

代码语言:javascript
复制
        degenerate_boxes = boxes[:, 2:] <= boxes[:, :2]
        if degenerate_boxes.any():
            # print the first degenerate box
            bb_idx = torch.where(degenerate_boxes.any(dim=1))[0][0]
            degen_bb: List[float] = boxes[bb_idx].tolist()
            raise ValueError("All bounding boxes should have positive height and width."
                             " Found invalid box {} for target at index {}."
                             .format(degen_bb, target_idx))

默认最小最大图像大小;模块:检测/ faster_rcnn

代码语言:javascript
复制
def __init__(self, backbone, num_classes=None,
             # transform parameters
             min_size=800, max_size=1333,
             image_mean=None, image_std=None,
             ............................

transform = GeneralizedRCNNTransform(min_size, max_size, image_mean, image_std)

调整目标大小;模块:检测/ transform.py

代码语言:javascript
复制
def resize_boxes(boxes, original_size, new_size):
    # type: (Tensor, List[int], List[int]) -> Tensor
    ratios = [
        torch.tensor(s, dtype=torch.float32, device=boxes.device) /
        torch.tensor(s_orig, dtype=torch.float32, device=boxes.device)
        for s, s_orig in zip(new_size, original_size)
    ]
    ratio_height, ratio_width = ratios
    xmin, ymin, xmax, ymax = boxes.unbind(1)

    xmin = xmin * ratio_width
    xmax = xmax * ratio_width
    ymin = ymin * ratio_height
    ymax = ymax * ratio_height
    return torch.stack((xmin, ymin, xmax, ymax), dim=1)

您可以通过删除/更正原始数据集中xmin和xmax或ymin和ymax相等的边界框来修复此问题。

票数 0
EN

Stack Overflow用户

发布于 2021-06-25 15:03:59

我认为你需要检查你的图像边界框。我通过确保我的xmin < xmax和ymin < ymax解决了这个问题。如果xmin等于xmax,那么它将是一行

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63572304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档