Created
August 13, 2020 04:06
-
-
Save Neelam96/e629c9a147054ed4c897f222f61af1f9 to your computer and use it in GitHub Desktop.
Code that I used to extract the weights from .npy file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
#ht width channels filter_nos : provided | |
#filter_nos channels h w : output | |
# file_open = open('mtcnn_layer_wise_wts.txt', 'w') | |
file_rnet_wts = open('mtcnn_rnet_wts.txt','w') | |
# file_onet_wts = open('mtcnn_onet_wts.txt','w') | |
def print4d(mat, file_open): | |
file_open.write("["+str(mat.shape[0])+"]"+"["+str(mat.shape[1])+"]"+"["+str(mat.shape[2])+"]"+"["+str(mat.shape[3])+"]"+" = "+"{") | |
for i in range(mat.shape[0]): | |
file_open.write("{") | |
for j in range(mat.shape[1]): | |
file_open.write("{") | |
for k in range(mat.shape[2]): | |
file_open.write("{") | |
for l in range(mat.shape[3]): | |
file_open.write(str(mat[i][j][k][l])) | |
if l!=mat.shape[3]-1: | |
file_open.write(",") | |
else: | |
file_open.write("}") | |
if k!=mat.shape[2]-1: | |
file_open.write(",") | |
else: | |
file_open.write("}") | |
if j!=mat.shape[1]-1: | |
file_open.write(",") | |
else: | |
file_open.write("}") | |
if i!=mat.shape[0]-1: | |
file_open.write(",") | |
else: | |
file_open.write("};\n") | |
def print1d(mat,file_open): | |
file_open.write("["+str(mat.shape[0])+"]"+" = {") | |
for i in range(len(mat)): | |
file_open.write(str(mat[i])) | |
if i!=len(mat) -1: | |
file_open.write(", ") | |
else: | |
file_open.write("};") | |
file_open.write("\n") | |
def print2d(mat,file_open): | |
file_open.write("["+str(mat.shape[0])+"]"+"["+str(mat.shape[1])+"]"+" = {") | |
for i in range(mat.shape[0]): | |
file_open.write('{') | |
for j in range(mat.shape[1]): | |
file_open.write(str(mat[i][j])) | |
if j!=mat.shape[1] -1: | |
file_open.write(", ") | |
else: | |
file_open.write("}") | |
if i!=mat.shape[0]-1: | |
file_open.write(", ") | |
else: | |
file_open.write("}") | |
file_open.write(";\n") | |
a = np.load('mtcnn_weights.npy', allow_pickle=True).tolist() | |
# # ###################################### o-net ################################# | |
# ################################# conv 1 #################################### | |
# onet_conv_3d_1=a['onet'][0] | |
# onet_conv_3d_1 = np.transpose(onet_conv_3d_1,(3,2,1,0)) | |
# file_onet_wts.write('conv1') | |
# print4d(onet_conv_3d_1,file_onet_wts) | |
# ############################## bias 1 ###################################### | |
# onet_bias_1 = a['onet'][1] | |
# print(onet_bias_1.shape) | |
# file_onet_wts.write('bias_1') | |
# print1d(onet_bias_1,file_onet_wts) | |
# ############################### prelu 1 #################################### | |
# onet_prelu1 = a['onet'][2] | |
# print(onet_prelu1.shape) | |
# file_onet_wts.write('prelu_1') | |
# onet_prelu1 = onet_prelu1.flatten() | |
# print1d(onet_prelu1,file_onet_wts) | |
# ################################# conv 2 #################################### | |
# onet_conv_3d_2=a['onet'][3] | |
# onet_conv_3d_2 = np.transpose(onet_conv_3d_2,(3,2,1,0)) | |
# file_onet_wts.write('conv2') | |
# print4d(onet_conv_3d_2,file_onet_wts) | |
# ############################## bias 2 ###################################### | |
# onet_bias_2 = a['onet'][4] | |
# print(onet_bias_2.shape) | |
# file_onet_wts.write('bias_2') | |
# print1d(onet_bias_2,file_onet_wts) | |
# ############################### prelu 2 #################################### | |
# onet_prelu2 = a['onet'][5] | |
# print(onet_prelu2.shape) | |
# file_onet_wts.write('prelu_2') | |
# onet_prelu2 = onet_prelu2.flatten() | |
# print1d(onet_prelu2,file_onet_wts) | |
# ################################# conv 3 #################################### | |
# onet_conv_3d_3=a['onet'][6] | |
# onet_conv_3d_3 = np.transpose(onet_conv_3d_3,(3,2,1,0)) | |
# file_onet_wts.write('conv3') | |
# print4d(onet_conv_3d_3,file_onet_wts) | |
# ############################## bias 3 ###################################### | |
# onet_bias_3 = a['onet'][7] | |
# print(onet_bias_3.shape) | |
# file_onet_wts.write('bias_3') | |
# print1d(onet_bias_3,file_onet_wts) | |
# ############################### prelu 3 #################################### | |
# onet_prelu3 = a['onet'][8] | |
# print(onet_prelu3.shape) | |
# file_onet_wts.write('prelu_1') | |
# onet_prelu3 = onet_prelu3.flatten() | |
# print1d(onet_prelu3,file_onet_wts) | |
# ################################# conv 4 #################################### | |
# onet_conv_3d_4=a['onet'][9] | |
# onet_conv_3d_4 = np.transpose(onet_conv_3d_4,(3,2,1,0)) | |
# file_onet_wts.write('conv4') | |
# print4d(onet_conv_3d_4,file_onet_wts) | |
# ############################## bias 4 ###################################### | |
# onet_bias_4 = a['onet'][10] | |
# print(onet_bias_4.shape) | |
# file_onet_wts.write('bias_4') | |
# print1d(onet_bias_4,file_onet_wts) | |
# ############################### prelu 4 #################################### | |
# onet_prelu4 = a['onet'][11] | |
# print(onet_prelu4.shape) | |
# file_onet_wts.write('prelu_4') | |
# onet_prelu4 = onet_prelu4.flatten() | |
# print1d(onet_prelu4,file_onet_wts) | |
# ############################## Dense-1-mat ################################### | |
# onet_dense_mat=a['onet'][12] | |
# onet_dense_mat = np.transpose(onet_dense_mat,(1,0)) | |
# print(onet_dense_mat.shape) | |
# file_onet_wts.write('dense_1_mat') | |
# print2d(onet_dense_mat,file_onet_wts) | |
# ########################### Dense-1-bias #################################### | |
# onet_dense_bias_1 = a['onet'][13] | |
# print(onet_dense_bias_1.shape) | |
# file_onet_wts.write('dense_1_bias') | |
# onet_dense_bias_1 = onet_dense_bias_1.flatten() | |
# print1d(onet_dense_bias_1,file_onet_wts) | |
# ############################### prelu 5 #################################### | |
# onet_prelu5 = a['onet'][14] | |
# print(onet_prelu5.shape) | |
# file_onet_wts.write('prelu_5') | |
# onet_prelu5 = onet_prelu5.flatten() | |
# print1d(onet_prelu5,file_onet_wts) | |
# ############################## Dense-2-mat ################################### | |
# onet_dense_mat2=a['onet'][15] | |
# onet_dense_mat2 = np.transpose(onet_dense_mat2,(1,0)) | |
# print(onet_dense_mat2.shape) | |
# file_onet_wts.write('dense_2_mat') | |
# print2d(onet_dense_mat2,file_onet_wts) | |
# ########################### Dense-2-bias #################################### | |
# onet_dense_bias_2 = a['onet'][16] | |
# print(onet_dense_bias_2.shape) | |
# file_onet_wts.write('dense_2_bias') | |
# onet_dense_bias_2 = onet_dense_bias_2.flatten() | |
# print1d(onet_dense_bias_2,file_onet_wts) | |
# ############################## Dense-3-mat ################################### | |
# onet_dense_mat3=a['onet'][17] | |
# onet_dense_mat3 = np.transpose(onet_dense_mat3,(1,0)) | |
# print(onet_dense_mat3.shape) | |
# file_onet_wts.write('dense_3_mat') | |
# print2d(onet_dense_mat3,file_onet_wts) | |
# ########################### Dense-3-bias #################################### | |
# onet_dense_bias_3 = a['onet'][18] | |
# print(onet_dense_bias_3.shape) | |
# file_onet_wts.write('dense_3_bias') | |
# onet_dense_bias_3 = onet_dense_bias_3.flatten() | |
# print1d(onet_dense_bias_3,file_onet_wts) | |
# ############################## Dense-4-mat ################################### | |
# onet_dense_mat4=a['onet'][19] | |
# onet_dense_mat4 = np.transpose(onet_dense_mat4,(1,0)) | |
# print(onet_dense_mat4.shape) | |
# file_onet_wts.write('dense_4_mat') | |
# print2d(onet_dense_mat4,file_onet_wts) | |
# ########################### Dense-4-bias #################################### | |
# onet_dense_bias_4 = a['onet'][20] | |
# print(onet_dense_bias_4.shape) | |
# file_onet_wts.write('dense_4_bias') | |
# onet_dense_bias_4 = onet_dense_bias_4.flatten() | |
# print1d(onet_dense_bias_4,file_onet_wts) | |
# ################################## weights for o-net ends #################### | |
# ###################################### r-net ################################### | |
# ################################# conv 1 #################################### | |
# rnet_conv_3d_1=a['rnet'][0] | |
# rnet_conv_3d_1 = np.transpose(rnet_conv_3d_1,(3,2,1,0)) | |
# file_rnet_wts.write('conv1') | |
# print4d(rnet_conv_3d_1,file_rnet_wts) | |
# print(rnet_conv_3d_1.shape) | |
# ############################## bias 1 ###################################### | |
# rnet_bias_1 = a['rnet'][1] | |
# print(rnet_bias_1.shape) | |
# file_rnet_wts.write('bias_1') | |
# print1d(rnet_bias_1,file_rnet_wts) | |
# ############################### prelu 1 #################################### | |
# rnet_prelu1 = a['rnet'][2] | |
# print(rnet_prelu1.shape) | |
# file_rnet_wts.write('prelu_1') | |
# rnet_prelu1 = rnet_prelu1.flatten() | |
# print1d(rnet_prelu1,file_rnet_wts) | |
# ############################# conv2 ###################################### | |
# rnet_conv_3d_2=a['rnet'][3] | |
# rnet_conv_3d_2 = np.transpose(rnet_conv_3d_2,(3,2,1,0)) | |
# file_rnet_wts.write('conv2') | |
# print4d(rnet_conv_3d_2,file_rnet_wts) | |
# print(rnet_conv_3d_2.shape) | |
# ############################## bias 2 ###################################### | |
# rnet_bias_2 = a['rnet'][4] | |
# print(rnet_bias_2.shape) | |
# file_rnet_wts.write('bias_2') | |
# print1d(rnet_bias_2,file_rnet_wts) | |
# ############################ prelu 2 #################################### | |
# rnet_prelu2 = a['rnet'][5] | |
# print(rnet_prelu2.shape) | |
# file_rnet_wts.write('prelu_2') | |
# rnet_prelu2 = rnet_prelu2.flatten() | |
# print1d(rnet_prelu2,file_rnet_wts) | |
# ############################# conv3 ###################################### | |
# rnet_conv_3d_3=a['rnet'][6] | |
# rnet_conv_3d_3 = np.transpose(rnet_conv_3d_3,(3,2,1,0)) | |
# file_rnet_wts.write('conv3') | |
# print4d(rnet_conv_3d_3,file_rnet_wts) | |
# print(rnet_conv_3d_3.shape) | |
# ############################## bias 3 ###################################### | |
# rnet_bias_3 = a['rnet'][7] | |
# print(rnet_bias_3.shape) | |
# file_rnet_wts.write('bias_3') | |
# print1d(rnet_bias_3,file_rnet_wts) | |
# ############################ prelu 3 #################################### | |
# rnet_prelu3 = a['rnet'][8] | |
# print(rnet_prelu3.shape) | |
# file_rnet_wts.write('prelu_3') | |
# rnet_prelu3 = rnet_prelu3.flatten() | |
# print1d(rnet_prelu3,file_rnet_wts) | |
# ############################## Dense-1-mat ################################### | |
rnet_dense_mat=a['rnet'][9] | |
print(rnet_dense_mat.shape) | |
file_rnet_wts.write('dense_1_mat') | |
print2d(rnet_dense_mat,file_rnet_wts) | |
# ########################### Dense-1-bias #################################### | |
# rnet_dense_bias = a['rnet'][10] | |
# print(rnet_dense_bias.shape) | |
# file_rnet_wts.write('dense_1_bias') | |
# rnet_dense_bias = rnet_dense_bias.flatten() | |
# print1d(rnet_dense_bias,file_rnet_wts) | |
# ############################ prelu 4 #################################### | |
# rnet_prelu4 = a['rnet'][11] | |
# print(rnet_prelu4.shape) | |
# file_rnet_wts.write('prelu_4') | |
# rnet_prelu4 = rnet_prelu4.flatten() | |
# print1d(rnet_prelu4,file_rnet_wts) | |
# ############################## Dense 2a-mat ################################### | |
# rnet_dense_2a_mat=a['rnet'][12] | |
# print('rnet dense 2a mat shape: ',rnet_dense_2a_mat.shape) | |
# file_rnet_wts.write('rnet_dense_2a_mat') | |
# print2d(rnet_dense_2a_mat,file_rnet_wts) | |
# ########################### Dense-2a-bias #################################### | |
# rnet_dense_2a_bias = a['rnet'][13] | |
# print(rnet_dense_2a_bias.shape) | |
# file_rnet_wts.write('rnet_dense_2a_bias') | |
# rnet_dense_2a_bias = rnet_dense_2a_bias.flatten() | |
# print1d(rnet_dense_2a_bias,file_rnet_wts) | |
# ############################## Dense 2b-mat ################################### | |
# rnet_dense_2b_mat=a['rnet'][14] | |
# print('rnet dense 2b mat shape: ',rnet_dense_2b_mat.shape) | |
# file_rnet_wts.write('rnet_dense_2b_mat') | |
# print2d(rnet_dense_2b_mat,file_rnet_wts) | |
# ########################### Dense-2b-bias #################################### | |
# rnet_dense_2b_bias = a['rnet'][15] | |
# print(rnet_dense_2b_bias.shape) | |
# file_rnet_wts.write('rnet_dense_2b_bias') | |
# rnet_dense_2b_bias = rnet_dense_2b_bias.flatten() | |
# print1d(rnet_dense_2b_bias,file_rnet_wts) | |
# ##################################### 1st conv layer ####################### | |
# conv_2d_1 = a['pnet'][0] | |
# conv_2d_1 = np.transpose(conv_2d_1,(3,2,1,0)) | |
# file_open.write('conv1') | |
# print4d(conv_2d_1,file_open) | |
# print(conv_2d_1.shape) | |
# ############################################################################ | |
# ##################################### bias conv1 layer ####################### | |
# bias_1 = a['pnet'][1] | |
# print(bias_1.shape) | |
# file_open.write('bias_1') | |
# print1d(bias_1,file_open) | |
# ############################################################################ | |
# ##################################### prelu 1 layer ####################### | |
# prelu1 = a['pnet'][2] | |
# print(prelu1.shape) | |
# file_open.write('prelu_1') | |
# prelu1 = prelu1.flatten() | |
# print1d(prelu1,file_open) | |
# ############################################################################ | |
# ##################################### 2nd conv layer ####################### | |
# conv_2d_2 = a['pnet'][3] | |
# conv_2d_2 = np.transpose(conv_2d_2,(3,2,1,0)) | |
# file_open.write('conv2') | |
# print4d(conv_2d_2,file_open) | |
# print(conv_2d_2.shape) | |
# ############################################################################ | |
# ##################################### bias conv2 layer ####################### | |
# bias_2 = a['pnet'][4] | |
# print(bias_2.shape) | |
# file_open.write('bias_2') | |
# print1d(bias_2,file_open) | |
# ############################################################################ | |
# ##################################### prelu 2 layer ####################### | |
# prelu2 = a['pnet'][5] | |
# print(prelu2.shape) | |
# file_open.write('prelu_2') | |
# prelu2 = prelu2.flatten() | |
# print1d(prelu2,file_open) | |
# ############################################################################ | |
# ##################################### 3rd conv layer ####################### | |
# conv_2d_3 = a['pnet'][6] | |
# conv_2d_3 = np.transpose(conv_2d_3,(3,2,1,0)) | |
# file_open.write('conv3') | |
# print4d(conv_2d_3,file_open) | |
# print(conv_2d_3.shape) | |
# ############################################################################ | |
# ##################################### bias conv3 layer ####################### | |
# bias_3 = a['pnet'][7] | |
# print(bias_3.shape) | |
# file_open.write('bias_3') | |
# print1d(bias_3,file_open) | |
# ############################################################################ | |
# ##################################### prelu 3 layer ####################### | |
# prelu3 = a['pnet'][8] | |
# print(prelu3.shape) | |
# file_open.write('prelu_3') | |
# prelu3 = prelu3.flatten() | |
# print1d(prelu3,file_open) | |
# ############################################################################ | |
# ##################################### 4a conv layer ####################### | |
# conv_2d_4a = a['pnet'][9] | |
# conv_2d_4a = np.transpose(conv_2d_4a,(3,2,1,0)) | |
# file_open.write('conv4a') | |
# print4d(conv_2d_4a,file_open) | |
# print(conv_2d_4a.shape) | |
# ############################################################################ | |
# ##################################### bias conv4a layer ####################### | |
# bias_4a = a['pnet'][10] | |
# print(bias_4a.shape) | |
# file_open.write('bias_4a') | |
# print1d(bias_4a,file_open) | |
# ############################################################################ | |
# ##################################### 4b conv layer ####################### | |
# conv_2d_4b = a['pnet'][11] | |
# conv_2d_4b = np.transpose(conv_2d_4b,(3,2,1,0)) | |
# file_open.write('conv4b') | |
# print4d(conv_2d_4b,file_open) | |
# print(conv_2d_4b.shape) | |
# ############################################################################ | |
# ##################################### bias conv4b layer ####################### | |
# bias_4b = a['pnet'][12] | |
# print(bias_4b.shape) | |
# file_open.write('bias_4b') | |
# print1d(bias_4b,file_open) | |
# ############################################################################ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment