4

I'm stuck with restoring pre-trained network with 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/')

I'd like to use pre-trained network which was trained for face recognition, and then wanna add some layers for transfer learning. (I downloaded the model from here. https://github.com/davidsandberg/facenet)

When I execute the code above, it shows the error,

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)]]

I can't understand why the system can't find pre-trained data... And the directory structure is as below

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

model test.py

./model:

20170512-110547

./model/20170512-110547:

20170512-110547.pb

model-20170512-110547.ckpt-250000.index

model-20170512-110547.ckpt-250000.data-00000-of-00001

model-20170512-110547.meta

soshi shimada
  • 425
  • 1
  • 7
  • 21
  • Try an older version of tensorflow : `The saved meta_graph is possibly from an older release`. That model was built with r0.12 – Maxim Oct 14 '17 at 15:42
  • Thank you. I tried version 0.12 and 1.2.0(it's written in the requirement). but still shows the same error.... – soshi shimada Oct 14 '17 at 15:55
  • Try passing the full absolute path to the model directory when you call `saver.restore()` (rather than the relative path `'./model/20170512-110547/'`). Older versions of TensorFlow (including 0.12, I think) had a bug where they did not accept relative paths in some APIs, but this should have been fixed in the latest version. – mrry Oct 16 '17 at 15:35

2 Answers2

3

Import the .pb file.

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))

Linked questions:

Import a simple Tensorflow frozen_model.pb file and make prediction in C++

get the value weights from .pb file by Tensorflow

Related documentation: GraphDef

bartolo-otrit
  • 2,396
  • 3
  • 32
  • 50
0

You need use the ckpt path "./model/20170512-110547/model-20170512-110547.ckpt-250000" instead of the folder path.

hsc
  • 1,226
  • 11
  • 24