Francesco Barban
Francesco Barban

Reputation: 21

Why do I get a low accuracy in this neural network (tensorflow)?

I have made a convolutional neural network with tensorflow, i've trained it and tested it (about 98% accuracy)... I saved the model with

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

Then i restored with the saver, but i always get an accuracy lower than 50%... why ? Here's the code:

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

The problem can't be overfitting, since i'm testing it with elements of the training dataset ... I'm quite sure that the problem is the saver, but i can't figure out how to solve it ...

Upvotes: 2

Views: 630

Answers (1)

Parvez Khan
Parvez Khan

Reputation: 577

When you train a model using tensorlfow , make sure that your using the tensorflow version 1.0 and above. once you trained model using latest version 3 file will be created named as follows :

  1. modelname.data

    It is TensorBundle collection, save the values of all variables.

  2. modelname.index

    .index stores the list of variable names and shapes saved.

  3. modelname.meta

    this file describes the saved graph structure, includes GraphDef, SaverDef, and so on.

To reload/restore your model use model.load(modelname) it not only loads your model but also the accuracy won't be fluctuated.

Note : Please use TFLearn , TFLearn introduces a High-Level API that makes neural network building and training fast and easy.For more detail visit http://tflearn.org/getting_started/

The simple and Generalized way of building and using CNN using tensorflow is as follows:

Construct Network :

  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

Train model :

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

Save Model :

   Save your model using model.save(modelName)

Reload Model :

   Reload your model using model.load(modelName)

This is the generic and simplified way to build and use CNN.

Hope it may help you :)

Upvotes: 1

Related Questions