|
a |
|
b/tools/print_f1.py |
|
|
1 |
# Copyright 2018 Google LLC. |
|
|
2 |
# |
|
|
3 |
# Redistribution and use in source and binary forms, with or without |
|
|
4 |
# modification, are permitted provided that the following conditions |
|
|
5 |
# are met: |
|
|
6 |
# |
|
|
7 |
# 1. Redistributions of source code must retain the above copyright notice, |
|
|
8 |
# this list of conditions and the following disclaimer. |
|
|
9 |
# |
|
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright |
|
|
11 |
# notice, this list of conditions and the following disclaimer in the |
|
|
12 |
# documentation and/or other materials provided with the distribution. |
|
|
13 |
# |
|
|
14 |
# 3. Neither the name of the copyright holder nor the names of its |
|
|
15 |
# contributors may be used to endorse or promote products derived from this |
|
|
16 |
# software without specific prior written permission. |
|
|
17 |
# |
|
|
18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
|
19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
|
20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
21 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
|
22 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
|
23 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
|
24 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
|
25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
|
26 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
|
27 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
|
28 |
# POSSIBILITY OF SUCH DAMAGE. |
|
|
29 |
r"""Parse and extract metrics from *.metrics files.""" |
|
|
30 |
|
|
|
31 |
import argparse |
|
|
32 |
import json |
|
|
33 |
import logging |
|
|
34 |
from os import listdir |
|
|
35 |
from os.path import isfile |
|
|
36 |
from os.path import join |
|
|
37 |
import re |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
def parse_cmdline(argv): |
|
|
41 |
"""Parse the commandline.""" |
|
|
42 |
parser = argparse.ArgumentParser() |
|
|
43 |
|
|
|
44 |
parser.add_argument( |
|
|
45 |
'--metrics_dir', help='Path to the directory with metrics files.') |
|
|
46 |
|
|
|
47 |
known_args, _ = parser.parse_known_args(argv) |
|
|
48 |
|
|
|
49 |
return known_args |
|
|
50 |
|
|
|
51 |
|
|
|
52 |
def extract_checkpoint_number_from_metrics_filename(filename): |
|
|
53 |
match = re.search(r'ckpt-([\d]*)\.metrics', filename) |
|
|
54 |
if match: |
|
|
55 |
return int(match.group(1)) |
|
|
56 |
|
|
|
57 |
|
|
|
58 |
def read_metrics_file(path): |
|
|
59 |
"""Reads metrics f and outputs metrics in a dict.""" |
|
|
60 |
with open(path) as f: |
|
|
61 |
metrics = { |
|
|
62 |
key.replace('/', '_'): float(value) |
|
|
63 |
for key, value in json.loads(f.read()).items() |
|
|
64 |
} |
|
|
65 |
metrics['checkpoint'] = extract_checkpoint_number_from_metrics_filename(path) |
|
|
66 |
metrics['F1_All'] = 2 * metrics['TPs_All'] / ( |
|
|
67 |
2 * metrics['TPs_All'] + metrics['FNs_All'] + metrics['FPs_All']) |
|
|
68 |
metrics['TPs+FNs_All'] = metrics['TPs_All'] + metrics['FNs_All'] |
|
|
69 |
return metrics |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
def main(argv=None): |
|
|
73 |
"""Main entry point.""" |
|
|
74 |
known_args = parse_cmdline(argv) |
|
|
75 |
metrics_dir = known_args.metrics_dir |
|
|
76 |
metrics_files = [ |
|
|
77 |
join(metrics_dir, f) |
|
|
78 |
for f in listdir(metrics_dir) |
|
|
79 |
if isfile(join(metrics_dir, f)) |
|
|
80 |
] |
|
|
81 |
metrics = [read_metrics_file(f) for f in metrics_files] |
|
|
82 |
for m in metrics: |
|
|
83 |
print('%s\t%s\t%s' % (m['checkpoint'], m['TPs+FNs_All'], m['F1_All'])) |
|
|
84 |
|
|
|
85 |
|
|
|
86 |
if __name__ == '__main__': |
|
|
87 |
logging.getLogger().setLevel(logging.INFO) |
|
|
88 |
main() |