예제 #1
0
def test(mnist):
    with tf.Graph().as_default() as g:#复现之前定义的计算图
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)
        #加载参数的滑动平均值
        ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        #实例化具有滑动平均的 saver 对象
        saver = tf.train.Saver(ema_restore)
		#从第一维中提取最大值,即提取出最有可能的标签,进行比较
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        #.cast转换成指定类型(将布尔型转换为实数型),求准确率平均值
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        while True:
            with tf.Session() as sess:
                #断点续训
                #从保存模型的路径中加载模型,
                ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    #加载神经网络模型
                    saver.restore(sess, ckpt.model_checkpoint_path)

                    #从保存路径中加载训练轮数
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    #计算出准确率
                    accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
                    print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
                else:
                    print('No checkpoint file found')
                    return
            time.sleep(TEST_INTERVAL_SECS)
예제 #2
0
def mytest(mnist):
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, mnist_forward.input_node])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.output_node])
        y = mnist_forward.forward(x, mnist_backward.regularizer)

        ema = tf.train.ExponentialMovingAverage(
            mnist_backward.moving_average_decay)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)

        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        for i in range(100):
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(
                    mnist_backward.model_save_path)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split(
                        '/')[-1].split('-')[-1]
                    accuracy_score = sess.run(accuracy,
                                              feed_dict={
                                                  x: mnist.test.images,
                                                  y_: mnist.test.labels
                                              })
                    print(
                        "After {} training step(s), test accuracy = {}".format(
                            global_step, accuracy_score))
                else:
                    print("No checkpoint file found")
                    return
            time.sleep(test_interval_secs)
예제 #3
0
def backward(mnist):
    x = tf.placeholder(tf.float32, [None,mnist_forward.INPUT_NODE])
    y_ = tf.placeholder(tf.float32, [None,mnist_forward.OUTPUT_NODE])
    y = mnist_forward.forward(x,REGULARIZER)
    global_step = tf.Variable(0, trainable=False)

    ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y_,1))
    cem = tf.reduce_mean(ce)
    loss = cem + tf.add_n(tf.get_collection('losses'))

    learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,mnist.train.num_examples/BATCH_SIZE,LEARNING_RATE_DECAY,staircase=True)
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_step)

    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)
    ema_op = ema.apply(tf.trainable_variables())
    with tf.control_dependencies([train_step,ema_op]):
        train_op = tf.no_op(name='train')

    saver = tf.train.Saver()

    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        for i in range(STEPS):
            xs, ys=mnist.train.next_batch(BATCH_SIZE)
            _, loss_value, step = sess.run([train_op,loss,global_step], feed_dict={x:xs,y_:ys})
            if i % 1000 == 0:
                print("After %d training steps, loss on training batch is %g."% (step,loss_value))
                saver.save(sess, os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step=global_step)
예제 #4
0
def test(mnist):
    # 利用tf.Graph()复现之前定义的计算图
    with tf.Graph().as_default() as g:
        # 利用placeholder给训练数据x和标签y_占位
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        # 调用mnist_forward文件中的前向传播过程forword()函数
        y = mnist_forward.forward(x, None)
        # 实例化具有滑动平均的saver对象,从而在会话被加载时模型中的所有参数被赋值为各自的滑动平均值,增强模型的稳定性
        ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)
        # 计算模型在测试集上的准确率
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        while True:
            with tf.Session() as sess:
                # 加载指定路径下的ckpt
                ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
                # 若模型存在,则加载出模型到当前对话,在测试数据集上进行准确率验证,并打印出当前轮数下的准确率
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
                    print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
                # 若模型不存在,则打印出模型不存在的提示,从而test()函数完成
                else:
                    print('No checkpoint file found')
                    return
            time.sleep(TEST_INTERVAL_SECS)
