0%

Extract kernel from tensorflow pretrained model

Tensor to numpy array

In Tensorflow, kernel (tf.Variable) can be easily accessed by slim.get_model_variables() (return a list of tf.Variable)if using slim package. Also, if we want to access corresponding

  • name: simply by .name
  • value: sess.run
1
2
3
4
5
6
7
   weights = slim.get_model_variables()
#sturcture: list of variable name
structure = []
for i in range(len(weights)):
structure.append(weights[i].name)
#weights: list of variable value(numpy array)
weights = self.sess.run(weights,feed_dict={})

Numpy array to file

BAD TRY: numpy.savetxt

Extracted numpy array is usually multi-dimentional, therefore numpy.savetxt is not applicable, because it requires a 1-d or 2-d array.

BAD TRY: numpy.save

pretty easy to use, but only applicable when we only need to load the file using Python, but we need to implement a C code in our project

SUCCESSFUL: scipy.io.loadmat

numpy to .mat file, need a dic to generate the file, therefore we need the name of tf.variable above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np
import scipy.io
class extractor:
def __init__(self,filepath):
self.filepath = filepath
def tensor_to_file(self,tensor_list,structure):
mat_dict = {}
for i in range(len(tensor_list)):
mat_dict.update({structure[i]:tensor_list[i]})
scipy.io.savemat(self.filepath,mdict=mat_dict)
print '.mat file has been saved.'
def file_to_tensor(self):
tensor_dic = scipy.io.loadmat(self.filepath)
print '.mat file has been loaded.'
return tensor_dic