全连接网络实践

  人工智能实践:Tensorflow笔记(8)

Posted by     Keyon                      on April 16, 2018

断点续训

关键处理:加入 ckpt 操作

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

注解:

  • tf.train.get_checkpoint_state(checkpoint_dir,latest_filename=None) 该函数表示如果断点文件夹中包含有效断点状态文件,则返回该文件。
    • checkpoint_dir:表示存储断点文件的目录
    • latest_filename=None:断点文件的可选名称,默认为“checkpoint”
  • saver.restore(sess, ckpt.model_checkpoint_path) 该函数表示恢复当前会话,将 ckpt 中的值赋给 w 和 b。
    • sess:表示当前会话,之前保存的结果将被加载入这个会话。
    • ckpt.model_checkpoint_path:表示模型存储的位置,不需要提供模型的名字,它会去查看 checkpoint 文件,看看最新的是谁,叫做什么。

具体代码:mnist_backward.py

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import os

BATCH_SIZE = 200
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZER = 0.0001
STEPS = 50000
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH="./model/"
MODEL_NAME="mnist_model"


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)


def main():
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    backward(mnist)

if __name__ == '__main__':
    main()

输入真实图片,输出预测结果

网络输入:一维数组(784 个像素点)

像素点:0-1 之间的浮点数(接近 0 越黑,接近 1 越白)

网络输出:一维数组(十个可能性概率),数组中最大的那个元素所对应的索引号就是预测的结果。

关键处理:

def application():
	testNum = input("input the number of test pictures:") 
	for i in range(testNum):
		testPic = raw_input("the path of test picture:")
		testPicArr = pre_pic(testPic)
		preValue = restore_model(testPicArr)
		print "The prediction number is:", preValue

注解:

  • testPicArr = pre_pic(testPic) 对手写数字图片做预处理
  • preValue = restore_model(testPicArr) 将符合神经网络输入要求的图片喂给复现的神经网络模型,输出预测值

具体代码:mnist_app.py

#coding:utf-8

import tensorflow as tf
import numpy as np
from PIL import Image
import mnist_backward
import mnist_forward

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

def pre_pic(picName):
	img = Image.open(picName)
	reIm = img.resize((28,28), Image.ANTIALIAS)
	im_arr = np.array(reIm.convert('L'))
	threshold = 50
	for i in range(28):
		for j in range(28):
			im_arr[i][j] = 255 - im_arr[i][j]
 			if (im_arr[i][j] < threshold):
 				im_arr[i][j] = 0
			else: im_arr[i][j] = 255

	nm_arr = im_arr.reshape([1, 784])
	nm_arr = nm_arr.astype(np.float32)
	img_ready = np.multiply(nm_arr, 1.0/255.0)

	return img_ready

def application():
	testNum = input("input the number of test pictures:")
	for i in range(testNum):
		testPic = raw_input("the path of test picture:")
		testPicArr = pre_pic(testPic)
		preValue = restore_model(testPicArr)
		print "The prediction number is:", preValue

def main():
	application()

if __name__ == '__main__':
	main()

注解:

  • main 函数中的 application 函数:输入要识别的几张图片(注意要给出待识别图片的路径和名称)
  • 代码处理过程:
    • 模型的要求是黑底白字,但输入的图是白底黑字,所以需要对每个像素点的值改为 255 减去原值以得到互补的反色。
    • 对图片做二值化处理(这样以滤掉噪声,另外调试中可适当调节阈值)。
    • 把图片形状拉成 1 行 784 列,并把值变为浮点型(因为要求像素点是 0-1 之间的浮点数)。
    • 接着让现有的 RGB 图从 0-255 之间的数变为 0-1 之间的浮点数。
    • 运行完成后返回到 main 函数。
    • 计算求得输出 y,y 的最大值所对应的列表索引号就是预测结果。

制作数据集

数据集生成读取文件(mnist_generateds.py)

关键处理:tfrecords 文件

  • tfrecords:是一种二进制文件,可先将图片和标签制作成该格式的文件。 使用 tfrecords 进行数据读取,会提高内存利用率。
  • tf.train.Example:用来存储训练数据。训练数据的特征用键值对的形式表示。
  • SerializeToString( ):把数据序列化成字符串存储。

具体代码:mnist_generateds.py

#coding:utf-8
import tensorflow as tf
import numpy as np
from PIL import Image
import os

image_train_path='./mnist_data_jpg/mnist_train_jpg_60000/'
label_train_path='./mnist_data_jpg/mnist_train_jpg_60000.txt'
tfRecord_train='./data/mnist_train.tfrecords'
image_test_path='./mnist_data_jpg/mnist_test_jpg_10000/'
label_test_path='./mnist_data_jpg/mnist_test_jpg_10000.txt'
tfRecord_test='./data/mnist_test.tfrecords'
data_path='./data'
resize_height = 28
resize_width = 28