예제 #5
0
def test(mnist):
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])

        y_hat = mnist_forward.forward(x, None)

        correct = tf.equal(tf.argmax(y, 1), tf.argmax(y_hat, 1))
        accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

        ema = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVEING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(
                    mnist_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split(
                        "/")[-1].split("-")[-1]
                    accuracy_value = sess.run(accuracy,
                                              feed_dict={
                                                  x: mnist.test.images,
                                                  y: mnist.test.labels
                                              })
                    print(
                        "After training %s steps, the accuracy in test set is %f"
                        % (global_step, accuracy_value))
                else:
                    print("model checkpoint path is not found")
                    return
            time.sleep(TEST_INTERVEL_SEC)
예제 #6
0
def restore_model(testPicArr):
    # 复现计算图
    with tf.Graph().as_default() as tg:
        # 搭建整个计算图
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        preValue = tf.argmax(y, 1)

        # 实例化带有滑动平均的saver
        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variable_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variable_to_restore)

        with tf.Session() as sess:
            # 恢复ckpt
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            # 如果ckpt存在,则将ckpt恢复到当前会话
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                # 将准备好的图片喂入神经网络
                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                return preValue
            else:
                print("No checkpoint file found")
                return -1
예제 #7
0
파일: mnist_app.py 프로젝트: lmnbeck/tf
def restore_model(testPicArr):
    # 重现计算图
    with tf.Graph().as_default() as tg:
        # 仅需要给输入x占位
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        # y的最大值对应的列表索引号,就是预测结果
        preValue = tf.argmax(y, 1)

        # 实例化带有滑动平均值得saver
        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                # 如果ckpt存在,恢复ckpt的参数等信息到当前会话sess
                saver.restore(sess, ckpt.model_checkpoint_path)
                # 图片魏如网络,执行预测操作
                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                return preValue
            else:
                # 如果没有找到ckpt,给出提示
                print "No checkpoint file found"
                return -1
예제 #8
0
def test(mnist):
    with tf.Graph().as_default() as g:  # 使用数据流图
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)  # 开始前向传播,得到输出值y

        ema = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)  # 滑动平均
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)  # 保存

        # tf.equal():比较两个矩阵的对应元素是否相等,相等返回true
        # tf.argmax(x, axis):取最大值,0:求列的最大值,1:求行的最大值
        correct_prediction = tf.equal(tf.argmax(y, 1),
                                      tf.argmax(y_, 1))  # 比较:标签与预测值
        # tf.cast(x, dtype):将参数x转换为指定的类型dtype
        # tf.reduce_mean(x, axis):取张量指定维度的平均值。
        # 不指定第二参数时,则在所有元素中取平均值,0:在第一维元素中取平均值,即每一列求平均。1:在第二维元素上取平均,即每一行取平均
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,
                                          tf.float32))  # 定义准确度,观察所得神经网络的准确度
        accuracy_y = []
        accuracy_x = []
        global_step_old = 0

        while True:
            with tf.Session() as sess:  # 运行计算图
                ckpt = tf.train.get_checkpoint_state(
                    mnist_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split(
                        '/')[-1].split('-')[-1]
                    # print(global_step, type(global_step))
                    if global_step_old == int(global_step):
                        break
                    global_step_old = int(global_step)
                    # print(global_step_old, type(global_step_old))
                    # 每次输入的数据来自于测试集
                    accuracy_score = sess.run(accuracy,
                                              feed_dict={
                                                  x: mnist.test.images,
                                                  y_: mnist.test.labels
                                              })
                    print("After %s training step(s), test accuracy = %g" %
                          (global_step, accuracy_score))
                    accuracy_x.append(int(global_step))
                    accuracy_y.append(accuracy_score)
                else:
                    print('No checkpoint file found')
                    plt.plot(accuracy_x, accuracy_y)
                    plt.show()
                    return
            time.sleep(TEST_INTERVAL_SECS)

        plt.figure(1)
        plt.plot(accuracy_x, accuracy_y)
        plt.xlabel("global_step")
        plt.ylabel("accuracy")
        plt.show()
        return
def test(mnist):
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)

        ema = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)  #恢复滑动平均变量

        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(
                    mnist_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:  #恢复已保存模型
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split(
                        '/')[-1].split('-')[-1]
                    accuracy_score = sess.run(accuracy,
                                              feed_dict={
                                                  x: mnist.test.images,
                                                  y_: mnist.test.labels
                                              })
                    print('After %s training step(s), test accuracy = %g' %
                          (global_step, accuracy_score))
                else:
                    print('No checkpoint file found')
                    return
예제 #10
0
def test():
    with tf.Graph().as_default() as g:
        x=tf.placeholder(tf.float32,shape=[None,mnist_forward.INPUT_NODE])
        y_=tf.placeholder(tf.float32,shape=[None,mnist_forward.OUTPUT_NODE])
        y=mnist_forward.forward(x,None)

        ema=tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore=ema.variables_to_restore()
        saver=tf.train.Saver(ema_restore)

        correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
        accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
        img_batch,label_batch=mnist_generateds.get_tfrecord(TEST_NUM,isTrain=False)
        while True:
            with tf.Session() as sess:
                ckpt=tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess,ckpt.model_checkpoint_path)
                    global_step=ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]

                    coord=tf.train.Coordinator()
                    threads=tf.train.start_queue_runners(sess=sess,coord=coord)
                    xs,ys=sess.run([img_batch,label_batch])

                    accuracy_score=sess.run(accuracy,feed_dict={x:xs,y_:ys})
                    print('After %s steps,test accuracy is %g'%(global_step,accuracy_score))

                    coord.request_stop()
                    coord.join(threads)
                else:
                    print('No checkpoints file found')
                    return
            time.sleep(TEST_INTERVAL_SECS)
