修改测试

This commit is contained in:
lee
2025-07-01 13:39:34 +08:00
parent 96a9445761
commit ed6a4144fa
8 changed files with 55 additions and 14 deletions

2
.gitignore vendored
View File

@ -166,3 +166,5 @@ pnnx*
/ultralytics/assets/
confusion_gift_cls4_0.45/
*.jpg
*.png
*.txt

View File

@ -3,8 +3,9 @@ import numpy as np
# model = YOLOv10.from_pretrained('jameslahm/yolov10{n/s/m/b/l/x}')
# or
# wget https://github.com/THU-MIG/yolov10/releases/download/v1.1/yolov10{n/s/m/b/l/x}.pt
model = YOLOv10('ckpts/20250514/best_gift_v10n.pt')
model = YOLOv10('ckpts/20250630/best_gift_v10n.pt')
result = model.predict('./data/bandage.jpg', save=False, imgsz=[224, 224], conf=0.1)
# result = model.predict('./data/bandage.jpg', save=True, imgsz=[224, 224], conf=0.1)
result = model.predict('/home/lc/data_center/gift/trace_subimgs/predict_actual_test/gift', save=True, imgsz=[224, 224], conf=0.1)
print(result)
print(result[0].boxes.conf)
# print(result[0].boxes.conf)

View File

@ -43,7 +43,7 @@ def get_image_list(path):
def _init():
model = YOLOv10('ckpts/20250620/best_gift_v10n.pt')
model = YOLOv10('ckpts/20250701/best_gift_v10n.pt')
return model
@ -94,8 +94,5 @@ def main(path):
if __name__ == "__main__":
# path = '../data_center/gift/trace_subimgs/d50' # 间距为50时
# path = '../data_center/gift/trace_subimgs/actual_test' # 永辉超市实测
path = '../data_center/gift/gift_test' #yolov10单图测试
# path = '../data_center/gift/trace_subimgs/tracluster' # tracluster方法过滤
main(path)

View File

@ -43,7 +43,7 @@ def get_image_list(path):
def _init():
model = YOLOv10('ckpts/20250620/best_gift_v10n.pt')
model = YOLOv10('ckpts/20250701/best_gift_v10n.pt')
return model

View File

@ -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

View File

@ -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]

View File

@ -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)

2
val.py
View File

@ -1,4 +1,4 @@
from ultralytics import YOLOv10
model = YOLOv10('/home/lc/ieemoo-ai-gift/runs/detect/train/weights/best_gift_v10n.pt')
model = YOLOv10('./ckpts/20250701/best_gift_v10n.pt')
metrics = model.val(batch=1, data='gift.yaml', imgsz=224)