car-detection-bayes/test.py

133 lines
5.2 KiB
Python
Raw Normal View History

2018-08-26 08:51:39 +00:00
import argparse
2018-10-10 15:07:21 +00:00
2018-08-26 08:51:39 +00:00
from models import *
from utils.datasets import *
from utils.utils import *
parser = argparse.ArgumentParser()
2018-08-26 15:09:10 +00:00
parser.add_argument('-batch_size', type=int, default=32, help='size of each image batch')
parser.add_argument('-cfg', type=str, default='cfg/yolov3.cfg', help='path to model config file')
parser.add_argument('-data_config_path', type=str, default='cfg/coco.data', help='path to data config file')
2018-10-26 22:42:34 +00:00
parser.add_argument('-weights_path', type=str, default='weights/yolov3.pt', help='path to weights file')
2018-08-26 15:09:10 +00:00
parser.add_argument('-class_path', type=str, default='data/coco.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.5, help='object confidence threshold')
2018-09-10 15:00:39 +00:00
parser.add_argument('-nms_thres', type=float, default=0.45, help='iou threshold for non-maximum suppression')
2018-08-26 15:09:10 +00:00
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=416, help='size of each image dimension')
2018-08-26 08:51:39 +00:00
opt = parser.parse_args()
print(opt)
2018-09-24 01:06:04 +00:00
cuda = torch.cuda.is_available()
2018-08-26 08:51:39 +00:00
device = torch.device('cuda:0' if cuda else 'cpu')
2018-09-02 11:17:28 +00:00
# Configure run
2018-08-26 08:51:39 +00:00
data_config = parse_data_config(opt.data_config_path)
num_classes = int(data_config['classes'])
2018-09-02 11:17:28 +00:00
if platform == 'darwin': # MacOS (local)
test_path = data_config['valid']
else: # linux (cloud, i.e. gcp)
2018-09-02 11:18:59 +00:00
test_path = '../coco/5k.part'
2018-09-02 11:17:28 +00:00
2018-08-26 08:51:39 +00:00
# Initiate model
2018-08-26 15:09:10 +00:00
model = Darknet(opt.cfg, opt.img_size)
2018-08-26 08:51:39 +00:00
# Load weights
2018-09-02 11:13:22 +00:00
if opt.weights_path.endswith('.weights'): # darknet format
2018-09-02 11:03:55 +00:00
load_weights(model, opt.weights_path)
elif opt.weights_path.endswith('.pt'): # pytorch format
checkpoint = torch.load(opt.weights_path, map_location='cpu')
2018-08-26 08:51:39 +00:00
model.load_state_dict(checkpoint['model'])
del checkpoint
model.to(device).eval()
2018-09-02 10:40:29 +00:00
# Get dataloader
2018-09-02 09:38:39 +00:00
# dataset = load_images_with_labels(test_path)
2018-08-26 08:51:39 +00:00
# dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batch_size, shuffle=False, num_workers=opt.n_cpu)
2018-09-02 09:38:39 +00:00
dataloader = load_images_and_labels(test_path, batch_size=opt.batch_size, img_size=opt.img_size)
2018-08-26 08:51:39 +00:00
print('Compute mAP...')
2018-10-10 15:07:21 +00:00
nC = 80 # number of classes
2018-09-10 13:50:37 +00:00
correct = 0
2018-08-26 08:51:39 +00:00
targets = None
2018-09-10 13:50:37 +00:00
outputs, mAPs, TP, confidence, pred_class, target_class = [], [], [], [], [], []
2018-10-10 15:07:21 +00:00
AP_accum, AP_accum_count = np.zeros(nC), np.zeros(nC)
2018-08-26 08:51:39 +00:00
for batch_i, (imgs, targets) in enumerate(dataloader):
imgs = imgs.to(device)
with torch.no_grad():
output = model(imgs)
output = non_max_suppression(output, conf_thres=opt.conf_thres, nms_thres=opt.nms_thres)
# Compute average precision for each sample
for sample_i in range(len(targets)):
correct = []
# Get labels for sample where width is not zero (dummies)
annotations = targets[sample_i]
# Extract detections
detections = output[sample_i]
if detections is None:
# If there are no detections but there are annotations mask as zero AP
if annotations.size(0) != 0:
2018-09-10 13:12:13 +00:00
mAPs.append(0)
2018-08-26 08:51:39 +00:00
continue
# Get detections sorted by decreasing confidence scores
detections = detections[np.argsort(-detections[:, 4])]
# If no annotations add number of detections as incorrect
if annotations.size(0) == 0:
2018-09-10 13:58:01 +00:00
target_cls = []
2018-10-10 15:07:21 +00:00
# correct.extend([0 for _ in range(len(detections))])
2018-09-10 14:26:40 +00:00
mAPs.append(0)
continue
2018-08-26 08:51:39 +00:00
else:
2018-09-10 13:58:01 +00:00
target_cls = annotations[:, 0]
2018-08-26 08:51:39 +00:00
# Extract target boxes as (x1, y1, x2, y2)
2018-09-02 11:17:38 +00:00
target_boxes = xywh2xyxy(annotations[:, 1:5])
2018-08-26 08:51:39 +00:00
target_boxes *= opt.img_size
detected = []
for *pred_bbox, conf, obj_conf, obj_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 == annotations[best_i, 0] and best_i not in detected:
correct.append(1)
detected.append(best_i)
else:
correct.append(0)
2018-09-10 13:12:13 +00:00
# Compute Average Precision (AP) per class
2018-10-10 15:07:21 +00:00
AP, AP_class = 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)
2018-08-26 08:51:39 +00:00
2018-09-10 13:12:13 +00:00
# Compute mean AP for this image
mAP = AP.mean()
2018-08-26 08:51:39 +00:00
2018-09-10 13:23:04 +00:00
# Append image mAP to list
2018-09-10 13:23:39 +00:00
mAPs.append(mAP)
2018-08-26 08:51:39 +00:00
2018-09-10 13:12:13 +00:00
# Print image mAP and running mean mAP
2018-09-10 13:23:04 +00:00
print('+ Sample [%d/%d] AP: %.4f (%.4f)' % (len(mAPs), len(dataloader) * opt.batch_size, mAP, np.mean(mAPs)))
2018-08-26 08:51:39 +00:00
2018-10-10 15:07:21 +00:00
# Print mAP 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 mAP
2018-09-10 13:12:13 +00:00
print('Mean Average Precision: %.4f' % np.mean(mAPs))