예제 #11
0
def restore_model(testPicArr):
    """复现视图 session ,对输入图片数组做出预测
    """
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        preValue = tf.argmax(y, 1)  # 得到概率最大的预测值

        # 实现滑动平均模型
        ema = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        ema_to_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_to_restore)

        with tf.Session() as sess:
            # 加载训练好的模型
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            # 如果已有 ckpt 模型则恢复会话、轮数、计算准确率
            if ckpt is not None:
                saver.restore(sess, ckpt.model_checkpoint_path)
                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                return preValue
            else:
                print('No checkpoint file found')
                return -1
예제 #12
0
def restore_model(testPicArr):
    #复现计算图
    with tf.Graph().as_default() as g:
        #给x占位
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        preValue = tf.argmax(y, 1)  #

        variable_average = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variable_restore = variable_average.variables_to_restore()
        saver = tf.train.Saver(variable_restore)

        with tf.Session() as sess:
            #加载CKPT
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            #判断是否有模型
            if ckpt and ckpt.model_checkpoint_path:
                #恢复模型到当前会话
                saver.restore(sess, ckpt.model_checkpoint_path)
                #执行预测操作
                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                #观察
                preArr = sess.run(y, feed_dict={x: testPicArr})
                preArr = preArr.reshape([1, 10])
                print(preArr)
                return preValue
            else:
                print('No checkpoint file found')
                return -1
def restore_model(testPicArr):
    # 创建一个默认图,在该图下执行以下操作
    with tf.Graph().as_default() as tg:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        preValue = tf.argmax(y, 1)  #得到最大的预测值

        # 实现滑动平均模型,参数MOVING_AVERAGE_DECAY用于控制模型更新的速度,训练过程中会对每个变量
        # 维护一个影子变量,这个影子变量就是相对应变量的初始值,每次变量更新时,影子变量就会随之更新
        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        with tf.Session() as sess:
            #通过checkpoint文件定位到最新保存的模型
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)

                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                return preValue
            else:
                print("No check file found")
                return -1
예제 #14
0
def test(mnist):
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)

        ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)
		
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))		

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
                    print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
                else:
                    print('No checkpoint file found')
                    return
            time.sleep(TEST_INTERVAL_SECS)
