I am trying to train the faster rcnn model on coco dataset, but i am experiencing some error related to the format of "targets" that i am supposed to pass to the model. I am passing a list of dictionaries which in turn have two keys namely 'boxes' and 'labels' which are both tensors. Yet when i run the script i am getting an error suggesting that my 'targets' has no key named 'boxes'.
This is the error: Traceback (most recent call last): File "C:\Stuff\Deep Learning\pretrained.py", line 72, in predictions = model(images, targets) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Stuff\Deep Learning\venv\Lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Stuff\Deep Learning\venv\Lib\site-packages\torchvision\models\detection\generalized_rcnn.py", line 65, in forward boxes = target["boxes"] ~~~~~~^^^^^^^^^ KeyError: 'boxes'
This is my training loop:
training_dataset = datasets.CocoDetection(root="coco/train2017", annFile="coco/annotations/instances_train2017.json", transform=transform)
train_loader = DataLoader(dataset=training_dataset, batch_size=batch_size, shuffle=False, collate_fn=collate_fn)
for epoch in range(epochs):
model.train()
total_loss = 0
for images, labels in train_loader:
images = [image.to(device) for image in images]
targets = []
for label in labels:
boxes = []
category = []
target = {}
for annotation in label:
bbox = annotation['bbox']
bbox[2] += bbox[0]
bbox[3] += bbox[1]
boxes.append(bbox)
category.append(annotation['category_id'])
target = {
'boxes': torch.tensor(boxes, dtype=torch.float32).to(device),
'labels': torch.tensor(category, dtype=torch.int64).to(device)
}
targets.append(target)
predictions = model(images, targets)
loss_dict = model(images, targets)
loss = sum(loss for loss in loss_dict.values())
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
print(f"Epoch {epoch + 1}, Loss: {total_loss / len(train_loader)}")