如何解决将 Pytorch 与 Pytorch-Lightning ROC 指标一起使用时出现分段错误
对于机器学习任务,我正在转换两个张量,如代码片段 (1) 所示,以使它们符合 pytorch-lightning 的 ROC 指标的预期格式。然后将两个结果张量 y_test
(gt_classes) 和 y_score
(detection_scores) 传递给代码片段 (2) 中的函数 calc_multi_class
,其中我使用了来自pytorch 闪电:fpr,tpr,thr = roc(y_score,y_test)
。 `
由于我集成了 pytorch-lightning 并使用了它们的 ROC 功能,因此我收到了如下部分所示的错误消息“分段错误”。
我使用 Python 3.7 和 Pytorch 1.7、Pytorch-Lightning 1.1.2 和 Cuda 10.2
Part (1)
...
# sort by class and by confidence value
# gt_classes = labels[:,0].int()
gt_classes_sorted = labels[labels[:,0].sort(descending=False)[1]]
gt_classes = gt_classes_sorted[:,0].int()
# x = x[x[:,4].argsort(descending=True)]
detections_sorted = detection[detection[:,4].sort(descending=True)[1]]
det_classes_sorted = detections_sorted[:,5].int()
# filter for scores
input = torch.empty(gt_classes.numel(),self.nc)
detection_scores = torch.ones_like(input) * 0.01
# check if no scores
if detections_sorted.nelement() > 0 and det_classes_sorted.nelement() > 0:
counter = 0
for j,dc in enumerate(det_classes_sorted):
if counter < gt_classes.numel():
for i,gc in enumerate(gt_classes):
# classes gc equals dc classes
if gc == dc:
# conf value
print('Found detection score {}'.format(detections_sorted[j,4]))
detection_scores[i,gc] = detections_sorted[j,4]
counter = counter + 1
else:
print('Other detection score {}'.format(detections_sorted[j,dc] = detections_sorted[j,4] #conf value
#print('Result classes {} scores {}'.format(gt_classes,detection_scores))
gt_classes,detection_scores = gt_classes.to(self.device),detection_scores.to(self.device)
return gt_classes,detection_scores
Part (2)
def calc_multi_class_(self,y_test,y_score,num_classes=3):
""" Use of Pytorch Lightning Metrics Library for ROC"""
fprs = dict()
tprs = dict()
roc_aucs = dict()
thresholds = dict()
# torch to cuda device
if isinstance(y_test,np.ndarray):
y_test = torch.from_numpy(y_test)
y_test = y_test.cpu()
if isinstance(y_score,np.ndarray):
y_score = torch.from_numpy(y_score)
y_score = y_score.cpu()
if y_score.nelement() > 0 and torch.max(y_score) > 0.2:
roc = classification.ROC(num_classes=num_classes) # no neg classes
fpr,y_test)
# convert multi-dim tensor to numpy ndarray
for i in range(num_classes):
thresholds[i] = thr[i].numpy()
fprs[i],tprs[i] = fpr[i].numpy(),tpr[i].numpy()
roc_aucs[i] = auc(fprs[i],tprs[i])
return fprs,tprs,roc_aucs,thresholds
由于我对 Pytorch 还很陌生,我试图弄清楚它来自哪里,并查看了我是否设置了错误的张量,但还是无法弄清楚。如果有人有提示或想法,这将非常有帮助!!
产生的错误信息如下:
Result classes tensor([1],device='cuda:0',dtype=torch.int32) scores tensor([[0.01000,0.01000,0.01000]],device='cuda:0')
Class Images Targets P R mAP@.5 mAP@.5:.95: 100%|██████████| 58/58 [00:13<00:00,4.41it/s]
all 928 489 0 0 3.57e-06 5.65e-07
/home/jm0365/.direnv/python-3.7.3/lib/python3.7/site-packages/pytorch_lightning/utilities/distributed.py:49: UserWarning: Metric ROC
将保存所有目标和缓冲区中的预测。对于大型数据集,这可能会导致大量内存占用。 warnings.warn(*args,**kwargs) 分段错误
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。