forked from jakc4103/DFQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniform_test.py
96 lines (85 loc) · 3.45 KB
/
uniform_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
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
#*
# @file Different utility functions
# Copyright (c) Yaohui Cai, Zhewei Yao, Zhen Dong, Amir Gholami
# All rights reserved.
# This file is part of ZeroQ repository.
#
# ZeroQ is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ZeroQ is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZeroQ repository. If not, see <http://www.gnu.org/licenses/>.
#*
import argparse
import torch
import numpy as np
import torch.nn as nn
from pytorchcv.model_provider import get_model as ptcv_get_model
from utils import *
from distill_data import *
# model settings
def arg_parse():
parser = argparse.ArgumentParser(
description='This repository contains the PyTorch implementation for the paper ZeroQ: A Novel Zero-Shot Quantization Framework.')
parser.add_argument('--dataset',
type=str,
default='imagenet',
choices=['imagenet', 'cifar10'],
help='type of dataset')
parser.add_argument('--model',
type=str,
default='resnet18',
choices=[
'resnet18', 'resnet50', 'inceptionv3',
'mobilenetv2_w1', 'shufflenet_g1_w1',
'resnet20_cifar10', 'sqnxt23_w2'
],
help='model to be quantized')
parser.add_argument('--batch_size',
type=int,
default=32,
help='batch size of distilled data')
parser.add_argument('--test_batch_size',
type=int,
default=128,
help='batch size of test data')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = arg_parse()
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Load pretrained model
model = ptcv_get_model(args.model, pretrained=True)
print('****** Full precision model loaded ******')
# Load validation data
test_loader = getTestData(args.dataset,
batch_size=args.test_batch_size,
path='./data/imagenet/',
for_inception=args.model.startswith('inception'))
# Generate distilled data
dataloader = getDistilData(
model.cuda(),
args.dataset,
batch_size=args.batch_size,
for_inception=args.model.startswith('inception'))
print('****** Data loaded ******')
# Quantize single-precision model to 8-bit model
quantized_model = quantize_model(model)
# Freeze BatchNorm statistics
quantized_model.eval()
quantized_model = quantized_model.cuda()
# Update activation range according to distilled data
update(quantized_model, dataloader)
# Freeze activation range during test
freeze_model(quantized_model)
quantized_model = nn.DataParallel(quantized_model).cuda()
# Test the final quantized model
test(quantized_model, test_loader)