You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

testing.py 2.4 kB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
  2. import logging
  3. import numpy as np
  4. import pprint
  5. import sys
  6. from collections import Mapping, OrderedDict
  7. def print_csv_format(results):
  8. """
  9. Print main metrics in a format similar to Detectron,
  10. so that they are easy to copypaste into a spreadsheet.
  11. Args:
  12. results (OrderedDict[dict]): task_name -> {metric -> score}
  13. """
  14. assert isinstance(results, OrderedDict), results # unordered results cannot be properly printed
  15. logger = logging.getLogger(__name__)
  16. for task, res in results.items():
  17. # Don't print "AP-category" metrics since they are usually not tracked.
  18. important_res = [(k, v) for k, v in res.items() if "-" not in k]
  19. logger.info("copypaste: Task: {}".format(task))
  20. logger.info("copypaste: " + ",".join([k[0] for k in important_res]))
  21. logger.info("copypaste: " + ",".join(["{0:.4f}".format(k[1]) for k in important_res]))
  22. def verify_results(cfg, results):
  23. """
  24. Args:
  25. results (OrderedDict[dict]): task_name -> {metric -> score}
  26. Returns:
  27. bool: whether the verification succeeds or not
  28. """
  29. expected_results = cfg.TEST.EXPECTED_RESULTS
  30. if not len(expected_results):
  31. return True
  32. ok = True
  33. for task, metric, expected, tolerance in expected_results:
  34. actual = results[task][metric]
  35. if not np.isfinite(actual):
  36. ok = False
  37. diff = abs(actual - expected)
  38. if diff > tolerance:
  39. ok = False
  40. logger = logging.getLogger(__name__)
  41. if not ok:
  42. logger.error("Result verification failed!")
  43. logger.error("Expected Results: " + str(expected_results))
  44. logger.error("Actual Results: " + pprint.pformat(results))
  45. sys.exit(1)
  46. else:
  47. logger.info("Results verification passed.")
  48. return ok
  49. def flatten_results_dict(results):
  50. """
  51. Expand a hierarchical dict of scalars into a flat dict of scalars.
  52. If results[k1][k2][k3] = v, the returned dict will have the entry
  53. {"k1/k2/k3": v}.
  54. Args:
  55. results (dict):
  56. """
  57. r = {}
  58. for k, v in results.items():
  59. if isinstance(v, Mapping):
  60. v = flatten_results_dict(v)
  61. for kk, vv in v.items():
  62. r[k + "/" + kk] = vv
  63. else:
  64. r[k] = v
  65. return r

No Description