'NestedTensor' object has no attribute 'size'
Aliweka2020 opened this issue · 3 comments
I am trying to use efficientnet bacbone with DETR but get this error
File "C:\Users\Ali\AppData\Local\Continuum\anaconda3\lib\site-packages\timm\models\layers\padding.py", line 32, in pad_same
ih, iw = x.size()[-2:]
AttributeError: 'NestedTensor' object has no attribute 'size'
I searched for NestedTensor to unpack to get the dimension but useless.
Could anyone help?!
I have a similar problem. This function requires a single tensor, but somehow I pass a batch of tensors to this. In my case list of tensors, so I have
ih, iw = x.size()[-2:]
AttributeError: 'list' object has no attribute 'size'
@Aliweka2020 So, I fixed this problem on my side.
I use train_one_epoch
function from engine.py. I needed to adapt those lines:
images = list(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)
to this:
images = torch.stack(images)
boxes = [target["boxes"].float() for target in targets]
labels = [target["labels"].float() for target in targets]
target = {
"bbox": boxes,
"cls": labels
}
loss_dict = model(images, target)
because as I mentioned, the efficientdet requires tensor (Batch x Channels x Height x Width) not list or NestedTensor.
There are more possible fixes to this problem.
- You can write your own collate_fn function that would do this if you are not using
train_one_epoch
. - You can write a wrapper around model() that would transform the NestedTensor to Tensor and then forward the proper call.
I'm going to move this to discussions since it isn't a bug.
The workound @arekmula provided if you're using a training setup that uses lists or tuples is correct, the 'bench' wrappers for training here expect tensors. Another approach would be to write an alternate Train/Predict bench wrapper (https://github.com/rwightman/efficientdet-pytorch/blob/master/effdet/bench.py#L79) that accept sequences. ie DetBenchTrainSeq / DetBenchPredictSeq ... and modify the factory create fn to accept 'train_seq' and 'predict_seq' as the bench type. I'd accept a PR for that.
For nested tensor, I haven't spent much time with it, but since it's out of PyTorch tree it's not a high priority for me to support but likely can be supported with a similar approach as above (specific bench wrappers).