coreml-community/ControlNet-v1-1-Annotators-cpu
15
1# Copyright (c) Facebook, Inc. and its affiliates.2import logging3import numpy as np4import pprint5import sys6from collections.abc import Mapping7 8 9def print_csv_format(results):10 """11 Print main metrics in a format similar to Detectron,12 so that they are easy to copypaste into a spreadsheet.13 14 Args:15 results (OrderedDict[dict]): task_name -> {metric -> score}16 unordered dict can also be printed, but in arbitrary order17 """18 assert isinstance(results, Mapping) or not len(results), results19 logger = logging.getLogger(__name__)20 for task, res in results.items():21 if isinstance(res, Mapping):22 # Don't print "AP-category" metrics since they are usually not tracked.23 important_res = [(k, v) for k, v in res.items() if "-" not in k]24 logger.info("copypaste: Task: {}".format(task))25 logger.info("copypaste: " + ",".join([k[0] for k in important_res]))26 logger.info("copypaste: " + ",".join(["{0:.4f}".format(k[1]) for k in important_res]))27 else:28 logger.info(f"copypaste: {task}={res}")29 30 31def verify_results(cfg, results):32 """33 Args:34 results (OrderedDict[dict]): task_name -> {metric -> score}35 36 Returns:37 bool: whether the verification succeeds or not38 """39 expected_results = cfg.TEST.EXPECTED_RESULTS40 if not len(expected_results):41 return True42 43 ok = True44 for task, metric, expected, tolerance in expected_results:45 actual = results[task].get(metric, None)46 if actual is None:47 ok = False48 continue49 if not np.isfinite(actual):50 ok = False51 continue52 diff = abs(actual - expected)53 if diff > tolerance:54 ok = False55 56 logger = logging.getLogger(__name__)57 if not ok:58 logger.error("Result verification failed!")59 logger.error("Expected Results: " + str(expected_results))60 logger.error("Actual Results: " + pprint.pformat(results))61 62 sys.exit(1)63 else:64 logger.info("Results verification passed.")65 return ok66 67 68def flatten_results_dict(results):69 """70 Expand a hierarchical dict of scalars into a flat dict of scalars.71 If results[k1][k2][k3] = v, the returned dict will have the entry72 {"k1/k2/k3": v}.73 74 Args:75 results (dict):76 """77 r = {}78 for k, v in results.items():79 if isinstance(v, Mapping):80 v = flatten_results_dict(v)81 for kk, vv in v.items():82 r[k + "/" + kk] = vv83 else:84 r[k] = v85 return r86 