-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain_pretrain.py
166 lines (134 loc) · 5.89 KB
/
main_pretrain.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
'''Train CIFAR10/CIFAR100 with PyTorch.'''
from __future__ import print_function
import os
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
from tqdm import tqdm
from tensorboardX import SummaryWriter
from utils.network_utils import get_network
from utils.data_utils import get_dataloader
from utils.common_utils import PresetLRScheduler, makedirs
# fetch args
parser = argparse.ArgumentParser()
parser.add_argument('--learning_rate', default=0.1, type=float)
parser.add_argument('--weight_decay', default=3e-3, type=float)
parser.add_argument('--batch_size', default=128, type=float)
parser.add_argument('--network', default='vgg', type=str)
parser.add_argument('--depth', default=19, type=int)
parser.add_argument('--dataset', default='cifar10', type=str)
parser.add_argument('--epoch', default=150, type=int)
parser.add_argument('--decay_every', default=60, type=int)
parser.add_argument('--decay_ratio', default=0.1, type=float)
parser.add_argument('--device', default='cuda', type=str)
parser.add_argument('--resume', '-r', action='store_true')
parser.add_argument('--load_path', default='', type=str)
parser.add_argument('--log_dir', default='runs/pretrain', type=str)
args = parser.parse_args()
# init model
net = get_network(network=args.network,
depth=args.depth,
dataset=args.dataset)
net = net.to(args.device)
# init dataloader
trainloader, testloader = get_dataloader(dataset=args.dataset,
train_batch_size=args.batch_size,
test_batch_size=256)
# init optimizer and lr scheduler
optimizer = optim.SGD(net.parameters(), lr=args.learning_rate, momentum=0.9, weight_decay=args.weight_decay)
lr_schedule = {0: args.learning_rate,
int(args.epoch*0.5): args.learning_rate*0.1,
int(args.epoch*0.75): args.learning_rate*0.01}
lr_scheduler = PresetLRScheduler(lr_schedule)
# lr_scheduler = #StairCaseLRScheduler(0, args.decay_every, args.decay_ratio)
# init criterion
criterion = nn.CrossEntropyLoss()
start_epoch = 0
best_acc = 0
if args.resume:
print('==> Resuming from checkpoint..')
assert os.path.isdir('checkpoint/pretrain'), 'Error: no checkpoint directory found!'
checkpoint = torch.load('checkpoint/pretrain/%s_%s%s_bn_best.t7' % (args.dataset, args.network, args.depth))
net.load_state_dict(checkpoint['net'])
best_acc = checkpoint['acc']
start_epoch = checkpoint['epoch']
print('==> Loaded checkpoint at epoch: %d, acc: %.2f%%' % (start_epoch, best_acc))
# init summary writter
log_dir = os.path.join(args.log_dir, '%s_%s%s' % (args.dataset,
args.network,
args.depth))
makedirs(log_dir)
writer = SummaryWriter(log_dir)
def train(epoch):
print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
correct = 0
total = 0
lr_scheduler(optimizer, epoch)
desc = ('[LR=%s] Loss: %.3f | Acc: %.3f%% (%d/%d)' %
(lr_scheduler.get_lr(optimizer), 0, 0, correct, total))
writer.add_scalar('train/lr', lr_scheduler.get_lr(optimizer), epoch)
prog_bar = tqdm(enumerate(trainloader), total=len(trainloader), desc=desc, leave=True)
for batch_idx, (inputs, targets) in prog_bar:
inputs, targets = inputs.to(args.device), targets.to(args.device)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
desc = ('[LR=%s] Loss: %.3f | Acc: %.3f%% (%d/%d)' %
(lr_scheduler.get_lr(optimizer), train_loss / (batch_idx + 1), 100. * correct / total, correct, total))
prog_bar.set_description(desc, refresh=True)
writer.add_scalar('train/loss', train_loss/(batch_idx + 1), epoch)
writer.add_scalar('train/acc', 100. * correct / total, epoch)
def test(epoch):
global best_acc
net.eval()
test_loss = 0
correct = 0
total = 0
desc = ('[LR=%s] Loss: %.3f | Acc: %.3f%% (%d/%d)'
% (lr_scheduler.get_lr(optimizer), test_loss/(0+1), 0, correct, total))
prog_bar = tqdm(enumerate(testloader), total=len(testloader), desc=desc, leave=True)
with torch.no_grad():
for batch_idx, (inputs, targets) in prog_bar:
inputs, targets = inputs.to(args.device), targets.to(args.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()
desc = ('[LR=%s] Loss: %.3f | Acc: %.3f%% (%d/%d)'
% (lr_scheduler.get_lr(optimizer), test_loss / (batch_idx + 1), 100. * correct / total, correct, total))
prog_bar.set_description(desc, refresh=True)
# Save checkpoint.
acc = 100.*correct/total
writer.add_scalar('test/loss', test_loss / (batch_idx + 1), epoch)
writer.add_scalar('test/acc', 100. * correct / total, epoch)
if acc > best_acc:
print('Saving..')
state = {
'net': net.state_dict(),
'acc': acc,
'epoch': epoch,
'loss': loss,
'args': args
}
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
if not os.path.isdir('checkpoint/pretrain'):
os.mkdir('checkpoint/pretrain')
torch.save(state, './checkpoint/pretrain/%s_%s%s_best.t7' % (args.dataset,
args.network,
args.depth))
best_acc = acc
for epoch in range(start_epoch, args.epoch):
train(epoch)
test(epoch)