-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_quant.py
218 lines (177 loc) · 6.64 KB
/
test_quant.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
import argparse
import time
import os
import sys
import random
import torch
import torch.nn as nn
import numpy as np
from models import *
from utils import *
from generate_data import generate_data
def get_args_parser():
parser = argparse.ArgumentParser(description="PSAQ-ViT", add_help=False)
parser.add_argument("--model", default="deit_tiny",
choices=['deit_tiny', 'deit_small', 'deit_base', 'swin_tiny', 'swin_small'],
help="model")
parser.add_argument('--dataset', default="/Path/to/Dataset/",
help='path to dataset')
parser.add_argument("--calib-batchsize", default=32,
type=int, help="batchsize of calibration set")
parser.add_argument("--val-batchsize", default=200,
type=int, help="batchsize of validation set")
parser.add_argument("--num-workers", default=16, type=int,
help="number of data loading workers (default: 16)")
parser.add_argument("--device", default="cuda", type=str, help="device")
parser.add_argument("--print-freq", default=100,
type=int, help="print frequency")
parser.add_argument("--seed", default=0, type=int, help="seed")
parser.add_argument("--mode", default=0,
type=int, help="mode of calibration data, 0: PSAQ-ViT, 1: Gaussian noise, 2: Real data")
parser.add_argument('--w_bit', default=8,
type=int, help='bit-precision of weights')
parser.add_argument('--a_bit', default=8,
type=int, help='bit-precision of activation')
return parser
class Config:
def __init__(self, w_bit, a_bit):
self.weight_bit = w_bit
self.activation_bit = a_bit
def str2model(name):
model_zoo = {'deit_tiny': deit_tiny_patch16_224,
'deit_small': deit_small_patch16_224,
'deit_base': deit_base_patch16_224,
'swin_tiny': swin_tiny_patch4_window7_224,
'swin_small': swin_small_patch4_window7_224
}
print('Model: %s' % model_zoo[name].__name__)
return model_zoo[name]
def seed(seed=0):
sys.setrecursionlimit(100000)
os.environ["PYTHONHASHSEED"] = str(seed)
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
np.random.seed(seed)
random.seed(seed)
def main():
print(args)
seed(args.seed)
device = torch.device(args.device)
# Load bit-config
cfg = Config(args.w_bit, args.a_bit)
# Build model
model = str2model(args.model)(pretrained=True, cfg=cfg)
model = model.to(device)
model.eval()
# Build dataloader
train_loader, val_loader = build_dataset(args)
# Define loss function (criterion)
criterion = nn.CrossEntropyLoss().to(device)
# Get calibration set
# Case 0: PASQ-ViT
if args.mode == 0:
print("Generating data...")
calibrate_data = generate_data(args)
print("Calibrating with generated data...")
with torch.no_grad():
output = model(calibrate_data)
# Case 1: Gaussian noise
elif args.mode == 1:
calibrate_data = torch.randn((args.calib_batchsize, 3, 224, 224)).to(device)
print("Calibrating with Gaussian noise...")
with torch.no_grad():
output = model(calibrate_data)
# Case 2: Real data (Standard)
elif args.mode == 2:
for data, target in train_loader:
calibrate_data = data.to(device)
break
print("Calibrating with real data...")
with torch.no_grad():
output = model(calibrate_data)
# Not implemented
else:
raise NotImplementedError
# Freeze model
model.model_quant()
model.model_freeze()
# Validate the quantized model
print("Validating...")
val_loss, val_prec1, val_prec5 = validate(
args, val_loader, model, criterion, device
)
def validate(args, val_loader, model, criterion, device):
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# Switch to evaluate mode
model.eval()
val_start_time = end = time.time()
for i, (data, target) in enumerate(val_loader):
target = target.to(device)
data = data.to(device)
target = target.to(device)
with torch.no_grad():
output = model(data)
loss = criterion(output, target)
# Measure accuracy and record loss
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
losses.update(loss.data.item(), data.size(0))
top1.update(prec1.data.item(), data.size(0))
top5.update(prec5.data.item(), data.size(0))
# Measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print(
"Test: [{0}/{1}]\t"
"Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t"
"Loss {loss.val:.4f} ({loss.avg:.4f})\t"
"Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t"
"Prec@5 {top5.val:.3f} ({top5.avg:.3f})".format(
i,
len(val_loader),
batch_time=batch_time,
loss=losses,
top1=top1,
top5=top5,
)
)
val_end_time = time.time()
print(" * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f} Time {time:.3f}".format(
top1=top1, top5=top5, time=val_end_time - val_start_time))
return losses.avg, top1.avg, top5.avg
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.reshape(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
if __name__ == "__main__":
parser = argparse.ArgumentParser('PSAQ', parents=[get_args_parser()])
args = parser.parse_args()
main()