facebookresearch / detr

End-to-End Object Detection with Transformers
Apache License 2.0
13.7k stars 2.47k forks source link

box_ops matcher_ index out of bounds #467

Open EvelyneCalista opened 3 years ago

EvelyneCalista commented 3 years ago

HI all, I work on custom dataset for object detction with bounding box only. I got below error " index 2 is out of bounds for dimension 1 with size 1" when doing matcher. does the width and height must be calculated in the dataset class? are there any people that got a similar problem with me? :(((((

def generalized_box_iou(boxes1, boxes2): assert (boxes1[:, 2:] >= boxes1[:, :2]).all() assert (boxes2[:, 2:] >= boxes2[:, :2]).all()

File "//detr-main/engine.py", line 34, in train_one_epoch loss_dict = criterion(outputs, targets) File "/home/anaconda3/envs/myenv/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl return forward_call(*input, kwargs) File "detr-main/models/detr.py", line 225, in forward indices = self.matcher(outputs_without_aux, targets) File "/home/anaconda3/envs/myenv/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl return forward_call(*input, *kwargs) File "/home/anaconda3/envs/myenv/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 28, in decorate_context return func(args, kwargs) File "/detr-main/models/matcher.py", line 74, in forward cost_giou = -generalized_box_iou(box_cxcywh_to_xyxy(out_bbox), box_cxcywh_to_xyxy(tgt_bbox)) File "/detr-main/util/box_ops.py", line 53, in generalized_box_iou iou, union = box_iou(boxes1, boxes2) File "/detr-main/util/box_ops.py", line 26, in box_iou area2 = box_area(boxes2) File "/home/anaconda3/envs/myenv/lib/python3.8/site-packages/torchvision/ops/boxes.py", line 229, in box_area return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) IndexError: index 2 is out of bounds for dimension 1 with size 1

truncs commented 2 years ago

Check the polygons in the dataset. Might be helpful to print the id the sample in the dataset where this is erroring out.

Sahar-DataScience commented 2 years ago

I'm trying to use DETR for instance segmentation on a custom dataset in coco format and I faced the same issue

reubenwenisch commented 2 years ago

Facing the same issue when using custom coco dataset. Isn't coco format xywh but in the code thinks bbox is of the format cxcywh therefore they use box_cxcywh_to_xyxy. Is this the issue? Were any of you able to solve this?

johannesSX commented 1 year ago

But the conversion doesn't make any sense, does it? Version given in the code:

def box_xyxy_to_cxcywh(x):
    x0, y0, x1, y1 = x.unbind(-1)
    b = [(x0 + x1) / 2, (y0 + y1) / 2,
         (x1 - x0), (y1 - y0)]
    return torch.stack(b, dim=-1)

I think that's correct though:

def box_xyxy_to_cxcywh(x):
    x0, y0, x1, y1 = x.unbind(-1)
    b = [x0 + x1 / 2, y0 + y1 / 2,
         (x1 / 2), (y1 / 2)]
    return torch.stack(b, dim=-1)

I think the brackets were put in the wrong place when really the center of the box is supposed to be the reference coordinate. But it works, where is my mistake?

aleenarayamajhi commented 1 year ago

I have same problem in boxes1. I'm not getting negative values, but the values in boxes1 keep changing. Did you figure out the solution? @EvelyneCalista


     52     pdb.set_trace()
---> 53     assert (boxes1[:, 2:] >= boxes1[:, :2]).all()
     54     assert (boxes2[:, 2:] >= boxes2[:, :2]).all()
     55     iou, union = box_iou(boxes1, boxes2)

When I run: run F:/DETR/detr/main.py --dataset_file face --data_path F:/DETR/dataset/ --output_dir F:/DETR/output --resume F:/DETR/weights/detr-r50-e632da11.pth

My output looks like this:

Not using distributed mode
git:
  sha: 3c5317060f0c2ee3d1dccd3c4da23b3c2cb251bf, status: has uncommited changes, branch: master

Namespace(aux_loss=True, backbone='resnet50', batch_size=2, bbox_loss_coef=5, clip_max_norm=0.1, coco_panoptic_path=None, data_path='F:/DETR/dataset/', dataset_file='face', dec_layers=6, device='cuda', dice_loss_coef=1, dilation=False, dim_feedforward=2048, dist_url='env://', distributed=False, dropout=0.1, enc_layers=6, eos_coef=0.1, epochs=300, eval=False, frozen_weights=None, giou_loss_coef=2, hidden_dim=256, lr=0.0001, lr_backbone=1e-05, lr_drop=200, mask_loss_coef=1, masks=False, nheads=8, num_queries=30, num_workers=2, output_dir='F:/DETR/output', position_embedding='sine', pre_norm=False, remove_difficult=False, resume='F:/DETR/weights/detr-r50-e632da11.pth', seed=42, set_cost_bbox=5, set_cost_class=1, set_cost_giou=2, start_epoch=0, weight_decay=0.0001, world_size=1)
number of params: 41261318
IMAGE FOLDER:  F:\DETR\dataset\train
ANN_FILE:  F:\DETR\dataset\train.json
loading annotations into memory...
Done (t=0.32s)
creating index...
index created!
IMAGE FOLDER:  F:\DETR\dataset\val
ANN_FILE:  F:\DETR\dataset\val.json
loading annotations into memory...
Done (t=0.02s)
creating index...
index created!
Length of Dataset train:  500
Length of Dataset val:  100
Start training
Epoch: [0]  [  0/250]  eta: 0:11:30  lr: 0.000100  class_error: 2.00  loss: 36.0587 (36.0587)  loss_ce: 0.3236 (0.3236)  loss_bbox: 4.4387 (4.4387)  loss_giou: 1.5512 (1.5512)  loss_ce_0: 0.2778 (0.2778)  loss_bbox_0: 3.5100 (3.5100)  loss_giou_0: 1.8308 (1.8308)  loss_ce_1: 0.2402 (0.2402)  loss_bbox_1: 3.9349 (3.9349)  loss_giou_1: 1.7132 (1.7132)  loss_ce_2: 0.2037 (0.2037)  loss_bbox_2: 4.0667 (4.0667)  loss_giou_2: 1.6071 (1.6071)  loss_ce_3: 0.2174 (0.2174)  loss_bbox_3: 4.4361 (4.4361)  loss_giou_3: 1.5737 (1.5737)  loss_ce_4: 0.2624 (0.2624)  loss_bbox_4: 4.3035 (4.3035)  loss_giou_4: 1.5675 (1.5675)  loss_ce_unscaled: 0.3236 (0.3236)  class_error_unscaled: 2.0000 (2.0000)  loss_bbox_unscaled: 0.8877 (0.8877)  loss_giou_unscaled: 0.7756 (0.7756)  cardinality_error_unscaled: 33.5000 (33.5000)  loss_ce_0_unscaled: 0.2778 (0.2778)  loss_bbox_0_unscaled: 0.7020 (0.7020)  loss_giou_0_unscaled: 0.9154 (0.9154)  cardinality_error_0_unscaled: 34.0000 (34.0000)  loss_ce_1_unscaled: 0.2402 (0.2402)  loss_bbox_1_unscaled: 0.7870 (0.7870)  loss_giou_1_unscaled: 0.8566 (0.8566)  cardinality_error_1_unscaled: 34.0000 (34.0000)  loss_ce_2_unscaled: 0.2037 (0.2037)  loss_bbox_2_unscaled: 0.8133 (0.8133)  loss_giou_2_unscaled: 0.8036 (0.8036)  cardinality_error_2_unscaled: 34.0000 (34.0000)  loss_ce_3_unscaled: 0.2174 (0.2174)  loss_bbox_3_unscaled: 0.8872 (0.8872)  loss_giou_3_unscaled: 0.7869 (0.7869)  cardinality_error_3_unscaled: 34.0000 (34.0000)  loss_ce_4_unscaled: 0.2624 (0.2624)  loss_bbox_4_unscaled: 0.8607 (0.8607)  loss_giou_4_unscaled: 0.7838 (0.7838)  cardinality_error_4_unscaled: 34.0000 (34.0000)  time: 2.7605  data: 2.1511  max mem: 3155
Traceback (most recent call last):

  File "F:\DETR\detr\main.py", line 259, in <module>
    main(args)

  File "F:\DETR\detr\main.py", line 209, in main
    args.clip_max_norm)

  File "F:\DETR\detr\engine.py", line 33, in train_one_epoch
    loss_dict = criterion(outputs, targets)

  File "C:\ProgramData\Anaconda3\envs\demo\lib\site-packages\torch\nn\modules\module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)

  File "F:\DETR\detr\models\detr.py", line 224, in forward
    indices = self.matcher(outputs_without_aux, targets)

  File "C:\ProgramData\Anaconda3\envs\demo\lib\site-packages\torch\nn\modules\module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)

  File "C:\ProgramData\Anaconda3\envs\demo\lib\site-packages\torch\autograd\grad_mode.py", line 15, in decorate_context
    return func(*args, **kwargs)

  File "F:\DETR\detr\models\matcher.py", line 74, in forward
    cost_giou = -generalized_box_iou(box_cxcywh_to_xyxy(out_bbox), box_cxcywh_to_xyxy(tgt_bbox))

  File "F:\DETR\detr\util\box_ops.py", line 51, in generalized_box_iou
    assert (boxes1[:, 2:] >= boxes1[:, :2]).all()

AssertionError

Please help!!