2017-10-14 246 views
0

我已经训练我的CNN模型,并将其存储在一个名为model它包含文件的目录如下图所示如何恢复训练模型和计算测试精度Tensorflow

\model 
|--- checkpoint 
|--- model.data-00000-of-00001 
|--- model.index 
|--- model.meta 

我要还原的模型和计算测试准确度,我用下面的代码

import tensorflow as tf 
import numpy as np 
import cv2 
import os 
import glob 

images = [] 
labels = [] 
img_names = [] 
cls  = [] 

test_path = 'data\\cifar-10\\test' 
image_size = 32 
num_channels = 3 

# Prepare input data 
with open('data\\cifar-10\\wnids.txt') as f: 
    classes = f.readlines() 
classes = [x.strip() for x in classes] 

num_classes = len(classes) 

for fields in classes: 
    index = classes.index(fields) 
    print('Read {} files (Index: {})'.format(fields, index)) 
    path = os.path.join(test_path, fields, '*g') 
    files = glob.glob(path) 
    for fl in files: 
     image = cv2.imread(fl) 
     image = cv2.resize(image, (image_size, image_size),0,0, cv2.INTER_LINEAR) 
     image = image.astype(np.float32) 
     image = np.multiply(image, 1.0/255.0) 
     images.append(image) 
     label = np.zeros(len(classes)) 
     label[index] = 1.0 
     labels.append(label) 
     flbase = os.path.basename(fl) 
     img_names.append(flbase) 
     cls.append(fields) 

images = np.array(images) 
labels = np.array(labels) 
img_names = np.array(img_names) 
cls  = np.array(cls) 

session = tf.Session() 
tf_saver = tf.train.import_meta_graph('model\\model.meta') 
tf_saver.restore(session, tf.train.latest_checkpoint('model')) 

x  = tf.placeholder(tf.float32, shape=[None, image_size, image_size, num_channels], name='x') 
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true') 
y_true_cls = tf.argmax(y_true, axis=1) 

y_pred  = tf.nn.softmax(layer_fc2, name='y_pred') 
y_pred_cls = tf.argmax(y_pred, axis=1) 

correct_prediction = tf.equal(y_pred_cls, y_true_cls) 
accuracy   = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

feed_dict_test = {x: images, y_true: labels} 

test_acc = session.run(accuracy, feed_dict=feed_dict_test) 

msg  = "Test Accuracy: {1:>6.1%}" 
print(msg.format(test_acc)) 

在运行上面的代码中,我发现了错误

NameError: name 'layer_fc2' is not defined

如何正确恢复模型并计算测试精度。

其中包含的代码用于训练模型

原始文件train.py

+0

在使用它之前,你不需要创建fc层吗? – jdv

回答

0

layer_fc2是在你的训练脚本中定义的Python变量(其中定义图),并在这里是不存在的。你需要做的是找到这个图层。不幸的是,你没有在火车时间里给它起名字。您create_fc_layer代码更改为

def create_fc_layer(input, num_inputs, num_outputs, name, use_relu=True): 
    weights = create_weights(shape=[num_inputs, num_outputs]) 
    biases = create_biases(num_outputs) 
    layer = tf.matmul(input, weights) + biases 
    if use_relu: 
    layer = tf.nn.relu(layer) 

    return tf.identity(layer, name=name) # return a named layer 

... 

layer_fc2 = create_fc_layer(input=layer_fc1, num_inputs=fc_layer_size, num_outputs=num_classes, name='layer_fc2', use_relu=False) 

在此之后在新的脚本:

layer_fc2 = session.graph.get_operation_by_name('layer_fc2') 

顺便说一句,你也不需要重新定义y_predy_pred_cls等给他们的名字和简单地得到它从恢复的图形。

+0

感谢您的回复。但我在修改后出现错误,如您所建议 'KeyError:'名称'layer_fc2'是指操作不在图中。'' –

+0

请参阅上述更新的注释 –

+0

正确。我想你没有重新训练模型。所以你现在的模型没有'layer_fc2'的名字。它获得了一些内部名称,如'Relu:0'。你可以尝试使用它,但它很容易出错。我建议你改变你的训练脚本,*重新训练*一个新的模型,其中所需的操作命名正确,并使用它 – Maxim