def write_tfRecord(tfRecordName, image_path, label_path):
    writer = tf.python_io.TFRecordWriter(tfRecordName)  
    num_pic = 0 
    f = open(label_path, 'r')
    contents = f.readlines()
    f.close()
    for content in contents:
        value = content.split()
        img_path = image_path + value[0] 
        img = Image.open(img_path)
        img_raw = img.tobytes() 
        labels = [0] * 10  
        labels[int(value[1])] = 1  
            
        example = tf.train.Example(features=tf.train.Features(feature={
                'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])),
                'label': tf.train.Feature(int64_list=tf.train.Int64List(value=labels))
                })) 
        writer.write(example.SerializeToString())
        num_pic += 1 
        print ("the number of picture:", num_pic)
    writer.close()
    print("write tfrecord successful")

def generate_tfRecord():
	isExists = os.path.exists(data_path) 
	if not isExists: 
 		os.makedirs(data_path)
		print 'The directory was created successfully'
	else:
		print 'directory already exists' 
	write_tfRecord(tfRecord_train, image_train_path, label_train_path)
 	write_tfRecord(tfRecord_test, image_test_path, label_test_path)
  
def read_tfRecord(tfRecord_path):
    filename_queue = tf.train.string_input_producer([tfRecord_path], shuffle=True)
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue) 
    features = tf.parse_single_example(serialized_example,
                                       features={
                                        'label': tf.FixedLenFeature([10], tf.int64),
                                        'img_raw': tf.FixedLenFeature([], tf.string)
                                        })
    img = tf.decode_raw(features['img_raw'], tf.uint8)
    img.set_shape([784])
    img = tf.cast(img, tf.float32) * (1. / 255)
    label = tf.cast(features['label'], tf.float32)
    return img, label 
      
def get_tfrecord(num, isTrain=True):
    if isTrain:
        tfRecord_path = tfRecord_train
    else:
        tfRecord_path = tfRecord_test
    img, label = read_tfRecord(tfRecord_path)
    img_batch, label_batch = tf.train.shuffle_batch([img, label],
                                                    batch_size = num,
                                                    num_threads = 2,
                                                    capacity = 1000,
                                                    min_after_dequeue = 700)
    return img_batch, label_batch

def main():
    generate_tfRecord()

if __name__ == '__main__':
    main()

注解:

  • 生成 tfrecords 文件
    • writer = tf.python_io.TFRecordWriter(tfRecordName) 新建一个 writer
    • for 循环遍历每张图和标签
    • example 把每张图片和标签封装到 example 中
    • writer.write(example.SerializeToString()) 把 example 进行序列化
    • writer.close() 关闭 writer
  • 解析 tfrecords 文件
    • tf.train.string_input_producer( string_tensor,num_epochs=None,shuffle=True,seed=None,capacity=32,shared_name=None,name=None,cancel_op=None) 该函数会生成一个先入先出的队列,文件阅读器会使用它来读取数据。
      • string_tensor:存储图像和标签信息的 TFRecord 文件名列表
      • num_epochs:循环读取的轮数(可选)
      • shuffle:布尔值(可选),如果为 True,则在每轮随机打乱读取顺序
      • seed:随机读取时设置的种子(可选)
      • capacity:设置队列容量
      • shared_name:(可选) 如果设置,该队列将在多个会话中以给定名 称共享。所有具有此队列的设备都可以通过 shared_name 访问它。在分布式设置中使用这种方法意味着每个名称只能被访问此操作的其中一个会话看到。
      • name:操作的名称(可选)
      • cancel_op:取消队列(None)
    • reader = tf.TFRecordReader() 新建一个 reader
    • tf.parse_single_example(serialized,features,name=None,example_names=None)
      • serialized: 一个标量字符串张量
      • features:一个字典映射功能键 FixedLenFeature 或 VarLenFeature 值,也就是在协议内存块中储存的
      • name:操作的名称(可选)
      • example_names:标量字符串联的名称(可选)
    • img = tf.decode_raw(features[‘img_raw’], tf.uint8) 将 img_raw 字符串转换为 8 位无符号整型
    • img.set_shape([784]) 将形状变为一行 784 列
    • img = tf.cast(img, tf.float32) * (1. / 255) 变成 0 到 1 之间的浮点数
    • label = tf.cast(features[‘label’], tf.float32) 把标签列表变为浮点数
    • return image,label 返回图片和标签(跳回到 get_tfrecord)
    • tf.train.shuffle_batch( tensors, batch_size,capacity,min_after_dequeue,num_threads=1,seed=None,enqueue_many=False,shapes=None,allow_smaller_final_batch=False,shared_name=None,name=None) 这个函数随机读取一个 batch 的数据
      • tensors:待乱序处理的列表中的样本(图像和标签)
      • batch_size: 从队列中提取的新批量大小
      • capacity:队列中元素的最大数量
      • min_after_dequeue: 出队后队列中的最小数量元素,用于确保元素的混合级别
      • num_threads: 排列 tensors 的线程数
      • seed:用于队列内的随机洗牌
      • enqueue_many: tensor 中的每个张量是否是一个例子
      • shapes: 每个示例的形状
      • allow_smaller_final_batch: (可选)布尔值。 如果为 True,则在队列中剩余数量不足时允许最终批次更小。
      • shared_name:(可选)如果设置,该队列将在多个会话中以给定名称共享。
      • name:操作的名称(可选)
    • return img_batch,label_batch 返回的图片和标签为随机抽取的 batch_size 组

