Skip to content

Tensorflow FaceNet Conversion

Jiahao Yao edited this page Jun 4, 2018 · 1 revision

Tensorflow FaceNet Conversion

FaceNet pretrained model can be download from the project page. However, if used directly, MMdnn might not work because of tf.cond, namely control flow in tensorflow.

One practical Approach

First, download the inception resnet v1 code as well as pretrained model from project page.

Second, write the script below to reload the model and transform the graph.

📄 bottleneck_layer_size=512 is consistent to the pretrained model

📄 phase_train=False can simply the batchnorm layer in conversion

⚠️ We only convert the main body of the FaceNet, a.k.a inception-resnet-v2. One might add something else to reach the effect of FaceNet.

import tensorflow as tf
import inception_resnet_v1

data_input = tf.placeholder(name='input', dtype=tf.float32, shape=[None, 299, 299, 3])

output, _ = inception_resnet_v1.inference(data_input, keep_probability=0.8, phase_train=False, bottleneck_layer_size=512)

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    saver = tf.train.Saver()
    saver.restore(sess, '/Users/kit/Downloads/20180408-102900/model-20180408-102900.ckpt-90')
    path = '/Users/kit/Downloads/'
    save_path = saver.save(sess, path + "imagenet_facenet.ckpt" )
    print("Model saved in file: %s" % save_path)

Then, you can use the mmdnn converter according to MMdnn tensorflow tutorial.

imagenet_inception_resnet_v2.ckpt.data-00000-of-00001
imagenet_inception_resnet_v2.ckpt.index
imagenet_inception_resnet_v2.ckpt.meta