-
Notifications
You must be signed in to change notification settings - Fork 10
/
demo2_second-stage-training.py
388 lines (228 loc) · 9.24 KB
/
demo2_second-stage-training.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python
# coding: utf-8
# # Long-Tailed Recognition via Weight Balancing
# ## Demonstration for the second stage training
# CVPR 2022 Submission #0020
#
# ## import packages
#
#
# Some packages are installed automatically if you use Anaconda. There are other packages not included in Anaconda, such as pandas, seaborn, PyTorch, and torchvision. They should be installed properly.
# In[1]:
from __future__ import print_function, division
import os, random, time, copy, scipy, pickle, sys, math
from skimage import io, transform
import numpy as np
import os.path as path
import scipy.io as sio
from scipy import misc
from scipy import ndimage, signal
import matplotlib.pyplot as plt
import PIL.Image
from io import BytesIO
from skimage import data, img_as_float
import pandas as pd
import seaborn as sn
import sklearn.metrics
from skimage.metrics import structural_similarity as ssim
from skimage.metrics import peak_signal_noise_ratio as psnr
import torch
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import torch.nn.functional as F
from torch.autograd import Variable
import torchvision
from torchvision import datasets, models, transforms
from utils.eval_funcs import *
from utils.dataset_CIFAR100LT import *
from utils.network_arch_resnet import *
from utils.trainval import *
from utils.plot_funcs import *
from utils.regularizers import *
from utils.class_balanced_loss import CB_loss
import warnings # ignore warnings
warnings.filterwarnings("ignore")
print(sys.version)
print(torch.__version__)
# ## Setup config parameters
#
# There are several things to setup, like which GPU to use, model name, hyper-parameters, etc. Please read the comments. By default, you should be able to run this script smoothly without changing anything.
# In[2]:
# fix the random seed for fair comparison.
# NOTE that we also removed "shuffle" lines in generating long-tailed CIFAR already (cf. util.dataset_CIFAR100LT.py)
torch.manual_seed(0)
np.random.seed(0)
# In[3]:
# set device, which gpu to use.
device ='cpu'
if torch.cuda.is_available():
device='cuda'
# In[4]:
curr_working_dir = os.getcwd()
project_name = 'demo_2'
imb_type = 'exp' # samling long-tailed training set with an exponetially-decaying function
imb_factor = 0.01 # imbalance factor = 100 = 1/0.01
nClasses = 100 # number of classes in CIFAR100-LT with imbalance factor 100
encoder_num_layers = 34 # network architecture is ResNet34
batch_size = 64 # batch size
isPretrained = False
torch.cuda.device_count()
torch.cuda.empty_cache()
save_dir = path.join(curr_working_dir, 'exp', project_name)
if not os.path.exists(save_dir): os.makedirs(save_dir)
log_filename = os.path.join(save_dir, 'train.log')
# ## setup dataset
# download the CIFAR100 dataset if there does not exist one.
# In[5]:
path_to_DB = './datasets'
if not os.path.exists(path_to_DB): os.makedirs(path_to_DB)
_ = torchvision.datasets.CIFAR100(root=path_to_DB, train=True, download=True)
# In[6]:
path_to_DB = path.join(path_to_DB, 'cifar-100-python')
datasets = {}
dataloaders = {}
setname = 'meta'
with open(os.path.join(path_to_DB, setname), 'rb') as obj:
labelnames = pickle.load(obj, encoding='bytes')
labelnames = labelnames[b'fine_label_names']
for i in range(len(labelnames)):
labelnames[i] = labelnames[i].decode("utf-8")
setname = 'train'
with open(os.path.join(path_to_DB, setname), 'rb') as obj:
DATA = pickle.load(obj, encoding='bytes')
imgList = DATA[b'data'].reshape((DATA[b'data'].shape[0],3, 32,32))
labelList = DATA[b'fine_labels']
total_num = len(labelList)
img_num_per_cls = get_img_num_per_cls(nClasses, total_num, imb_type, imb_factor)
new_imgList, new_labelList = gen_imbalanced_data(img_num_per_cls, imgList, labelList)
datasets[setname] = CIFAR100LT(
imageList=new_imgList, labelList=new_labelList, labelNames=labelnames,
set_name=setname, isAugment=setname=='train')
print('#examples in {}-set:'.format(setname), datasets[setname].current_set_len)
setname = 'test'
with open(os.path.join(path_to_DB, setname), 'rb') as obj:
DATA = pickle.load(obj, encoding='bytes')
imgList = DATA[b'data'].reshape((DATA[b'data'].shape[0],3, 32,32))
labelList = DATA[b'fine_labels']
total_num = len(labelList)
datasets[setname] = CIFAR100LT(
imageList=imgList, labelList=labelList, labelNames=labelnames,
set_name=setname, isAugment=setname=='train')
print('#examples in {}-set:'.format(setname), datasets[setname].current_set_len)
dataloaders = {set_name: DataLoader(datasets[set_name],
batch_size=batch_size,
shuffle=set_name=='train',
num_workers=4) # num_work can be set to batch_size
for set_name in ['train', 'test']} # 'train',
print('#train batch:', len(dataloaders['train']), '\t#test batch:', len(dataloaders['test']))
# In[7]:
plt.plot(img_num_per_cls)
plt.xlabel('class ID sorted by cardinality')
plt.ylabel('#training examples')
# In[8]:
data_sampler = iter(dataloaders['train'])
data = next(data_sampler)
imageList, labelList = data
imageList = imageList.to(device)
labelList = labelList.type(torch.long).view(-1).to(device)
print(imageList.shape)
imList = imageList.permute(0,2,3,1).cpu().numpy()
imList -= imList.min()
imList /= imList.max()+0.0001
imList = createMontage(imList, (32, 32, 64))
fig = plt.figure(figsize=(5,5), dpi=95) # better display with larger figure
plt.imshow(imList)
# In[9]:
data_sampler = iter(dataloaders['test'])
data = next(data_sampler)
imageList, labelList = data
imageList = imageList.to(device)
labelList = labelList.type(torch.long).view(-1).to(device)
print(imageList.shape)
imList = imageList.permute(0,2,3,1).cpu().numpy()
imList -= imList.min()
imList /= imList.max()+0.0001
imList = createMontage(imList, (32, 32, 64))
fig = plt.figure(figsize=(5,5), dpi=95) # better display with larger figure
plt.imshow(imList)
# # Second Stage training
#
# ## Download base model
# In[10]:
################## load base model ###################
path_to_model = './exp/demo_1/with_WD_model_best.paramOnly'
base_model = ResnetEncoder(encoder_num_layers, isPretrained, embDimension=nClasses, poolSize=4).to(device)
base_model.load_state_dict(torch.load(path_to_model, map_location=device))
base_model.to(device);
# In[11]:
models = {'base': base_model}
# In[12]:
print_accuracy(base_model, dataloaders, new_labelList, device = device)
# ## Applying L2 Normlaization
# In[13]:
model = copy.deepcopy(base_model)
L2_norm = Normalizer(tau=1)
L2_norm.apply_on(model)
models['L2 normalized'] = model
# In[14]:
print_accuracy(model, dataloaders, new_labelList, device = device)
# In[15]:
plot_per_class_accuracy(models, dataloaders, labelnames, img_num_per_cls, nClasses= nClasses, device = device)
# In[16]:
plot_norms(model, labelnames)
# ## Apply $\tau$-normalization
# In[17]:
model = copy.deepcopy(base_model)
tau_norm = Normalizer(tau=1.9)
tau_norm.apply_on(model)
models['tau normalized'] = model
# In[18]:
print_accuracy(model, dataloaders, new_labelList, device = device)
# In[19]:
cls_to_compare = {x: models[x] for x in ['base', 'tau normalized']} #dictionary of models we want to compare
plot_per_class_accuracy(cls_to_compare, dataloaders, labelnames, img_num_per_cls, nClasses= nClasses, device = device)
# In[20]:
plot_norms(models['tau normalized'], labelnames)
# ## Finetune with MaxNorm and Weight Decay
# In[30]:
model = copy.deepcopy(base_model)
model_name = 'MaxNorm_WD'
thresh = 0.1 #threshold value
pgdFunc = MaxNorm_via_PGD(thresh=thresh)
pgdFunc.setPerLayerThresh(model) # set per-layer thresholds
# In[31]:
active_layers = [model.encoder.fc.weight, model.encoder.fc.bias]
for param in model.parameters(): #freez all model paramters except the classifier layer
param.requires_grad = False
for param in active_layers:
param.requires_grad = True
# In[32]:
base_lr = 0.01
total_epoch_num = 10
weight_decay = 0.1 #weight decay value
def CB_lossFunc(logits, labelList): #defince CB loss function
return CB_loss(labelList, logits, img_num_per_cls, nClasses, "softmax", 0.9999, 2.0, device)
# In[33]:
optimizer = optim.SGD([{'params': active_layers, 'lr': base_lr}], lr=base_lr, momentum=0.9, weight_decay=weight_decay)
scheduler = lr_scheduler.CosineAnnealingLR(optimizer, total_epoch_num, eta_min=0.0)
save_dir = path.join(curr_working_dir, 'exp', project_name)
# In[34]:
trackRecords = train_model(dataloaders, model, CB_lossFunc, optimizer, scheduler, pgdFunc = pgdFunc,
num_epochs=total_epoch_num, device = device, work_dir='./exp/'+project_name,
model_name= model_name)
# In[35]:
# load model with best epoch accuracy
path_to_clsnet = os.path.join(save_dir, model_name+'_best.paramOnly')
model.load_state_dict(torch.load(path_to_clsnet, map_location=device));
model.to(device);
model.eval();
models['MaxNorm WD'] = model
# In[36]:
print_accuracy(model, dataloaders, new_labelList, device = device)
# In[37]:
models_to_visualize = {x: models[x] for x in ['base', 'MaxNorm WD']}
plot_per_class_accuracy(models_to_visualize, dataloaders, labelnames, img_num_per_cls, nClasses= nClasses, device = device)
# In[38]:
plot_norms(models['MaxNorm WD'], labelnames)