def test_f1_two_class(self): """Test F1-score for two class """ correct = np.array([[False], [True], [False]]) conf = np.array([0.654, 0.702, 0.432]) pred_cls = np.array([0., 0., 1.]) target_cls = np.array([1. , 0., 0.]) _, _, _, f1, _ = ap_per_class(correct, conf, pred_cls, target_cls) expect = np.array([[0.5], [0.]]) self.assertTrue((f1 == expect).all())
def test_f1_single_class(self): """Test F1-score for single class """ correct = np.array([[True], [True]]) conf = np.array([0.685, 0.702]) pred_cls = np.array([0., 0.]) target_cls = np.array([0., 0.]) _, _, _, f1, _ = ap_per_class(correct, conf, pred_cls, target_cls) expect = np.array([[1.]]) self.assertTrue((f1 == expect).all())
def test_ap_two_class(self): """Test average precision for two class """ correct = np.array([[False], [True], [False]]) conf = np.array([0.654, 0.702, 0.432]) pred_cls = np.array([0., 0., 1.]) target_cls = np.array([1. , 0., 0.]) _, _, ap, _, _ = ap_per_class(correct, conf, pred_cls, target_cls) expect = np.array([[0.5], [0.]]) self.assertTrue((ap == expect).all())
def test_ap_single_class(self): """Test average precision for single class """ correct = np.array([[True], [True]]) conf = np.array([0.685, 0.702]) pred_cls = np.array([0., 0.]) target_cls = np.array([0., 0.]) _, _, ap, _, _ = ap_per_class(correct, conf, pred_cls, target_cls) expect = np.array([[0.995]]) self.assertTrue((ap == expect).all())
def test_precision_recall_three_class(self): """Test precision and recall for three class """ correct = np.array([[False], [True], [False]]) conf = np.array([0.654, 0.702, 0.432]) pred_cls = np.array([0., 2., 1.]) target_cls = np.array([1. , 2., 0.]) p, r, _, _, _ = ap_per_class(correct, conf, pred_cls, target_cls) expect_p = np.array([[0.], [0.], [1.]]) expect_r = np.array([[0.], [0.], [1.]]) self.assertTrue((p == expect_p).all() and (r == expect_r).all())
def test_precision_recall_single_class(self): """Test precision and recall for single class """ correct = np.array([[True], [True]]) conf = np.array([0.685, 0.702]) pred_cls = np.array([0., 0.]) target_cls = np.array([0., 0.]) p, r, _, _, _ = ap_per_class(correct, conf, pred_cls, target_cls) output = np.concatenate([p, r]) expect = np.array([[1.], [1.]]) self.assertTrue((output == expect).all())
def test( cfg, data, weights=None, batch_size=16, imgsz=416, conf_thres=0.1, iou_thres=0.6, # for nms single_cls=False, augment=False, model=None, dataloader=None, multi_label=True): # Initialize/load model and set device if model is None: is_training = False device = select_device(opt.device, batch_size=batch_size) verbose = opt.task == 'test' # Remove previous for f in glob.glob('test_batch*.jpg'): os.remove(f) # Initialize model model = Darknet(cfg, imgsz) # Load weights attempt_download(weights) if weights.endswith('.pt'): # pytorch format model.load_state_dict( torch.load(weights, map_location=device)['model']) else: # darknet format load_darknet_weights(model, weights) # Fuse model.fuse() model.to(device) if device.type != 'cpu' and torch.cuda.device_count() > 1: model = nn.DataParallel(model) else: # called by train.py is_training = True device = next(model.parameters()).device # get model device verbose = False # Configure run data = parse_data_cfg(data) nc = 1 if single_cls else int(data['classes']) # number of classes path = data['valid'] # path to test images names = load_classes(data['names']) # class names iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for [email protected]:0.95 iouv = iouv[0].view(1) # comment for [email protected]:0.95 niou = iouv.numel() # Dataloader if dataloader is None: dataset = LoadImagesAndLabels(path, imgsz, batch_size, rect=True, single_cls=opt.single_cls, pad=0.5) batch_size = min(batch_size, len(dataset)) dataloader = DataLoader(dataset, batch_size=batch_size, num_workers=min([ os.cpu_count(), batch_size if batch_size > 1 else 0, 8 ]), pin_memory=True, collate_fn=dataset.collate_fn) seen = 0 model.eval() _ = model(torch.zeros( (1, 3, imgsz, imgsz), device=device)) if device.type != 'cpu' else None # run once s = ('%20s' + '%10s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', '[email protected]', 'F1') p, r, f1, mp, mr, map, mf1, t0, t1 = 0., 0., 0., 0., 0., 0., 0., 0., 0. loss = torch.zeros(3, device=device) jdict, stats, ap, ap_class = [], [], [], [] for batch_i, (imgs, targets, paths, shapes) in enumerate(tqdm(dataloader, desc=s)): imgs = imgs.to( device).float() / 255.0 # uint8 to float32, 0 - 255 to 0.0 - 1.0 targets = targets.to(device) nb, _, height, width = imgs.shape # batch size, channels, height, width whwh = torch.Tensor([width, height, width, height]).to(device) # Disable gradients with torch.no_grad(): # Run model t = time_synchronized() inf_out, train_out = model( imgs, augment=augment) # inference and training outputs t0 += time_synchronized() - t # Compute loss if is_training: # if model has loss hyperparameters loss += compute_loss(train_out, targets, model)[1][:3] # GIoU, obj, cls # Run NMS t = time_synchronized() output = non_max_suppression(inf_out, conf_thres=conf_thres, iou_thres=iou_thres, multi_label=multi_label) t1 += time_synchronized() - t # Statistics per image for si, pred in enumerate(output): labels = targets[targets[:, 0] == si, 1:] nl = len(labels) tcls = labels[:, 0].tolist() if nl else [] # target class seen += 1 if pred is None: if nl: stats.append((torch.zeros(0, niou, dtype=torch.bool), torch.Tensor(), torch.Tensor(), tcls)) continue # Append to text file # with open('test.txt', 'a') as file: # [file.write('%11.5g' * 7 % tuple(x) + '\n') for x in pred] # Clip boxes to image bounds clip_coords(pred, (height, width)) # Assign all predictions as incorrect correct = torch.zeros(pred.shape[0], niou, dtype=torch.bool, device=device) if nl: detected = [] # target indices tcls_tensor = labels[:, 0] # target boxes tbox = xywh2xyxy(labels[:, 1:5]) * whwh # Per target class for cls in torch.unique(tcls_tensor): ti = (cls == tcls_tensor).nonzero(as_tuple=False).view( -1) # target indices pi = (cls == pred[:, 5]).nonzero(as_tuple=False).view( -1) # prediction indices # Search for detections if pi.shape[0]: # Prediction to target ious ious, i = box_iou(pred[pi, :4], tbox[ti]).max( 1) # best ious, indices # Append detections for j in (ious > iouv[0]).nonzero(as_tuple=False): d = ti[i[j]] # detected target if d not in detected: detected.append(d) correct[ pi[j]] = ious[j] > iouv # iou_thres is 1xn if len( detected ) == nl: # all targets already located in image break # Append statistics (correct, conf, pcls, tcls) stats.append( (correct.cpu(), pred[:, 4].cpu(), pred[:, 5].cpu(), tcls)) # Plot images if batch_i < 1: f = 'test_batch%g_gt.jpg' % batch_i # filename plot_images(imgs, targets, paths=paths, names=names, fname=f) # ground truth f = 'test_batch%g_pred.jpg' % batch_i plot_images(imgs, output_to_target(output, width, height), paths=paths, names=names, fname=f) # predictions # Compute statistics stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy # Visualize probability of predictions # probs = np.array(stats[1]) # import seaborn as sns # plot = sns.distplot(probs, bins=probs.shape[0] // 3) # plot.figure.savefig("prob_%s.png" % imgsz) if len(stats): p, r, ap, f1, ap_class = ap_per_class(*stats) if niou > 1: p, r, ap, f1 = p[:, 0], r[:, 0], ap.mean( 1), ap[:, 0] # [P, R, [email protected]:0.95, [email protected]] mp, mr, map, mf1 = p.mean(), r.mean(), ap.mean(), f1.mean() nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class else: nt = torch.zeros(1) # Print results pf = '%20s' + '%10.3g' * 6 # print format print(pf % ('all', seen, nt.sum(), mp, mr, map, mf1)) # Print results per class if verbose and nc > 1 and len(stats): for i, c in enumerate(ap_class): print(pf % (names[c], seen, nt[c], p[i], r[i], ap[i], f1[i])) # Print speeds if verbose: t = tuple(x / seen * 1E3 for x in (t0, t1, t0 + t1)) + ( imgsz, imgsz, batch_size) # tuple print( 'Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t) # Return results maps = np.zeros(nc) + map for i, c in enumerate(ap_class): maps[c] = ap[i] return (mp, mr, map, mf1, *(loss.cpu() / len(dataloader)).tolist()), maps