-
Notifications
You must be signed in to change notification settings - Fork 118
/
training.py
executable file
·164 lines (142 loc) · 5.35 KB
/
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
#*
# @file Different utility functions
# Copyright (c) Zhewei Yao, Amir Gholami
# All rights reserved.
# This file is part of PyHessian library.
#
# PyHessian is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyHessian is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyHessian. If not, see <http://www.gnu.org/licenses/>.
#*
from __future__ import print_function
import logging
import os
import sys
import numpy as np
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable
from utils import *
from models.resnet import resnet
from tqdm import tqdm, trange
# Training settings
parser = argparse.ArgumentParser(description='Training on Cifar10')
parser.add_argument('--batch-size',
type=int,
default=128,
metavar='N',
help='input batch size for training (default: 128)')
parser.add_argument('--test-batch-size',
type=int,
default=256,
metavar='N',
help='input batch size for testing (default: 256)')
parser.add_argument('--epochs',
type=int,
default=180,
metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--lr',
type=float,
default=0.1,
metavar='LR',
help='learning rate (default: 0.01)')
parser.add_argument('--lr-decay',
type=float,
default=0.1,
help='learning rate ratio')
parser.add_argument('--lr-decay-epoch',
type=int,
nargs='+',
default=[80, 120],
help='Decrease learning rate at these epochs.')
parser.add_argument('--seed',
type=int,
default=1,
metavar='S',
help='random seed (default: 1)')
parser.add_argument('--weight-decay',
default=5e-4,
type=float,
metavar='W',
help='weight decay (default: 1e-4)')
parser.add_argument('--batch-norm',
action='store_false',
help='do we need batch norm or not')
parser.add_argument('--residual',
action='store_false',
help='do we need residula connect or not')
parser.add_argument('--cuda',
action='store_false',
help='do we use gpu or not')
parser.add_argument('--saving-folder',
type=str,
default='checkpoints/',
help='choose saving name')
args = parser.parse_args()
# set random seed to reproduce the work
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
for arg in vars(args):
print(arg, getattr(args, arg))
# get dataset
train_loader, test_loader = getData(name='cifar10',
train_bs=args.batch_size,
test_bs=args.test_batch_size)
# get model and optimizer
model = resnet(num_classes=10,
depth=20,
residual_not=args.residual,
batch_norm_not=args.batch_norm)
if args.cuda:
model = model.cuda()
model = torch.nn.DataParallel(model)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(),
lr=args.lr,
momentum=0.9,
weight_decay=args.weight_decay)
lr_scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
args.lr_decay_epoch,
gamma=args.lr_decay)
if not os.path.isdir(args.saving_folder):
os.makedirs(args.saving_folder)
for epoch in range(1, args.epochs + 1):
print('Current Epoch: ', epoch)
train_loss = 0.
total_num = 0
correct = 0
with tqdm(total=len(train_loader.dataset)) as progressbar:
for batch_idx, (data, target) in enumerate(train_loader):
model.train()
if args.cuda:
data, target = data.cuda(), target.cuda()
output = model(data)
loss = criterion(output, target)
loss.backward()
train_loss += loss.item() * target.size()[0]
total_num += target.size()[0]
_, predicted = output.max(1)
correct += predicted.eq(target).sum().item()
optimizer.step()
optimizer.zero_grad()
progressbar.set_postfix(loss=train_loss / total_num,
acc=100. * correct / total_num)
progressbar.update(target.size(0))
acc = test(model, test_loader)
lr_scheduler.step()
torch.save(model.state_dict(), args.saving_folder + 'net.pkl')