Skip to content

Commit

Permalink
Make changes to support stochastic rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
anson0910 committed Nov 16, 2015
1 parent a8bef25 commit 9c49717
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 405 deletions.
222 changes: 125 additions & 97 deletions face_net_surgery/.idea/workspace.xml

Large diffs are not rendered by default.

67 changes: 17 additions & 50 deletions face_net_surgery/face12c_full_conv_quantize_3_to_9.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
import numpy as np
import sys
from quantize_functions import *

stochasticRounding = True

# ================== caffe ======================================
caffe_root = '/home/anson/caffe-master/' # this file is expected to be in {caffe_root}/examples
sys.path.insert(0, caffe_root + 'python')
import caffe
# =========== quantize functions ======================================================================
def fixed_point_list(a, b):
'''
A(a, b) : range = -2^a ~ 2^a - 2^-b
:param a:
:param b:
:return: list of all numbers possible in A(a, b), from smallest to largest
'''
fixedPointList = []
numOfElements = 2**(a + b + 1)

for i in range(numOfElements):
fixedPointList.append( -(2**a) + (2**(-b))*i )

return fixedPointList
def round_number(num, fixedPointList):
'''
Rounds num to closest number in fixedPointList
:param num:
:param fixedPointList:
:return: quantized number
'''
low = 0
high = len(fixedPointList)
result = 0

while low < high:
midIdx = (low + high) / 2
result = fixedPointList[midIdx]
minDistance = abs(num - result)
leftIdx = midIdx - 1
rightIdx = midIdx + 1
if leftIdx > -1 and abs(num - fixedPointList[leftIdx]) < minDistance:
high = midIdx
elif rightIdx < len(fixedPointList) and abs(num - fixedPointList[rightIdx]) < minDistance:
low = midIdx + 1
else:
break

return result

# ================== load face12c_full_conv ======================================
MODEL_FILE = '/home/anson/caffe-master/models/face_12c/face12c_full_conv.prototxt'
Expand All @@ -58,8 +21,12 @@ def round_number(num, fixedPointList):
for quantize_bit_num in range(3, 10):
# ================== load file to save quantized parameters =======================
MODEL_FILE = '/home/anson/caffe-master/models/face_12c/face12c_full_conv.prototxt'
PRETRAINED = '/home/anson/caffe-master/models/face_12c/face12c_full_conv_quantize_' \
+ str(quantize_bit_num) +'.caffemodel'
if stochasticRounding:
PRETRAINED = '/home/anson/caffe-master/models/face_12c/face12c_full_conv_SRquantize_' \
+ str(quantize_bit_num) +'.caffemodel'
else:
PRETRAINED = '/home/anson/caffe-master/models/face_12c/face12c_full_conv_quantize_' \
+ str(quantize_bit_num) +'.caffemodel'
quantized_model = open(PRETRAINED, 'w')
net_quantized = caffe.Net(MODEL_FILE, PRETRAINED, caffe.TEST)
params_quantized = params
Expand Down Expand Up @@ -95,18 +62,18 @@ def round_number(num, fixedPointList):
weightFixedPointList = fixed_point_list(a_weight, b_weight)
biasFixedPointList = fixed_point_list(a_bias, b_bias)

print ("Shape of " + k + " weight params : " + str(filters_weights.shape))
print ("Max : " + str(filters_weights.max()) + " min : " + str(filters_weights.min()))
print ("Shape of " + k + " bias params: " + str(filters_bias.shape))
print ("Max : " + str(filters_bias.max()) + " min : " + str(filters_bias.min()))
# print ("Shape of " + k + " weight params : " + str(filters_weights.shape))
# print ("Max : " + str(filters_weights.max()) + " min : " + str(filters_weights.min()))
# print ("Shape of " + k + " bias params: " + str(filters_bias.shape))
# print ("Max : " + str(filters_bias.max()) + " min : " + str(filters_bias.min()))
print "Quantizing to " + str(quantize_bit_num) + " bits."

