-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheval_gan_drs_with_index.py
134 lines (118 loc) · 4.51 KB
/
eval_gan_drs_with_index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import argparse
import os
import random
from pathlib import Path
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
import pickle
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
import torch_mimicry as mmc
from diagan.datasets.predefined import get_predefined_dataset
from diagan.models.predefined_models import get_gan_model
from diagan.trainer.evaluate import evaluate_drs_with_index
from diagan.utils.settings import set_seed
from diagan.utils.plot import calculate_scores
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", "-d", default="cifar10", type=str)
parser.add_argument("--work_dir", default="./exp_results", type=str, help="output dir")
parser.add_argument("--exp_name", default="mimicry_pretrained-seed1", type=str, help="exp name")
parser.add_argument("--baseline_exp_name", type=str, help="exp name")
parser.add_argument('--p1_step', default=40000, type=int)
parser.add_argument("--model", default="sngan", type=str, help="network model")
parser.add_argument("--loss_type", default="hinge", type=str, help="loss type")
parser.add_argument('--gpu', default='0', type=str,
help='id(s) for CUDA_VISIBLE_DEVICES')
parser.add_argument('--batch_size', default=128, type=int)
parser.add_argument('--seed', default=1, type=int)
parser.add_argument("--netG_ckpt_step", type=int)
parser.add_argument("--netG_train_mode", action='store_true')
parser.add_argument("--use_original_netD", action='store_true')
parser.add_argument('--resample_score', type=str)
parser.add_argument('--gold', action='store_true')
parser.add_argument('--topk', action='store_true')
parser.add_argument("--index_num", default=100, type=int, help="number of index to use for FID score")
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
output_dir = f'{args.work_dir}/{args.exp_name}'
save_path = Path(output_dir)
save_path.mkdir(parents=True, exist_ok=True)
baseline_output_dir = f'{args.work_dir}/{args.baseline_exp_name}'
baseline_save_path = Path(baseline_output_dir)
set_seed(args.seed)
if torch.cuda.is_available():
device = "cuda"
cudnn.benchmark = True
else:
device = "cpu"
# load model
assert args.netG_ckpt_step
print(f'load model from {save_path} step: {args.netG_ckpt_step}')
netG, _, netD_drs, _, _, _ = get_gan_model(
dataset_name=args.dataset,
model=args.model,
loss_type=args.loss_type,
drs=True,
topk=args.topk,
gold=args.gold,
)
if not args.netG_train_mode:
netG.eval()
netD_drs.eval()
netG.to(device)
netD_drs.to(device)
if args.dataset == 'celeba':
dataset = 'celeba_64'
window = 5000
else:
dataset = args.dataset
window = 5000
logit_path = baseline_save_path / 'logits_netD_eval.pkl'
print(f'Use logit from: {logit_path}')
logits = pickle.load(open(logit_path, "rb"))
score_start_step = (args.p1_step - window)
score_end_step = args.p1_step
score_dict = calculate_scores(logits, start_epoch=score_start_step, end_epoch=score_end_step)
sample_weights = score_dict[args.resample_score]
print(
f'sample_weights mean: {sample_weights.mean()}, var: {sample_weights.var()}, max: {sample_weights.max()}, min: {sample_weights.min()}')
print(args)
sort_index = np.argsort(sample_weights)
high_index = sort_index[-args.index_num:]
low_index = sort_index[:args.index_num]
# Evaluate fid with index of high weight
evaluate_drs_with_index(
metric='fid',
index=high_index,
log_dir=save_path,
netG=netG,
netD_drs=netD_drs,
dataset=dataset,
num_fake_samples=50000,
evaluate_step=args.netG_ckpt_step,
num_runs=1,
device=device,
stats_file=None,
use_original_netD=args.use_original_netD,
name=f'high_{args.resample_score}', )
# Evaluate fid with index of low weight
evaluate_drs_with_index(
metric='fid',
index=low_index,
log_dir=save_path,
netG=netG,
netD_drs=netD_drs,
dataset=dataset,
num_fake_samples=50000,
evaluate_step=args.netG_ckpt_step,
num_runs=1,
device=device,
stats_file=None,
use_original_netD=args.use_original_netD,
name=f'low_{args.resample_score}', )
if __name__ == '__main__':
main()