2017-07-27 31 views
1

我与tensorflow卷积神经网络,我训练它,测试它(约98%的准确率)...我保存为什么我在这个神经网络(tensorflow)中得到低精度?

saver = tf.train.Saver() 
saver.save(sess, 'model.ckpt') 

那么模型我金丹恢复,但我总是得到低于50%的准确度......为什么? 下面的代码:

import tensorflow as tf 
import matplotlib.pyplot as plt 
import pickle 
import numpy as np 

with open('X_train.pickle', 'rb') as y: 
    u = pickle._Unpickler(y) 
    u.encoding = 'latin1' 
    X_train = u.load() 

with open('X_test.pickle', 'rb') as y: 
    u = pickle._Unpickler(y) 
    u.encoding = 'latin1' 
    X_test = u.load() 
    X_test = np.array(X_test).reshape(-1, 2500) 

with open('y_train.pickle', 'rb') as y: 
    u = pickle._Unpickler(y) 
    u.encoding = 'latin1' 
    y_train = u.load() 

with open('y_test.pickle', 'rb') as y: 
    u = pickle._Unpickler(y) 
    u.encoding = 'latin1' 
    y_test = u.load() 

n_classes = 3 
batch_size = 100 

x = tf.placeholder('float', [None, 2500]) 
y = tf.placeholder('float') 

keep_rate = 0.8 
keep_prob = tf.placeholder(tf.float32) 

def conv2d(x, W): 
    return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') 

def maxpool2d(x): 
    #      size of window   movement of window 
    return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') 



def convolutional_neural_network(x): 
    weights = {'W_conv1':tf.Variable(tf.random_normal([5,5,1,32])), 
       'W_conv2':tf.Variable(tf.random_normal([5,5,32,64])), 
       'W_fc':tf.Variable(tf.random_normal([13*13*64,1024])), 
       'out':tf.Variable(tf.random_normal([1024, n_classes]))} 

    biases = {'b_conv1':tf.Variable(tf.random_normal([32])), 
       'b_conv2':tf.Variable(tf.random_normal([64])), 
       'b_fc':tf.Variable(tf.random_normal([1024])), 
       'out':tf.Variable(tf.random_normal([n_classes]))} 

    x = tf.reshape(x, shape=[-1, 50, 50, 1]) 

    conv1 = tf.nn.relu(conv2d(x, weights['W_conv1']) + biases['b_conv1']) 
    conv1 = maxpool2d(conv1) 

    conv2 = tf.nn.relu(conv2d(conv1, weights['W_conv2']) + biases['b_conv2']) 
    conv2 = maxpool2d(conv2) 

    fc = tf.reshape(conv2,[-1, 13*13*64]) 
    fc = tf.nn.relu(tf.matmul(fc, weights['W_fc'])+biases['b_fc']) 
    #fc = tf.nn.dropout(fc, keep_rate) 

    output = tf.matmul(fc, weights['out'])+biases['out'] 

    return output 


def use_neural_network(input_data): 
    prediction = convolutional_neural_network(x) 
    sess.run(tf.global_variables_initializer()) 

    result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:[input_data]}),1))) 

    correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1)) 
    accuracy = tf.reduce_mean(tf.cast(correct, 'float')) 
    print('Accuracy:',accuracy.eval({x:X_test, y:y_test})) 

    return result 

with tf.Session() as sess: 

    c = convolutional_neural_network(x) 
    saver = tf.train.Saver() 
    saver.restore(sess, "model.ckpt") 

    sample = X_train[432].reshape(2500) 
    res = use_neural_network(sample) 

    if res == [0]: print('Go straight') 
    elif res == [1]: print('Turn right') 
    else: print('Turn left') 

    img = sample.reshape(50,50) 
    plt.imshow(img) 
    plt.show() 

    sample = X_train[1222].reshape(2500) 
    res = use_neural_network(sample) 

    if res == [0]: print('Go straight') 
    elif res == [1]: print('Turn right') 
    else: print('Turn left') 

    img = sample.reshape(50,50) 
    plt.imshow(img) 
    plt.show() 


    sample = X_train[2986].reshape(2500) 
    res = use_neural_network(sample) 

    if res == [0]: print('Go straight') 
    elif res == [1]: print('Turn right') 
    else: print('Turn left') 

    img = sample.reshape(50,50) 
    plt.imshow(img) 
    plt.show() 

的问题不能过学习,因为我与训练数据集的元素测试它... 我敢肯定,这个问题是金丹,但我无法弄清楚如何解决它...

+1

您的加载模型是不正确的。我已经回答了它:https://stackoverflow.com/questions/44873204/how-to-predict-with-meta-and-checkpoint-files-in-tensorflow/44876333#44876333 –

回答

1

当你使用tensorlfow来训练一个模型时,确保你使用的是tensorflow版本1.0和以上的一旦你使用最新版本文件训练模型将创建命名如下:

  1. MODELNAME。 data

    这是TensorBundle集合,保存所有变量的值。

  2. modelname。 索引

    .index存储变量名称和形状保存的列表。

  3. modelname。 meta

    此文件描述保存的图形结构,包括GraphDef,SaverDef等。

要重新载入/恢复模型中使用model.load(MODELNAME)它不仅载入你的模型,而且精度也不会忽高忽低。

注:请使用TFLearn,TFLearn引入了高级API,使神经网络的建设和培训,快速和容易。欲了解更多详情,请登录http://tflearn.org/getting_started/

建筑的简单,通用的方法和使用CNN使用tensorflow如下:

构建网络:

Here your will create n convolution , max-poll layer and fully connected layer then apply whatever activation function you want and return your model object 

火车模型:

fit your training data into your model using model.fit(X,Y) 

保存模型:

Save your model using model.save(modelName) 

刷新型号:

Reload your model using model.load(modelName) 

这是建立和使用CNN通用和简化的方式。

希望它可以帮助你:)

相关问题