-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain_biased_mnist_bc_uni.py
207 lines (156 loc) · 6.92 KB
/
train_biased_mnist_bc_uni.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
import argparse
import datetime
import logging
import os
import time
from pathlib import Path
import numpy as np
import torch
from torch import nn
from debias.datasets.biased_mnist import get_color_mnist
from debias.losses.bias_contrastive_uni import BiasContrastiveLossUni
from debias.networks.simple_conv import SimpleConvNet
from debias.utils.logging import set_logging
from debias.utils.utils import (AverageMeter, MultiDimAverageMeter, accuracy,
save_model, set_seed, pretty_dict)
def parse_option():
parser = argparse.ArgumentParser()
parser.add_argument('--exp_name', type=str, default='test', )
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--print_freq', type=int, default=300,
help='print frequency')
parser.add_argument('--save_freq', type=int, default=200,
help='save frequency')
parser.add_argument('--epochs', type=int, default=80,
help='number of training epochs')
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--corr', type=float, default=0.999)
parser.add_argument('--bs', type=int, default=128, help='batch_size')
parser.add_argument('--lr', type=float, default=1e-3)
parser.add_argument('--weight', type=float, default=0.01)
parser.add_argument('--bb', type=int, default=0)
opt = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = str(opt.gpu)
return opt
def train(train_loader, model, criterion, optimizer, epoch, opt):
model.train()
avg_ce_loss = AverageMeter()
avg_con_loss = AverageMeter()
avg_loss = AverageMeter()
train_iter = iter(train_loader)
for idx, (images, labels, biases, _) in enumerate(train_iter):
bsz = labels.shape[0]
labels, biases = labels.cuda(), biases.cuda()
images = images.cuda()
logits, feats = model(images)
ce_loss, con_loss = criterion(logits, labels, biases, feats)
loss = ce_loss * opt.weight + con_loss
avg_ce_loss.update(ce_loss.item(), bsz)
avg_con_loss.update(con_loss.item(), bsz)
avg_loss.update(loss.item(), bsz)
optimizer.zero_grad()
loss.backward()
optimizer.step()
return avg_ce_loss.avg, avg_con_loss.avg, avg_loss.avg
def validate(val_loader, model):
model.eval()
top1 = AverageMeter()
attrwise_acc_meter = MultiDimAverageMeter(dims=(10, 10))
with torch.no_grad():
for idx, (images, labels, biases, _) in enumerate(val_loader):
images, labels, biases = images.cuda(), labels.cuda(), biases.cuda()
bsz = labels.shape[0]
output, _ = model(images)
preds = output.data.max(1, keepdim=True)[1].squeeze(1)
acc1, = accuracy(output, labels, topk=(1,))
top1.update(acc1[0], bsz)
corrects = (preds == labels).long()
attrwise_acc_meter.add(corrects.cpu(), torch.stack([labels.cpu(), biases.cpu()], dim=1))
return top1.avg, attrwise_acc_meter.get_unbiased_acc()
def main():
opt = parse_option()
exp_name = f'bc_uni-bb{opt.bb}-color_mnist_corr{opt.corr}-{opt.exp_name}-lr{opt.lr}-bs{opt.bs}-weight{opt.weight}-seed{opt.seed}'
opt.exp_name = exp_name
output_dir = f'exp_results/{exp_name}'
save_path = Path(output_dir)
save_path.mkdir(parents=True, exist_ok=True)
set_logging(exp_name, 'INFO', str(save_path))
set_seed(opt.seed)
logging.info(f'save_path: {save_path}')
np.set_printoptions(precision=3)
torch.set_printoptions(precision=3)
root = './data/biased_mnist'
train_loader = get_color_mnist(
root,
batch_size=opt.bs,
data_label_correlation=opt.corr,
n_confusing_labels=9,
split='train',
seed=opt.seed,
aug=False, )
logging.info(
f'confusion_matrix - \n original: {train_loader.dataset.confusion_matrix_org}, \n normalized: {train_loader.dataset.confusion_matrix}')
val_loaders = {}
val_loaders['valid'] = get_color_mnist(
root,
batch_size=256,
data_label_correlation=0.1,
n_confusing_labels=9,
split='train_val',
seed=opt.seed,
aug=False,
)
val_loaders['test'] = get_color_mnist(
root,
batch_size=256,
data_label_correlation=0.1,
n_confusing_labels=9,
split='valid',
seed=opt.seed,
aug=False)
model = SimpleConvNet().cuda()
criterion = BiasContrastiveLossUni(
confusion_matrix=train_loader.dataset.confusion_matrix,
weight=opt.weight,
bb=opt.bb
)
decay_epochs = [opt.epochs // 3, opt.epochs * 2 // 3]
optimizer = torch.optim.Adam(model.parameters(), lr=opt.lr, weight_decay=1e-4)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=decay_epochs, gamma=0.1)
logging.info(f"decay_epochs: {decay_epochs}")
(save_path / 'checkpoints').mkdir(parents=True, exist_ok=True)
best_accs = {'valid': 0, 'test': 0}
best_epochs = {'valid': 0, 'test': 0}
best_stats = {}
start_time = time.time()
last_epoch_time = time.time()
for epoch in range(1, opt.epochs + 1):
logging.info(f'[{epoch} / {opt.epochs}] Learning rate: {scheduler.get_last_lr()[0]} weight: {opt.weight}')
ce_loss, con_loss, loss = train(train_loader, model, criterion, optimizer, epoch, opt)
logging.info(f'[{epoch} / {opt.epochs}] Loss: {loss} CE Loss: {ce_loss} Con Loss: {con_loss}')
scheduler.step()
stats = pretty_dict(epoch=epoch)
for key, val_loader in val_loaders.items():
_, acc_unbiased = validate(val_loader, model)
stats[f'{key}/acc_unbiased'] = acc_unbiased.item() * 100
logging.info(f'[{epoch} / {opt.epochs}] {stats}')
for tag in best_accs.keys():
if stats[f'{tag}/acc_unbiased'] > best_accs[tag]:
best_accs[tag] = stats[f'{tag}/acc_unbiased']
best_epochs[tag] = epoch
best_stats[tag] = pretty_dict(**{f'best_{tag}_{k}': v for k, v in stats.items()})
save_file = save_path / 'checkpoints' / f'best_{tag}.pth'
save_model(model, optimizer, opt, epoch, save_file)
logging.info(
f'[{epoch} / {opt.epochs}] best {tag} accuracy: {best_accs[tag]:.3f} at epoch {best_epochs[tag]} \n best_stats: {best_stats[tag]}')
epoch_time = time.time()
time_diff_str = str(datetime.timedelta(seconds=int(epoch_time - last_epoch_time)))
last_epoch_time = epoch_time
logging.info(f'[{epoch} / {opt.epochs}] Time spent in this epoch: {time_diff_str}')
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
logging.info(f'Total training time: {total_time_str}')
save_file = save_path / 'checkpoints' / f'last.pth'
save_model(model, optimizer, opt, opt.epochs, save_file)
if __name__ == '__main__':
main()