예제 #15
0
def restore_model(testPicArr):
    # 定义计算图
    with tf.Graph().as_default() as tg:
        # 定义大小为【None,INPUT_NODE】大小的,类型为float32 的 占位符常量 x(后面需要喂进去数据)
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        # 获取前向传播的过程的结果
        y = mnist_forward.forward(x, None)
        # 获取预测的值
        preValue = tf.argmax(y, 1)
        # 定义变量的滑动平均
        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        # 预备可 存储的变量
        variables_to_restore = variable_averages.variables_to_restore()
        # 存储模型中 填入滑动平均的变量值
        saver = tf.train.Saver(variables_to_restore)
        # 定义新的会话模型
        with tf.Session() as sess:
            # 加载ckpt模型
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            # 判断是否存在 checkpoint的模型
            if ckpt and ckpt.model_checkpoint_path:
                # 若存在则 从checkpoint中 恢复会话
                saver.restore(sess, ckpt.model_checkpoint_path)
                # 获取预测的结果
                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                return preValue
            else:
                # 若不存在 则打印错误信息
                print("No checkoint file found")
                return -1
예제 #16
0
def restore_model(testPicArr):
    #creates a new graph and places everything (declared inside its scope) into this graph.
    with tf.Graph().as_default() as g:
        #compute forwa
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        #define the predicted value
        predictValue = tf.argmax(y,1)

        #Create an ExponentialMovingAverage object ema
        ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        #method variable_to_restore() return a dict ({ema_variables : variables}) 
        ema_restore = ema.variables_to_restore()
        #Create a saver that loads variables from their saved shadow values.
        saver = tf.train.Saver(ema_restore)

        #create session to manage the context
        with tf.Session() as sess:
            #fetch chechpoint from sepcified path
            ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
                #if got the checkpoint sucessfully do things below
            if ckpt and ckpt.model_checkpoint_path:
                    #restore the model to current neural network
                saver.restore(sess, ckpt.model_checkpoint_path)
                #input the testPicArr through feed_dict, and return the predicted answer to predictValue 
                predictValue = sess.run(predictValue, feed_dict={x:testPicArr})
                #return the value of the picture
                return predictValue
            else:
                #fail to fetch the checkpoint,print error infomation
                print "no checkpoint file found"
                #return -1 means this function ends abnormally
                return -1
예제 #17
0
def restore_model(testPicArr):
	#复现训练完成的神经网络计算图
	with tf.Graph().as_default() as tg:
		#前向传播图片数据得到预测值
		x = tf.placeholder(tf.float32,[None,mnist_forward.INPUT_NODE])
		y = mnist_forward.forward(x,None)
		preValue = tf.argmax(y,1)
		#加载滑动平均过程
		variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
		variables_to_restore = variable_averages.variables_to_restore()
		saver = tf.train.Saver(variables_to_restore)
		
		with tf.Session() as sess:
			#加载训练完成的模型
			ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
			if ckpt and ckpt.model_checkpoint_path:
				#加载保存的会话
				saver.restore(sess,ckpt.model_checkpoint_path)
				
				preValue = sess.run(preValue,feed_dict={x:testPicArr})
				return preValue

			else:
				print("No checkpoints file found")
				return -1
