-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_snn.py
260 lines (214 loc) · 9.89 KB
/
train_snn.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import torch
import args_config
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import os, time
import torch.backends.cudnn as cudnn
import dill
import pickle
from quant_net import *
from quant_resnet import *
from training_utils import *
import tracemalloc
import math
import gc
def main():
torch.manual_seed(23)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
cudnn.benchmark = True
cudnn.deterministic = True
args = args_config.get_args()
print("********** SNN simulation parameters **********")
print(args)
if args.dataset == 'cifar10':
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
train_dataset = torchvision.datasets.CIFAR10(
root=args.dataset_dir,
train=True,
transform=transform_train,
download=True)
test_dataset = torchvision.datasets.CIFAR10(
root=args.dataset_dir,
train=False,
transform=transform_test,
download=True)
train_data_loader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=args.batch_size,
shuffle=True,
drop_last=True,
num_workers=4,
pin_memory=True)
test_data_loader = torch.utils.data.DataLoader(
dataset=test_dataset,
batch_size=args.batch_size,
shuffle=False,
drop_last=False,
num_workers=4,
pin_memory=True)
num_classes = 10
elif args.dataset == 'svhn':
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train_dataset = torchvision.datasets.SVHN(
root=args.dataset_dir,
split='train',
transform=transform_train,
download=True)
test_dataset = torchvision.datasets.SVHN(
root=args.dataset_dir,
split='test',
transform=transform_test,
download=True)
train_data_loader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=args.batch_size,
shuffle=True,
drop_last=True,
num_workers=0,
pin_memory=True)
test_data_loader = torch.utils.data.DataLoader(
dataset=test_dataset,
batch_size=args.batch_size,
shuffle=False,
drop_last=False,
num_workers=0,
pin_memory=True)
num_classes = 10
elif args.dataset == 'tiny':
traindir = os.path.join('/gpfs/gibbs/project/panda/shared/tiny-imagenet-200/train')
valdir = os.path.join('/gpfs/gibbs/project/panda/shared/tiny-imagenet-200/val')
# normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
# std=[0.229, 0.224, 0.225])
train_transforms = transforms.Compose([
transforms.RandomRotation(20),
transforms.RandomHorizontalFlip(0.5),
transforms.ToTensor(),
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]),
])
test_transforms = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]),
])
train_dataset = torchvision.datasets.ImageFolder(traindir, train_transforms)
test_dataset = torchvision.datasets.ImageFolder(valdir, test_transforms)
train_data_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=4,pin_memory=True)
test_data_loader = torch.utils.data.DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False, num_workers=4,pin_memory=True)
num_classes = 200
elif args.dataset == 'dvs':
train_dataset_dvs=torch.load("./train_dataset_dvs_8.pt",pickle_module=dill)
test_dataset_dvs=torch.load("./test_dataset_dvs_8.pt",pickle_module=dill)
train_data_loader = torch.utils.data.DataLoader(train_dataset_dvs,
batch_size=args.batch_size,
shuffle=True,
num_workers=4,
pin_memory=True)
test_data_loader = torch.utils.data.DataLoader(test_dataset_dvs,
batch_size=args.batch_size,
shuffle=False,
num_workers=4,
pin_memory=True)
num_classes = 10
# print(type(train_dataset_dvs))
# print(len(train_dataset_dvs))
# print(train_dataset_dvs[0])
# print(type(test_dataset_dvs))
# print(len(test_dataset_dvs))
# print(test_dataset_dvs[0])
# exit()
# # check the test and train sets
# train_indices = set(train_dataset_dvs.indices)
# test_indices = set(test_dataset_dvs.indices)
# # The intersection should be an empty set, if they have no common elements
# common_indices = train_indices.intersection(test_indices)
# print(f"Common indices between train and test datasets: {common_indices}")
# exit()
criterion = nn.CrossEntropyLoss()
if args.arch == 'vgg16':
model = Q_ShareScale_VGG16(args.T,args.dataset).cuda()
elif args.arch == 'vgg9':
model = Q_ShareScale_VGG9(args.T,args.dataset).cuda()
elif args.arch == 'res19':
model = ResNet19(num_classes, args.T).cuda()
# model = VGG19_Direct_TS_UQ(args.T, args.leak_mem, args.th, args.rst, args.uq, args.xq, args.wq, args.xa).cuda()
# else:
# model = VGG9_Direct_Uniform_UQ_List(args.T, args.leak_mem, args.th, args.rst).cuda()
# print(model)
if args.optim == 'sgd':
optimizer = torch.optim.SGD(model.parameters(), args.lr, 0.9, weight_decay=5e-4)
elif args.optim == 'adam':
optimizer = torch.optim.Adam(model.parameters(), args.lr,weight_decay=1e-4)
else:
print ("Current does not support other optimizers other than sgd or adam.")
exit()
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer,T_max= args.epoch, eta_min= 0)
# scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer,T_max=args.epoch)
best_accuracy = 0
# tracemalloc.start()
for epoch_ in range(args.epoch):
# snap1 = tracemalloc.take_snapshot()
# time1 = time.time()
loss = 0
accuracy = 0
loss = train(args, train_data_loader, model, criterion, optimizer, epoch_)
accuracy= test(model, test_data_loader, criterion)
scheduler.step()
# time2 = time.time()
# print("Training time for one epoch: ", time2-time1)
if accuracy > best_accuracy:
best_accuracy = accuracy
# checkdir(f"{os.getcwd()}/model_dumps/{args.arch}/{args.dataset}/{args.rst}/w{model.num_bits_w}/u{model.num_bits_u}/share/T4")
# torch.save(model, f"{os.getcwd()}/model_dumps/{args.arch}/{args.dataset}/{args.rst}/w{model.num_bits_w}/u{model.num_bits_u}/share/T4/final_dict.pth.tar")
# checkdir(f"{os.getcwd()}/model_dumps/{args.arch}/{args.dataset}/{args.rst}/w4u4")
# torch.save(model, f"{os.getcwd()}/model_dumps/{args.arch}/{args.dataset}/{args.rst}/w4u4/final_dict.pth.tar")
checkdir(f"{os.getcwd()}/model_dumps/{args.arch}/{args.dataset}/{args.rst}/T10/baseline")
torch.save(model, f"{os.getcwd()}/model_dumps/{args.arch}/{args.dataset}/{args.rst}/T10/baseline/final_dict.pth.tar")
if (epoch_+1) % args.test_display_freq == 0:
print(f'Train Epoch: {epoch_}/{args.epoch} Loss: {loss:.6f} Accuracy: {accuracy:.3f}% Best Accuracy: {best_accuracy:.3f}%')
# gc.collect()
# snap2 = tracemalloc.take_snapshot()
# top_stats=snap1.compare_to(snap2, "lineno")
# for stat in top_stats[:50]:
# line = str(stat)
# if("muless-int-snn" in line):
# print(line)
def train(args, train_data, model, criterion, optimizer, epoch):
model.train()
for batch_idx, (imgs, targets) in enumerate(train_data):
train_loss = 0.0
optimizer.zero_grad()
imgs, targets = imgs.cuda(), targets.cuda()
output = model(imgs)
train_loss = sum([criterion(s, targets) for s in output]) / args.T
train_loss.backward()
if args.share:
for m in model.modules():
if isinstance(m,QConvBN2dLIF):
# print(m.scaling.grad)
m.beta[0].grad.data = m.beta[0].grad/math.sqrt(torch.numel(m.conv_module.weight)*(2**(m.num_bits_w-1)-1))
elif isinstance(m,QConvBN2d):
m.beta[0].grad.data = m.beta[0].grad/math.sqrt(torch.numel(m.conv_module.weight)*(2**(m.num_bits_w-1)-1))
# for a in model.alpha_list:
# a.grad.data = a.grad/1000
optimizer.step()
return train_loss.item()
if __name__ == '__main__':
main()