Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
140 lines (115 loc) · 6.44 KB
/
Copy pathtest.py
File metadata and controls
140 lines (115 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import argparse
from models import *
from utils.datasets import *
from utils.utils import *
parser = argparse.ArgumentParser(prog='test.py')
parser.add_argument('-batch_size', type=int, default=8, help='size of each image batch')
parser.add_argument('-cfg', type=str, default='cfg/yolo3-832.cfg', help='path to model config file')
parser.add_argument('-data_config_path', type=str, default='cfg/omr.data', help='path to data config file')
parser.add_argument('-weights_path', type=str, default='weights/best.pt', help='path to weights file')
parser.add_argument('-class_path', type=str, default='data/new_omr.names', help='path to class label file')
parser.add_argument('-iou_thres', type=float, default=0.5, help='iou threshold required to qualify as detected')
parser.add_argument('-conf_thres', type=float, default=0.3, help='object confidence threshold')
parser.add_argument('-nms_thres', type=float, default=0.45, help='iou threshold for non-maximum suppression')
parser.add_argument('-n_cpu', type=int, default=0, help='number of cpu threads to use during batch generation')
parser.add_argument('-img_size', type=int, default=576, help='size of each image dimension')
opt = parser.parse_args()
print(opt, end='\n\n')
cuda = torch.cuda.is_available()
device = torch.device('cuda:0' if cuda else 'cpu')
def main(opt):
# Configure run
data_config = parse_data_config(opt.data_config_path)
nC = int(data_config['classes']) # number of classes (80 for COCO)
test_path = data_config['valid']
# Initiate model
model = Darknet(opt.cfg, opt.img_size)
# Load weights
if opt.weights_path.endswith('.weights'): # darknet format
load_weights(model, opt.weights_path)
elif opt.weights_path.endswith('.pt'): # pytorch format
checkpoint = torch.load(opt.weights_path, map_location='cpu')
model.load_state_dict(checkpoint['model'])
del checkpoint
model.to(device).eval()
# Get dataloader
# dataset = load_images_with_labels(test_path)
# dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batch_size, shuffle=False, num_workers=opt.n_cpu)
dataloader = load_images_and_labels(test_path, batch_size=opt.batch_size, img_size=opt.img_size)
print('%11s' * 5 % ('Image', 'Total', 'P', 'R', 'mAP'))
outputs, mAPs, mR, mP, TP, confidence, pred_class, target_class,duration_conf,duration_pred = [], [], [], [], [], [], [], [],[], []
AP_accum, AP_accum_count = np.zeros(nC), np.zeros(nC)
count = 0
all_note = 0
count_p = 0
for batch_i, (imgs, targets) in enumerate(dataloader):
with torch.no_grad():
output = model(imgs.to(device))
output = non_max_suppression(output, conf_thres=opt.conf_thres, nms_thres=opt.nms_thres)
# Compute average precision for each sample
for sample_i, (labels, detections) in enumerate(zip(targets, output)):
correct = []
if detections is None:
# If there are no detections but there are labels mask as zero AP
if labels.size(0) != 0:
mAPs.append(0), mR.append(0), mP.append(0)
continue
# Get detections sorted by decreasing confidence scores
detections = detections.cpu().numpy()
detections = detections[np.argsort(-detections[:, 4])]
# If no labels add number of detections as incorrect
if labels.size(0) == 0:
# correct.extend([0 for _ in range(len(detections))])
mAPs.append(0), mR.append(0), mP.append(0)
continue
else:
target_cls = labels[:, 0]
# Extract target boxes as (x1, y1, x2, y2)
target_boxes = xywh2xyxy(labels[:, 1:5]) * opt.img_size
detected = []
for *pred_bbox, conf, obj_conf, obj_pred, duration_conf,duration_pred ,pitch_conf, pitch_pred in detections:
pred_bbox = torch.FloatTensor(pred_bbox).view(1, -1)
# Compute iou with target boxes
iou = bbox_iou(pred_bbox, target_boxes)
# Extract index of largest overlap
best_i = np.argmax(iou)
# If overlap exceeds threshold and classification is correct mark as correct
if iou[best_i] > opt.iou_thres and obj_pred == labels[best_i, 0] and best_i not in detected:
correct.append(1)
detected.append(best_i)
else:
correct.append(0)
if iou[best_i] > opt.iou_thres and obj_pred == labels[best_i,0] and int(obj_pred) in [8, 9]:
all_note +=1
if iou[best_i] > opt.iou_thres and obj_pred == labels[best_i,0] and int(obj_pred) in [8, 9] and duration_pred==labels[best_i,5]:
count+=1
if iou[best_i] > opt.iou_thres and obj_pred == labels[best_i,0] and int(obj_pred)in [8, 9] and pitch_pred==labels[best_i,6]:
count_p+=1
# Compute Average Precision (AP) per class
# Compute Average Precision (AP) per class
AP, AP_class, R, P = ap_per_class(tp=correct, conf=detections[:, 4], pred_cls=detections[:, 6],
target_cls=target_cls)
# Accumulate AP per class
AP_accum_count += np.bincount(AP_class, minlength=nC)
AP_accum += np.bincount(AP_class, minlength=nC, weights=AP)
# Compute mean AP across all classes in this image, and append to image list
mAPs.append(AP.mean())
mR.append(R.mean())
mP.append(P.mean())
# Means of all images
mean_mAP = np.mean(mAPs)
mean_R = np.mean(mR)
mean_P = np.mean(mP)
# Print image mAP and running mean mAP
print(('%11s%11s' + '%11.3g' * 3) % (len(mAPs), dataloader.nF, mean_P, mean_R, mean_mAP))
# Print mAP per class
print('%11s' * 5 % ('Image', 'Total', 'P', 'R', 'mAP') + '\n\nmAP Per Class:')
classes = load_classes(opt.class_path) # Extracts class labels from file
for i, c in enumerate(classes):
print('%15s: %-.4f' % (c, AP_accum[i] / AP_accum_count[i]))
print('-----------duration_acc------- %f'%(count/all_note))
print('-----------pitch_acc------- %f'%(count_p/all_note))
# Return mAP
return mean_mAP, mean_R, mean_P
if __name__ == '__main__':
mAP = main(opt)