-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_train_offline.py
219 lines (193 loc) · 9.35 KB
/
main_train_offline.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
import argparse
import os
import numpy as np
from sklearn.linear_model import LogisticRegression
from models.logistic import MulticlassLogisticRegressionModel
from models.gaussnb import *
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import log_loss
import torch
from torch import nn
from torch.utils.data import TensorDataset, DataLoader
from tqdm import tqdm
from utils.tools import *
from utils.vis import save_vis
parser = argparse.ArgumentParser(description='Offline Linear eval')
parser.add_argument('--dataset', default='cifar10', type=str, choices=['cifar10','cifar100'],
help='dataset the features belong to.')
parser.add_argument('--backbone', default='clip', type=str, choices=['moco_v1', 'moco_v2', 'clip','resnet','vit', 'simclr_v2', 'simclr_v1', 'vit2', 'mae', 'simmim'],
help='pretrained backbone.')
parser.add_argument('--model', default='lr_bgfs', type=str, choices=['lr_bgfs', 'lr_sgd', 'nb_diag'],
help='model.')
parser.add_argument('--C', default=1, type=float,
help='peanlty of l2, lr_bgfs.')
parser.add_argument('--epsilon', default=1e-9, type=float,
help='var smoothing of naive Bayes.')
parser.add_argument('--epochs', default=100, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--bs', default=256, type=int, metavar='N',
help='mini-batch size (default: 256)')
parser.add_argument('--lr', default=30., type=float,
metavar='LR', help='initial learning rate', dest='lr')
parser.add_argument('--schedule', default=[60, 80], nargs='*', type=int,
help='learning rate schedule (when to drop lr by a ratio)')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
parser.add_argument('--wd', default=0., type=float,
metavar='W', help='weight decay (default: 0.)',
dest='wd')
parser.add_argument('--repeat', default=5, type=int, metavar='N',
help='repeat times')
parser.add_argument('--gpu', default=None, type=int,
help='GPU id to use.')
parser.add_argument('--minmax', dest='minmax', action='store_true',
help='scaler to [0,1]')
def main():
args = parser.parse_args()
data_dir = os.path.join('./datasets', args.backbone, args.dataset)
if not args.minmax:
log_dir = os.path.join('./log', 'offline', args.backbone, args.dataset, args.model)
else:
log_dir = os.path.join('./log', 'offline', args.backbone + '_minmax', args.dataset, args.model)
if args.model == 'lr_bgfs':
log_dir = os.path.join(log_dir, 'C' + str(args.C))
elif args.model == 'lr_sgd':
log_dir = os.path.join(log_dir, 'lr' + str(args.lr) + '_wd' + str(args.wd))
if not os.path.exists(log_dir):
os.makedirs(log_dir, exist_ok=True)
loss_path = os.path.join(log_dir, 'loss.npy')
pic_path = os.path.join(log_dir, 'vis.png')
logger = get_console_file_logger(name='offline, %s, %s, %s' % (args.backbone, args.dataset, args.model), logdir=log_dir)
logger.info(args._get_kwargs())
if args.dataset == 'cifar10':
K = 10
m_step = [20,50,100,200,500,1000,2000,5000,10000,20000,30000,50000]
else:
K = 100
m_step = [3*K,5*K,10*K,20*K,50*K,100*K,200*K,500*K]
train_set_path = os.path.join(data_dir, 'train_features.npy')
test_set_path = os.path.join(data_dir, 'val_features.npy')
train_set = np.load(train_set_path)
test_set = np.load(test_set_path)
X_train, y_train = train_set[:,0:-1], train_set[:,-1]
X_test, y_test = test_set[:,0:-1], test_set[:,-1]
if args.model == 'lr_sgd':
loss_func = nn.CrossEntropyLoss()
args.device = torch.device('cuda', args.gpu) if args.gpu is not None else 'cuda'
X_test, y_test = torch.from_numpy(X_test).float(), torch.from_numpy(y_test).long()
test_set = TensorDataset(X_test, y_test)
test_loader = DataLoader(test_set, batch_size=args.bs, shuffle=False)
errors = np.zeros((args.repeat, len(m_step)))
for m_idx, m in enumerate(m_step):
if args.model == 'lr_sgd':
errors = train_fix_m_sgd(X_train, y_train, test_loader, loss_func, m_idx, m, K, errors, logger, args)
else:
errors = train_fix_m_no_sgd(X_train, y_train, X_test, y_test, m_idx, m, K, errors, logger, args)
logger.info('m = '+ str(m))
logger.info(errors)
np.save(loss_path, errors)
save_vis(m_step, errors, pic_path, args)
def get_model(args):
if args.model == 'lr_bgfs':
model = LogisticRegression(penalty='l2', C=args.C, solver='lbfgs', max_iter=1000)
elif args.model == 'lr_sgd':
if args.backbone in ['moco_v1', 'moco_v2', 'simclr_v1', 'simclr_v2', 'resnet']:
if args.dataset == 'cifar10':
model = MulticlassLogisticRegressionModel(features=2048, K=10)
elif args.dataset == 'cifar100':
model = MulticlassLogisticRegressionModel(features=2048, K=100)
elif args.backbone == 'clip' and args.dataset == 'cifar10':
model = MulticlassLogisticRegressionModel(features=1024, K=10)
elif args.backbone == 'clip' and args.dataset == 'cifar100':
model = MulticlassLogisticRegressionModel(features=1024, K=100)
elif args.backbone in ['vit', 'vit2', 'mae', 'simmim']:
if args.dataset == 'cifar10':
model = MulticlassLogisticRegressionModel(features=768, K=10)
elif args.dataset == 'cifar100':
model = MulticlassLogisticRegressionModel(features=768, K=100)
elif args.model == 'nb_diag':
model = GaussianNB(val_epsilon=args.epsilon)
else:
print('fault')
return model
def train_fix_m_no_sgd(X_train, y_train, X_test, y_test, m_idx, m, K, errors, logger, args):
i = 0
flag = False
while flag == False:
for _ in tqdm(range(10)):
if m < 50000:
X_train_m, _, y_train_m, _ = train_test_split(X_train, y_train, train_size=m)
else:
X_train_m, y_train_m = X_train, y_train
if len(set(list(y_train_m))) < K:
continue
if args.minmax:
scaler = MinMaxScaler()
X_train_m = scaler.fit_transform(X_train_m)
X_test_temp = scaler.transform(X_test)
i += 1
model = get_model(args)
model.fit(X_train_m, y_train_m)
errors[i-1, m_idx] = (1 - model.score(X_test_temp, y_test))
if i > args.repeat - 1:
flag = True
break
return errors
def train_fix_m_sgd(X_train, y_train, test_loader, loss_func, m_idx, m, K, errors, logger, args):
i = 0
flag = False
while flag == False:
for _ in tqdm(range(10)):
if m < 50000:
X_train_m, _, y_train_m, _ = train_test_split(X_train, y_train, train_size=m)
else:
X_train_m, y_train_m = X_train, y_train
if len(set(list(y_train_m))) < K:
continue
i += 1
X_train_m, y_train_m = torch.from_numpy(X_train_m).float(), torch.from_numpy(y_train_m).long()
train_set = TensorDataset(X_train_m, y_train_m)
train_loader = DataLoader(train_set, batch_size=args.bs, shuffle=True)
best_error_test = 1
early_stop = 0
model = get_model(args).to(args.device)
model.apply(model.init_weights)
optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.wd)
# lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1,gamma=0.9)
for epoch in tqdm(range(args.epochs)):
model.train()
adjust_learning_rate(optimizer, epoch, args)
for x, label in train_loader:
x = x.to(args.device)
label = label.to(args.device)
pred = model(x).squeeze()
loss = loss_func(pred, label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 5 == 0:
correct = 0
model.eval()
for x, label in test_loader:
x = x.to(args.device)
label = label.to(args.device)
pred = model(x).argmax(axis=1).squeeze()
correct += (pred == label).sum().item()
error_test = 1 - correct / 10000
logger.info('epoch = %d, test_error = %.6f' % (epoch+1, error_test))
if error_test < best_error_test:
early_stop = 0
best_error_test = error_test
else:
early_stop += 1
if early_stop > 100:
break
# lr_scheduler.step()
errors[i-1, m_idx] = best_error_test
if i > args.repeat - 1:
flag = True
break
return errors
if __name__ == '__main__':
main()