-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain_test.py
69 lines (54 loc) · 2.3 KB
/
main_test.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
import argparse
import os
import torch
import warnings
import torch.nn as nn
import torch.nn.parallel
import torch.optim
from Models import modelpool
from Preprocess import datapool
from utils import train, val, seed_all, get_logger
from Models.layer import *
parser = argparse.ArgumentParser(description='PyTorch Training')
# just use default setting
parser.add_argument('-j','--workers',default=4, type=int,metavar='N',help='number of data loading workers')
parser.add_argument('-b','--batch_size',default=200, type=int,metavar='N',help='mini-batch size')
parser.add_argument('--seed',default=42,type=int,help='seed for initializing training. ')
parser.add_argument('-suffix','--suffix',default='', type=str,help='suffix')
# model configuration
parser.add_argument('-data', '--dataset',default='cifar100',type=str,help='dataset')
parser.add_argument('-arch','--model',default='vgg16',type=str,help='model')
parser.add_argument('-id', '--identifier', type=str,help='model statedict identifier')
# test configuration
parser.add_argument('-dev','--device',default='0',type=str,help='device')
parser.add_argument('-T', '--time', default=0, type=int, help='snn simulation time')
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = args.device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def main():
global args
seed_all(args.seed)
# preparing data
train_loader, test_loader = datapool(args.dataset, args.batch_size)
# preparing model
model = modelpool(args.model, args.dataset)
model_dir = '%s-checkpoints'% (args.dataset)
state_dict = torch.load(os.path.join(model_dir, args.identifier + '.pth'), map_location=torch.device('cpu'))
# if old version state_dict
keys = list(state_dict.keys())
for k in keys:
if "relu.up" in k:
state_dict[k[:-7]+'act.thresh'] = state_dict.pop(k)
elif "up" in k:
state_dict[k[:-2]+'thresh'] = state_dict.pop(k)
model.load_state_dict(state_dict)
model.to(device)
model.set_T(args.time)
model.set_L(8)
# for m in model.modules():
# if isinstance(m, IF):
# print(m.thresh)
acc = val(model, test_loader, device, args.time)
print(acc)
if __name__ == "__main__":
main()