예제 #18
0
def backward():

    x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
    y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
    y = mnist_forward.forward(x, REGULARIZER)
    global_step = tf.Variable(0, trainable=False)

    ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,
                                                        labels=tf.argmax(
                                                            y_, 1))
    cem = tf.reduce_mean(ce)
    loss = cem + tf.add_n(tf.get_collection('losses'))

    learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,
                                               global_step,
                                               train_num_examples / BATCH_SIZE,
                                               LEARNING_RATE_DECAY,
                                               staircase=True)

    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(
        loss, global_step=global_step)

    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    ema_op = ema.apply(tf.trainable_variables())
    with tf.control_dependencies([train_step, ema_op]):
        train_op = tf.no_op(name='train')

    saver = tf.train.Saver()
    img_batch, label_batch = mnist_generateds.get_tfrecord(BATCH_SIZE,
                                                           isTrain=True)

    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        #实现断点续训
        ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        for i in range(STEPS):
            xs, ys = sess.run([img_batch, label_batch])
            _, loss_value, step = sess.run([train_op, loss, global_step],
                                           feed_dict={
                                               x: xs,
                                               y_: ys
                                           })
            if i % 1000 == 0:
                print(
                    "After %d training step(s),loss on training batch is %g." %
                    (step, loss_value))
                saver.save(sess,
                           os.path.join(MODEL_SAVE_PATH, MODEL_NAME),
                           global_step=global_step)

        coord.regust_stop()
        coord.join(threads)
예제 #19
0
def restore_model(picarr):
    with tf.Graph().as_default() as tg:
        x = tf.placeholder(tf.float32,
                           shape=[
                               BATCH_SIZE, mnist_forward.IMAGE_SIZE,
                               mnist_forward.IMAGE_SIZE,
                               mnist_forward.NUM_CHANNELS
                           ])
        y = mnist_forward.forward(x, False, None)
        preValue = tf.argmax(y, 1)

        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.LEARNING_RATE_DECAY)
        variable_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variable_to_restore)

        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                preValue = sess.run(preValue, feed_dict={x: picarr})
                return preValue
            else:
                print("无模型存在,无法输出")
                return -1
예제 #20
0
def restore_model(testPicArr):
	#创建一个默认图
	with tf.Graph().as_default() as tg:
		x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
		y = mnist_forward.forward(x, None)
		preValue = tf.argmax(y, 1)

		# 实现滑动平均模型,参数MOVING_AVERAGE_DECAY 用于控制模型更新的速度。
		# 训练过程中会对每一个变量维护一个影子变量,影子变量的初始值就是相应变量的初始值。
		# 每次更新时影子变量会随之更新
		variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
 		variables_to_restore = variable_averages.variables_to_restore()
 		saver = tf.train.Saver(variables_to_restore)

		with tf.Session() as sess:
			# 通过checkpoint文件定位到保存的模型
			ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
			if ckpt and ckpt.model_checkpoint_path:
				saver.restore(sess, ckpt.model_checkpoint_path)
		
				preValue = sess.run(preValue, feed_dict={x:testPicArr})
				return preValue
			else:
				print("No checkpoint file found")
				return -1
예제 #21
0
def restore_model(testPicArr):
    with tf.Graph().as_default() as tg:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        preValue = tf.argmax(y, 1)    # 得到概率最大的预测值.
        """
            实现滑动平均模型,参数MOVING_AVERAGE_DECAY用于控制模型更新的速度.
            训练过程中会对每一个变量维护一个影子变量,这个影子变量的初始值就是相应变量的初始值,
            每次变量更新时,影子变量就会随之更新.    
        """
        variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        with tf.Session() as sess:
            # 通过checkpoint文件定位到最新保存的模型.
            ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)

                preValue = sess.run(preValue, feed_dict={x:testPicArr})
                return preValue
            else:
                print('找不到断点续训文件')
                return -1
예제 #22
0
def restore_model(testPicArr):
    #创建一个默认图,在该图中执行以下操作(多数操作和train中一样)
    with tf.Graph().as_default() as tg:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        preValue = tf.argmax(y, 1)  #得到概率最大的预测值
        #实现滑动平均模型 ,参数moving——average——decay用于控制模型更新的速度 训练过程中会对每一个变量维护一个影子变量 这个
        #影子变量的初始值就是相应变量的初始值,每次变量更新时 影子变量就会随之更新
        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver()

        with tf.Session() as sess:
            #通过checkpoint定位到最新模型
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)  #存储路径
            # 如果已有 ckpt 模型则恢复
            if ckpt and ckpt.model_checkpoint_path:
                # 恢复会话
                saver.restore(sess, ckpt.model_checkpoint_path)
                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                return preValue
            else:
                print("no checkpoint file found")
                return -1
