-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain1.py
70 lines (54 loc) · 1.9 KB
/
train1.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
import torch
import torch.backends.cudnn as cudnn
from reactnet1 import ReActNet
from utils import *
# the same parameter setting in article
EPOCH = 100
LR = 5e-4
MOMENTUM = 0.9
WEIGHT_DECAY = 1e-5
save_path = './reactnet1.pth'
def main():
if not torch.cuda.is_available():
exit(0)
cudnn.benchmark = True
cudnn.enabled = True
train_loader, test_loader = dataset()
model = ReActNet()
model = torch.nn.DataParallel(model).cuda()
bnbias = []
weight = []
for name, param in model.named_parameters():
if len(param.shape) == 1 or 'bias' in name:
bnbias.append(param)
else:
weight.append(param)
'''
print('Load Previous Model')
model.load_state_dict(torch.load('./binary_best.pth'))
'''
criterion = torch.nn.CrossEntropyLoss()
criterion = criterion.cuda()
optimizer = torch.optim.Adam(
[{'params' : bnbias},
{'params' : weight, 'weight_decay' : WEIGHT_DECAY}],
lr = LR)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max = EPOCH)
#scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lambda step : (1.0 - step / EPOCH))
'''
optimizer = torch.optim.SGD(model.parameters(), lr = LR, momentum = MOMENTUM,
weight_decay = WEIGHT_DECAY, nesterov = True)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max = EPOCH)
'''
best_accuracy = 0
print('Start Training 100 EPOCHs')
for epoch in range(EPOCH):
train(model, train_loader, criterion, optimizer, epoch)
accuracy = validate(model, test_loader, epoch)
if best_accuracy < accuracy:
best_accuracy = accuracy
torch.save(model.state_dict(), save_path)
scheduler.step()
print('Best Prec@1 %.3f' % (best_accuracy))
if __name__ == '__main__':
main()