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 inrange(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 % 100 == 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)
RESTART: G:\TestProject\python\tensorflow\...\mnist_backward.py After 16203 training step(s), loss on training batch is 0.155758. After 16303 training step(s), loss on training batch is 0.173135. After 16403 training step(s), loss on training batch is 0.159716.
defapplication(): testNum = input("input the number of test pictures:") for i inrange(testNum): testPic = raw_input("the path of test picture:") testPicArr = pre_pic(testPic) preValue = restore_model(testPicArr) print"The prediction number is:", preValue
defrestore_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)
RESTART: G:\TestProject\python\tensorflow\...\mnist_backward.py After 16203 training step(s), loss on training batch is 0.155758. After 16303 training step(s), loss on training batch is 0.173135. After 16403 training step(s), loss on training batch is 0.159716.
2)运行 mnist_test.py 使用测试集,监测模型的准确率
1 2
RESTART: G:\TestProject\python\tensorflow\...\mnist_test.py After 16703 training step(s), test accuracy = 0.9798
3)运行 mnist_app.py 输入1~10之间的数(表示循环验证的图片数量)
1 2 3 4 5 6 7 8 9 10 11 12 13
RESTART: G:\TestProject\python\tensorflow\...\mnist_app.py input the number of test pictures:5 the path of test picture:pic\0.png The prediction number is: [0] the path of test picture:pic\1.png The prediction number is: [3] the path of test picture:pic\5.png The prediction number is: [5] the path of test picture:pic\8.png The prediction number is: [8] the path of test picture:pic\9.png The prediction number is: [9] >>>
deftest(): with tf.Graph().as_default() as g: ... img_batch, label_batch = mnist_generateds.get_tfrecord(TEST_NUM, isTrain=False)#【2】
whileTrue: 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]
RESTART: G:\TestProject\python\tensorflow\...\mnist_test.py After 16703 training step(s), test accuracy = 0.9794 After 16703 training step(s), test accuracy = 0.9797 After 16703 training step(s), test accuracy = 0.9795 After 16703 training step(s), test accuracy = 0.9792
运行测试代码 mnist_app.py
1 2 3 4 5 6 7 8 9 10 11 12 13
RESTART: G:\TestProject\python\tensorflow\...\mnist_app.py input the number of test pictures:5 the path of test picture:pic\0.png The prediction number is: [0] the path of test picture:pic\1.png The prediction number is: [3] the path of test picture:pic\5.png The prediction number is: [5] the path of test picture:pic\8.png The prediction number is: [8] the path of test picture:pic\9.png The prediction number is: [9] >>>