-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantize.py
80 lines (65 loc) · 2.87 KB
/
quantize.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
import os
import sys
import argparse
import torch
import torchvision
from common import *
from pytorch_nndct.apis import torch_quantizer
DIVIDER = '-----------------------------------------'
def quantize(build_dir, quant_mode, batchsize):
dset_dir = build_dir + '/dataset'
float_model = build_dir + '/float_model'
quant_model = build_dir + '/quant_model'
if torch.cuda.is_available():
print('You have', torch.cuda.device_count(), 'CUDA devices available')
for i in range(torch.cuda.device_count()):
print(' Device', str(i), ': ', torch.cuda.get_device_name(i))
print('Selecting device 0..')
device = torch.device('cuda:0')
else:
print('No CUDA devices available..selecting CPU')
device = torch.device('cpu')
# Load trained LeNet-5 model
model = LeNet5().to(device)
model.load_state_dict(torch.load(os.path.join(float_model, 'mnist_0.9869.pkl')))
# Force to merge BN with CONV for better quantization accuracy
optimize = 1
# Override batchsize if in test mode
if quant_mode == 'test':
batchsize = 1
rand_in = torch.randn([batchsize, 1, 28, 28]) # Use 28x28 input for LeNet-5
quantizer = torch_quantizer(quant_mode, model, (rand_in), output_dir=quant_model)
quantized_model = quantizer.quant_model
# Data loader
test_dataset = torchvision.datasets.MNIST(dset_dir, train=False, download=True, transform=test_transform)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batchsize, shuffle=False)
# Export config
if quant_mode == 'calib':
test_loss, accuracy = test(quantized_model, device, test_loader)
quantizer.export_quant_config()
if quant_mode == 'test':
# Evaluate
test_loss, accuracy = test(quantized_model, device, test_loader)
print("Accuracy is {}, Loss is {}".format(accuracy, test_loss))
quantizer.export_xmodel(deploy_check=False, output_dir=quant_model)
return
def run_main():
ap = argparse.ArgumentParser()
ap.add_argument('-d', '--build_dir', type=str, default='build', help='Path to build folder. Default is build')
ap.add_argument('-q', '--quant_mode', type=str, default='calib', choices=['calib', 'test'],
help='Quantization mode (calib or test). Default is calib')
ap.add_argument('-b', '--batchsize', type=int, default=100, help='Testing batchsize - must be an integer. Default is 100')
args = ap.parse_args()
print('\n' + DIVIDER)
print('PyTorch version : ', torch.__version__)
print(sys.version)
print(DIVIDER)
print(' Command line options:')
print('--build_dir : ', args.build_dir)
print('--quant_mode : ', args.quant_mode)
print('--batchsize : ', args.batchsize)
print(DIVIDER)
quantize(args.build_dir, args.quant_mode, args.batchsize)
return
if __name__ == '__main__':
run_main()