Error while training efficientdet

#12
by sagaypotatoe - opened

Hi,
I am new to the problem of object detection and currently learning more about it. I am trying to train EfficientDet for this problem. However, I am getting this error even after resizing the images to the same size along with the bboxes using albumentations. I am using Ross Wightman's pytorch implementation of EfficientDet. Could someone please help me out here?
Thanks in advance!

Batchsize = 4
images resized to 224

0%|          | 0/4349 [00:00<?, ?it/s]
torch.Size([4, 3, 224, 224])
  0%|          | 0/4349 [00:02<?, ?it/s]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_331/3869611587.py in <module>
      1 for i in range(CFG.EPOCHS):
----> 2     avg_train_loss = train_model(train_loader, model, optimizer)
      3     avg_val_loss = val_model(val_loader, model)

/tmp/ipykernel_331/3403888848.py in train_model(dataloader, model, optimizer)
     18 
     19         with torch.autograd.set_detect_anomaly(True):
---> 20             output = model(images, input_targets)
     21             print(output)
     22 

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1188         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1189                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1190             return forward_call(*input, **kwargs)
   1191         # Do not call functions when jit is used
   1192         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/effdet/bench.py in forward(self, x, target)
    131 
    132     def forward(self, x, target: Dict[str, torch.Tensor]):
--> 133         class_out, box_out = self.model(x)
    134         if self.anchor_labeler is None:
    135             # target should contain pre-computed anchor labels if labeler not present in bench

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1188         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1189                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1190             return forward_call(*input, **kwargs)
   1191         # Do not call functions when jit is used
   1192         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/effdet/efficientdet.py in forward(self, x)
    601     def forward(self, x):
    602         x = self.backbone(x)
--> 603         x = self.fpn(x)
    604         x_class = self.class_net(x)
    605         x_box = self.box_net(x)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1188         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1189                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1190             return forward_call(*input, **kwargs)
   1191         # Do not call functions when jit is used
   1192         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/effdet/efficientdet.py in forward(self, x)
    344         for resample in self.resample.values():
    345             x.append(resample(x[-1]))
--> 346         x = self.cell(x)
    347         return x
    348 

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1188         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1189                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1190             return forward_call(*input, **kwargs)
   1191         # Do not call functions when jit is used
   1192         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/effdet/efficientdet.py in forward(self, x)
     34     def forward(self, x: List[torch.Tensor]) -> List[torch.Tensor]:
     35         for module in self:
---> 36             x = module(x)
     37         return x
     38 

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1188         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1189                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1190             return forward_call(*input, **kwargs)
   1191         # Do not call functions when jit is used
   1192         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/effdet/efficientdet.py in forward(self, x)
    277     def forward(self, x: List[torch.Tensor]):
    278         for fn in self.fnode:
--> 279             x.append(fn(x))
    280         return x[-self.num_levels::]
    281 

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1188         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1189                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1190             return forward_call(*input, **kwargs)
   1191         # Do not call functions when jit is used
   1192         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/effdet/efficientdet.py in forward(self, x)
    238 
    239     def forward(self, x: List[torch.Tensor]) -> torch.Tensor:
--> 240         return self.after_combine(self.combine(x))
    241 
    242 

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1188         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1189                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1190             return forward_call(*input, **kwargs)
   1191         # Do not call functions when jit is used
   1192         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/effdet/efficientdet.py in forward(self, x)
    219             weights_sum = torch.sum(edge_weights)
    220             out = torch.stack(
--> 221                 [(nodes[i] * edge_weights[i]) / (weights_sum + 0.0001) for i in range(len(nodes))], dim=-1)
    222         elif self.weight_method == 'sum':
    223             out = torch.stack(nodes, dim=-1)

RuntimeError: stack expects each tensor to be equal size, but got [4, 64, 4, 4] at entry 0 and [4, 64, 8, 8] at entry 1

It's a shape mismatch error. If you are using Data Transforms then try to add a manual transforms.Resize() in transforms.Compose()

You may refer to this link:- https://discuss.pytorch.org/t/runtimeerror-stack-expects-each-tensor-to-be-equal-size-but-got-3-224-224-at-entry-0-and-3-224-336-at-entry-3/87211
As I can get only this information from the stack trace, so if possible try to share the notebook.

Hi @Raghvender , thank you for the reply.

I have resized the images using albumentations. I tried again by setting the batch size=1 to check if the images are not getting resized properly, but im still getting the same error.

Notebook link - https://www.kaggle.com/code/sagarikajadon/ship-effdet/notebook

Let me check and get back to you

As you are using efficientdet_d0, you need to use the image size defined in model_config (512x512)
Because, even if you resize your image to 224, the model is build for input of size 512.

You have two option, either you change your CFG to (512, 512) or you can git clone efficientdet-pytorch and edit directly the model_config.py file.

Resolved it, thank you @Yohann84L

sagaypotatoe changed discussion status to closed

Sign up or log in to comment