예제 #23
0
def restore_model(testPicArr):  #输入图象像素处理后的矩阵
    #创建一个默认图,在图中操作
    with tf.Graph().as_default() as tg:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        preValue = tf.arg_max(y, 1)  #预测标签
        #滑动平均
        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        with tf.Session() as sess:
            #模型加载
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                #恢复当前会话
                saver.restore(sess, ckpt.model_checkpoint_path)

                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                return preValue
            else:
                print("没有发现文件")
                return -1
예제 #24
0
def test(mnist):
    with tf.Graph().as_default():
        x = tf.placeholder(tf.float32, [None, mhp.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mhp.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)

        ema = tf.train.ExponentialMovingAverage(mhp.MOVE_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)

        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        while True:
            with tf.Session() as sess:
                save_path = mhp.get_save_path()
                if save_path:
                    saver.restore(sess, save_path)
                    global_step = save_path.split('/')[-1].split('-')[-1]
                    accuracy_score = sess.run(accuracy,
                                              feed_dict={
                                                  x: mnist.test.images,
                                                  y_: mnist.test.labels
                                              })
                    print("after %s training step(s), test accuracy=%g" %
                          (global_step, accuracy_score))
                else:
                    print("No checkpoint file found")
                    return
            time.sleep(TEST_INTERVAL_SECE)
예제 #25
0
def test(mnist):
    with tf.Graph().as_default() as g:  #复现计算图
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)      #前向传播计算输出y

        #实例化带滑动平均的saver对象
        ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)
        #所有参数在会话中被加载时会被赋值为各自的滑动平均值
		
        #计算准确率
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
                #加载模型,赋参数滑动平均值
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path) #恢复到当前会话
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
                    print "after %s training steps(s), test accuracy = %g"%(global_step, accuracy_score)
                else:
                    print 'no found model'
                    return
            time.sleep(TEST_INTERVAL_SECS)
	    flags = raw_input("输入y或n决定是否继续:")
	    if flags == "y":
		continue
	    else:
		break
예제 #26
0
def restore_model(testPicArr):
    #利用tf.Graph()复现之前定义的计算图
    with tf.Graph().as_default() as tg:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        #调用mnist_forward文件中的前向传播过程forword()函数
        y = mnist_forward.forward(x, None)
        #得到概率最大的预测值
        preValue = tf.argmax(y, 1)

        #实例化具有滑动平均的saver对象
        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        with tf.Session() as sess:
            #通过ckpt获取最新保存的模型
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)

                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                return preValue
            else:
                print("No checkpoint file found")
                return -1
예제 #27
0
def test(mnist):
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)

        ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)

        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuacy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    print("PATH:"+str(ckpt.model_checkpoint_path))
                    global_step = ckpt.model_checkpoint_path.split('\\')[-1].split('-')[-1]
                    print("global_step",str(global_step))
                    accuacy_score = sess.run(accuacy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
                    print("After %s train steps,test accuracy is %g." % (global_step, accuacy_score))
                else:
                    print("No such model was found!")
                    return
                time.sleep(TEST_INTERVAL_SECS)
예제 #28
0
def test(mnist):
    with tf.Graph().as_default() as g:  # 复现计算图
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)  # 前向传播 算出y

        ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)

        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        
        while True:
            
            with tf.Session() as sess:  # 加载训练好的模型
                ckpt = tf.train.get_checkpoint_state(
                    mnist_backward.MODEL_SAVE_PATH)  # 如果已有 ckpt 模型则恢复
                if ckpt and ckpt.model_checkpoint_path:  # 恢复会话
                    saver.restore(sess, ckpt.model_checkpoint_path)  # 恢复轮数
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    # 计算准确率
                    accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
                    # 打印提示   
                    print("After %s training step(s), test accuracy= %g" % (global_step, accuracy_score))
                    # 如果没有模型
                else:
                    
                    print('No checkpoint file found')  # 模型不存在提示
                    return

            time.sleep(TEST_INTERVAL_SECS)