for currentNum in np.nditer(filters_weights, op_flags=['readwrite']):
currentNum[...] = round_number(currentNum[...], weightFixedPointList)
currentNum[...] = round_number(currentNum[...], weightFixedPointList, stochasticRounding)

for currentNum in np.nditer(filters_bias, op_flags=['readwrite']):
currentNum[...] = round_number(currentNum[...], biasFixedPointList)
currentNum[...] = round_number(currentNum[...], biasFixedPointList, stochasticRounding)

net_quantized.save('/home/anson/caffe-master/models/face_12c/face12c_full_conv_quantize_'
+ str(quantize_bit_num) +'.caffemodel')
net_quantized.save(PRETRAINED)

quantized_model.close()
49 changes: 16 additions & 33 deletions face_net_surgery/face_12_cal_quantize_3_to_9.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import numpy as np
import sys
from quantize_functions import *

stochasticRounding = True

modelName = 'face_12_cal'
modelFileName = 'face_12_cal_train_iter_400000.caffemodel'
Expand All @@ -23,31 +26,6 @@ def fixed_point_list(a, b):
fixedPointList.append( -(2**a) + (2**(-b))*i )

return fixedPointList
def round_number(num, fixedPointList):
'''
Rounds num to closest number in fixedPointList
:param num:
:param fixedPointList:
:return: quantized number
'''
low = 0
high = len(fixedPointList)
result = 0

while low < high:
midIdx = (low + high) / 2
result = fixedPointList[midIdx]
minDistance = abs(num - result)
leftIdx = midIdx - 1
rightIdx = midIdx + 1
if leftIdx > -1 and abs(num - fixedPointList[leftIdx]) < minDistance:
high = midIdx
elif rightIdx < len(fixedPointList) and abs(num - fixedPointList[rightIdx]) < minDistance:
low = midIdx + 1
else:
break

return result

# ================== load face12c_full_conv ======================================
MODEL_FILE = '/home/anson/caffe-master/models/' + modelName + '/deploy.prototxt'
Expand All @@ -63,8 +41,12 @@ def round_number(num, fixedPointList):
for quantize_bit_num in range(3, 10):
# ================== load file to save quantized parameters =======================
MODEL_FILE = '/home/anson/caffe-master/models/' + modelName +'/deploy.prototxt'
PRETRAINED = '/home/anson/caffe-master/models/' + modelName + '/' + modelName + '_quantize_' \
+ str(quantize_bit_num) +'.caffemodel'
if stochasticRounding:
PRETRAINED = '/home/anson/caffe-master/models/' + modelName + '/' + modelName + '_SRquantize_' \
+ str(quantize_bit_num) +'.caffemodel'
else:
PRETRAINED = '/home/anson/caffe-master/models/' + modelName + '/' + modelName + '_quantize_' \
+ str(quantize_bit_num) +'.caffemodel'
quantized_model = open(PRETRAINED, 'w')
net_quantized = caffe.Net(MODEL_FILE, PRETRAINED, caffe.TEST)
params_quantized = params
Expand Down Expand Up @@ -102,16 +84,17 @@ def round_number(num, fixedPointList):
weightFixedPointList = fixed_point_list(a_weight, b_weight)
biasFixedPointList = fixed_point_list(a_bias, b_bias)

print ("Shape of " + k + " weight params : " + str(filters_weights.shape))
print ("Max : " + str(filters_weights.max()) + " min : " + str(filters_weights.min()))
print ("Shape of " + k + " bias params: " + str(filters_bias.shape))
print ("Max : " + str(filters_bias.max()) + " min : " + str(filters_bias.min()))
# print ("Shape of " + k + " weight params : " + str(filters_weights.shape))
# print ("Max : " + str(filters_weights.max()) + " min : " + str(filters_weights.min()))
# print ("Shape of " + k + " bias params: " + str(filters_bias.shape))
# print ("Max : " + str(filters_bias.max()) + " min : " + str(filters_bias.min()))
print "Quantizing to " + str(quantize_bit_num) + " bits."

for currentNum in np.nditer(filters_weights, op_flags=['readwrite']):
currentNum[...] = round_number(currentNum[...], weightFixedPointList)
currentNum[...] = round_number(currentNum[...], weightFixedPointList, stochasticRounding)

