4

我坚持使用 Tensorflow 恢复预训练的网络......

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

sess=tf.Session()
saver = tf.train.import_meta_graph('./model/20170512-110547/model-20170512-110547.meta')
saver.restore(sess,'./model/20170512-110547/')

我想使用经过训练的人脸识别预训练网络,然后想添加一些层进行迁移学习。(我从这里下载了模型。https://github.com/davidsandberg/facenet

当我执行上面的代码时,它显示错误,

WARNING:tensorflow:The saved meta_graph is possibly from an older release:
'model_variables' collection should be of type 'byte_list', but instead is of type 'node_list'.
Traceback (most recent call last):
  File "/Users/user/Desktop/desktop/Python/HCR/Transfer_face/test.py", line 7, in <module>
    saver.restore(sess,'./model/20170512-110547/')
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1560, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run
    run_metadata_ptr)
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run
    feed_dict_tensor, options, run_metadata)
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
    options, run_metadata)
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ./model/20170512-110547/
     [[Node: save/RestoreV2_491 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_491/tensor_names, save/RestoreV2_491/shape_and_slices)]]

Caused by op u'save/RestoreV2_491', defined at:
  File "/Users/user/Desktop/desktop/Python/HCR/Transfer_face/test.py", line 6, in <module>
    saver = tf.train.import_meta_graph('./model/20170512-110547/model-20170512-110547.meta')
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1698, in import_meta_graph
    **kwargs)
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/meta_graph.py", line 656, in import_scoped_meta_graph
    producer_op_list=producer_op_list)
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/importer.py", line 313, in import_graph_def
    op_def=op_def)
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Users/user/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

NotFoundError (see above for traceback): Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ./model/20170512-110547/
     [[Node: save/RestoreV2_491 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_491/tensor_names, save/RestoreV2_491/shape_and_slices)]]

我不明白为什么系统找不到预训练的数据......目录结构如下

USER-no-MacBook-Pro:Transfer_face user$ ls -R

模型测试.py

。/模型:

20170512-110547

./model/20170512-110547:

20170512-110547.pb

型号-20170512-110547.ckpt-250000.index

型号-20170512-110547.ckpt-250000.data-00000-of-00001

型号-20170512-110547.meta

4

2 回答 2

3

导入 .pb 文件。

import tensorflow as tf
from tensorflow.python.framework import tensor_util

with tf.gfile.GFile('20170512-110547.pb', "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

#import into default graph
tf.import_graph_def(graph_def)

#print some data
wts = [n for n in graph_def.node if n.op == 'Const']

for n in wts:
    print(tensor_util.MakeNdarray(n.attr['value'].tensor))

相关问题:

导入一个简单的 Tensorflow frozen_model.pb 文件并在 C++ 中进行预测

通过 Tensorflow 从 .pb 文件中获取权重

相关文档:GraphDef

于 2017-11-29T16:15:24.507 回答
0

您需要使用 ckpt 路径“./model/20170512-110547/model-20170512-110547.ckpt-250000”而不是文件夹路径。

于 2018-03-05T08:42:41.023 回答