2017-07-27 70 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通用和簡化的方式。

希望它可以幫助你:)

相關問題