for currentNum in np.nditer(filters_bias, op_flags=['readwrite']):
currentNum[...] = round_number(currentNum[...], biasFixedPointList)
currentNum[...] = round_number(currentNum[...], biasFixedPointList, stochasticRounding)

net_quantized.save(PRETRAINED)

Expand Down
53 changes: 10 additions & 43 deletions face_net_surgery/face_24_cal_quantize_3_to_9.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import numpy as np
import sys
from quantize_functions import *

stochasticRounding = True

modelName = 'face_24_cal'
modelFileName = 'face_24_cal_train_iter_400000.caffemodel'
Expand All @@ -8,46 +11,6 @@
caffe_root = '/home/anson/caffe-master/' # this file is expected to be in {caffe_root}/examples
sys.path.insert(0, caffe_root + 'python')
import caffe
# =========== quantize functions ======================================================================
def fixed_point_list(a, b):
'''
A(a, b) : range = -2^a ~ 2^a - 2^-b
:param a:
:param b:
:return: list of all numbers possible in A(a, b), from smallest to largest
'''
fixedPointList = []
numOfElements = 2**(a + b + 1)

for i in range(numOfElements):
fixedPointList.append( -(2**a) + (2**(-b))*i )

return fixedPointList
def round_number(num, fixedPointList):
'''
Rounds num to closest number in fixedPointList
:param num:
:param fixedPointList:
:return: quantized number
'''
low = 0
high = len(fixedPointList)
result = 0

while low < high:
midIdx = (low + high) / 2
result = fixedPointList[midIdx]
minDistance = abs(num - result)
leftIdx = midIdx - 1
rightIdx = midIdx + 1
if leftIdx > -1 and abs(num - fixedPointList[leftIdx]) < minDistance:
high = midIdx
elif rightIdx < len(fixedPointList) and abs(num - fixedPointList[rightIdx]) < minDistance:
low = midIdx + 1
else:
break

return result

# ================== load face12c_full_conv ======================================
MODEL_FILE = '/home/anson/caffe-master/models/' + modelName + '/deploy.prototxt'
Expand All @@ -63,8 +26,12 @@ def round_number(num, fixedPointList):
for quantize_bit_num in range(3, 10):
# ================== load file to save quantized parameters =======================
MODEL_FILE = '/home/anson/caffe-master/models/' + modelName +'/deploy.prototxt'
PRETRAINED = '/home/anson/caffe-master/models/' + modelName + '/' + modelName + '_quantize_' \
if stochasticRounding:
PRETRAINED = '/home/anson/caffe-master/models/' + modelName + '/' + modelName + '_SRquantize_' \
+ str(quantize_bit_num) +'.caffemodel'
else:
PRETRAINED = '/home/anson/caffe-master/models/' + modelName + '/' + modelName + '_quantize_' \
+ str(quantize_bit_num) +'.caffemodel'
quantized_model = open(PRETRAINED, 'w')
net_quantized = caffe.Net(MODEL_FILE, PRETRAINED, caffe.TEST)
params_quantized = params
Expand Down Expand Up @@ -109,10 +76,10 @@ def round_number(num, fixedPointList):
print "Quantizing to " + str(quantize_bit_num) + " bits."

for currentNum in np.nditer(filters_weights, op_flags=['readwrite']):
currentNum[...] = round_number(currentNum[...], weightFixedPointList)
currentNum[...] = round_number(currentNum[...], weightFixedPointList, stochasticRounding)

for currentNum in np.nditer(filters_bias, op_flags=['readwrite']):
currentNum[...] = round_number(currentNum[...], biasFixedPointList)
currentNum[...] = round_number(currentNum[...], biasFixedPointList, stochasticRounding)

net_quantized.save(PRETRAINED)

Expand Down
62 changes: 15 additions & 47 deletions face_net_surgery/face_24c_quantize_3_to_9.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import numpy as np
import sys
from quantize_functions import *

stochasticRounding = True

