Im studying objective detection code with efficientdet-pytorch at the first time.
I used pretrained Weight model as reference:
https://www.kaggle.com/shonenkov/inference-efficientdet
https://github.com/toandaominh1997/EfficientDet.Pytorch
https://github.com/toandaominh1997/EfficientDet.Pytorch
I tried to check efficientdet model's output...
from effdet import get_efficientdet_config, EfficientDet, DetBenchTrain
from effdet.efficientdet import HeadNet
#load sample efficientdet code
config = get_efficientdet_config('tf_efficientdet_d0')
config.image_size = [512,512]
config.norm_kwargs=dict(eps=.001, momentum=.01)
net = EfficientDet(config, pretrained_backbone=False)
checkpoint = torch.load('efficientdet_d0-d92fd44f.pth')
net.load_state_dict(checkpoint)
#net.reset_head(num_classes=1)
#net.class_net = HeadNet(config, num_outputs=config.num_classes)
net=DetBenchTrain(net, config)
print("Loaded pretrained weights")
#>>>Loaded pretrained weights"
#img.shape:[3,512,512]
net.eval()
with torch.no_grad():
detected=net(img.unsqueeze(0))
#error of target is below.
TypeError: forward() missing 1 required positional argument: 'target'
And refering to https://www.kaggle.com/shonenkov/inference-efficientdet I tried below code.
def make_predictions(images, score_threshold=0.22):
predictions = []
with torch.no_grad():
det = net(images, torch.tensor([1]*images.shape[0]).float())
print(det.shape)
for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy()[:,:4]
scores = det[i].detach().cpu().numpy()[:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
predictions.append({
'boxes': boxes[indexes],
'scores': scores[indexes],
})
return [predictions]
output=make_predictions(img.unsqueeze(0))
#error is below...
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-412-e81ace805280> in <module>
----> 1 output=make_predictions(img.unsqueeze(0))
<ipython-input-407-a6aab7dca874> in make_predictions(images, score_threshold)
3 predictions = []
4 with torch.no_grad():
----> 5 det = net(images, torch.tensor([1]*images.shape[0]).float())
6 print(det.shape)
7 for i in range(images.shape[0]):
/opt/anaconda3/envs/yohenv/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
/opt/anaconda3/envs/yohenv/lib/python3.7/site-packages/effdet/bench.py in forward(self, x, target)
117 else:
118 cls_targets, box_targets, num_positives = self.anchor_labeler.batch_label_anchors(
--> 119 target['bbox'], target['cls'])
120
121 loss, class_loss, box_loss = self.loss_fn(class_out, box_out, cls_targets, box_targets, num_positives)
IndexError: too many indices for tensor of dimension 1
When inference with TestData ,How do I set parameter of target?
Sorry for the inconvenience,Can you give me advice?