-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzs_train.py
More file actions
191 lines (156 loc) · 6.38 KB
/
zs_train.py
File metadata and controls
191 lines (156 loc) · 6.38 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
import os
import sys
import torch
import torch.optim as optim
from torch import nn
from config import cfg
from models import default_model_path, init_models_faulty, init_models
__all__ = ["training"]
debug = False
torch.manual_seed(0)
class WarmUpLR(optim.lr_scheduler._LRScheduler):
"""warmup_training learning rate scheduler
Args:
optimizer: optimzier(e.g. SGD)
total_iters: totoal_iters of warmup phase
"""
def __init__(self, optimizer, total_iters, last_epoch=-1):
self.total_iters = total_iters
super().__init__(optimizer, last_epoch)
def get_lr(self):
"""we will use the first m batches, and set the learning
rate to base_lr * m / total_iters
"""
return [base_lr * self.last_epoch / (self.total_iters + 1e-8) for base_lr in self.base_lrs]
def training(
trainloader,
arch,
dataset,
in_channels,
precision,
retrain,
checkpoint_path,
force,
device,
fl,
ber,
pos,
):
"""
Apply quantization aware training.
:param trainloader: The loader of training data.
:param arch: A string. The architecture of the model would be used.
:param dataset: A string. The name of the training data.
:param in_channels: An int. The input channels of the training data.
:param precision: An int. The number of bits would be used to quantize
the model.
:param retrain: A boolean. Start from checkpoint.
:param checkpoint_path: A string. The path that stores the models.
:param force: Overwrite checkpoint.
:param device: A string. Specify using GPU or CPU.
"""
model, checkpoint_epoch = init_models(arch, 3, precision, retrain, checkpoint_path, dataset) # Quantization Aware Training without using bit error!
print("Training with Learning rate %.4f" % (cfg.learning_rate))
if dataset == 'cifar100':
print('cifar100')
opt = optim.SGD(model.parameters(), lr=cfg.learning_rate, momentum=0.9)
#iter_per_epoch = len(trainloader)
#warmup_scheduler = WarmUpLR(opt, iter_per_epoch * 1) # warmup = 1
#train_scheduler = optim.lr_scheduler.MultiStepLR(opt, milestones=[60, 120, 160], gamma=0.2)
else:
opt = optim.SGD(model.parameters(), lr=cfg.learning_rate, momentum=0.9)
model = model.to(device)
from torchsummary import summary
if dataset == 'imagenet128':
print('imagenet128')
summary(model, (3, 128, 128))
elif dataset == 'imagenet224':
print('imagenet224')
summary(model, (3, 224, 224))
else:
summary(model, (3, 32, 32))
# model = torch.nn.DataParallel(model)
torch.backends.cudnn.benchmark = True
for x in range(checkpoint_epoch + 1, cfg.epochs):
print("Epoch: %03d" % x)
running_loss = 0.0
running_correct = 0
for batch_id, (inputs, outputs) in enumerate(trainloader):
inputs = inputs.to(device)
outputs = outputs.to(device)
opt.zero_grad()
# Store original model parameters before
# quantization/perturbation, detached from graph
if precision > 0:
list_init_params = []
with torch.no_grad():
for init_params in model.parameters():
list_init_params.append(init_params.clone().detach())
if debug:
if batch_id % 100 == 0:
print("initial params")
print(model.fc2.weight[0:3, 0:3])
print(model.conv1.weight[0, 0, :, :])
model.train()
model_outputs = model(inputs) # pylint: disable=E1102
_, preds = torch.max(model_outputs, 1)
outputs = outputs.view(
outputs.size(0)
) # changing the size from (batch_size,1) to batch_size.
if precision > 0:
if debug:
if batch_id % 100 == 0:
print("quantized params")
print(model.fc2.weight[0:3, 0:3])
print(model.conv1.weight[0, 0, :, :])
loss = nn.CrossEntropyLoss()(model_outputs, outputs)
# Compute gradient of perturbed weights with perturbed loss
loss.backward()
# restore model weights with unquantized value
# This step is not important because list_init_params == model.parameters()
# Therefore, apply gradients on model.parameters() directly is OK.
if precision > 0:
with torch.no_grad():
for i, restored_params in enumerate(model.parameters()):
restored_params.copy_(list_init_params[i])
if debug:
if batch_id % 100 == 0:
print("restored params")
print(model.fc2.weight[0:3, 0:3])
print(model.conv1.weight[0, 0, :, :])
# update restored weights with gradient
opt.step()
#if dataset == 'cifar100':
# if x <= 1: # warmup = 1
# warmup_scheduler.step()
# else:
# train_scheduler.step()
# lr_scheduler.step()
running_loss += loss.item()
running_correct += torch.sum(preds == outputs.data)
accuracy = running_correct.double() / (len(trainloader.dataset))
print("For epoch: {}, loss: {:.6f}, accuracy: {:.5f}".format(
x,
running_loss / len(trainloader.dataset),
accuracy
)
)
if (x+1)%10 == 0:
model_path = default_model_path(
cfg.model_dir, arch, dataset, precision, fl, ber, pos, x+1
)
if not os.path.exists(os.path.dirname(model_path)):
os.makedirs(os.path.dirname(model_path))
if os.path.exists(model_path) and not force:
print("Checkpoint already present ('%s')" % model_path)
sys.exit(1)
torch.save(
{
"epoch": x,
"model_state_dict": model.state_dict(),
"optimizer_state_dict": opt.state_dict(),
"loss": running_loss / batch_id,
"accuracy": accuracy,
},
model_path,
)