如何解决推理时,如何使用 EfficientDet-Pytorch 设置目标参数?
我第一次在研究使用高效det-pytorch的客观检测代码。
我使用预训练的权重模型作为参考:
https://www.kaggle.com/shonenkov/inference-efficientdet
https://github.com/toandaominh1997/EfficientDet.Pytorch
https://github.com/toandaominh1997/EfficientDet.Pytorch
我试图检查efficientdet 模型的输出...
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'
并参考 https://www.kaggle.com/shonenkov/inference-efficientdet 我试过下面的代码。
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,num_positives)
IndexError: too many indices for tensor of dimension 1
使用 TestData 进行推理时,如何设置目标参数?
很抱歉给您带来不便,您能给我建议吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。