forked from BUG1989/caffe-int8-convert-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaffe-int8-convert-tool.py
468 lines (378 loc) · 15 KB
/
caffe-int8-convert-tool.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# -*- coding: utf-8 -*-
# SenseNets is pleased to support the open source community by making caffe-int8-convert-tool available.
#
# Copyright (C) 2018 SenseNets Technology Ltd. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
"""
Quantization module for generating the calibration tables will be used by
quantized (INT8) models from FP32 models.
This tool is based on Caffe Framework.
"""
from __future__ import division
from __future__ import print_function
import argparse
import numpy as np
import math, copy
import matplotlib.pyplot as plt
import sys,os
import caffe
import caffe.proto.caffe_pb2 as caffe_pb2
import time
from google.protobuf import text_format
def parse_args():
parser = argparse.ArgumentParser(
description='find the pretrained caffe models int8 quantize scale value')
parser.add_argument('--proto', dest='proto',
help="path to deploy prototxt.", type=str)
parser.add_argument('--model', dest='model',
help='path to pretrained weights', type=str)
parser.add_argument('--mean', dest='mean',
help='value of mean', type=float, nargs=3)
parser.add_argument('--norm', dest='norm',
help='value of normalize', type=float, nargs=1, default=1.0)
parser.add_argument('--images', dest='images',
help='path to calibration images', type=str)
parser.add_argument('--output', dest='output',
help='path to output calibration table file', type=str, default='calibration.table')
parser.add_argument('--gpu', dest='gpu',
help='use gpu to forward', type=int, default=0)
args = parser.parse_args()
return args, parser
global args, parser
args, parser = parse_args()
# global params
QUANTIZE_NUM = 127
STATISTIC = 1
INTERVAL_NUM = 2048
# ugly global params
top_blob_names = []
distribution_intervals = []
save_temp = []
def add_to_distribution(blob, distribution, interval):
"""
add the distribution
Args:
blob: the output blob of caffe layer
distribution: a list ,size is 2048
interval: a float number
Returns:
none
"""
max_index = len(distribution) - 1
indexes = np.minimum((np.abs(blob[blob!=0]) / interval).astype(np.int32), max_index)
for index in indexes:
distribution[index] = distribution[index] + 1
def normalize_distribution(distribution):
"""
Normalize the input list
Args:
distribution: a list ,size is 2048
Returns:
none
"""
num_sum = sum(distribution)
for i, data in enumerate(distribution):
distribution[i] = data / float(num_sum)
def compute_kl_divergence(dist_a, dist_b):
"""
Returen kl_divergence between
Args:
dist_a: list
dist_b: list
Returns:
kl_divergence: float, kl_divergence
"""
nonzero_inds = dist_a != 0
return np.sum(dist_a[nonzero_inds] * np.log(dist_a[nonzero_inds] / dist_b[nonzero_inds]))
def threshold_distribution(distribution, target_bin=128):
"""
Returen the best cut off num of bin
Args:
distribution: list, activations has been processed by histogram and normalize,size is 2048
target_bin: int, the num of bin that is used by quantize, Int8 default value is 128
Returns:
target_threshold: int, num of bin with the minimum KL
"""
target_threshold = target_bin
min_kl_divergence = 1000
length = distribution.size
quantize_distribution = np.zeros(target_bin)
threshold_sum = 0.0
threshold_sum = sum(distribution[target_bin:])
for threshold in range(target_bin, length):
t_distribution = copy.deepcopy(distribution[:threshold])
t_distribution[threshold-1] = t_distribution[threshold-1] + threshold_sum
threshold_sum = threshold_sum - distribution[threshold]
# ************************ threshold ************************
quantize_distribution = np.zeros(target_bin)
num_per_bin = threshold / target_bin
for i in range(0, target_bin):
start = i * num_per_bin
end = start + num_per_bin
left_upper = (int)(math.ceil(start))
if(left_upper > start):
left_scale = left_upper - start
quantize_distribution[i] += left_scale * distribution[left_upper - 1]
right_lower = (int)(math.floor(end))
if (right_lower < end):
right_scale = end - right_lower
quantize_distribution[i] += right_scale * distribution[right_lower]
for j in range(left_upper, right_lower):
quantize_distribution[i] += distribution[j]
# ************************ threshold ************************
# ************************ quantzie ************************
expand_distribution = np.zeros(threshold, dtype=np.float32)
for i in range(0, target_bin):
start = i * num_per_bin
end = start + num_per_bin
count = 0
left_upper = (int)(math.ceil(start))
left_scale = 0.0
if (left_upper > start):
left_scale = left_upper - start
if (distribution[left_upper - 1] != 0):
count += left_scale
right_lower = (int)(math.floor(end))
right_scale = 0.0
if (right_lower < end):
right_scale = end - right_lower
if (distribution[right_lower] != 0):
count += right_scale
for j in range(left_upper, right_lower):
if (distribution[j] != 0):
count = count + 1
expand_value = quantize_distribution[i] / count
if (left_upper > start):
if (distribution[left_upper - 1] != 0):
expand_distribution[left_upper - 1] += expand_value * left_scale
if (right_lower < end):
if (distribution[right_lower] != 0):
expand_distribution[right_lower] += expand_value * right_scale
for j in range(left_upper, right_lower):
if (distribution[j] != 0):
expand_distribution[j] += expand_value
# ************************ quantzie ************************
kl_divergence = compute_kl_divergence(t_distribution, expand_distribution)
if kl_divergence < min_kl_divergence:
min_kl_divergence = kl_divergence
target_threshold = threshold
return target_threshold
def net_forward(net, image_path, transformer):
"""
network inference and statistics the cost time
Args:
net: the instance of Caffe inference
image_path: a image need to be inference
transformer:
Returns:
none
"""
# load image
image = caffe.io.load_image(image_path)
# transformer.preprocess the image
net.blobs['data'].data[...] = transformer.preprocess('data',image)
# net forward
start = time.clock()
output = net.forward()
end = time.clock()
print("%s forward time : %.3f s" % (image_path, end - start))
def file_name(file_dir):
"""
Find the all file path with the directory
Args:
file_dir: The source file directory
Returns:
files_path: all the file path into a list
"""
files_path = []
for root, dir, files in os.walk(file_dir):
for name in files:
file_path = root + "/" + name
print(file_path)
files_path.append(file_path)
return files_path
def network_prepare(net, mean, norm):
"""
instance the prepare process param of caffe network inference
Args:
net: the instance of Caffe inference
mean: the value of mean
norm: the value of normalize
Returns:
none
"""
print("Network initial")
img_mean = np.array(mean)
# initial transformer
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
# convert shape from RBG to BGR
transformer.set_transpose('data', (2,0,1))
# load meanfile
transformer.set_mean('data', img_mean)
# resize image data from [0,1] to [0,255]
transformer.set_raw_scale('data', 255)
# convert RGB -> BGR
transformer.set_channel_swap('data', (2,1,0))
# normalize
transformer.set_input_scale('data', norm)
return transformer
def weight_quantize(net, net_file):
"""
CaffeModel convolution weight blob Int8 quantize
Args:
net: the instance of Caffe inference
net_file: deploy caffe prototxt
Returns:
none
"""
print("\nQuantize the kernel weight:")
# parse the net param from deploy prototxt
params = caffe_pb2.NetParameter()
with open(net_file) as f:
text_format.Merge(f.read(), params)
for i, layer in enumerate(params.layer):
if i == 0:
if layer.type != "Input":
raise ValueError("First layer should be input")
# find the convolution 3x3 and 1x1 layers to get out the weight_scale
if(layer.type == "Convolution" or layer.type == "ConvolutionDepthwise"):
kernel_size = layer.convolution_param.kernel_size[0]
if(kernel_size == 3 or kernel_size == 1):
layer_name = layer.name
# create the weight param scale name
weight_name = layer.name + "_param_0"
weight_data = net.params[layer_name][0].data
# find the blob threshold
max_val = np.max(weight_data)
min_val = np.min(weight_data)
threshold = max(abs(max_val), abs(min_val))
weight_scale = QUANTIZE_NUM / threshold
print("%-30s max_val : %-10f scale_val : %-10f" % (weight_name, max_val, weight_scale))
save_str = weight_name + " " + str(weight_scale)
save_temp.append(save_str)
# find the top blob name on every layer
top_blob = layer.top[0]
if top_blob not in top_blob_names:
top_blob_names.append(layer.top[0])
return None
def distribution_num(distribution):
return sum(distribution)
def activation_quantize(net, transformer, images_files):
"""
Activation Int8 quantize, optimaize threshold selection with KL divergence,
given a dataset, find the optimal threshold for quantizing it.
Ref: http://on-demand.gputechconf.com/gtc/2017/presentation/s7310-8-bit-inference-with-tensorrt.pdf
Args:
net: the instance of Caffe inference
transformer:
images_files: calibration dataset
Returns:
none
"""
print("\nQuantize the Activation:")
blob_num = len(top_blob_names)
max_vals = [0 for x in range(0, blob_num)]
distribution_intervals = [0 for x in range(0, blob_num)]
distributions = []
# run float32 inference on calibration dataset to find the activations range
for i , image in enumerate(images_files):
net_forward(net, image, transformer)
print("loop stage 1 : %d" % (i))
# find max threshold
for i, blob_name in enumerate(top_blob_names):
blob = net.blobs[blob_name].data[0].flatten()
max_val = np.max(blob)
min_val = np.min(blob)
max_vals[i] = max(max_vals[i], max(abs(max_val), abs(min_val)))
# calculate statistic blob scope and interval distribution
for i, blob_name in enumerate(top_blob_names):
distribution_intervals[i] = STATISTIC * max_vals[i] / INTERVAL_NUM
distribution = [0 for x in range(0, INTERVAL_NUM)]
distributions.append(distribution)
print("%-20s max_val : %-10.8f distribution_intervals : %-10.8f" % (blob_name, max_vals[i], distribution_intervals[i]))
# for each layers
# collect histograms of activations
print("\nCollect histograms of activations:")
for i, image in enumerate(images_files):
net_forward(net, image, transformer)
print("loop stage 2 : %d" % (i))
start = time.clock()
for i, blob_name in enumerate(top_blob_names):
blob = net.blobs[blob_name].data[0].flatten()
add_to_distribution(blob, distributions[i], distribution_intervals[i])
end = time.clock()
print("add cost %.3f s" % (end - start))
# calculate threshold
for i, distribution in enumerate(distributions):
# normalize distributions
normalize_distribution(distribution)
distribution = np.array(distribution)
# pick threshold which minimizes KL divergence
threshold_bin = threshold_distribution(distribution)
threshold = (threshold_bin + 0.5) * distribution_intervals[i]
# get the activation calibration value
calibration_val = QUANTIZE_NUM / threshold
# save the calibration value with it's layer name
print("%-20s bin : %-8d threshold : %-10f interval : %-10f scale : %-10f" % (top_blob_names[i], threshold_bin, threshold, distribution_intervals[i], calibration_val))
blob_str = top_blob_names[i] + " " + str(calibration_val)
save_temp.append(blob_str)
return None
def usage_info():
"""
usage info
"""
print("Input params is illegal...╮(╯3╰)╭")
print("try it again:\n python caffe-int8-scale-tools.py -h")
def main():
"""
main function
"""
print(args)
if args.proto == None or args.model == None or args.mean == None or args.images == None:
usage_info()
return None
# deploy caffe prototxt path
net_file = args.proto
# trained caffemodel path
caffe_model = args.model
# mean value
mean = args.mean
# norm value
norm = 1.0
if args.norm != 1.0:
norm = args.norm[0]
# calibration dataset
images_path = args.images
# the output calibration file
calibration_path = args.output
# default use CPU to forwark
if args.gpu != 0:
caffe.set_device(0)
caffe.set_mode_gpu()
# initial caffe net and the forword model(GPU or CPU)
net = caffe.Net(net_file,caffe_model,caffe.TEST)
# prepare the cnn network
transformer = network_prepare(net, mean, norm)
# get the calibration datasets images files path
images_files = file_name(images_path)
# quanitze kernel weight of the caffemodel to find it's calibration table
weight_quantize(net, net_file)
# quantize activation value of the caffemodel to find it's calibration table
activation_quantize(net, transformer, images_files)
# save the calibration tables,best wish for your INT8 inference have low accuracy loss :)
calibration_file = open(calibration_path, 'w')
for data in save_temp:
calibration_file.write(data + "\n")
calibration_file.close()
print("\nCaffe Int8 Calibration table create success,best wish for your INT8 inference has a low accuracy loss...\(^▽^)/")
if __name__ == "__main__":
main()