예제 #29
0
def restore_model(testPicArr):
    with tf.Graph().as_default() as tg:  # 重现计算图
        x = tf.placeholder(tf.float32,
                           [None, mnist_forward.INPUT_NODE])  # 仅需要给x占位
        y = mnist_forward.forward(x, None)  # 计算求得输出y
        preValue = tf.argmax(y, 1)  # y的最大值对应的列表索引号就是预测结果

        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variable_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variable_to_restore)  # 实例化带有滑动平均值的saver

        with tf.Session() as sess:  # 用with结构加载ckpt
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:  # 如果ckpt存在则恢复ckpt参数的相关信息到当前会话
                saver.restore(sess, ckpt.model_checkpoint_path)

                preValue = sess.run(preValue,
                                    feed_dict={x: testPicArr
                                               })  # 将准备好的图片喂入神经网络,执行预测操作
                return preValue
            else:
                print('No checkpoint file found')  # 若没有找到ckpt给出提示
                return -1
예제 #30
0
def restore_model(testPicArr):
    with tf.Graph().as_default() as tg:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        preValue = tf.argmax(y, 1)

        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)

                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                yr = sess.run(y, feed_dict={x: testPicArr})
                print(yr)
                print(yr[0][preValue[0]])
                return preValue
            else:
                print("No checkpoint file found")
                return -1
예제 #31
0
def backward(mnist):
    graph = tf.Graph()
    tf.reset_default_graph()
    with graph.as_default():
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_SIZE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_SIZE])
        y = mnist_forward.forward(x, REGULARIZER)
        global_step = tf.Variable(0, trainable=False)

        ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,
                                                            labels=tf.argmax(
                                                                y_, 1))
        cem = tf.reduce_mean(ce)
        loss = cem + tf.add_n(tf.get_collection('losses'))

        learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,
                                                   global_step,
                                                   mnist.train.num_examples /
                                                   BATCH_SIZE,
                                                   LEARNING_RATE_DECAY,
                                                   staircase=True)

        train_step = tf.train.AdamOptimizer(learning_rate).minimize(
            loss, global_step=global_step)

        ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,
                                                global_step)
        ema_op = ema.apply(tf.trainable_variables())
        with tf.control_dependencies([train_step, ema_op]):
            train_op = tf.no_op(name='train')

        saver = tf.train.Saver()

        with tf.Session(graph=graph) as sess:
            init_op = tf.global_variables_initializer()
            sess.run(init_op)

            ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
            if not os.path.exists(CHECK_POINT):
                os.mkdir(CHECK_POINT)

            tf.summary.merge_all()
            tf.summary.FileWriter(CHECK_POINT + "/summary", graph)

            for i in range(STEPS):
                xs, ys = mnist.train.next_batch(BATCH_SIZE)
                _, loss_value, step = sess.run([train_op, loss, global_step],
                                               feed_dict={
                                                   x: xs,
                                                   y_: ys
                                               })
                if i % 1000 == 0:
                    print(
                        "After %d training step(s), loss on training batch is %g."
                        % (step, loss_value))
                    saver.save(sess,
                               os.path.join(MODEL_SAVE_PATH, MODEL_NAME),
                               global_step=global_step)
