eval-sAP.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. """Evaluate sAP5, sAP10, sAP15 for LCNN
  3. Usage:
  4. eval-sAP.py <path>...
  5. eval-sAP.py (-h | --help )
  6. Examples:
  7. python eval-sAP.py logs/*/npz/000*
  8. Arguments:
  9. <path> One or more directories from train.py
  10. Options:
  11. -h --help Show this screen.
  12. """
  13. import os
  14. import sys
  15. import glob
  16. import os.path as osp
  17. import numpy as np
  18. import scipy.io
  19. import matplotlib as mpl
  20. import matplotlib.pyplot as plt
  21. from docopt import docopt
  22. import lcnn.utils
  23. import lcnn.metric
  24. GT = "data/wireframe/valid/*.npz"
  25. def line_score(path, threshold=5):
  26. preds = sorted(glob.glob(path))
  27. gts = sorted(glob.glob(GT))
  28. n_gt = 0
  29. lcnn_tp, lcnn_fp, lcnn_scores = [], [], []
  30. for pred_name, gt_name in zip(preds, gts):
  31. with np.load(pred_name) as fpred:
  32. lcnn_line = fpred["lines"][:, :, :2]
  33. lcnn_score = fpred["score"]
  34. with np.load(gt_name) as fgt:
  35. gt_line = fgt["lpos"][:, :, :2]
  36. n_gt += len(gt_line)
  37. for i in range(len(lcnn_line)):
  38. if i > 0 and (lcnn_line[i] == lcnn_line[0]).all():
  39. lcnn_line = lcnn_line[:i]
  40. lcnn_score = lcnn_score[:i]
  41. break
  42. tp, fp = lcnn.metric.msTPFP(lcnn_line, gt_line, threshold)
  43. lcnn_tp.append(tp)
  44. lcnn_fp.append(fp)
  45. lcnn_scores.append(lcnn_score)
  46. lcnn_tp = np.concatenate(lcnn_tp)
  47. lcnn_fp = np.concatenate(lcnn_fp)
  48. lcnn_scores = np.concatenate(lcnn_scores)
  49. lcnn_index = np.argsort(-lcnn_scores)
  50. lcnn_tp = np.cumsum(lcnn_tp[lcnn_index]) / n_gt
  51. lcnn_fp = np.cumsum(lcnn_fp[lcnn_index]) / n_gt
  52. return lcnn.metric.ap(lcnn_tp, lcnn_fp)
  53. if __name__ == "__main__":
  54. args = docopt(__doc__)
  55. def work(path):
  56. print(f"Working on {path}")
  57. return [100 * line_score(f"{path}/*.npz", t) for t in [5, 10, 15]]
  58. dirs = sorted(sum([glob.glob(p) for p in args["<path>"]], []))
  59. results = lcnn.utils.parmap(work, dirs)
  60. for d, msAP in zip(dirs, results):
  61. print(f"{d}: {msAP[0]:2.1f} {msAP[1]:2.1f} {msAP[2]:2.1f}")