본문 바로가기

Deep Learning

[tensorflow] how to save the filter weights of the trained network as matfile

반응형

** 학습한 network를 읽어서 mat 파일 형식으로 저장하기 **


** transfer training


In this post, I will show you how to save the filter weights of the trained network as mat file format.

First, you need to read the following post.


2017/07/18 - [Deep Learning] - [tensorflow] how to save trained network

2017/07/18 - [Deep Learning] - [tensorflow] how to freeze trained network (make one pb file)

2017/07/18 - [Deep Learning] - [tensorflow] how to load and use the saved trained network in python



Before you load the saved trained network, you need to know the name of the tensors defined in your network.

For example, if you define a convolutional filter with the name 'w', the name of the filter may be 'w:0'

# the name of 'conv_filter' may 'w:0'
initial = tf.truncated_normal(shape, stddev=stddev_xavier)
conv_filter = tf.Variable(initial, name='w')

If you define the filter under name scope, the name may be 'name_scope/w:0'



Next, put all the names of the filters (trainable variables) into a dictionary.

# dictionary that includes all the filters of vggnet
layers = (
'layer1/cons11w:0', 'layer1/cons11b:0',
'layer1/cons12w:0', 'layer1/cons12b:0',
'layer2/cons21w:0', 'layer2/cons21b:0',
'layer2/cons22w:0', 'layer2/cons22b:0',
'layer3/cons31w:0', 'layer3/cons31b:0',
'layer3/cons32w:0', 'layer3/cons32b:0',
'layer3/cons33w:0', 'layer3/cons33b:0',
'layer4/cons41w:0', 'layer4/cons41b:0',
'layer4/cons42w:0', 'layer4/cons42b:0',
'layer4/conv43w:0', 'layer4/conv43b:0',
'layer5/cons51w:0', 'layer5/cons51b:0',
'layer5/cons52w:0', 'layer5/cons52b:0',
'layer5/conv53w:0', 'layer5/conv53b:0',
'layer6/fcl_6w:0', 'layer6/fcl_6b:0',
'layer7/fcl_7w:0', 'layer7/fcl_7b:0',
'layer8/fcl_8w:0', 'layer8/fcl_8b:0',
'layer9/outpw:0', 'layer9/outpb:0'
)


finally, using the names in the dictionary, 1) load the tensor with the name, 2) run the tensor in order to make it numpy arrary, and 3) save the numpy arrary in a new dictionary.

# dictionary for saving vgg_net
vgg_net = {}

with tf.Session() as sess:

# Step1) Initialize global variables
sess.run(tf.global_variables_initializer())

# Step2) Load graph from meta file (meta file contains the graph that I had defined before)
saver = tf.train.import_meta_graph(path_to_net + 'saved_checkpoint-0.meta')

# Step3) Restore all the weights values
saver.restore(sess, path_to_net + 'saved_checkpoint-0')
print('Trained Deep Network is restored')

# Step4) get all the trainable variables
graph = tf.get_default_graph()

for i, name in enumerate(layers):
vgg_net[name[7:]] = sess.run(graph.get_tensor_by_name(name))

# save as matfile
scipy.io.savemat( path_to_net + 'vgg_net', vgg_net)

sess.close()

To save the dictionary as mat file, I use scipy.io.savemat function.