modelName = 'face_24c'
modelFileName = 'face_24c_train_iter_400000.caffemodel'
Expand All @@ -8,46 +11,6 @@
caffe_root = '/home/anson/caffe-master/' # this file is expected to be in {caffe_root}/examples
sys.path.insert(0, caffe_root + 'python')
import caffe
# =========== quantize functions ======================================================================
def fixed_point_list(a, b):
'''
A(a, b) : range = -2^a ~ 2^a - 2^-b
:param a:
:param b:
:return: list of all numbers possible in A(a, b), from smallest to largest
'''
fixedPointList = []
numOfElements = 2**(a + b + 1)

for i in range(numOfElements):
fixedPointList.append( -(2**a) + (2**(-b))*i )

return fixedPointList
def round_number(num, fixedPointList):
'''
Rounds num to closest number in fixedPointList
:param num:
:param fixedPointList:
:return: quantized number
'''
low = 0
high = len(fixedPointList)
result = 0

while low < high:
midIdx = (low + high) / 2
result = fixedPointList[midIdx]
minDistance = abs(num - result)
leftIdx = midIdx - 1
rightIdx = midIdx + 1
if leftIdx > -1 and abs(num - fixedPointList[leftIdx]) < minDistance:
high = midIdx
elif rightIdx < len(fixedPointList) and abs(num - fixedPointList[rightIdx]) < minDistance:
low = midIdx + 1
else:
break

return result

# ================== load face12c_full_conv ======================================
MODEL_FILE = '/home/anson/caffe-master/models/' + modelName + '/deploy.prototxt'
Expand All @@ -63,8 +26,12 @@ def round_number(num, fixedPointList):
for quantize_bit_num in range(3, 10):
# ================== load file to save quantized parameters =======================
MODEL_FILE = '/home/anson/caffe-master/models/' + modelName +'/deploy.prototxt'
PRETRAINED = '/home/anson/caffe-master/models/' + modelName + '/' + modelName + '_quantize_' \
if stochasticRounding:
PRETRAINED = '/home/anson/caffe-master/models/' + modelName + '/' + modelName + '_SRquantize_' \
+ str(quantize_bit_num) +'.caffemodel'
else:
PRETRAINED = '/home/anson/caffe-master/models/' + modelName + '/' + modelName + '_quantize_' \
+ str(quantize_bit_num) +'.caffemodel'
quantized_model = open(PRETRAINED, 'w')
net_quantized = caffe.Net(MODEL_FILE, PRETRAINED, caffe.TEST)
params_quantized = params
Expand Down Expand Up @@ -102,16 +69,17 @@ def round_number(num, fixedPointList):
weightFixedPointList = fixed_point_list(a_weight, b_weight)
biasFixedPointList = fixed_point_list(a_bias, b_bias)

print ("Shape of " + k + " weight params : " + str(filters_weights.shape))
print ("Max : " + str(filters_weights.max()) + " min : " + str(filters_weights.min()))
print ("Shape of " + k + " bias params: " + str(filters_bias.shape))
print ("Max : " + str(filters_bias.max()) + " min : " + str(filters_bias.min()))
# print ("Shape of " + k + " weight params : " + str(filters_weights.shape))
# print ("Max : " + str(filters_weights.max()) + " min : " + str(filters_weights.min()))
# print ("Shape of " + k + " bias params: " + str(filters_bias.shape))
# print ("Max : " + str(filters_bias.max()) + " min : " + str(filters_bias.min()))
print "Quantizing to " + str(quantize_bit_num) + " bits."

for currentNum in np.nditer(filters_weights, op_flags=['readwrite']):
currentNum[...] = round_number(currentNum[...], weightFixedPointList)
currentNum[...] = round_number(currentNum[...], weightFixedPointList, stochasticRounding)

for currentNum in np.nditer(filters_bias, op_flags=['readwrite']):
currentNum[...] = round_number(currentNum[...], biasFixedPointList)
currentNum[...] = round_number(currentNum[...], biasFixedPointList, stochasticRounding)

net_quantized.save(PRETRAINED)

Expand Down
Loading

0 comments on commit 9c49717

Please sign in to comment.