AttributeError: 'dict' object has no attribute 'size' in BCEWithLogitsLoss

I’m kind of new to Pytorch so sorry if the question makes no sense.
I’m trying to build a simple Convnet for object detection on the PascalVOC dataset.

I run into the following error :

Traceback (most recent call last):
loss = criterion(labels, outputs)
File “C:\Users\Sofiane\anaconda3\lib\site-packages\torch\nn\modules\”, line 727, in _call_impl
result = self.forward(*input, **kwargs)
File “C:\Users\Sofiane\anaconda3\lib\site-packages\torch\nn\modules\”, line 629, in forward
return F.binary_cross_entropy_with_logits(input, target,
File “C:\Users\Sofiane\anaconda3\lib\site-packages\torch\nn\”, line 2579, in binary_cross_entropy_with_logits
if not (target.size() == input.size()):
AttributeError: ‘dict’ object has no attribute ‘size’

When I try to run this snippet :

criterion = torch.nn.BCEWithLogitsLoss()

and later call :

    for i, data in enumerate(train_loader):

        images, labels = data

        images =

        outputs = model(images)
        loss = criterion(outputs, labels)

I tried printing out labels and I get the following

{‘annotation’: {‘filename’: [‘2009_000133.jpg’], ‘folder’: [‘VOC2012’], ‘object’: [{‘name’: [‘aeroplane’], ‘bndbox’: {‘xmax’: [‘383’], ‘xmin’: [‘111’], ‘ymax’: [‘234’], ‘ymin’: [‘144’]}, ‘difficult’: [‘0’], ‘occluded’: [‘0’], ‘pose’: [‘Unspecified’], ‘truncated’: [‘0’]}], ‘segmented’: [‘1’], ‘size’: {‘depth’: [‘3’], ‘height’: [‘273’], ‘width’: [‘500’]}, ‘source’: {‘annotation’: [‘PASCAL VOC2009’], ‘database’: [‘The VOC2009 Database’], ‘image’: [‘flickr’]}}}

So I’m wondering what I’m doing wrong.

As the error message and the print suggests, labels is a dict, while a tensor is expected.
I’m not familiar with your use case, but you would have to extract the valid target value out of the current dict, which contains the filename, folder, and more information.