-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathget_rate_imagenet.py
200 lines (169 loc) · 7.77 KB
/
get_rate_imagenet.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
import datetime
import os
import time
import torch
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
import sys
from torch.cuda import amp
from models import spiking_resnet_imagenet
from modules import neuron, surrogate
import argparse
import math
from utils import Bar, Logger, AverageMeter, accuracy, mkdir_p, savefig
import torch.utils.data as data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
_seed_ = 2022
import random
random.seed(2022)
torch.manual_seed(_seed_) # use torch.manual_seed() to seed the RNG for all devices (both CPU and CUDA)
torch.cuda.manual_seed_all(_seed_)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
import numpy as np
np.random.seed(_seed_)
def main():
parser = argparse.ArgumentParser(description='Classify ImageNet')
parser.add_argument('-T', default=6, type=int, help='simulating time-steps')
parser.add_argument('-tau', default=2., type=float)
parser.add_argument('-b', default=256, type=int, help='batch size')
parser.add_argument('-j', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument('-data_dir', type=str, default=None)
parser.add_argument('-resume', type=str, help='resume from the checkpoint path')
parser.add_argument('-model', type=str, default='online_spiking_nfresnet34')
parser.add_argument('-drop_rate', type=float, default=0.0)
parser.add_argument('-stochdepth_rate', type=float, default=0.0)
parser.add_argument('-weight_decay', type=float, default=2e-5)
parser.add_argument('-cnf', type=str)
parser.add_argument('-dts_cache', type=str, default='./dts_cache')
parser.add_argument('-loss_lambda', type=float, default=0.0)
parser.add_argument('-gpu-id', default='0', type=str, help='gpu id')
args = parser.parse_args()
#print(args)
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
num_classes = 1000
traindir = os.path.join(args.data_dir, 'train')
valdir = os.path.join(args.data_dir, 'val')
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_data_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(traindir, transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])),
batch_size=args.b, shuffle=True,
num_workers=args.j, pin_memory=True)
test_data_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(valdir, transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
])),
batch_size=args.b, shuffle=False,
num_workers=args.j, pin_memory=True)
net = spiking_resnet_imagenet.__dict__[args.model](single_step_neuron=neuron.OnlineLIFNode, tau=args.tau, surrogate_function=surrogate.Sigmoid(), track_rate=True, c_in=3, num_classes=num_classes, drop_rate=args.drop_rate, stochdepth_rate=args.stochdepth_rate, neuron_dropout=0.0, grad_with_rate=True, v_reset=None)
#print(net)
print('Total Parameters: %.2fM' % (sum(p.numel() for p in net.parameters()) / 1000000.0))
net.cuda()
if args.resume:
checkpoint = torch.load(args.resume, map_location='cpu')
net.load_state_dict(checkpoint['net'])
criterion_mse = nn.MSELoss(reduce=True)
for epoch in range(1):
start_time = time.time()
net.eval()
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
end = time.time()
bar = Bar('Processing', max=len(test_data_loader))
test_loss = 0
test_acc = 0
test_samples = 0
batch_idx = 0
spikes_all = None
dims = None
with torch.no_grad():
for frame, label in test_data_loader:
batch_idx += 1
frame = frame.float().cuda()
label = label.cuda()
t_step = args.T
total_loss = 0
for t in range(t_step):
input_frame = frame
if t == 0:
out_fr = net(input_frame, init=True, save_spike=True)
total_fr = out_fr.clone().detach()
else:
out_fr = net(input_frame, save_spike=True)
total_fr += out_fr.clone().detach()
#total_fr = total_fr * (1 - 1. / args.tau) + out_fr
if args.loss_lambda > 0.0:
label_one_hot = F.one_hot(label, num_classes).float()
mse_loss = criterion_mse(out_fr, label_one_hot)
loss = ((1 - args.loss_lambda) * F.cross_entropy(out_fr, label) + args.loss_lambda * mse_loss) / t_step
else:
loss = F.cross_entropy(out_fr, label) / t_step
total_loss += loss
spikes_batch = net.get_spike()
if spikes_all is None:
spikes_all = []
dims = []
for i in range(len(spikes_batch)):
spikes_all.append(torch.sum(torch.mean(spikes_batch[i], dim=1)).item())
dims.append(spikes_batch[i].shape[1])
else:
for i in range(len(spikes_all)):
spikes_all[i] = spikes_all[i] + torch.sum(torch.mean(spikes_batch[i], dim=1)).item()
test_samples += label.numel()
test_loss += total_loss.item() * label.numel()
test_acc += (total_fr.argmax(1) == label).float().sum().item()
# measure accuracy and record loss
prec1, prec5 = accuracy(total_fr.data, label.data, topk=(1, 5))
losses.update(total_loss, input_frame.size(0))
top1.update(prec1.item(), input_frame.size(0))
top5.update(prec5.item(), input_frame.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# plot progress
bar.suffix = '({batch}/{size}) Data: {data:.3f}s | Batch: {bt:.3f}s | Total: {total:} | ETA: {eta:} | Loss: {loss:.4f} | top1: {top1: .4f} | top5: {top5: .4f}'.format(
batch=batch_idx,
size=len(test_data_loader),
data=data_time.avg,
bt=batch_time.avg,
total=bar.elapsed_td,
eta=bar.eta_td,
loss=losses.avg,
top1=top1.avg,
top5=top5.avg,
)
bar.next()
bar.finish()
test_loss /= test_samples
test_acc /= test_samples
for i in range(len(spikes_all)):
spikes_all[i] = spikes_all[i] / (test_samples * t_step)
total_rate = 0.
total_dim = 0
for i in range(len(spikes_all)):
total_rate += spikes_all[i] * dims[i]
total_dim += dims[i]
total_rate /= total_dim
total_time = time.time() - start_time
print(f'test_loss={test_loss}, test_acc={test_acc}, total_time={total_time}')
for i in range(len(spikes_all)):
print(f'layer={i+1}, spike_rate={spikes_all[i]}')
print(f'total_spike_rate={total_rate}')
if __name__ == '__main__':
main()