Skip to content
Snippets Groups Projects
Commit 04f31bec authored by Yuanjing Shi's avatar Yuanjing Shi
Browse files

rename keras_frontend

parent 62c6a211
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
from scipy import stats
from numpy import linalg
import numpy as np
import sys
# NOTE: enable/disable smart quantization of weights and activations
smart_quantization = False
def quantize_arr(input_arr, min_val, max_val):
quantize_range = 256.0
input_range = max_val - min_val
mul_factor = input_range / quantize_range
v1 = np.subtract(input_arr, min_val)
v2 = np.divide(v1, mul_factor)
v3 = v2.astype(int)
v4 = np.multiply(v3, mul_factor)
v5 = np.add(v4, min_val)
v6 = np.clip(v5, min_val, max_val)
return v6
def compute_norm(a1, a2):
norm_inp = np.subtract(a1, a2)
#norm = linalg.norm(norm_inp, ord = 1)
norm = np.sum(np.abs(norm_inp))
print ("*** norm = ", norm)
return norm
def get_best_quant_range(input_arr):
# For disabled smart quantization, skip expensive quant range computation
if smart_quantization == False:
min_val = np.percentile(input_arr, 0.1)
max_val = np.percentile(input_arr, 99.9)
return (min_val, max_val)
# Trying different threshold values for INT8 quantization
min_percentiles = [0, 0.1, 0.2, 0.3]
max_percentiles = [100, 99.9, 99.8, 99.7]
min_norm = 100000000
min_pair = (0, 100)
range_vals = (0, 0)
for i in min_percentiles:
for j in max_percentiles:
print (" i = ", i, " j = ", j, " \n")
min_val = np.percentile(input_arr, i)
max_val = np.percentile(input_arr, j)
res = quantize_arr(input_arr, min_val, max_val)
norm = compute_norm(res, input_arr)
if norm < min_norm:
min_norm = norm
min_pair = (i, j)
range_vals = (min_val, max_val)
print ("--- min_norm = ", min_norm, " , min_pair = ", min_pair , " range_vals = ", range_vals)
return range_vals
if __name__ == "__main__":
vals = np.zeros((2,3))
vals[0][0] = 1.2
vals[0][1] = 0.48
vals[0][2] = 0.5
vals[1][0] = -0.3
vals[1][1] = 0.25
vals[1][2] = 0.46
input_arr = np.array(vals)
res = quantize_arr(input_arr, -0.3, 1.4)
print (res, "\n")
divergence = compute_norm(res, input_arr)
print ("divergence = ", divergence, "\n")
from setuptools import setup
setup(
name='frontend',
version='1.0',
description='ApproxHPVM frontend modules',
author='Hashim',
author_email='hsharif3@illinois.edu',
packages=['frontend'],
install_requires=[],
)
def nodeHasBias(cur_node):
if cur_node.layer_type == "Conv2D" or cur_node.layer_type == "DepthwiseConv2D" or cur_node.layer_type == "Dense":
#return True
return cur_node.use_bias
else:
return False
def layerHasActivationAttr(cur_node):
if cur_node.layer_type == "Conv2D" or cur_node.layer_type == "DepthwiseConv2D" \
or cur_node.layer_type == "Dense" or cur_node.layer_type == "Activation":
return True
else:
return False
def nodeHasActivation(cur_node):
if cur_node.layer_type == "Conv2D" or cur_node.layer_type == "DepthwiseConv2D" \
or cur_node.layer_type == "Dense" or cur_node.layer_type == "Activation":
#return True
return cur_node.activation_type != "linear"
else:
return False
def genActivationCallStr(input_var, output_var, activation_type):
func_name = ""
if activation_type == "tanh":
func_name = "Tanh"
if activation_type == "relu":
func_name = "Relu"
if activation_type == "softmax":
func_name = "Softmax"
inst_str = "void* " + output_var + " = "
inst_str += "tensor" + func_name + "(" + input_var + "); \n"
print ("***** inst_str = ", inst_str, "\n")
return inst_str
import numpy as np
import struct
import random
def dumpLabels(file_name, Y_test):
f = open(file_name, "wb")
labels_map = {}
for label in Y_test:
label_val = 0
if len(Y_test.shape) > 1:
#label_val = np.int8(label[0])
label_val = np.int32(label[0])
else:
#label_val = np.int8(label)
label_val = np.int32(label)
if label_val not in labels_map:
labels_map[label_val] = 0
labels_map[label_val] += 1
f.write(label_val)
f.close()
"""def dumpData(file_name, X_test):
N = X_test.shape[0]
C = X_test.shape[1]
H = X_test.shape[2]
W = X_test.shape[3]
print ("*DumpData")
print("-min_val = ", np.amin(X_test))
print("-max_val = ", np.amax(X_test))
f = open(file_name, "wb")
for i in range(N):
for j in range(C):
for k in range(H):
for l in range(W):
val = struct.unpack("f", struct.pack("f", X_test[i][j][k][l]))
f.write(np.float32(val[0]))
f.close()
"""
def dumpData(file_name, X_test):
N = X_test.shape[0]
C = X_test.shape[1]
H = X_test.shape[2]
W = X_test.shape[3]
print ("*DumpData")
print("-min_val = ", np.amin(X_test))
print("-max_val = ", np.amax(X_test))
f = open(file_name, "wb")
X_test.tofile(f)
f.close()
def dumpConvWeights(file_name, weights, N, C, H, W):
print (weights.shape)
print ("*DumpConvWeights")
print("-min_val = ", np.amin(weights))
print("-max_val = ", np.amax(weights))
f = open(file_name, "wb")
for i in range(N):
for j in range(C):
for k in range(H):
for l in range(W):
f.write(weights[k][l][j][i])
f.close()
def dumpFcWeights(file_name, weights, H, W):
print (weights.shape)
print ("*DumpFcWeights")
print("-min_val = ", np.amin(weights))
print("-max_val = ", np.amax(weights))
f = open(file_name, "wb")
for i in range(H):
for j in range(W):
f.write(weights[i][j])
f.close()
def dumpFcBias(file_name, bias, W):
print (bias.shape)
print ("*DumpFcBias")
print("-min_val = ", np.amin(bias))
print("-max_val = ", np.amax(bias))
f = open(file_name, "wb")
for i in range(W):
f.write(bias[i])
f.close()
def dumpCalibrationData(file_name, X_train, labels_fname, train_labels):
combined_list = []
for i in range(len(X_train)):
tup = (X_train[i], train_labels[i])
combined_list.append(tup)
np.random.shuffle(combined_list)
#X_calibration = X_train[0:5000]
data_list = []
labels_list = []
for i in range(5000):
tup = combined_list[i]
data_list.append(tup[0])
labels_list.append(tup[1])
data_list = np.array(data_list)
labels_list = np.array(labels_list)
dumpData(file_name, data_list)
dumpLabels(labels_fname, labels_list)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment