-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·235 lines (183 loc) · 8.68 KB
/
Copy pathtest.py
File metadata and controls
executable file
·235 lines (183 loc) · 8.68 KB
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
import os
import argparse
import pickle
import pprint
import torch
import torch.nn as nn
import torch.optim as optim
import model
import util
from dataloader import data_loader
dev = 'cuda' if torch.cuda.is_available() else 'cpu'
''' ######################## < Step 1 > Parsing test arguments ######################## '''
parser = argparse.ArgumentParser()
# Config - Path
parser.add_argument('--dataset_root', type=str, default='/home/nas_datasets/sanghyeon_data/STL-10/',
help='Root directory of test dataset.')
parser.add_argument('--encoder_output_root', type=str, default='output',
help='Root directory of training results.')
parser.add_argument('--encoder_dataset_name', type=str, default='IMAGENET-64',
help='Name of dataset that was used to train an encoder.')
parser.add_argument('--encoder_exp_version', type=str, default='v1',
help='Version of experiment.')
# Config - Hyperparameter
parser.add_argument('--trn_batch_size', type=int, default=256,
help='Batch size to train a linear classifier.')
parser.add_argument('--tst_batch_size', type=int, default=100,
help='Batch size to evaluate a linear classifier.')
parser.add_argument('--lr', type=float, default=10,
help='Learning rate to train a linear classifier.')
parser.add_argument('--SGD_momentum', type=float, default=0.9,
help='Momentum of SGD optimizer to train a linear classifier.')
parser.add_argument('--weight_decay', type=float, default=0,
help='Weight of L2 regularization of SGD optimizer.')
# Config - Architecture
parser.add_argument('--out_dim', type=int, default=128,
help='Output dimension of a last fully connected layer in encoder.')
parser.add_argument('--in_dim', type=int, default=2048,
help='Intput dimension of a last fully connected layer in encoder or a linear classifier.')
# Config - Setting
parser.add_argument('--load_pretrained_epoch', type=int, default=100,
help='Epoch to load the weight of pretrained encoder.')
parser.add_argument('--cls_num', type=int, default=10,
help='Number of test dataset classes.')
parser.add_argument('--resize', type=int, default=84,
help='Image is resized to this value.')
parser.add_argument('--crop', type=int, default=64,
help='Image is cropped to this value. This is the final size of image transformation.')
parser.add_argument('--max_epoch', type=int, default=100,
help='Maximum epoch to train an encoder.')
parser.add_argument('--eval_epoch', type=int, default=2,
help='Frequency of evaluate an encoder.')
parser.add_argument('--save_weight_epoch', type=int, default=10,
help='Frequency of saving weight.')
parser.add_argument('--num_workers', type=int, default=16,
help='Number of workers for data loader.')
config = parser.parse_args()
# Show config
print('\n======================= Training configuration =======================\n')
pprint.pprint(vars(config))
print('\n======================================================================\n')
# Make output directories
loss_path = os.path.join(config.encoder_output_root, config.encoder_dataset_name, config.encoder_exp_version, 'eval/loss')
accr_path = os.path.join(config.encoder_output_root, config.encoder_dataset_name, config.encoder_exp_version, 'eval/accr')
weight_path = os.path.join(config.encoder_output_root, config.encoder_dataset_name, config.encoder_exp_version, 'eval/weight')
if not os.path.exists(loss_path):
os.makedirs(loss_path)
if not os.path.exists(weight_path):
os.makedirs(weight_path)
if not os.path.exists(accr_path):
os.makedirs(accr_path)
''' ######################## < Step 2 > Create instances ######################## '''
# Build dataloader
print('[1 / 2]. Build data loader.. \n')
trn_dloader, trn_dlen = data_loader(dataset_root=config.dataset_root,
resize=config.resize,
crop=config.crop,
batch_size=config.trn_batch_size,
num_workers=config.num_workers,
type='classifier_train')
tst_dloader, tst_dlen = data_loader(dataset_root=config.dataset_root,
resize=config.resize,
crop=config.crop,
batch_size=config.tst_batch_size,
num_workers=config.num_workers,
type='classifier_test')
# Build models
print('[2 / 2]. Build models.. \n')
encoder = nn.DataParallel(model.Resnet50(dim=config.out_dim)).to(dev)
ckpt_name = 'ckpt_' + str(config.load_pretrained_epoch) + '.pkl'
ckpt_path = os.path.join(config.encoder_output_root, config.encoder_dataset_name, config.encoder_exp_version, 'weight', ckpt_name)
ckpt = torch.load(ckpt_path)
encoder.load_state_dict(ckpt['encoder'])
feature_extractor = nn.Sequential(* list(encoder.module.resnet.children())[:-1]) # feature extractor from encoder
linear = nn.Linear(config.in_dim, config.cls_num).to(dev) # linear classifier
# Freeze encoder
for param in feature_extractor.parameters():
param.requires_grad = False
# Optimizer
optim_linear = optim.SGD(linear.parameters(),
lr=config.lr,
momentum=config.SGD_momentum,
weight_decay=config.weight_decay)
# Loss function
cross_entropy = nn.CrossEntropyLoss()
# Status
loss_hist = []
accr_hist = []
''' ######################## < Step 3 > Define methods ######################## '''
def get_accuracy():
'''
Calculate classification accuracy
'''
total_num = 0
correct_num = 0
print('\n Calculate accuracy ...')
with torch.no_grad():
for idx, (img, label) in enumerate(tst_dloader):
if idx % 50 == 0:
print(' [%d / %d] ... ' % (idx, int(tst_dlen / config.tst_batch_size)))
img = img.to(dev)
label = label.to(dev)
feature = feature_extractor(img)
score = linear(feature.view(feature.size(0), feature.size(1)))
pred = torch.argmax(score, dim=1, keepdim=True)
total_num = total_num + img.size(0)
correct_num = correct_num + (label.unsqueeze(dim=1) == pred).sum().item()
print()
return correct_num / total_num
def update_lr(epoch):
'''
Learning rate scheduling.
Args:
epoch (float): Set new learning rate by a given epoch.
'''
# Decay 0.1 times every 20 epoch.
factor = int(epoch / 20)
lr = config.lr * (0.2**factor)
for param_group in optim_linear.param_groups:
print('LR is updated to %f ...' % lr)
param_group['lr'] = lr
''' ######################## < Step 4 > Start training ######################## '''
epoch = 0
total_iters = 0
# Train a linear classifier
while(epoch < config.max_epoch):
for i, (img, label) in enumerate(trn_dloader):
# Preprocess
linear.train()
optim_linear.zero_grad()
# Forward prop
img = img.to(dev)
label = label.to(dev)
feature = feature_extractor(img).detach()
score = linear(feature.view(feature.size(0), feature.size(1)))
loss = cross_entropy(score, label)
# Back prop
loss.backward()
optim_linear.step()
# Print training status and save log
total_iters += 1
print('[Epoch : %d / Total iters : %d] : loss : %f ...' %(epoch, total_iters, loss.item()))
epoch += 1
# Update learning rate
update_lr(epoch)
# Save loss value
loss_hist.append(loss.item())
# Calculate the current accuracy and plot the graphs
if (epoch - 1) % config.eval_epoch == 0:
linear.eval()
accr = get_accuracy()
accr_hist.append(accr)
print('[Epoch : %d / Total iters : %d] : accr : %f ...' %(epoch, total_iters, accr))
util.cls_loss_plot(loss_hist, loss_path, record_epoch=1)
util.accr_plot(accr_hist, accr_path, record_epoch=config.eval_epoch)
# Save
if (epoch - 1) % config.save_weight_epoch == 0:
path_ckpt = os.path.join(weight_path, 'ckpt_' + str(epoch-1) + '.pkl')
ckpt = linear.state_dict()
torch.save(ckpt, path_ckpt)
with open(os.path.join(loss_path, 'loss.pkl'), 'wb') as f:
pickle.dump(loss_hist, f)
with open(os.path.join(accr_path, 'accr.pkl'), 'wb') as f:
pickle.dump(accr_hist, f)