反向传播文件修改图片标签获取的接口(mnist_backward.py)

关键操作:利用多线程提高图片和标签的批获取效率

方法:将批获取的操作放到线程协调器开启和关闭之间

开启线程协调器:

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

关闭线程协调器:

coord.request_stop( ) 
coord.join(threads)

注解:

  • tf.train.start_queue_runners( sess=None,coord=None,daemon=True,start=True,collection=tf.GraphKeys.QUEUE_RUNNERS) 这个函数将会启动输入队列的线程,填充训练样本到队列中,以便出队操作可以从队列中拿到样本。这种情况下最好配合使用一个 tf.train.Coordinator ,这样可以在发生错误的情况下正确地关闭这些线程。
    • sess:用于运行队列操作的会话,默认为默认会话。
    • coord:可选协调器,用于协调启动的线程。
    • daemon: 守护进程,线程是否应该标记为守护进程,这意味着它们不会阻止程序退出。
    • start:设置为 False 只创建线程,不启动它们。
    • collection:指定图集合以获取启动队列的 GraphKey。默认为GraphKeys.QUEUE_RUNNERS。

具体对比:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import os
import mnist_generateds#1

BATCH_SIZE = 200
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZER = 0.0001
STEPS = 50000
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH="./model/"
MODEL_NAME="mnist_model"
train_num_examples = 60000#2

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()
    img_batch, label_batch = mnist_generateds.get_tfrecord(BATCH_SIZE, isTrain=True)#3

    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()#4
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)#5

        for i in range(STEPS):
            xs, ys = sess.run([img_batch, label_batch])#6
            _, 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.request_stop()#7
        coord.join(threads)#8

def main():
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    backward(mnist)

if __name__ == '__main__':
    main()

注解:

  • train_num_examples=60000 在梯度下降学习率中需要计算多少轮更新一次学习率,这个值是 总样本数/batch size
    • 之前:用 mnist.train.num_examples 表示总样本数;
    • 现在:要手动给出训练的总样本数,这个数是 6 万。
  • image_batch, label_batch=mnist_generateds.get_tfrecord(BATCH_SIZE, isTrain=True)
    • 之前:用 mnist.train.next_batch 函数读出图片和标签喂给网络;
    • 现在:用函数 get_tfrecord 替换,一次批获取 batch_size 张图片和标签。
    • isTrain:用来区分训练阶段和测试阶段,True 表示训练,False 表示测试。
  • xs,ys=sess.run([img_batch,label_batch])
    • 之前:使用函数 xs,ys=mnist.train.next_batch(BATCH_SIZE)
    • 现在:在 sess.run 中执行图片和标签的批获取。

测试文件修改图片标签获取的接口(mnist_test.py)

具体对比:

#coding:utf-8
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import mnist_backward
import mnist_generateds
TEST_INTERVAL_SECS = 5
TEST_NUM = 10000#1

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)

def main():
    test()#8

if __name__ == '__main__':
    main()

注释:

  • TEST_NUM=10000
    • 之前:用 mnist.test.num_examples 表示总样本数;
    • 现在:要手动给出测试的总样本数,这个数是 1 万。
  • image_batch, label_batch=mnist_generateds.get_tfrecord(TEST_NUM, isTrain=False)
    • 之前:用 mnist.test.next_batch 函数读出图片和标签喂给网络;
    • 现在:用函数 get_tfrecord 替换读取所有测试集 1 万张图片。
    • isTrain:用来区分训练阶段和测试阶段,True 表示训练,False 表示测试。
  • xs,ys=sess.run([img_batch,label_batch])
    • 之前:使用函数 xs,ys=mnist.test.next_batch(BATCH_SIZE)
    • 现在:在 sess.run 中执行图片和标签的批获取。