예제 #32
0
def restore_model(testPicArr):
    with tf.Graph().as_default() as tg:
        # 为输入输出占位
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y = mnist_forward.forward(x, None)
        # 最大值为输出结果
        preValue = tf.argmax(y, 1)

        # 实例化saver
        variable_averages = tf.train.ExponentialMovingAverage(
            mnist_backward.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        # 用with结构实现断点续训
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(
                mnist_backward.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                # 执行预测操作
                preValue = sess.run(preValue, feed_dict={x: testPicArr})
                return preValue
            else:
                # 没有找到ckpt时给出提示
                print("No checkpoint is found")
                return -1
예제 #33
0
def backward(mnist):

    #给x和y_占位
    x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
    y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
    #调用前向传播的程序,计算输出y
    y = mnist_forward.forward(x, REGULARIZER)
    #轮数计数器赋初始值,设定为不可训练
    global_step = tf.Variable(0, trainable=False)

    #调用包含正则化的损失函数loss
    ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    cem = tf.reduce_mean(ce)
    loss = cem + tf.add_n(tf.get_collection('losses'))

    #定义指数衰减学习率
    learning_rate = tf.train.exponential_decay(
        LEARNING_RATE_BASE,
        global_step,
        mnist.train.num_examples / BATCH_SIZE, 
        LEARNING_RATE_DECAY,
        staircase=True)

    #定义训练过程
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)

    #定义滑动平均
    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    ema_op = ema.apply(tf.trainable_variables())
    with tf.control_dependencies([train_step, ema_op]):
        train_op = tf.no_op(name='train')

    #实例化saver
    saver = tf.train.Saver()

    with tf.Session() as sess:
        #初始化所有变量
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        for i in range(STEPS):
            #每次读入BATCH_SIZE组图片和标签,并喂入神经网络,执行训练过程
            xs, ys = mnist.train.next_batch(BATCH_SIZE)
            _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys})

            #每1000轮打印出当前的loss值
            if i % 1000 == 0:
                print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
                saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
예제 #34
0
def backward(mnist):

    x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
    y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
    y = mnist_forward.forward(x, REGULARIZER)
    global_step = tf.Variable(0, trainable=False)

    ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    cem = tf.reduce_mean(ce)
    loss = cem + tf.add_n(tf.get_collection('losses'))

    learning_rate = tf.train.exponential_decay(
        LEARNING_RATE_BASE,
        global_step,
        mnist.train.num_examples / BATCH_SIZE, 
        LEARNING_RATE_DECAY,
        staircase=True)

    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)

    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    ema_op = ema.apply(tf.trainable_variables())
    with tf.control_dependencies([train_step, ema_op]):
        train_op = tf.no_op(name='train')

    saver = tf.train.Saver()

    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        # 实现断点续训
        ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
        if ckpt and ckpt.model_checkpoint_path:
		    saver.restore(sess, ckpt.model_checkpoint_path)

        for i in range(STEPS):
            xs, ys = mnist.train.next_batch(BATCH_SIZE)
            _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys})
            if i % 1000 == 0:
                print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
                saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
예제 #35
0
def restore_model(testPicArr):
	with tf.Graph().as_default() as tg:
		x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
		y = mnist_forward.forward(x, None)
		preValue = tf.argmax(y, 1)

		variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
 		variables_to_restore = variable_averages.variables_to_restore()
 		saver = tf.train.Saver(variables_to_restore)

		with tf.Session() as sess:
			ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
			if ckpt and ckpt.model_checkpoint_path:
				saver.restore(sess, ckpt.model_checkpoint_path)
		
				preValue = sess.run(preValue, feed_dict={x:testPicArr})
				return preValue
			else:
				print("No checkpoint file found")
				return -1
예제 #36
0
def test():
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)

        ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)
		
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        img_batch, label_batch = mnist_generateds.get_tfrecord(TEST_NUM, isTrain=False)#2

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]

                    coord = tf.train.Coordinator()#3
                    threads = tf.train.start_queue_runners(sess=sess, coord=coord)#4

                    xs, ys = sess.run([img_batch, label_batch])#5

                    accuracy_score = sess.run(accuracy, feed_dict={x: xs, y_: ys})

                    print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))

                    coord.request_stop()#6
                    coord.join(threads)#7

                else:
                    print('No checkpoint file found')
                    return
            time.sleep(TEST_INTERVAL_SECS)