Cro
Cro

Reputation: 338

Tensorflow with MNIST trained model always prints the wrong number

I'm using Tensorflow and Python for text recognition, but when I'm trying to do some number recognition, training is fine, but when I restore the model and use it, no error but always the wrong prediction, here's my code for training and using model. Can someone point out what's the matter with this thing?

Training:

import input_data
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
mn = input_data.read_data_sets("tmp/data", one_hot=True)
training_epoch = 10000
learning_rate = 0.001
batch_size = 20000
display_step = 1
n_hidden1 = 512
n_hidden2 = 512
input_size = 784
n_class = 10
x = tf.placeholder("float", [None, input_size])
y = tf.placeholder("float", [None, n_class])
h = tf.Variable(tf.random_normal([input_size, n_hidden1]))
layer1_bias = tf.Variable(tf.random_normal([n_hidden1]))
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x,h),layer1_bias))
w = tf.Variable(tf.random_normal([n_hidden1, n_hidden2]))
layer2_bias = tf.Variable(tf.random_normal([n_hidden2]))
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,w),layer2_bias))
output = tf.Variable(tf.random_normal([n_hidden2, n_class]))
bias_output = tf.Variable(tf.random_normal([n_class]))
output_layer = tf.matmul(layer_2, output) + bias_output
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,     logits=output_layer))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
avg_set = []
epoch_set = []
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    saver = tf.train.Saver()
    for epoch in range(training_epoch):
        avg_cost = 0.
        batch_total = int(mn.train.num_examples/batch_size)
        for i in range(batch_total):
            batch_x, batch_y = mn.train.next_batch(batch_size)
            print(batch_x.shape)
            sess.run(optimizer, feed_dict={x:batch_x, y:batch_y})
            avg_cost += sess.run(cost, feed_dict={x:batch_x, y:batch_y})/batch_total            
        if(epoch % display_step == 0):
            print("Epoch:%d " % (epoch), "cost:", "{:.9f}".format(avg_cost))
        avg_set.append(avg_cost)
        epoch_set.append(epoch+1)
    print("Training finished")
    plt.plot(epoch_set,avg_set, 'o', label='MLP Training phase')
    plt.ylabel('cost')
    plt.xlabel('epoch')
    plt.legend()
    plt.show()
    correct_prediction = tf.equal(tf.argmax(output_layer, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print("Model Accuracy:", accuracy.eval({x: mn.test.images, y: mn.test.labels}))
    saver.save(sess, "model-batchsize-20000-epoch-10000-learningrate-0.001/tf_mlp_model.ckpt")

Testing:

import numpy as np
import tensorflow as tf
import input_data
import cv2
import os
dir = os.path.dirname(os.path.realpath(__file__))
img = cv2.imread('6-1.png')
img.astype("float")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = img.flatten()
img = np.expand_dims(img, axis=0)
n_hidden1 = 512
n_hidden2 = 512
input_size = 784
n_class = 10
x = tf.placeholder("float", [1, input_size])
y = tf.placeholder("float", [None, n_class])
h = tf.Variable(tf.random_normal([input_size, n_hidden1]))
layer1_bias = tf.Variable(tf.random_normal([n_hidden1]))
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x,h),layer1_bias))
w = tf.Variable(tf.random_normal([n_hidden1, n_hidden2]))
layer2_bias = tf.Variable(tf.random_normal([n_hidden2]))
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,w),layer2_bias))
output = tf.Variable(tf.random_normal([n_hidden2, n_class]))
bias_output = tf.Variable(tf.random_normal([n_class]))
output_layer = tf.matmul(layer_2, output) + bias_output
with tf.Session() as sess:
    tf.train.Saver()
    tf.train.Saver().restore(sess, dir + "/model-batchsize-20000-epoch-10000-   learningrate-0.001/tf_mlp_model.ckpt")
    pred = tf.argmax(sess.run(output_layer, feed_dict={x:img}), 1)
    print(pred.eval())

Testing output:

[2]

sess.run(output_layer, feed_dict={x:img}):

[[ -4.48937702e+00  -8.70745659e+00   2.27353687e+01   2.25894527e+01
1.72218680e-02   1.78360157e+01   2.39438486e+00   5.72816038e+00
-2.13753247e+00   4.05950975e+00]]

EDIT 1: I forgot to say that the real value is 6, not 2, it's a 28x28 screenshot from my MacBook, link:6-1.png

EDIT 2: Tried number 2, return:

[3]

Upvotes: 2

Views: 179

Answers (1)

Cro
Cro

Reputation: 338

Turned out that it is normal with MLP. I should use Convolutional Neural Network back then. Problem solved.

Upvotes: 0

Related Questions