forked from Mephisto405/Learning-Loss-for-Active-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
283 lines (228 loc) · 10.5 KB
/
main.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
'''Active Learning Procedure in PyTorch.
Reference:
[Yoo et al. 2019] Learning Loss for Active Learning (https://arxiv.org/abs/1905.03677)
'''
# Python
import os
import random
# Torch
import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import torch.optim.lr_scheduler as lr_scheduler
from torch.utils.data.sampler import SubsetRandomSampler
# Torchvison
import torchvision.transforms as T
import torchvision.models as models
from torchvision.datasets import CIFAR100, CIFAR10
# Utils
import visdom
from tqdm import tqdm
# Custom
import models.resnet as resnet
import models.lossnet as lossnet
from config import *
from data.sampler import SubsetSequentialSampler
##
# Data
train_transform = T.Compose([
T.RandomHorizontalFlip(),
T.RandomCrop(size=32, padding=4),
T.ToTensor(),
T.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) # T.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)) # CIFAR-100
])
test_transform = T.Compose([
T.ToTensor(),
T.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]) # T.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)) # CIFAR-100
])
cifar10_train = CIFAR10('../cifar10', train=True, download=True, transform=train_transform)
cifar10_unlabeled = CIFAR10('../cifar10', train=True, download=True, transform=test_transform)
cifar10_test = CIFAR10('../cifar10', train=False, download=True, transform=test_transform)
##
# Loss Prediction Loss
def LossPredLoss(input, target, margin=1.0, reduction='mean'):
assert len(input) % 2 == 0, 'the batch size is not even.'
assert input.shape == input.flip(0).shape
input = (input - input.flip(0))[:len(input)//2] # [l_1 - l_2B, l_2 - l_2B-1, ... , l_B - l_B+1], where batch_size = 2B
target = (target - target.flip(0))[:len(target)//2]
target = target.detach()
one = 2 * torch.sign(torch.clamp(target, min=0)) - 1 # 1 operation which is defined by the authors
if reduction == 'mean':
loss = torch.sum(torch.clamp(margin - one * input, min=0))
loss = loss / input.size(0) # Note that the size of input is already halved
elif reduction == 'none':
loss = torch.clamp(margin - one * input, min=0)
else:
NotImplementedError()
return loss
##
# Train Utils
iters = 0
#
def train_epoch(models, criterion, optimizers, dataloaders, epoch, epoch_loss, vis=None, plot_data=None):
models['backbone'].train()
models['module'].train()
global iters
for data in tqdm(dataloaders['train'], leave=False, total=len(dataloaders['train'])):
inputs = data[0].cuda()
labels = data[1].cuda()
iters += 1
optimizers['backbone'].zero_grad()
optimizers['module'].zero_grad()
scores, features = models['backbone'](inputs)
target_loss = criterion(scores, labels)
if epoch > epoch_loss:
# After 120 epochs, stop the gradient from the loss prediction module propagated to the target model.
features[0] = features[0].detach()
features[1] = features[1].detach()
features[2] = features[2].detach()
features[3] = features[3].detach()
pred_loss = models['module'](features)
pred_loss = pred_loss.view(pred_loss.size(0))
m_backbone_loss = torch.sum(target_loss) / target_loss.size(0)
m_module_loss = LossPredLoss(pred_loss, target_loss, margin=MARGIN)
loss = m_backbone_loss + WEIGHT * m_module_loss
loss.backward()
optimizers['backbone'].step()
optimizers['module'].step()
# Visualize
if (iters % 100 == 0) and (vis != None) and (plot_data != None):
plot_data['X'].append(iters)
plot_data['Y'].append([
m_backbone_loss.item(),
m_module_loss.item(),
loss.item()
])
vis.line(
X=np.stack([np.array(plot_data['X'])] * len(plot_data['legend']), 1),
Y=np.array(plot_data['Y']),
opts={
'title': 'Loss over Time',
'legend': plot_data['legend'],
'xlabel': 'Iterations',
'ylabel': 'Loss',
'width': 1200,
'height': 390,
},
win=1
)
#
def test(models, dataloaders, mode='val'):
assert mode == 'val' or mode == 'test'
models['backbone'].eval()
models['module'].eval()
total = 0
correct = 0
with torch.no_grad():
for (inputs, labels) in dataloaders[mode]:
inputs = inputs.cuda()
labels = labels.cuda()
scores, _ = models['backbone'](inputs)
_, preds = torch.max(scores.data, 1)
total += labels.size(0)
correct += (preds == labels).sum().item()
return 100 * correct / total
#
def train(models, criterion, optimizers, schedulers, dataloaders, num_epochs, epoch_loss, vis, plot_data):
print('>> Train a Model.')
best_acc = 0.
checkpoint_dir = os.path.join('./cifar10', 'train', 'weights')
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
for epoch in range(num_epochs):
schedulers['backbone'].step()
schedulers['module'].step()
train_epoch(models, criterion, optimizers, dataloaders, epoch, epoch_loss, vis, plot_data)
# Save a checkpoint
if False and epoch % 5 == 4:
acc = test(models, dataloaders, 'test')
if best_acc < acc:
best_acc = acc
torch.save({
'epoch': epoch + 1,
'state_dict_backbone': models['backbone'].state_dict(),
'state_dict_module': models['module'].state_dict()
},
'%s/active_resnet18_cifar10.pth' % (checkpoint_dir))
print('Val Acc: {:.3f} \t Best Acc: {:.3f}'.format(acc, best_acc))
print('>> Finished.')
#
def get_uncertainty(models, unlabeled_loader):
models['backbone'].eval()
models['module'].eval()
uncertainty = torch.tensor([]).cuda()
with torch.no_grad():
for (inputs, labels) in unlabeled_loader:
inputs = inputs.cuda()
# labels = labels.cuda()
scores, features = models['backbone'](inputs)
pred_loss = models['module'](features) # pred_loss = criterion(scores, labels) # ground truth loss
pred_loss = pred_loss.view(pred_loss.size(0))
uncertainty = torch.cat((uncertainty, pred_loss), 0)
return uncertainty.cpu()
##
# Main
if __name__ == '__main__':
vis = visdom.Visdom(server='http://localhost', port=9000)
plot_data = {'X': [], 'Y': [], 'legend': ['Backbone Loss', 'Module Loss', 'Total Loss']}
for trial in range(TRIALS):
# Initialize a labeled dataset by randomly sampling K=ADDENDUM=1,000 data points from the entire dataset.
indices = list(range(NUM_TRAIN))
random.shuffle(indices)
labeled_set = indices[:ADDENDUM]
unlabeled_set = indices[ADDENDUM:]
train_loader = DataLoader(cifar10_train, batch_size=BATCH,
sampler=SubsetRandomSampler(labeled_set),
pin_memory=True)
test_loader = DataLoader(cifar10_test, batch_size=BATCH)
dataloaders = {'train': train_loader, 'test': test_loader}
# Model
resnet18 = resnet.ResNet18(num_classes=10).cuda()
loss_module = lossnet.LossNet().cuda()
models = {'backbone': resnet18, 'module': loss_module}
torch.backends.cudnn.benchmark = True
# Active learning cycles
for cycle in range(CYCLES):
# Loss, criterion and scheduler (re)initialization
criterion = nn.CrossEntropyLoss(reduction='none')
optim_backbone = optim.SGD(models['backbone'].parameters(), lr=LR,
momentum=MOMENTUM, weight_decay=WDECAY)
optim_module = optim.SGD(models['module'].parameters(), lr=LR,
momentum=MOMENTUM, weight_decay=WDECAY)
sched_backbone = lr_scheduler.MultiStepLR(optim_backbone, milestones=MILESTONES)
sched_module = lr_scheduler.MultiStepLR(optim_module, milestones=MILESTONES)
optimizers = {'backbone': optim_backbone, 'module': optim_module}
schedulers = {'backbone': sched_backbone, 'module': sched_module}
# Training and test
train(models, criterion, optimizers, schedulers, dataloaders, EPOCH, EPOCHL, vis, plot_data)
acc = test(models, dataloaders, mode='test')
print('Trial {}/{} || Cycle {}/{} || Label set size {}: Test acc {}'.format(trial+1, TRIALS, cycle+1, CYCLES, len(labeled_set), acc))
##
# Update the labeled dataset via loss prediction-based uncertainty measurement
# Randomly sample 10000 unlabeled data points
random.shuffle(unlabeled_set)
subset = unlabeled_set[:SUBSET]
# Create unlabeled dataloader for the unlabeled subset
unlabeled_loader = DataLoader(cifar10_unlabeled, batch_size=BATCH,
sampler=SubsetSequentialSampler(subset), # more convenient if we maintain the order of subset
pin_memory=True)
# Measure uncertainty of each data points in the subset
uncertainty = get_uncertainty(models, unlabeled_loader)
# Index in ascending order
arg = np.argsort(uncertainty)
# Update the labeled dataset and the unlabeled dataset, respectively
labeled_set += list(torch.tensor(subset)[arg][-ADDENDUM:].numpy())
unlabeled_set = list(torch.tensor(subset)[arg][:-ADDENDUM].numpy()) + unlabeled_set[SUBSET:]
# Create a new dataloader for the updated labeled dataset
dataloaders['train'] = DataLoader(cifar10_train, batch_size=BATCH,
sampler=SubsetRandomSampler(labeled_set),
pin_memory=True)
# Save a checkpoint
torch.save({
'trial': trial + 1,
'state_dict_backbone': models['backbone'].state_dict(),
'state_dict_module': models['module'].state_dict()
},
'./cifar10/train/weights/active_resnet18_cifar10_trial{}.pth'.format(trial))