0 votes
1 view
in Data Science by (12.5k points)

I'm training a CNN with tensorflow for recognizing handwriting digits. First code trains model with mnist dataset. After that saves the model.

import numpy as np

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

mnist=input_data.read_data_sets("data/mnist",one_hot=True,reshape=False)

X=tf.placeholder(tf.float32,[None,28,28,1])

Y=tf.placeholder(tf.float32,[None,10])

wc1=tf.Variable(tf.random.truncated_normal([6,6,1,16],stddev=0.2))

bc1=tf.Variable(tf.random.truncated_normal([16],stddev=0.2))

wc2=tf.Variable(tf.random.truncated_normal([5,5,16,32],stddev=0.2))

bc2=tf.Variable(tf.random.truncated_normal([32],stddev=0.2))

wd1=tf.Variable(tf.random.truncated_normal([1568,256],stddev=0.2))

bd1=tf.Variable(tf.random.truncated_normal([256],stddev=0.2))

wd2=tf.Variable(tf.random.truncated_normal([256,64],stddev=0.2))

bd2=tf.Variable(tf.random.truncated_normal([64],stddev=0.2))

wdo=tf.Variable(tf.random.truncated_normal([64,10],stddev=0.2))

bdo=tf.Variable(tf.random.truncated_normal([10],stddev=0.2))

y=tf.nn.relu(tf.nn.conv2d(X,wc1,strides=[1,1,1,1],padding="SAME")+bc1)

y=tf.nn.max_pool(y,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")

y=tf.nn.relu(tf.nn.conv2d(y,wc2,strides=[1,1,1,1],padding="SAME")+bc2)

y=tf.nn.max_pool(y,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")

y=tf.reshape(y,(-1,1568))

y=tf.nn.tanh(tf.linalg.matmul(y,wd1)+bd1)

y=tf.nn.tanh(tf.linalg.matmul(y,wd2)+bd2)

y_pred=tf.nn.softmax(tf.linalg.matmul(y,wdo)+bdo)

xent=-tf.reduce_sum(Y*tf.math.log(y_pred))

l2=tf.reduce_sum(tf.math.square(Y-y_pred))

correct_pred=tf.equal(tf.argmax(Y,1),tf.argmax(y_pred,1))

accuracy=tf.reduce_mean(tf.cast(correct_pred,tf.float32))

optimizer=tf.train.AdamOptimizer(1e-3).minimize(xent)

images=[]

saver=tf.train.Saver()

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    for i in range(3001):

        bx,by=mnist.train.next_batch(50)

        sess.run(optimizer,feed_dict={X:bx,Y:by})

    print("Model is trained")

    acc,x_l,l2_l=sess.run([accuracy,xent,l2],feed_dict={X:bx,Y:by})

    print("Iteration",i,"Accuracy="+str(acc),"Cross Entropy Loss="+str(x_l),"Mean Squared Error="+str(l2_l))

    test_acc,test_x,test_l=sess.run([accuracy,xent,l2],feed_dict={X:mnist.test.images,Y:mnist.test.labels})

    print("Train Accuracy="+str(acc),"Cross Entropy Loss="+str(x_l),"Mean Squared Error="+str(l2_l),"\n\n")

    save_path = saver.save(sess, "tmp/model.ckpt")

    print("Model saved in path: %s" % save_path)

Second code restores model. It predicts image with restored model.

import cv2

import numpy as np

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

mnist=input_data.read_data_sets("data/mnist",one_hot=True,reshape=False)

X=tf.placeholder(tf.float32,[None,28,28,1])

Y=tf.placeholder(tf.float32,[None,10])

wc1=tf.Variable(tf.random.truncated_normal([6,6,1,16],stddev=0.2))

bc1=tf.Variable(tf.random.truncated_normal([16],stddev=0.2))

wc2=tf.Variable(tf.random.truncated_normal([5,5,16,32],stddev=0.2))

bc2=tf.Variable(tf.random.truncated_normal([32],stddev=0.2))

wd1=tf.Variable(tf.random.truncated_normal([1568,256],stddev=0.2))

bd1=tf.Variable(tf.random.truncated_normal([256],stddev=0.2))

wd2=tf.Variable(tf.random.truncated_normal([256,64],stddev=0.2))

bd2=tf.Variable(tf.random.truncated_normal([64],stddev=0.2))

wdo=tf.Variable(tf.random.truncated_normal([64,10],stddev=0.2))

bdo=tf.Variable(tf.random.truncated_normal([10],stddev=0.2))

y=tf.nn.relu(tf.nn.conv2d(X,wc1,strides=[1,1,1,1],padding="SAME")+bc1)

y=tf.nn.max_pool(y,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")

y=tf.nn.relu(tf.nn.conv2d(y,wc2,strides=[1,1,1,1],padding="SAME")+bc2)

y=tf.nn.max_pool(y,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")

y=tf.reshape(y,(-1,1568))

y=tf.nn.tanh(tf.linalg.matmul(y,wd1)+bd1)

y=tf.nn.tanh(tf.linalg.matmul(y,wd2)+bd2)

y_pred=tf.nn.softmax(tf.linalg.matmul(y,wdo)+bdo)

xent=-tf.reduce_sum(Y*tf.math.log(y_pred))

l2=tf.reduce_sum(tf.math.square(Y-y_pred))

correct_pred=tf.equal(tf.argmax(Y,1),tf.argmax(y_pred,1))

accuracy=tf.reduce_mean(tf.cast(correct_pred,tf.float32))

optimizer=tf.train.AdamOptimizer(1e-3).minimize(xent)

saver=tf.train.Saver()

with tf.Session() as sess:

    saver.restore(sess, "tmp/model.ckpt")

    print("Acc is",sess.run(accuracy,feed_dict={X:mnist.test.images[0:59],Y:mnist.train.labels[0:59]}))

    while(True):

        path=input("file:")

        image=cv2.imread(path,0)

        cv2.imshow("Image",image)

        image=cv2.resize(image,(28,28)).reshape(28,28,1)

        val_=sess.run(y_pred,feed_dict={X:(image,)})

        val=sess.run(tf.argmax(y_pred,1),feed_dict={X:(image,)})

        print(val_)

        print(val)

        print("Value=",val)

When I test the restored model, accuracy is under 0.1 .But model trained with 1.0 accuracy. How to fix this problem?

1 Answer

0 votes
by (18.8k points)

The below line has an error:

print("Acc is",sess.run(accuracy,feed_dict={X:mnist.test.images[0:59],Y:mnist.train.labels[0:59]}))

It should be like this:

print("Acc is",sess.run(accuracy,feed_dict:{X:mnist.test.images[0:59],Y:mnist.test.labels[0:59]}))

...