mirror of
https://gitee.com/nanjing-yimao-information/ieemoo-ai-gift.git
synced 2025-08-18 21:30:25 +00:00
修改测试
This commit is contained in:
@ -253,6 +253,8 @@ class BasePredictor:
|
||||
# Postprocess
|
||||
with profilers[2]:
|
||||
self.results = self.postprocess(preds, im, im0s)
|
||||
if len(self.results) == 0:
|
||||
continue
|
||||
self.run_callbacks("on_predict_postprocess_end")
|
||||
|
||||
# Visualize, save, write results
|
||||
|
@ -16,14 +16,14 @@ class YOLOv10DetectionPredictor(DetectionPredictor):
|
||||
pass
|
||||
else:
|
||||
preds = preds.transpose(-1, -2)
|
||||
bboxes, scores, labels = ops.v10postprocess(preds, self.args.max_det, preds.shape[-1]-4)
|
||||
bboxes, scores, labels = ops.v10postprocess(preds, self.args.max_det, preds.shape[-1] - 4)
|
||||
bboxes = ops.xywh2xyxy(bboxes)
|
||||
preds = torch.cat([bboxes, scores.unsqueeze(-1), labels.unsqueeze(-1)], dim=-1)
|
||||
|
||||
mask = preds[..., 4] > self.args.conf
|
||||
if self.args.classes is not None:
|
||||
mask = mask & (preds[..., 5:6] == torch.tensor(self.args.classes, device=preds.device).unsqueeze(0)).any(2)
|
||||
|
||||
|
||||
preds = [p[mask[idx]] for idx, p in enumerate(preds)]
|
||||
|
||||
if not isinstance(orig_imgs, list): # input images are a torch.Tensor, not a list
|
||||
@ -31,6 +31,18 @@ class YOLOv10DetectionPredictor(DetectionPredictor):
|
||||
|
||||
results = []
|
||||
for i, pred in enumerate(preds):
|
||||
#### 不保存负样本predict的结果#######
|
||||
# if pred.numel() == 0:
|
||||
# continue
|
||||
# print('pred >>> {}'.format(pred[:, 4]))
|
||||
# if float(pred[:, 4][0]) < 0.1:
|
||||
# continue
|
||||
##################################
|
||||
#####保存正样本predict漏检的结果######
|
||||
# if pred.numel() != 0:
|
||||
# if float(pred[:, 4][-1]) > 0.1:
|
||||
# continue
|
||||
##################################
|
||||
orig_img = orig_imgs[i]
|
||||
pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape)
|
||||
img_path = self.batch[0][i]
|
||||
|
@ -26,6 +26,17 @@ class ShowPR:
|
||||
values.append(value)
|
||||
return values
|
||||
|
||||
def calculate_mena(self, ratio=0.5):
|
||||
values = []
|
||||
for data in self.prec_value:
|
||||
thres_num = int(len(data)*ratio)
|
||||
sorted_data = sorted(data, reverse=True)
|
||||
value = sorted_data[:thres_num]
|
||||
if len(value) == 0:
|
||||
value = sorted_data[:1]
|
||||
values.append(sum(value)/len(value))
|
||||
return values
|
||||
|
||||
def _calculate_pr(self, prec_value):
|
||||
FN, FP, TN, TP = 0, 0, 0, 0
|
||||
for output, target in zip(prec_value, self.tags):
|
||||
@ -53,7 +64,7 @@ class ShowPR:
|
||||
# print("TP>>{}, FP>>{}, TN>>{}, FN>>{}".format(TP, FP, TN, FN))
|
||||
return prec, recall, tn_prec, tn_recall
|
||||
|
||||
def calculate_multiple(self, ratio=0.2):
|
||||
def calculate_multiple_1(self, ratio=0.2): # 方案1 计算满足阈值判断的占比(ratio)
|
||||
recall, recall_TN, PrecisePos, PreciseNeg = [], [], [], []
|
||||
for thre in self.thres:
|
||||
prec_value = []
|
||||
@ -73,6 +84,21 @@ class ShowPR:
|
||||
recall_TN.append(tn_recall)
|
||||
return recall, recall_TN, PrecisePos, PreciseNeg
|
||||
|
||||
def calculate_multiple_2(self, ratio=0.2): # 方案2 计算前ratio的预测试值的平均值大于thre为赠品小于为非赠品
|
||||
recall, recall_TN, PrecisePos, PreciseNeg = [], [], [], []
|
||||
event_value = self.calculate_mena(ratio)
|
||||
for thre in self.thres:
|
||||
prec_value = [1 if num >= thre else 0 for num in event_value]
|
||||
prec, recall_pos, tn_prec, tn_recall = self._calculate_pr(prec_value)
|
||||
print(
|
||||
f"thre>>{ratio:.2f}, recall>>{recall_pos:.4f}, precise_pos>>{prec:.4f}, recall_tn>>{tn_recall:.4f}, precise_neg>>{tn_prec:4f}")
|
||||
PrecisePos.append(prec)
|
||||
recall.append(recall_pos)
|
||||
PreciseNeg.append(tn_prec)
|
||||
recall_TN.append(tn_recall)
|
||||
return recall, recall_TN, PrecisePos, PreciseNeg
|
||||
|
||||
|
||||
def write_results_to_file(self, recall, recall_TN, PrecisePos, PreciseNeg, ratio):
|
||||
file_path = os.sep.join(['./ckpts/tracePR', self.title_name + f"_{ratio:.2f}" + '.txt'])
|
||||
with open(file_path, 'w') as file:
|
||||
@ -112,5 +138,6 @@ class ShowPR:
|
||||
# ratio = 0.5
|
||||
if ratio < 0.1 or ratio > 0.95:
|
||||
continue
|
||||
recall, recall_TN, PrecisePos, PreciseNeg = self.calculate_multiple(ratio)
|
||||
recall, recall_TN, PrecisePos, PreciseNeg = self.calculate_multiple_1(ratio)
|
||||
# recall, recall_TN, PrecisePos, PreciseNeg = self.calculate_multiple_2(ratio)
|
||||
self.show_pr(recall, recall_TN, PrecisePos, PreciseNeg, ratio)
|
||||
|
Reference in New Issue
Block a user