如何加载训练的张量流模型

时间:2017-08-17 20:20:18

标签: python tensorflow jupyter-notebook

我在加载张量流模型以测试一些新数据时遇到各种麻烦。当我训练模型时,我使用了这个:

save_model_file = 'my_saved_model'
saver = tf.train.Saver()
save_path = saver.save(sess, save_model_file)

这似乎导致创建以下文件:

my_saved_model.meta
checkpoint
my_saved_model.index
my_saved_model.data-00000-of-00001

我不知道我应该注意哪些文件。

现在模型已经过训练,我似乎无法加载或使用它而不会抛出异常。这就是我在做的事情:

def neural_net_data_input(data_shape):
    theshape=(None,)+tuple(data_shape)
    return tf.placeholder(tf.float32,shape=theshape,name='x')

def neural_net_label_input(n_out):
    return tf.placeholder(tf.float32,shape=(None,n_out),name='one_hot_labels')

def neural_net_keep_prob_input(): 
    return tf.placeholder(tf.float32,name='keep_prob')

def do_generate_network(x):
    #
    # here is where i generate the network layer by layer.
    # this code works fine so i am not showing it here
    #
    pass

#
# Now I want to restore the model
#
tf.reset_default_graph()

input_data_shape=(32,32,1)
final_num_outputs=43

graph1 = tf.Graph()
with graph1.as_default():
    x = neural_net_data_input(input_data_shape)
    one_hot_labels = neural_net_label_input(final_num_outputs)
    keep_prob=neural_net_keep_prob_input()
    logits = do_generate_network(x)
    # Name logits Tensor, so that is can be loaded from disk after training
    logits = tf.identity(logits, name='logits')
    #
    # accuracy: we use this for validation testing
    #
    correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_labels, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')

################################
# Evaluate
################################

new_data=myutils.load_pickle_file(SOME_DATA_FILE_NAME)
new_features=new_data['features']
new_one_hot_labels=new_data['labels']

print('Evaluating on new data...')
with tf.Session(graph=graph1) as sess:
    # Initializing the variables
    sess.run(tf.global_variables_initializer())

    saver.restore(sess,save_model_file)
    new_acc = sess.run(accuracy, feed_dict={x: new_features, one_hot_labels: new_one_hot_labels, keep_prob: 1.})
    print('Testing Accuracy For New Images: {}'.format(new_acc))

但是当我这样做时,我明白了:

TypeError: Cannot interpret feed_dict key as Tensor: The name 'save/Const:0' refers to a Tensor which does not exist. The operation, 'save/Const', does not exist in the graph.

所以,我尝试在会话中移动我的图形,如下所示:

################################
# Evaluate
################################

print('Evaluating on web data...')
with tf.Session() as sess:

    x = neural_net_data_input(input_data_shape)
    one_hot_labels = neural_net_label_input(final_num_outputs)
    keep_prob=neural_net_keep_prob_input()
    logits = do_generate_network(x)
    # Name logits Tensor, so that is can be loaded from disk after training
    logits = tf.identity(logits, name='logits')
    #
    # accuracy: we use this for validation testing
    #
    correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_labels, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')

    sess.run(tf.global_variables_initializer())

    my_save_dir="/home/carnd/CarND-Traffic-Sign-Classifier-Project"
    load_model_meta_file=os.path.join(my_save_dir,"my_saved_model.meta")
    load_model_path=os.path.join(my_save_dir,"my_saved_model")
    new_saver = tf.train.import_meta_graph(load_model_meta_file)
    new_saver.restore(sess, load_model_path)

    web_acc = sess.run(accuracy, feed_dict={x: web_features, one_hot_labels: web_one_hot_labels, keep_prob: 1.})
    print('Testing Accuracy For Web Images: {}'.format(web_acc))

现在它运行时没有抛出错误,但它打印的精度结果是0.02!我正在提供相同的数据,在训练期间,我的准确率达到了95%。所以看来我在某种程度上错误地加载了我的模型。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

加载训练模型的步骤:

  1. 加载图表 :    您可以使用tf.train.import_meta_graph()加载图表。示例代码为:

    model_path = "my_saved_model"
    inference_graph = tf.Graph()
    with tf.Session(graph= inference_graph) as sess:
       # Load the graph with the trained states
      loader = tf.train.import_meta_graph(model_path+'.meta')
      loader.restore(sess, model_path)
    
  2. 获取张量: 使用get_tensor_by_name()获取推理需要进行推理。因此,在您的模型中,请确保按名称命名张量,以便在推理期间调用它。

      #Get the tensors by their variable name 
    
      _accuracy = inference_graph.get_tensor_by_name('accuracy:0')
      _x  = inference_graph get_tensor_by_name('x:0')
      _y  = inference_graph.get_tensor_by_name('y:0')
    
  3. 测试: 可以使用加载的张量来完成。 sess.run(_accuracy, feed_dict={_x: ... , _y:...}