-
Notifications
You must be signed in to change notification settings - Fork 2
/
gradients.py
executable file
·162 lines (142 loc) · 5.5 KB
/
gradients.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import yaml
import torch
import argparse
import tqdm
import pandas as pd
import numpy as np
import torch.nn as nn
from numpy import save
from baseline import ModelBase, DataBase
from influencer.IF import IF
from influencer.TracIn import TracIn
from influencer.buildGradient import build_gradient
from influencer.GD import GD
from influencer.GC import GC
from influencer.RIF import RIF
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', type=str, required=True, help='config yaml path')
parser.add_argument('--seed', type=int, default=0)
opt = parser.parse_args()
SEED = opt.seed
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
with open(opt.cfg, "r") as f:
cfg = yaml.safe_load(f)
if not torch.cuda.is_available() and cfg['device'] == 'cuda':
print('Your device don\'t have cuda, please check or select cpu and retraining')
exit(0)
if cfg['device'] == 'cuda':
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
# get name
name_train = cfg['df_train'].split('/')[-1].split('.')[0]
name_test = cfg['df_test'].split('/')[-1].split('.')[0]
print("Name train: {}, Name test: {}".format(name_train, name_test))
print("Directory: {}".format(cfg['dir_checkpoint']))
# Load data
df_train = pd.read_csv(cfg['df_train'])
df_test = pd.read_csv(cfg['df_test'])
data_base = DataBase(cfg['data'])
train_loader = data_base.get_dataloader(
df=df_train,
batch_size=1,
mode='test',
num_workers=0
)
test_loader = data_base.get_dataloader(
df=df_test,
batch_size=1,
mode='test',
num_workers=0
)
# Config model
model_base = ModelBase(
model_type=cfg['model'],
number_classes=cfg['number_classes'],
device=cfg['device']
)
model_base.load_model(os.path.join(cfg['dir_checkpoint'],'best.pt'))
loss_fn = nn.CrossEntropyLoss()
params = [p for p in model_base.model.parameters() if p.requires_grad][-2:]
# Build gradient
if not os.path.exists(os.path.join(cfg['dir_checkpoint'],f'{name_train}.grad')):
train_gradients = build_gradient(
inference_fn = model_base.inference,
loss_fn = loss_fn,
params = params,
dataloader = train_loader
)
torch.save(train_gradients, os.path.join(cfg['dir_checkpoint'],f'{name_train}.grad'))
else:
train_gradients = torch.load(os.path.join(cfg['dir_checkpoint'], f'{name_train}.grad'))
if not os.path.exists(os.path.join(cfg['dir_checkpoint'],f'{name_test}.grad')):
test_gradients = build_gradient(
inference_fn = model_base.inference,
loss_fn = loss_fn,
params = params,
dataloader = test_loader
)
torch.save(test_gradients, os.path.join(cfg['dir_checkpoint'], f'{name_test}.grad'))
else:
test_gradients = torch.load(os.path.join(cfg['dir_checkpoint'], f'{name_test}.grad'))
# Run methods
if 'IF' in cfg['methods']:
print("Run Influence Function:")
results = IF(
test_loader=test_loader,
train_loader=train_loader,
test_gradients=test_gradients,
train_gradients=train_gradients,
inference_fn=model_base.inference,
loss_fn=loss_fn,
params=params,
use_exact_hessian=cfg['use_exact_hessian']
)
if cfg['use_exact_hessian']:
save(os.path.join(cfg['dir_checkpoint'],f'IF_exact_{name_train}_{name_test}'), results)
else:
save(os.path.join(cfg['dir_checkpoint'],f'IF_approximate_{name_train}_{name_test}'), results)
if 'GD' in cfg['methods']:
print("Run Grad-Dot:")
results = GD(train_gradients, test_gradients)
save(os.path.join(cfg['dir_checkpoint'],f'GD_{name_train}_{name_test}'), results)
if 'GC' in cfg['methods']:
print("Run Grad-Cos:")
results = GC(train_gradients, test_gradients)
save(os.path.join(cfg['dir_checkpoint'],f'GC_{name_train}_{name_test}'), results)
if 'RIF' in cfg['methods']:
print("Run RelatIF:")
results = RIF(
test_loader=test_loader,
train_loader=train_loader,
test_gradients=test_gradients,
train_gradients=train_gradients,
inference_fn=model_base.inference,
loss_fn=loss_fn,
params=params,
use_exact_hessian=cfg['use_exact_hessian']
)
if cfg['use_exact_hessian']:
save(os.path.join(cfg['dir_checkpoint'],f'RIF_exact_{name_train}_{name_test}'), results)
else:
save(os.path.join(cfg['dir_checkpoint'],f'RIF_approximate_{name_train}_{name_test}'), results)
if 'TracIn' in cfg['methods']:
start = 0
f = open(os.path.join(cfg['dir_checkpoint'],'best_epoch.txt'))
end = int(f.readline())
print("Run Tracin from epoch {} to epoch {}".format(start, end))
results = TracIn(
dir_checkpoint=cfg['dir_checkpoint'],
model_base=model_base,
train_loader=train_loader,
test_loader=test_loader,
loss_fn=loss_fn,
start=start,
end=end
)
save(os.path.join(cfg['dir_checkpoint'],f'TracIn_{name_train}_{name_test}'), results)