[BUG] TypeError: forward() missing 1 required positional argument: 'target'
Tomakko opened this issue · 0 comments
Tomakko commented
Hi, i am trying to run your efficient det on my custom dataset. In thee forward pass
TypeError: forward() missing 1 required positional argument: 'target'
is thrown. I would kindly ask for your help with this issue.
This is how i instantiate the model:
def get_train_efficientdet():
config = get_efficientdet_config('tf_efficientdet_d5')
config.image_size = (IMG_SIZE, IMG_SIZE)
config.norm_kwargs=dict(eps=.001, momentum=.01)
net = create_model_from_config(
config, bench_task='train',num_classes=num_cats,
pretrained=True,
)
print('EfficientDet Config:', config)
return DetBenchTrain(net, config)
This is how i call the forward pass from within the pytorch lightning framework:
def training_step(self, batch, batch_idx):
images, targets = batch
targets = [{k: v for k, v in t.items()} for t in targets]
# separate losses
images = torch.stack(images).float()
targets2 = {}
targets2["bbox"] = [
target["boxes"].float() for target in targets
] # variable number of instances, so the entire structure can be forced to tensor
targets2["cls"] = [target["labels"].float() for target in targets]
targets2["image_id"] = torch.tensor(
[target["image_id"] for target in targets]
).float()
targets2["img_scale"] = torch.tensor(
[target["img_scale"] for target in targets]
).float()
targets2["img_size"] = torch.tensor(
[(IMG_SIZE, IMG_SIZE) for target in targets]
).float()
print('targets2:')
print(targets2)
print('type of dict: ', type(targets2))
losses_dict = self.forward(images, targets2)
return {"loss": losses_dict["loss"], "log": losses_dict}
Printed targets2:
{'bbox': [tensor([[139.1957, 137.6129, 210.4661, 204.0420]])], 'cls': [tensor([[0., 0., 0., 1., 0., 0., 0., 0.]])], 'image_id': tensor([47727.]), 'img_scale': tensor([1.]), 'img_size': tensor([[256., 256.]])}
The full callstack is:
TypeError Traceback (most recent call last)
<ipython-input-272-d56bad246dc1> in <module>
6 # logger = loggers.TensorBoardLogger("logs", name="effdet-b5", version="fold_0")
7 trainer = Trainer(fast_dev_run=True)
----> 8 trainer.fit(model)
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py in fit(self, model, train_dataloader, val_dataloaders, datamodule)
497
498 # dispath `start_training` or `start_testing` or `start_predicting`
--> 499 self.dispatch()
500
501 # plugin will finalized fitting (e.g. ddp_spawn will load trained model)
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py in dispatch(self)
544
545 else:
--> 546 self.accelerator.start_training(self)
547
548 def train_or_test_or_predict(self):
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/accelerators/accelerator.py in start_training(self, trainer)
71
72 def start_training(self, trainer):
---> 73 self.training_type_plugin.start_training(trainer)
74
75 def start_testing(self, trainer):
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py in start_training(self, trainer)
112 def start_training(self, trainer: 'Trainer') -> None:
113 # double dispatch to initiate the training loop
--> 114 self._results = trainer.run_train()
115
116 def start_testing(self, trainer: 'Trainer') -> None:
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py in run_train(self)
635 with self.profiler.profile("run_training_epoch"):
636 # run train epoch
--> 637 self.train_loop.run_training_epoch()
638
639 if self.max_steps and self.max_steps <= self.global_step:
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/trainer/training_loop.py in run_training_epoch(self)
490 # ------------------------------------
491 with self.trainer.profiler.profile("run_training_batch"):
--> 492 batch_output = self.run_training_batch(batch, batch_idx, dataloader_idx)
493
494 # when returning -1 from train_step, we end epoch early
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/trainer/training_loop.py in run_training_batch(self, batch, batch_idx, dataloader_idx)
652
653 # optimizer step
--> 654 self.optimizer_step(optimizer, opt_idx, batch_idx, train_step_and_backward_closure)
655
656 else:
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/trainer/training_loop.py in optimizer_step(self, optimizer, opt_idx, batch_idx, train_step_and_backward_closure)
423
424 # model hook
--> 425 model_ref.optimizer_step(
426 self.trainer.current_epoch,
427 batch_idx,
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/core/lightning.py in optimizer_step(self, epoch, batch_idx, optimizer, optimizer_idx, optimizer_closure, on_tpu, using_native_amp, using_lbfgs)
1388 # wraps into LightingOptimizer only for running step
1389 optimizer = LightningOptimizer._to_lightning_optimizer(optimizer, self.trainer, optimizer_idx)
-> 1390 optimizer.step(closure=optimizer_closure)
1391
1392 def optimizer_zero_grad(self, epoch: int, batch_idx: int, optimizer: Optimizer, optimizer_idx: int):
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/core/optimizer.py in step(self, closure, *args, **kwargs)
212 profiler_name = f"optimizer_step_and_closure_{self._optimizer_idx}"
213
--> 214 self.__optimizer_step(*args, closure=closure, profiler_name=profiler_name, **kwargs)
215 self._total_optimizer_step_calls += 1
216
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/core/optimizer.py in __optimizer_step(self, closure, profiler_name, **kwargs)
132
133 with trainer.profiler.profile(profiler_name):
--> 134 trainer.accelerator.optimizer_step(optimizer, self._optimizer_idx, lambda_closure=closure, **kwargs)
135
136 def step(self, *args, closure: Optional[Callable] = None, **kwargs):
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/accelerators/accelerator.py in optimizer_step(self, optimizer, opt_idx, lambda_closure, **kwargs)
275 )
276 if make_optimizer_step:
--> 277 self.run_optimizer_step(optimizer, opt_idx, lambda_closure, **kwargs)
278 self.precision_plugin.post_optimizer_step(optimizer, opt_idx)
279 self.training_type_plugin.post_optimizer_step(optimizer, opt_idx, **kwargs)
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/accelerators/accelerator.py in run_optimizer_step(self, optimizer, optimizer_idx, lambda_closure, **kwargs)
280
281 def run_optimizer_step(self, optimizer: Optimizer, optimizer_idx: int, lambda_closure: Callable, **kwargs):
--> 282 self.training_type_plugin.optimizer_step(optimizer, lambda_closure=lambda_closure, **kwargs)
283
284 def optimizer_zero_grad(self, current_epoch: int, batch_idx: int, optimizer: Optimizer, opt_idx: int) -> None:
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py in optimizer_step(self, optimizer, lambda_closure, **kwargs)
161
162 def optimizer_step(self, optimizer: torch.optim.Optimizer, lambda_closure: Callable, **kwargs):
--> 163 optimizer.step(closure=lambda_closure, **kwargs)
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/torch/optim/optimizer.py in wrapper(*args, **kwargs)
87 profile_name = "Optimizer.step#{}.step".format(obj.__class__.__name__)
88 with torch.autograd.profiler.record_function(profile_name):
---> 89 return func(*args, **kwargs)
90 return wrapper
91
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)
25 def decorate_context(*args, **kwargs):
26 with self.__class__():
---> 27 return func(*args, **kwargs)
28 return cast(F, decorate_context)
29
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/torch/optim/adamw.py in step(self, closure)
63 if closure is not None:
64 with torch.enable_grad():
---> 65 loss = closure()
66
67 for group in self.param_groups:
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/trainer/training_loop.py in train_step_and_backward_closure()
646
647 def train_step_and_backward_closure():
--> 648 result = self.training_step_and_backward(
649 split_batch, batch_idx, opt_idx, optimizer, self.trainer.hiddens
650 )
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/trainer/training_loop.py in training_step_and_backward(self, split_batch, batch_idx, opt_idx, optimizer, hiddens)
740 with self.trainer.profiler.profile("training_step_and_backward"):
741 # lightning module hook
--> 742 result = self.training_step(split_batch, batch_idx, opt_idx, hiddens)
743 self._curr_step_result = result
744
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/trainer/training_loop.py in training_step(self, split_batch, batch_idx, opt_idx, hiddens)
291 model_ref._results = Result()
292 with self.trainer.profiler.profile("training_step"):
--> 293 training_step_output = self.trainer.accelerator.training_step(args)
294 self.trainer.accelerator.post_training_step()
295
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/accelerators/accelerator.py in training_step(self, args)
154
155 with self.precision_plugin.train_step_context(), self.training_type_plugin.train_step_context():
--> 156 return self.training_type_plugin.training_step(*args)
157
158 def post_training_step(self):
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py in training_step(self, *args, **kwargs)
123
124 def training_step(self, *args, **kwargs):
--> 125 return self.lightning_module.training_step(*args, **kwargs)
126
127 def post_training_step(self):
<ipython-input-271-6e3db7ea7bf0> in training_step(self, batch, batch_idx)
78 print(targets2)
79 print('type of dict: ', type(targets2))
---> 80 losses_dict = self.forward(images, targets2)
81
82 return {"loss": losses_dict["loss"], "log": losses_dict}
<ipython-input-271-6e3db7ea7bf0> in forward(self, image, target)
32 self.batch_size = 1
33 def forward(self, image, target):
---> 34 return self.model(image, target)
35
36 # Let's add the train and validation data loaders.
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/effdet/bench.py in forward(self, x, target)
108
109 def forward(self, x, target: Dict[str, torch.Tensor]):
--> 110 class_out, box_out = self.model(x)
111 if self.anchor_labeler is None:
112 # target should contain pre-computed anchor labels if labeler not present in bench
~/object-detection-for-forgotten-items/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
TypeError: forward() missing 1 required positional argument: 'target'
Desktop (please complete the following information):
- Ubuntu 20.04
- effdet 0.2.1
- PyTorch 1.8.1