无法在Tensorflow中保存或还原模型

时间:2019-07-16 09:58:49

标签: python numpy opencv tensorflow data-science

我正在训练具有Tensorflow的CNN以识别手写数字。第一个代码使用mnist数据集训练模型。之后,保存模型。

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)

第二个代码恢复模型。它使用还原的模型预测图像。

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)

当我测试恢复的模型时,精度低于0.1,但是训练模型的精度为1.0。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

下一行有错误:

<style>

您正在从print("Acc is",sess.run(accuracy,feed_dict={X:mnist.test.images[0:59],Y:mnist.train.labels[0:59]})) 数据集中获取X值,但从mnist.test数据集中获取Y标签。

应该是

mnist.train