-
Notifications
You must be signed in to change notification settings - Fork 10
/
train_vanilla.py
232 lines (188 loc) · 6.78 KB
/
train_vanilla.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import os
import pickle
from tqdm import tqdm
from datetime import datetime
import numpy as np
import torch
from torch.utils.data import DataLoader, RandomSampler, BatchSampler, WeightedRandomSampler
from torch.utils.data.dataset import Subset
from torchvision import transforms as T
import torch.nn.functional as F
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
from torch.utils.tensorboard import SummaryWriter
from config import ex
from data.util import get_dataset, IdxDataset, ZippedDataset
from module.util import get_model
from util import MultiDimAverageMeter
@ex.automain
def train(
main_tag,
dataset_tag,
model_tag,
data_dir,
log_dir,
device,
target_attr_idx,
bias_attr_idx,
main_num_steps,
main_valid_freq,
main_batch_size,
main_optimizer_tag,
main_learning_rate,
main_weight_decay,
):
print(dataset_tag)
device = torch.device(device)
start_time = datetime.now()
writer = SummaryWriter(os.path.join(log_dir, "summary", main_tag))
train_dataset = get_dataset(
dataset_tag,
data_dir=data_dir,
dataset_split="train",
transform_split="train"
)
valid_dataset = get_dataset(
dataset_tag,
data_dir=data_dir,
dataset_split="eval",
transform_split="eval"
)
train_target_attr = train_dataset.attr[:, target_attr_idx]
train_bias_attr = train_dataset.attr[:, bias_attr_idx]
attr_dims = []
attr_dims.append(torch.max(train_target_attr).item() + 1)
attr_dims.append(torch.max(train_bias_attr).item() + 1)
num_classes = attr_dims[0]
train_dataset = IdxDataset(train_dataset)
valid_dataset = IdxDataset(valid_dataset)
train_loader = DataLoader(
train_dataset,
batch_size=main_batch_size,
shuffle=True,
num_workers=16,
pin_memory=True,
)
valid_loader = DataLoader(
valid_dataset,
batch_size=256,
shuffle=False,
num_workers=16,
pin_memory=True,
)
# define model and optimizer
model = get_model(model_tag, num_classes).to(device)
if main_optimizer_tag == "SGD":
optimizer = torch.optim.SGD(
model.parameters(),
lr=main_learning_rate,
weight_decay=main_weight_decay,
momentum=0.9,
)
elif main_optimizer_tag == "Adam":
optimizer = torch.optim.Adam(
model.parameters(),
lr=main_learning_rate,
weight_decay=main_weight_decay,
)
elif main_optimizer_tag == "AdamW":
optimizer = torch.optim.AdamW(
model.parameters(),
lr=main_learning_rate,
weight_decay=main_weight_decay,
)
else:
raise NotImplementedError
# define loss
criterion = torch.nn.CrossEntropyLoss()
label_criterion = torch.nn.CrossEntropyLoss(reduction="none")
# define evaluation function
def evaluate(model, data_loader):
model.eval()
acc = 0
attrwise_acc_meter = MultiDimAverageMeter(attr_dims)
for index, data, attr in tqdm(data_loader, leave=False):
label = attr[:, target_attr_idx]
data = data.to(device)
attr = attr.to(device)
label = label.to(device)
with torch.no_grad():
logit = model(data)
pred = logit.data.max(1, keepdim=True)[1].squeeze(1)
correct = (pred == label).long()
attr = attr[:, [target_attr_idx, bias_attr_idx]]
attrwise_acc_meter.add(correct.cpu(), attr.cpu())
accs = attrwise_acc_meter.get_mean()
model.train()
return accs
# define extracting indices function
def get_align_skew_indices (lookup_list, indices):
'''
lookup_list:
A list of non-negative integer. 0 should indicate bias-align sample and otherwise(>0) indicate bias-skewed sample.
Length of (lookup_list) should be the number of unique samples
indices:
True indices of sample to look up.
'''
pseudo_bias_label = lookup_list[indices]
skewed_indices = (pseudo_bias_label != 0).nonzero().squeeze(1)
aligned_indices = (pseudo_bias_label == 0).nonzero().squeeze(1)
return aligned_indices, skewed_indices
valid_attrwise_accs_list = []
for step in tqdm(range(main_num_steps)):
try:
index, data, attr = next(train_iter)
except:
train_iter = iter(train_loader)
index, data, attr = next(train_iter)
data = data.to(device)
attr = attr.to(device)
label = attr[:, target_attr_idx]
logit = model(data)
loss_per_sample = label_criterion(logit.squeeze(1), label)
loss = loss_per_sample.mean()
optimizer.zero_grad()
loss.backward()
optimizer.step()
main_log_freq = 10
if step % main_log_freq == 0:
loss = loss.detach().cpu()
writer.add_scalar("loss/train", loss, step)
bias_attr = attr[:, bias_attr_idx] # oracle
loss_per_sample = loss_per_sample.detach()
if (label == bias_attr).any().item():
aligned_loss = loss_per_sample[label == bias_attr].mean()
writer.add_scalar("loss/train_aligned", aligned_loss, step)
if (label != bias_attr).any().item():
skewed_loss = loss_per_sample[label != bias_attr].mean()
writer.add_scalar("loss/train_skewed", skewed_loss, step)
if step % main_valid_freq == 0:
valid_attrwise_accs = evaluate(model, valid_loader)
valid_attrwise_accs_list.append(valid_attrwise_accs)
valid_accs = torch.mean(valid_attrwise_accs)
writer.add_scalar("acc/valid", valid_accs, step)
eye_tsr = torch.eye(num_classes)
writer.add_scalar(
"acc/valid_aligned",
valid_attrwise_accs[eye_tsr > 0.0].mean(),
step
)
writer.add_scalar(
"acc/valid_skewed",
valid_attrwise_accs[eye_tsr == 0.0].mean(),
step
)
os.makedirs(os.path.join(log_dir, "result", main_tag), exist_ok=True)
result_path = os.path.join(log_dir, "result", main_tag, "result.th")
valid_attrwise_accs_list = torch.cat(valid_attrwise_accs_list)
with open(result_path, "wb") as f:
torch.save({"valid/attrwise_accs": valid_attrwise_accs_list}, f)
model_path = os.path.join(log_dir, "result", main_tag, "model.th")
state_dict = {
'steps': step,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
}
with open(model_path, "wb") as f:
torch.save(state_dict, f)