-
Notifications
You must be signed in to change notification settings - Fork 3
/
calc_fid.py
88 lines (57 loc) · 1.71 KB
/
calc_fid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import click
import json
def read_txt(file):
with open(file) as f:
lines = f.readlines()
key = []
val = []
for line in lines:
if line=="\n":
continue
key.append(line.split(":")[0])
val.append(line.split(":")[1].split("\n")[0])
print("existing key: ", key)
# print("val: ", val)
return key, val
@click.command()
@click.option('--path', help='root path', metavar='PATH', required=True)
@click.option('--gpus', help='Number of GPUs to use', type=int, default=1, metavar='INT', show_default=True)
@click.option('--inter', help='keep every # of ckpt for fid computing', type=int, default=5, metavar='INT', show_default=True)
def main(path, gpus, inter):
pre = "network-snapshot-"
# load and sort all pkl
ckpt_all_list = []
for file in os.listdir(path):
if not file.endswith(".pkl"):
continue
tmp = file.split(".pkl")[0]
num = tmp.split("-")[-1]
ckpt_all_list.append(num)
ckpt_all_list.sort()
print("all ckpt in folder ",ckpt_all_list)
# filter list by interval
ckpt_inter_list = ckpt_all_list[0::inter]
print("ckpt filtered by interval ",ckpt_inter_list)
# filter by existing pkl
txt_file = os.path.join(path, 'fid.txt')
existing_ckpt_list = []
if os.path.exists(txt_file):
existing_ckpt_list, _ = read_txt(txt_file)
ckpt_final_list = []
for ckpt in ckpt_inter_list:
# print(ckpt)
if ckpt in existing_ckpt_list:
# print("remove")
continue
ckpt_final_list.append(ckpt)
print("final ckpt list ",ckpt_final_list)
for ckpt in ckpt_final_list:
network_path = os.path.join(path, pre+ckpt+".pkl")
cmd = 'python calc_metrics.py' \
+ ' --network=' + network_path \
+ ' --gpus ' + str(gpus) \
print(cmd)
os.system(cmd)
if __name__=="__main__":
main()