import torch
import numpy as np
import math
# model = torch.load('model_best.pth.tar')
params = torch.load('/home/huke/python_code/object_detection/point_target/v10/model_compression_WbWtAb/Result/07-10_21-11-55/nin_gc.pth') # 4-layers;max channel 64
print("key in params:")
for key in params:
print(key + ":")
print()
print("key in params state_dict:")
for key in params['state_dict']:
print(key + ":")
print()
mappp = params['state_dict']
# print(model['best_prec1'])
print(params['min_avg_loss'])
# ********************* W(模型参数)量化(三/二值) ***********************
def meancenter_clampConvParams(w):
mean = w.data.mean(1, keepdim=True)
w.data.sub(mean) # W中心化(C方向)
w.data.clamp(-1.0, 1.0) # W截断
return w
def Binary_w(input):
output = torch.sign(input)
return output
def binary(input):
output = Binary_w(input)
return output
def weight_tnn_bin(input):
output = meancenter_clampConvParams(input) # W中心化+截断
# **************** channel级 - E(|W|) ****************
E = torch.mean(torch.abs(output), (3, 2, 1), keepdim=True)
# **************** α(缩放因子) ****************
alpha = E
# ************** W —— +-1 **************
output = binary(output)
return output
# '''
print("-"*60)
print("conv1:")
print("-"*60)
# print(mappp['tnn_bin_conv1.tnn_bin_conv.weight'].shape)
print('tnn_bin_conv1.tnn_bin_conv.bias')
print(mappp['tnn_bin_conv1.tnn_bin_conv.bias'].shape)
print(mappp['tnn_bin_conv1.tnn_bin_conv.bias'])
print()
print("-"*60)
print("bn1:")
print("-"*60)
print('tnn_bin_conv1.bn.weight')
print(mappp['tnn_bin_conv1.bn.weight'].shape)
print(mappp['tnn_bin_conv1.bn.weight'])
print('tnn_bin_conv1.bn.bias')
print(mappp['tnn_bin_conv1.bn.bias'].shape)
print(mappp['tnn_bin_conv1.bn.bias'])
print('tnn_bin_conv1.bn.running_mean')
print(mappp['tnn_bin_conv1.bn.running_mean'].shape)
print(mappp['tnn_bin_conv1.bn.running_mean'])
print('tnn_bin_conv1.bn.running_var')
print(mappp['tnn_bin_conv1.bn.running_var'].shape)
print(mappp['tnn_bin_conv1.bn.running_var'])
print('tnn_bin_conv1.bn.num_batches_tracked')
print(mappp['tnn_bin_conv1.bn.num_batches_tracked'].shape)
print(mappp['tnn_bin_conv1.bn.num_batches_tracked'])
print()
print("-"*60)
print("conv2:")
print("-"*60)
# print(mappp['tnn_bin_conv2.tnn_bin_conv.weight'].shape)
print('tnn_bin_conv2.tnn_bin_conv.bias')
print(mappp['tnn_bin_conv2.tnn_bin_conv.bias'].shape)
print(mappp['tnn_bin_conv2.tnn_bin_conv.bias'])
print()
print("-"*60)
print("bn2:")
print("-"*60)
print('tnn_bin_conv2.bn.weight')
print(mappp['tnn_bin_conv2.bn.weight'].shape)
print(mappp['tnn_bin_conv2.bn.weight'])
print('tnn_bin_conv2.bn.bias')
print(mappp['tnn_bin_conv2.bn.bias'].shape)
print(mappp['tnn_bin_conv2.bn.bias'])
print('tnn_bin_conv2.bn.running_mean')
print(mappp['tnn_bin_conv2.bn.running_mean'].shape)
print(mappp['tnn_bin_conv2.bn.running_mean'])
print('tnn_bin_conv2.bn.running_var')
print(mappp['tnn_bin_conv2.bn.running_var'].shape)
print(mappp['tnn_bin_conv2.bn.running_var'])
print('tnn_bin_conv2.bn.num_batches_tracked')
print(mappp['tnn_bin_conv2.bn.num_batches_tracked'].shape)
print(mappp['tnn_bin_conv2.bn.num_batches_tracked'])
print()
print("-"*60)
print("conv3:")
print("-"*60)
# print(mappp['tnn_bin_conv3.tnn_bin_conv.weight'].shape)
print('tnn_bin_conv3.tnn_bin_conv.bias')
print(mappp['tnn_bin_conv3.tnn_bin_conv.bias'].shape)
print(mappp['tnn_bin_conv3.tnn_bin_conv.bias'])
print()
print("-"*60)
print("bn3:")
print("-"*60)
print('tnn_bin_conv3.bn.weight')
print(mappp['tnn_bin_conv3.bn.weight'].shape)
print(mappp['tnn_bin_conv3.bn.weight'])
print('tnn_bin_conv3.bn.bias')
print(mappp['tnn_bin_conv3.bn.bias'].shape)
print(mappp['tnn_bin_conv3.bn.bias'])
print('tnn_bin_conv3.bn.running_mean')
print(mappp['tnn_bin_conv3.bn.running_mean'].shape)
print(mappp['tnn_bin_conv3.bn.running_mean'])
print('tnn_bin_conv3.bn.running_var')
print(mappp['tnn_bin_conv3.bn.running_var'].shape)
print(mappp['tnn_bin_conv3.bn.running_var'])
print('tnn_bin_conv3.bn.num_batches_tracked')
print(mappp['tnn_bin_conv3.bn.num_batches_tracked'].shape)
print(mappp['tnn_bin_conv3.bn.num_batches_tracked'])
print()
print("-"*60)
print("conv4:")
print("-"*60)
# print(mappp['tnn_bin_conv4.tnn_bin_conv.weight'].shape)
print('tnn_bin_conv4.tnn_bin_conv.bias')
print(mappp['tnn_bin_conv4.tnn_bin_conv.bias'].shape)
print(mappp['tnn_bin_conv4.tnn_bin_conv.bias'])
print()
print("-"*60)
print("bn4:")
print("-"*60)
print('tnn_bin_conv4.bn.weight')
print(mappp['tnn_bin_conv4.bn.weight'].shape)
print(mappp['tnn_bin_conv4.bn.weight'])
print('tnn_bin_conv4.bn.bias')
print(mappp['tnn_bin_conv4.bn.bias'].shape)
print(mappp['tnn_bin_conv4.bn.bias'])
print('tnn_bin_conv4.bn.running_mean')
print(mappp['tnn_bin_conv4.bn.running_mean'].shape)
print(mappp['tnn_bin_conv4.bn.running_mean'])
print('tnn_bin_conv4.bn.running_var')
print(mappp['tnn_bin_conv4.bn.running_var'].shape)
print(mappp['tnn_bin_conv4.bn.running_var'])
print('tnn_bin_conv4.bn.num_batches_tracked')
print(mappp['tnn_bin_conv4.bn.num_batches_tracked'].shape)
print(mappp['tnn_bin_conv4.bn.num_batches_tracked'])
print()
# for i in range(16):
# for j in range(1):
# print('------------------------------')
# print('i---j:',i,j)
# print('------------------------------')
# print(mappp['tnn_bin_conv1.tnn_bin_conv.weight'][i][j])
#
# conv1_weight = weight_tnn_bin(mappp['tnn_bin_conv1.tnn_bin_conv.weight'])
#
# print("-"*50)
# print("after binary")
# for i in range(16):
# for j in range(1):
# print('------------------------------')
# print('i---j:',i,j)
# print('------------------------------')
# print(conv1_weight[i][j])
# '''
#Layer1
conv1_weight = weight_tnn_bin(mappp['tnn_bin_conv1.tnn_bin_conv.weight'])
conv1_weight = conv1_weight.cpu().numpy()
conv1_weight = np.reshape(conv1_weight,(16*1*3,3))
np.savetxt('./float_parameters/conv1_weight.txt',conv1_weight,fmt='%2.0f',delimiter=',')
conv1_bias = mappp['tnn_bin_conv1.tnn_bin_conv.bias'].cpu().numpy()
np.savetxt('./float_parameters/conv1_bias.txt',conv1_bias,fmt='%2.10f',delimiter=',')
BN1_weight = mappp['tnn_bin_conv1.bn.weight'].cpu().numpy()
np.savetxt('./float_parameters/BN1_weight.txt',BN1_weight,fmt='%2.8f',delimiter=',')
BN1_bias = mappp['tnn_bin_conv1.bn.bias'].cpu().numpy()
np.savetxt('./float_parameters/BN1_bias.txt',BN1_bias,fmt='%2.8f',delimiter=',')
BN1_running_mean = mappp['tnn_bin_conv1.bn.running_mean'].cpu().numpy()
np.savetxt('./float_parameters/BN1_running_mean.txt',BN1_running_mean,fmt='%2.8f',delimiter=',')
BN1_running_var = mappp['tnn_bin_conv1.bn.running_var'].cpu().numpy()
np.savetxt('./float_parameters/BN1_running_var.txt',BN1_running_var,fmt='%4.8f',delimiter=',')
#Layer2
conv2_weight = weight_tnn_bin(mappp['tnn_bin_conv2.tnn_bin_conv.weight'])
conv2_weight = conv2_weight.cpu().numpy()
conv2_weight = np.reshape(conv2_weight,(32*16*3,3))
np.savetxt('./float_parameters/conv2_weight.txt',conv2_weight,fmt='%2.0f',delimiter=',')
conv2_bias = mappp['tnn_bin_conv2.tnn_bin_conv.bias'].cpu().numpy()
np.savetxt('./float_parameters/conv2_bias.txt',conv2_bias,fmt='%2.10f',delimiter=',')
BN2_weight = mappp['tnn_bin_conv2.bn.weight'].cpu().numpy()
np.savetxt('./float_parameters/BN2_weight.txt',BN2_weight,fmt='%2.8f',delimiter=',')
BN2_bias = mappp['tnn_bin_conv2.bn.bias'].cpu().numpy()
np.savetxt('./float_parameters/BN2_bias.txt',BN2_bias,fmt='%2.8f',delimiter=',')
BN2_running_mean = mappp['tnn_bin_conv2.bn.running_mean'].cpu().numpy()
np.savetxt('./float_parameters/BN2_running_mean.txt',BN2_running_mean,fmt='%2.8f',delimiter=',')
BN2_running_var = mappp['tnn_bin_conv2.bn.running_var'].cpu().numpy()
np.savetxt('./float_parameters/BN2_running_var.txt',BN2_running_var,fmt='%4.8f',delimiter=',')
#Layer3
conv3_weight = weight_tnn_bin(mappp['tnn_bin_conv3.tnn_bin_conv.weight'])
conv3_weight = conv3_weight.cpu().numpy()
conv3_weight = np.reshape(co