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

**URL:** <https://discuss.pytorch.org/t/attributeerror-dict-object-has-no-attribute-size-in-bcewithlogitsloss/115891>\
**Category:** vision\
**Created:** [March 25, 2021, 2:14am UTC](https://discuss.pytorch.org/t/attributeerror-dict-object-has-no-attribute-size-in-bcewithlogitsloss/115891 "2021-03-25T02:14:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sofiane\_Meraouna](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sofiane_meraouna/32/36249_2.png) [@Sofiane\_Meraouna](https://discuss.pytorch.org/u/Sofiane_Meraouna)\
**Post date:** [March 25, 2021, 2:14am UTC](https://discuss.pytorch.org/t/attributeerror-dict-object-has-no-attribute-size-in-bcewithlogitsloss/115891/1 "2021-03-25T02:14:29Z")

</div>

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\module.py”, line 727, in \_call\_impl  
result = self.forward(\*input, \*\*kwargs)  
File “C:\Users\Sofiane\anaconda3\lib\site-packages\torch\nn\modules\loss.py”, line 629, in forward  
return F.binary\_cross\_entropy\_with\_logits(input, target,  
File “C:\Users\Sofiane\anaconda3\lib\site-packages\torch\nn\functional.py”, 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 :

```auto
criterion = torch.nn.BCEWithLogitsLoss()

```

and later call :

```auto
    for i, data in enumerate(train_loader):

        images, labels = data

        images = images.to(device)

        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.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 26, 2021, 9:10am UTC](https://discuss.pytorch.org/t/attributeerror-dict-object-has-no-attribute-size-in-bcewithlogitsloss/115891/2 "2021-03-26T09:10:48Z")

</div>

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.
