提问者:小点点

如何用tensorflow中实现的facenet使用自己训练好的模型?


我使用 shell 命令训练模型:

python src/facenet_train.py \
           --batch_size 15 \
           --gpu_memory_fraction 0.25 \
           --models_base_dir trained_model_2017_05_15_10_24 \
           --pretrained_model trained_model_2017_05_15_10_24/20170515-121856/model-20170515-121856.ckpt-182784 \
           --model_def models.nn2 \
           --logs_base_dir logs \
           --data_dir /data/user_set/training/2017_05_15_10_24 \
           --lfw_pairs /data/user_set/lfw_pairs.txt \
           --image_size 224 \
           --lfw_dir /data/user_set/lfw \
           --optimizer ADAM \
           --max_nrof_epochs 1000 \
           --learning_rate 0.00001

但当使用我自己训练的模型时,我会得到如下错误信息:

2017-05-17 14:23:05.448285:W tensorflow/core/platform/cpu_feature_guard.cc:45] TensorFlow 库未编译为 使用 SSE4.1 指令,但这些指令在您的计算机上可用,可以加快 CPU 计算速度。2017-05-17 14:23:05.448318:W tensorflow/core/platform/cpu_feature_guard.cc:45] TensorFlow 库未编译为 使用 SSE4.2 指令,但这些指令在您的计算机上可用,可以加快 CPU 计算速度。2017-05-17 14:23:05.448324:W tensorflow/core/platform/cpu_feature_guard.cc:45] TensorFlow 库未编译为 使用 AVX 指令,但这些指令在您的机器上可用,可以加快 CPU 计算速度。2017-05-17 14:23:05.448329:W tensorflow/core/platform/cpu_feature_guard.cc:45] TensorFlow 库未编译为 使用 AVX2 指令,但这些指令在您的机器上可用,可以加快 CPU 计算速度。2017-05-17 14:23:05.448334:W tensorflow/core/platform/cpu_feature_guard.cc:45] TensorFlow 库未编译为 使用 FMA 指令,但这些指令在您的计算机上可用,可以加快 CPU 计算速度。2017-05-17 14:23:05.674872:I tensorflow/core/common_runtime/gpu/gpu_device.cc:887] 找到具有属性的设备 0:名称:Quadro M4000 主要:5 次要:2 内存时钟速率 (GHz) 0.7725 pciBusID 0000:03:00.0 总内存:7.93GiB 可用内存:2.89GiB 2017-05-17 14:23:05.674917: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0 2017-05-17 14:23:05.674935: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0: Y 2017-05-17 14:23:05.674957:I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] 创建 TensorFlow 设备 (/gpu:0) -

获取特征码:

import tensorflow as tf
import facenet
w_MODEL_PATH_='/home/chen/demo_dir/facenet_tensorflow_train/trained_model_2017_05_15_10_24/20170515-121856'

with tf.Graph().as_default():
    with tf.Session() as sess:
        # load the model
        meta_file, ckpt_file = facenet.get_model_filenames(w_MODEL_PATH_)
        facenet.load_model(w_MODEL_PATH_, meta_file, ckpt_file)
        # print("model_path:", w_MODEL_PATH_,"meta_file:", meta_file,"ckpt_file:", ckpt_file)

        # Get input and output tensors
        # ops = tf.get_default_graph().get_operations()
        #
        # print(ops)

        images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
        embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
        phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

        image_size = images_placeholder.get_shape()[1]
        embedding_size = embeddings.get_shape()[1]

        # print(image_size)

        paths = ['one.png', 'two.png']

        # Run forward pass to calculate embeddings
        images = facenet.load_data(paths, do_random_crop=False, do_random_flip=False, image_size=image_size,
                                   do_prewhiten=True)
        # print("images:", idx, images)
        feed_dict = {images_placeholder: images, phase_train_placeholder: False}
        # print(idx,"embeddings:", embeddings)
        emb_array = sess.run(embeddings, feed_dict=feed_dict)
        # print(idx, "emb_array:", emb_array)
        print(emb_array)

我不知道如何使用我自己训练的模型,请帮忙。


共1个答案

匿名用户

如果你说的是最后一部分,那么用这段代码来看看你的模型有哪些操作。

for i in tf.get_default_graph().get_operations():
    print(i.name)

如果您谈论的是优化。

您收到此错误是因为您需要在自己的机器上编译张量流。这很容易做到。

您可以阅读留档以获取完整的选项列表,但基本上您需要执行几个步骤。

https://www.tensorflow.org/install/install_sources

  1. git clone tenorflow
  2. 存储库安装
  3. bazel一个ensorflow构建系统
  4. 配置tenorflow
  5. 构建ensorflow
  6. 如果您使用的是python,请在您的环境中安装tenorflow,如果是anaconda或Virtual alenv

也就是说,当然还需要安装其他必需的库。这在Ubuntu上很容易做到。

或者,如果您使用的是anaconda,您可以尝试tensorflow gpu的conda forge版本,但我无法验证它是否也使用针对您的cpu的优化进行编译。

https://conda-forge.org/

  1. 安装蟒蛇
  2. 添加conda forge repo url
  3. 更新conda
  4. 安装ensorflow-gpu