user22267185
user22267185

Reputation: 11

IndexError: "index 1 is out of bounds for dimension 0 with size 1" during PyTorch training step

I am currently working on an object detection task using PyTorch and encountering an "IndexError" during the training step. The error message is as follows:

IndexError: index 1 is out of bounds for dimension 0 with size 1

To provide some context, I have a data loader that loads images and their corresponding targets, which include bounding boxes and labels. The data loader is producing the following output when I print the first batch:

print(next(iter(data_loader_train)))
out:
((<PIL.Image.Image image mode=RGB size=640x640 at 0x7BE2368E08E0>, <PIL.Image.Image image mode=RGB size=640x640 at 0x7BE2368E07F0>), ({'boxes': tensor([[ 51., 347.,  73., 358.],
        [183., 340., 210., 349.],
        [409., 324., 436., 336.],
        [356., 298., 365., 302.],
        [396., 289., 403., 293.]]), 'labels': tensor([0]), 'image_id': tensor([160]), 'area': tensor([242., 243., 324.,  36.,  28.])}, {'boxes': tensor([[141., 339., 186., 357.],
        [420., 255., 430., 258.],
        [361., 257., 373., 261.]]), 'labels': tensor([0]), 'image_id': tensor([167]), 'area': tensor([810.,  30.,  48.])}))

Here is my training loop:

def train_one_epoch(model, optimizer, data_loader, device):
    model.train()
    total_loss = 0
    for images, targets in data_loader:
        images = list(transform(image).to(device) for image in images)

        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

        loss_dict = model(images, targets)

        losses = sum(loss for loss in loss_dict.values())
        total_loss += losses
        optimizer.zero_grad()
        losses.backward()
        optimizer.step()
    return total_loss / data_loader.__len__()

# let's train it for 10 epochs
num_epochs = 10
for epoch in range(num_epochs):
    # train for one epoch, printing every 10 iterations
    loss = train_one_epoch(model, optimizer, data_loader_train, device)
    # update the learning rate
    print('epoch [{}]:  \t lr: {}  \t loss: {}  '.format(epoch, lr_scheduler.get_lr(), loss))
    lr_scheduler.step()

The error occurs in the "assign_targets_to_proposals" method in "roi_heads.py" from the torchvision models when trying to access an index that is out of bounds.

I have verified the data loading process and checked the dimensions of the bounding boxes and labels. I also tried printing the tensors at various points in the code to see if there is any discrepancy. However, I couldn't find any apparent issues in the data.

I expected the training process to run smoothly without any errors. The data should be correctly loaded, and the model should be trained for the specified number of epochs.

I'm not sure why this error is happening, and I would appreciate any insights or suggestions on how to resolve it. Thank you!

Upvotes: 1

Views: 350

Answers (1)

E5-SR
E5-SR

Reputation: 1

I had the same error during my model training and reshaping my data fixed it:

y = df.iloc[:,-1].values.reshape(-1, 1)

Change the values to fit your data.

Upvotes: 0

Related Questions