本篇博客为学习中国大学MOOC-人工智能实践:Tensorflow笔记课程时的个人笔记记录。具体课程情况可以点击链接查看。(这里推一波中国大学MOOC,很好的学习平台,质量高,种类全,想要学习的话很有用的)
本篇是第六章的学习笔记,前面五章的笔记可以翻看我的博客~
修改mnist_backward.py文件
修改后的文件内容如下,修改的地方进行了标记
#coding:utf-8
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 = 'G:/model/' #这里是我选择放置训练好的model的路径,根据自己的需要进行修改
MODEL_NAME = 'mnist_model'
DATA_PATH = 'G:/datasets/mnist' #这里是我放置dataset的路径,根据自己的需要进行修改
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)
# 加入断点续训功能 #################################################modified
ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
# 读入原本训练的结果,继续训练############end
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)
def main():
mnist = input_data.read_data_sets(DATA_PATH, one_hot = True)
backward(mnist)
if __name__ == '__main__':
main()
Q:如何对输入的真实图片,输出预测的结果,
输入层是784个节点,每个节点是[0,1]之间的浮点数,
输出层是10个可能性概率组成的以为数组
def application():
testNum = input("input the number of test pictures:")
for i in range (testNum):
testPic = raw_input("the path of test pictures:")
testPicArr = pre_pic(testPic)
preValue = restore_model(testPicArr)
print("The prediction number is:",preValue)
相比第五章的代码新增代码文件 mnist_app.py
具体的代码文件 mnist_app.py 内容如下:
#coding:utf-8
import tensorflow as tf
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
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.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("No checkpoint file found")
return -1
def pre_pic(picName):
img = Image.open(picName)
#img = img.convert("L")
reIm = img.resize((28,28), Image.ANTIALIAS)
im_arr = np.array(reIm.convert('L'))
threshold = 100
for i in range(28):
for j in range(28):
im_arr[i][j] = 255-im_arr[i][j]
if (im_arr[i][j]0
else: im_arr[i][j] = 255
plt.figure("figure")
plt.imshow(im_arr)
plt.show()
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 = int(input("input the number of test picture: "))
for i in range(testNum):
testPic = input("the path of test picture: ")
testPicArr = pre_pic(testPic)
preValue = restore_model(testPicArr)
print("The prediction number is:", preValue)
'''
for i in range(10):
imName = 'pic/'+str(i)+'.jpg'
print("ImageName is:", imName)
testPicArr = pre_pic(imName)
preValue = restore_model(testPicArr)
print("The prediction number is:", preValue)
def main():
application()
if __name__ == '__main__':
main()
注意:这里我修改了老师上课使用的代码,改成自动循环十张测试图片,每次读入一张图片,然后处理完成后显示该图片,手动关闭图片窗口程序会继续运行输出测试结果.老师原本使用的代码我注释掉了,可以自行修改.
测试使用的十张手写数字图片我没有找到,于是自己制作了一份,从0~9十张手写数字图片,压缩了一下上传到了CSDN,需要的朋友可以自行下载,(可能我写的太丑了,有几个数字就是识别不对,不过个人感觉也很正常,毕竟只是这么简单的全连接层,识别效果不是很理想也很正常,加上我自己制作的数据集和mnist本身的样式可能差的比较远.)
十张测试图片下载链接:https://download.csdn.net/download/tuzixini/10560123
下载后解压放在和代码同一目录下就好.
Q:如何制作数据集,实现特定应用
tfrecords文件
tfrecords文件是一种二进制文件,可先将图片和标签制作成该格式的文件,使用tensorflow进行数据读取,会提高内存利用率.
使用tf.train.Example的协议存储训练数据,训练数据的特征用键值对的形式表示.
如:’img_raw’:值 ‘label’:值 值是Byteslist/FloatList/int64List
用SerializeToString()把数据序列化成字符串存储.
生成tfrecords文件
writer = tf.python_io.TFRecordWriter(tfRecoderName) #新建一个writer
for 循环遍历每张图片和标签:
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.List(value=labels))})) # 把每张图片和标签封装到Example中
writer.write(example.SerializeToString()) #把example进行序列化
writer.close()
解析tfrecords文件
filename_queue = tf.train.string_input_producer([tfRecord_path])
reader = tf.TFRecordReader() # 新建一个reader
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example, feature={'img_raw':tf.FixedLenFeature([],tf.string),'label':tf.FixedLenFeature([10],tf.int64)})
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)
相比6.1 新增代码文件 mnist_generateds.py, 同时修改了mnist_backward.py文件和mnist_test.py文件中图片和标签获取的接口.
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_train_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()
image_path = image_path + value[0]
img = Image.open(image_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 sussessful")
def generate_tfRecord():
isExists = os.path.exists(data_path)
if not isExists:
os.makedirs(data_path)
print("The directory was create 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])
reader = tf.TFRecordReader() # 新建一个reader
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example, feature={'img_raw':tf.FixedLenFeature([],tf.string),'label':tf.FixedLenFeature([10],tf.int64)})
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()
使用多线程提升图片标签批获取的效率
把批获取的操作放到线程协调器开启和关闭的中间
# 开启线程协调器
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
pass
# 关闭线程协调器
coord.request_stop()
coord.join(threads)
修改后用于Chapter 6 的mnist_backward.py代码如下
#coding:utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import os
# chapter 6 添加 ## start
import mnist_generateds
## end
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 = 'G:/model/' #这里是我选择放置训练好的model的路径,根据自己的需要进行修改
MODEL_NAME = 'mnist_model'
DATA_PATH = 'G:/datasets/mnist' #这里是我放置dataset的路径,根据自己的需要进行修改
# chapter 6 添加 ## start
train_num_examples = 60000
## end
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'))
# chapter 5 使用,在chapter 6被注释
# learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,mnist.train.num_examples / BATCH_SIZE,LEARNING_RATE_DECAY,staircase = True)
# 替换为:
learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,train_num_examples / BATCH_SIZE,LEARNING_RATE_DECAY,staircase = True)
## end
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()
# chapter 6 添加 ## start
img_batch, label_batch = mnist_generateds.generate_tfRecord(BATCH_SIZE, isTrain=True)
## end
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
# 加入断点续训功能 ###########################################################################modified
ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
# 读入原本训练的结果,继续训练
# chapter 6 添加 ## start
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
## end
for i in range(STEPS):
# chapter 5 使用,在chapter 6被注释
# xs, ys = mnist.train.next_batch(BATCH_SIZE)
# 替换为:
xs, ys = sess.run([img_batch, label_batch])
## end
_, 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)
# chapter 6 添加 ## start
coord.request_stop()
coord.join(threads)
## end
def main():
mnist = input_data.read_data_sets(DATA_PATH, one_hot = True)
backward(mnist)
if __name__ == '__main__':
main()
修改后用于Chapter6 的 mnist_test.py 文件代码如下:
#coding:utf-8
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_backward
import mnist_forward
## Chapter 6 添加
import mnist_generateds
# end
TEST_INTERVAL_SECS = 5
DATA_PATH = 'G:/datasets/mnist' #这里是我放置dataset的路径,根据自己的需要进行修改
## chapter 6 添加
TEST_NUM = 10000
# end
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))
## Chapter 6 添加
img_batch, label_batch = mnist_generateds.get_tfRecord(TEST_num, isTrain=False)
# end
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]
## chapter 6 添加
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
xs,ys = sess.run([img_batch, label_batch])
# end
## chapter 5 使用,在chapter 6注释
# accuracy_score = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})
# 替换为:
accuracy_score = sess.run(accuracy, feed_dict={x: xs, y_: ys})
# end
print("After %s training steps, test accuracy = %g" % (global_step, accuracy_score))
## chapter 6添加
coord.request_stop()
coord.join(threads)
# end
else:
print("No checkpoint file found!")
return
time.sleep(TEST_INTERVAL_SECS)
def main():
mnist = input_data.read_data_sets(DATA_PATH, one_hot=True)
test(mnist)
if __name__ == '__main__':
main()