-
Notifications
You must be signed in to change notification settings - Fork 1
/
psgd_cifar10_noisy_label.py
367 lines (311 loc) · 13.3 KB
/
psgd_cifar10_noisy_label.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import sys
import math
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import transforms
import numpy as np
import preconditioned_stochastic_gradient_descent as psgd
from tqdm import tqdm
import random
import os
from torch.autograd import Variable
import random
import copy
import math
from data_loaders.loaders import *
from models.resnet import ResNet18
from models.resnet_affine import ResNet18Affine
from reproduce.seeds import *
parser = argparse.ArgumentParser()
parser.add_argument("--experiment", default='noisy_soft', help="pick which experiment")
parser.add_argument("--stage2", default='cifar10', help="pick stage2 of experiment")
parser.add_argument("--epoch_concept_switch", default=201, help="when should we switch to stage2 of experiment")
parser.add_argument("--num_epoch", default=100, help="how long should our full experiment be")
parser.add_argument("--device", default='cuda:0', help="for example, cuda:0")
parser.add_argument("--optimizer", default='PSGD_Affine', help="choices are SGD, PSGD_XMat and PSGD_UVd")
parser.add_argument("--lr_scheduler", default='cos', help="choices are stage and cos")
parser.add_argument("--shortcut_connection", default=1, type=int, help="choices are 0 and 1")
parser.add_argument('--seed', default=2048, type=int, help='random seed')
parser.add_argument('--data_seed', default=1738, type=int, help='random data_seed')
parser.add_argument('--data_root', default='./data/ntga_cnn_best', help='root of data')
args = parser.parse_args()
experiment = args.experiment
stage2 = args.stage2
num_epoch = args.num_epoch
epoch_concept_switch = args.epoch_concept_switch
device = torch.device(args.device)
optimizer = args.optimizer
lr_scheduler = args.lr_scheduler
shortcut_connection = bool(args.shortcut_connection)
seed = args.seed
data_seed = args.data_seed
data_root = args.data_root
print("Experiment Stage 1: \t\t\t{}".format(experiment))
print("Experiment Stage 2: \t\t\t{}".format(stage2))
print("Total Epochs: \t\t\t{}".format(num_epoch))
print("Change Experiment at Epoch: \t\t\t{}".format(epoch_concept_switch))
print("Device: \t\t\t{}".format(device))
print("Optimizer: \t\t\t{}".format(optimizer))
print("Learning rate schedular:\t{}".format(lr_scheduler))
print("With short connections: \t{}".format(shortcut_connection))
print("Seed: \t\t\t\t{}".format(seed))
print("Data Seed: \t\t\t{}".format(data_seed))
print("Data Root: \t\t\t{}".format(data_root))
set_seed(args.seed)
set_cuda(deterministic=True)
if optimizer == 'SGD':
lr0 = 1.0 # 0.1 -> 1.0 when momentum factor = 0.9 as momentum in PSGD is the moving average of gradient
decay = 5e-4
elif 'Affine' in optimizer:
lr0 = 2e-1
if shortcut_connection:
decay = 2e-2
else:
decay = 1e-2
else: # PSGD_XMat or PSGD_UVd
lr0 = 2e-2
if shortcut_connection:
decay = 2e-2
else:
decay = 1e-2
if shortcut_connection:
batchsize = 128
else:
batchsize = 64
def test(net, device, data_loader, criterion):
# if torch.__version__.startswith('2'):
# net = torch.compile(net)
net.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets, index) in enumerate(data_loader):
# for batch_idx, (inputs, targets) in tqdm( enumerate(data_loader), total = len(data_loader)):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
accuracy = 100.0 * correct / total
return accuracy
def test_clean(net, device, data_loader, criterion):
# if torch.__version__.startswith('2'):
# net = torch.compile(net)
net.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(data_loader):
# for batch_idx, (inputs, targets) in tqdm( enumerate(data_loader), total = len(data_loader)):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
accuracy = 100.0 * correct / total
return accuracy
def train(net, device, train_loader, loss_cores,noise_prior_cur):
# if torch.__version__.startswith('2'):
# net = torch.compile(net)
net.train() # do not forget it as there is BN
total = 0
train_loss = 0
correct = 0
v_list = np.zeros(num_training_samples)
idx_each_class_noisy = [[] for i in range(num_classes)]
if not isinstance(noise_prior_cur, torch.Tensor):
noise_prior_cur = torch.tensor(noise_prior_cur.astype('float32')).to(device).unsqueeze(0)
for batch_idx, (inputs, targets, indexes) in tqdm( enumerate(train_loader), total = len(train_loader)):
inputs, targets = inputs.to(device), targets.to(device)
ind=indexes.cpu().numpy().transpose()
batch_size = len(ind)
class_list = range(num_classes)
outputs = net(inputs)
loss, loss_v = loss_cores(epoch, outputs, targets ,class_list,ind, True, noise_prior = noise_prior_cur )
loss + sum(
[torch.sum(decay * torch.rand_like(param) * param * param) for param in net.parameters()]
)
v_list[ind] = loss_v
for i in range(batch_size):
if loss_v[i] == 0:
idx_each_class_noisy[targets[i]].append(ind[i])
def closure():
"""
Weight decaying is explicitly realized by adding L2 regularization to the loss
"""
return loss, loss_v , outputs
loss, loss_v, outputs = opt.step(closure)
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
# make new noise prior
class_size_noisy = [len(idx_each_class_noisy[i]) for i in range(num_classes)]
noise_prior_delta = np.array(class_size_noisy)
noise_prior_cur = noise_prior*num_training_samples - noise_prior_delta
noise_prior_cur = noise_prior_cur/sum(noise_prior_cur)
train_accuracy = 100.0 * correct / total
return train_loss, train_accuracy, noise_prior_cur
set_seed(args.seed)
# net = ResNet18(shortcut_connection=True).to(device)
net = ResNet18Affine(shortcut_connection=True).to(device)
if optimizer == 'SGD':
# SGD baseline
opt = psgd.XMat(
net.parameters(),
lr_params = lr0, # note that momentum in PSGD is the moving average of gradient
momentum = 0.9, # so lr 0.1 becomes 1 when momentum factor is 0.9
preconditioner_update_probability = 0.0, # PSGD reduces to SGD when P = eye()
)
elif optimizer == 'PSGD_XMat':
# PSGD with X-shape matrix preconditioner
opt = psgd.XMat(
net.parameters(),
lr_params = lr0,
momentum = 0.9,
preconditioner_update_probability = 0.1,
)
elif optimizer == 'PSGD_Affine':
opt = psgd.Affine(
net.parameters(),
lr_params = lr0,
momentum = 0.9,
preconditioner_update_probability = 0.1,
)
else:
# PSGD with low rank approximation preconditioner
opt = psgd.UVd(
net.parameters(),
rank_of_approximation = 50,
lr_params = lr0,
momentum = 0.9,
preconditioner_update_probability = 0.1,
)
# stage 1 of experiment
# please note noisy label experiment requires different training loop -- see psgd_cifar10_noisy_label.py
train_loader, init_noise_prior, test_loader = get_dataset(experiment, batchsize, data_root, seed, data_seed)
num_classes = 10
num_training_samples = 50000
criterion = nn.CrossEntropyLoss()
def loss_cores(epoch, y, t,class_list, ind, noise_or_not, noise_prior = None):
beta = f_beta(epoch)
loss = F.cross_entropy(y, t, reduce = False)
loss_numpy = loss.data.cpu().numpy()
num_batch = len(loss_numpy)
loss_v = np.zeros(num_batch)
loss_div_numpy = float(np.array(0))
loss_ = -torch.log(F.softmax(y) + 1e-8)
# sel metric
loss_sel = loss - torch.mean(loss_,1)
if noise_prior is None:
loss = loss - beta*torch.mean(loss_,1)
else:
loss = loss - beta*torch.sum(torch.mul(noise_prior, loss_),1)
loss_div_numpy = loss_sel.data.cpu().numpy()
for i in range(len(loss_numpy)):
if epoch <=30:
loss_v[i] = 1.0
elif loss_div_numpy[i] <= 0:
loss_v[i] = 1.0
loss_v = loss_v.astype(np.float32)
loss_v_var = Variable(torch.from_numpy(loss_v)).to(device)
loss_ = loss_v_var * loss
if sum(loss_v) == 0.0:
return torch.mean(loss_)/100000000
else:
return torch.sum(loss_)/sum(loss_v), loss_v.astype(int)
#TODO: extend f_beta to 200 epochs -- needs some deeper understanding
def f_beta(epoch):
beta1 = np.linspace(0.0, 0.0, num=10)
beta2 = np.linspace(0.0, 2, num=30)
beta3 = np.linspace(2, 2, num=60)
beta = np.concatenate((beta1,beta2,beta3),axis=0)
return beta[epoch]
num_epoch = 100
train_accs = []
test_accs = []
noise_prior = copy.deepcopy(init_noise_prior)
noise_prior_cur = noise_prior
test_accuracy_best = -1
for epoch in range(num_epoch):
if lr_scheduler == 'cos':
opt.lr_params = lr0*(1 + math.cos(math.pi*epoch/num_epoch))/2
else:
# schedule the learning rate
if epoch == int(num_epoch * 0.7):
opt.lr_params *= 0.1
if epoch == int(num_epoch * 0.9):
opt.lr_params *= 0.1
train_loss, train_accuracy, noise_prior_cur = train(net, device, train_loader, loss_cores, noise_prior_cur)
test_accuracy = test(net, device, test_loader, criterion)
if test_accuracy > test_accuracy_best:
test_accuracy_best = test_accuracy
print(
"epoch: {}; train loss: {:.2f}; train accuracy: {:.2f}; test accuracy: {:.2f}; best test accuracy: {:.2f}".format(
epoch + 1, train_loss, train_accuracy, test_accuracy, test_accuracy_best
)
)
train_accs.append(train_accuracy)
test_accs.append(test_accuracy)
print("train_accuracy: {}".format(train_accs))
print("test_accuracy: {}".format(test_accs))
## if want a stage2...
if stage2 == 'cifar10':
num_epoch = 100
train_accs = []
test_accs = []
train_loader_clean, test_loader_clean = get_dataset('cifar10',batchsize,data_root,seed,data_seed)
for epoch in range(num_epoch):
if lr_scheduler == 'cos':
opt.lr_params = lr0*(1 + math.cos(math.pi*epoch/num_epoch))/2
else:
# schedule the learning rate
if epoch == int(num_epoch * 0.7):
opt.lr_params *= 0.1
if epoch == int(num_epoch * 0.9):
opt.lr_params *= 0.1
net.train() # do not forget it as there is BN
total = 0
train_loss = 0
correct = 0
# for batch_idx, (inputs, targets) in enumerate(train_loader):
for batch_idx, (inputs, targets) in tqdm( enumerate(train_loader_clean), total = len(train_loader_clean)):
inputs, targets = inputs.to(device), targets.to(device)
class_list = range(num_classes)
outputs = net(inputs)
loss, loss_v = loss_cores(epoch, outputs, targets ,class_list, False, noise_prior = None )
loss + sum(
[torch.sum(decay * torch.rand_like(param) * param * param) for param in net.parameters()]
)
def closure():
"""
Weight decaying is explicitly realized by adding L2 regularization to the loss
"""
return loss
loss = opt.step(closure)
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
train_accuracy = 100.0 * correct / total
test_accuracy = test_clean(net, device, test_loader_clean, criterion)
if test_accuracy > test_accuracy_best:
test_accuracy_best = test_accuracy
print(
"epoch: {}; train loss: {:.2f}; train accuracy: {:.2f}; test accuracy: {:.2f}; best test accuracy: {:.2f}".format(
epoch + 1, train_loss, train_accuracy, test_accuracy, test_accuracy_best
)
)
train_accs.append(train_accuracy)
test_accs.append(test_accuracy)
print("train_accuracy: {}".format(train_accs))
print("test_accuracy: {}".format(test_accs))