____tz_zs小练习
案例来源《TensorFlow实战Google深度学习框架》
# -*- coding: utf-8 -*-
"""
@author: tz_zs
卷积神经网络 mnist_inference.py
"""
import tensorflow as tf
# 定义神经网络结构相关的参数
INPUT_NODE = 784
OUTPUT_NODE = 10
IMAGE_SIZE = 28
NUM_CHANNELS = 1
NUM_LABELS = 10
# 第一层卷积层的尺寸和深度
CONV1_DEEP = 32
CONV1_SIZE = 5
# 第二层卷积层的尺寸和深度
CONV2_DEEP = 64
CONV2_SIZE = 5
# 全连接层的节点个数
FC_SIZE = 512
def inference(input_tensor, train, regularizer):
with tf.variable_scope('layer1-conv1'):
conv1_weights = tf.get_variable("weight", [CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_DEEP],
initializer=tf.truncated_normal_initializer(stddev=0.1))
conv1_biases = tf.get_variable("bias", [CONV1_DEEP], initializer=tf.constant_initializer(0.0))
# print(conv1_weights) #
# print(conv1_biases) #
conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
# print(conv1) # Tensor("layer1-conv1/Conv2D:0", shape=(100, 28, 28, 32), dtype=float32)
# print(relu1) # Tensor("layer1-conv1/Relu:0", shape=(100, 28, 28, 32), dtype=float32)
with tf.name_scope('layer2-pool1'):
pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# print(pool1) # Tensor("layer2-pool1/MaxPool:0", shape=(100, 14, 14, 32), dtype=float32)
with tf.variable_scope('layer3-conv2'):
conv2_weights = tf.get_variable("weight", [CONV2_SIZE, CONV2_SIZE, CONV1_DEEP, CONV2_DEEP],
initializer=tf.truncated_normal_initializer(stddev=0.1))
conv2_biases = tf.get_variable("bias", [CONV2_DEEP], initializer=tf.constant_initializer(0.0))
conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
with tf.name_scope('layer4-pool2'):
pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
pool_shape = pool2.get_shape().as_list()
# print(pool_shape)# [100, 7, 7, 64]-------[5000, 7, 7, 64]
nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]
reshaped = tf.reshape(pool2, [pool_shape[0], nodes])
with tf.variable_scope('layer5-fc1'):
fc1_weights = tf.get_variable("weight", [nodes, FC_SIZE],
initializer=tf.truncated_normal_initializer(stddev=0.1))
if regularizer != None:
tf.add_to_collection('losses', regularizer(fc1_weights))
fc1_biases = tf.get_variable("bias", [FC_SIZE], initializer=tf.constant_initializer(0.1))
fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights) + fc1_biases)
if train:
fc1 = tf.nn.dropout(fc1, 0.5)
with tf.variable_scope('layer6-fc2'):
fc2_weights = tf.get_variable("weight", [FC_SIZE, NUM_LABELS],
initializer=tf.truncated_normal_initializer(stddev=0.1))
if regularizer != None:
tf.add_to_collection('losses', regularizer(fc2_weights))
fc2_biases = tf.get_variable("bias", [NUM_LABELS], initializer=tf.constant_initializer(0.1))
logit = tf.matmul(fc1, fc2_weights) + fc2_biases
return logit
# -*- coding: utf-8 -*-
"""
@author: tz_zs
卷积神经网络 mnist_train.py
"""
import tensorflow as tf
import os
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
# 加载函数
import cnn.mnist_inference as mnist_inference
# 配置神经网络参数
BATCH_SIZE = 100
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZATION_RATE = 0.0001
TRAINING_STEPS = 30000
MOVING_AVERAGE_DECAY = 0.99
# 模型保存路径和文件名
MODEL_SAVE_PATH = "/path/to/model/cnn/"
MODEL_NAME = "model.ckpt"
def train(mnist):
# 定义输入输出的placeholder
# x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
x = tf.placeholder(tf.float32, [BATCH_SIZE,
mnist_inference.IMAGE_SIZE,
mnist_inference.IMAGE_SIZE,
mnist_inference.NUM_CHANNELS],
name='x-input')
y_ = tf.placeholder(tf.float32, [BATCH_SIZE, mnist_inference.OUTPUT_NODE], name='y-input')
# 定义正则化
regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
# 使用前向传播
y = mnist_inference.inference(x, True, regularizer)
global_step = tf.Variable(0, trainable=False)
# 滑动平均
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
variable_averages_op = variable_averages.apply(tf.trainable_variables())
# print(tf.trainable_variables())
# [,
# ,
# ,
# ,
# ,
# ,
# ,
# ]
# 损失函数
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y_, 1), logits=y)
cross_entropy_mean = tf.reduce_mean(cross_entropy)
loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))
# print(tf.get_collection('losses'))
# #[,
# ]
# 学习率
learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,
global_step,
mnist.train.num_examples / BATCH_SIZE,
LEARNING_RATE_DECAY)
# 优化算法
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
with tf.control_dependencies([train_step, variable_averages_op]):
train_op = tf.no_op(name="train")
# 持久化
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
tf.global_variables_initializer().run()
for i in range(TRAINING_STEPS):
xs, ys = mnist.train.next_batch(BATCH_SIZE)
# 调整为四维矩阵
reshaped_xs = np.reshape(xs, [BATCH_SIZE,
mnist_inference.IMAGE_SIZE,
mnist_inference.IMAGE_SIZE,
mnist_inference.NUM_CHANNELS])
# 运行
_, loss_valuue, step = sess.run([train_op, loss, global_step], feed_dict={x: reshaped_xs, y_: ys})
# 每1000轮保存一次模型
if i % 1000 == 0:
print("After %d training step(s), loss on training batch is %g." % (step, loss_valuue))
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def main(argv=None):
mnist = input_data.read_data_sets("/tmp/data", one_hot=True)
train(mnist)
if __name__ == '__main__':
tf.app.run()
'''
运行结果:
After 1 training step(s), loss on training batch is 6.73231.
After 1001 training step(s), loss on training batch is 0.730202.
After 2001 training step(s), loss on training batch is 0.644094.
After 3001 training step(s), loss on training batch is 0.640496.
After 4001 training step(s), loss on training batch is 0.634515.
After 5001 training step(s), loss on training batch is 0.64231.
After 6001 training step(s), loss on training batch is 0.581734.
After 7001 training step(s), loss on training batch is 0.590254.
After 8001 training step(s), loss on training batch is 0.546791.
After 9001 training step(s), loss on training batch is 0.553352.
After 10001 training step(s), loss on training batch is 0.526924.
After 11001 training step(s), loss on training batch is 0.516263.
After 12001 training step(s), loss on training batch is 0.510524.
After 13001 training step(s), loss on training batch is 0.530617.
After 14001 training step(s), loss on training batch is 0.500552.
After 15001 training step(s), loss on training batch is 0.49316.
After 16001 training step(s), loss on training batch is 0.478148.
After 17001 training step(s), loss on training batch is 0.470733.
After 18001 training step(s), loss on training batch is 0.471833.
After 19001 training step(s), loss on training batch is 0.456701.
After 20001 training step(s), loss on training batch is 0.451218.
After 21001 training step(s), loss on training batch is 0.446669.
After 22001 training step(s), loss on training batch is 0.440087.
After 23001 training step(s), loss on training batch is 0.43465.
After 24001 training step(s), loss on training batch is 0.428076.
After 25001 training step(s), loss on training batch is 0.42475.
After 26001 training step(s), loss on training batch is 0.416584.
After 27001 training step(s), loss on training batch is 0.428798.
After 28001 training step(s), loss on training batch is 0.406561.
After 29001 training step(s), loss on training batch is 0.404045.
'''
# -*- coding: utf-8 -*-
"""
@author: tz_zs
卷积神经网络 测试程序 mnist_eval.py
"""
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import cnn.mnist_inference
import cnn.mnist_train
import numpy as np
# 每十秒加载一次最新的模型,并在测试数据上测试最新模型的准确率
EVAL_INTERVAL_SECS = 10
def evaluate(mnist):
with tf.Graph().as_default() as g:
# x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name="x-input")
x = tf.placeholder(tf.float32, [mnist.validation.num_examples,
cnn.mnist_inference.IMAGE_SIZE,
cnn.mnist_inference.IMAGE_SIZE,
cnn.mnist_inference.NUM_CHANNELS],
name='x-input')
y_ = tf.placeholder(tf.float32, [mnist.validation.num_examples, cnn.mnist_inference.OUTPUT_NODE],
name="y-input")
# 数据输入调整为四维矩阵
reshaped_xs = np.reshape(mnist.validation.images,
[mnist.validation.num_examples,
cnn.mnist_inference.IMAGE_SIZE,
cnn.mnist_inference.IMAGE_SIZE,
cnn.mnist_inference.NUM_CHANNELS])
validate_feed = {x: reshaped_xs, y_: mnist.validation.labels}
# 测试(测试时不用计算正则化损失)
y = cnn.mnist_inference.inference(x, False, None)
# 计算准确率
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 加载模型
variable_averages = tf.train.ExponentialMovingAverage(cnn.mnist_train.MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
#print(variables_to_restore)
# {'layer3-conv2/bias/ExponentialMovingAverage': ,
# 'layer1-conv1/bias/ExponentialMovingAverage': ,
# 'layer6-fc2/bias/ExponentialMovingAverage': ,
# 'layer3-conv2/weight/ExponentialMovingAverage': ,
# 'layer6-fc2/weight/ExponentialMovingAverage': ,
# 'layer1-conv1/weight/ExponentialMovingAverage': ,
# 'layer5-fc1/bias/ExponentialMovingAverage': ,
# 'layer5-fc1/weight/ExponentialMovingAverage': }
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
while True:
with tf.Session(config=config) as sess:
# 找到文件名
ckpt = tf.train.get_checkpoint_state(cnn.mnist_train.MODEL_SAVE_PATH)
# print(ckpt)
# model_checkpoint_path: "/path/to/model/cnn/model.ckpt-4001"
# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-1"
# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-1001"
# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-2001"
# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-3001"
# all_model_checkpoint_paths: "/path/to/model/cnn/model.ckpt-4001"
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=validate_feed)
print("After %s training stpe(s), validation accuracy = %g" % (global_step, accuracy_score))
else:
print("No checkpoint file found")
return
time.sleep(EVAL_INTERVAL_SECS)
def main(argv=None):
mnist = input_data.read_data_sets("/tmp/data", one_hot=True)
evaluate(mnist)
if __name__ == '__main__':
tf.app.run()
'''
After 1 training stpe(s), validation accuracy = 0.047
After 1001 training stpe(s), validation accuracy = 0.9848
After 2001 training stpe(s), validation accuracy = 0.9894
After 3001 training stpe(s), validation accuracy = 0.9904
...
...
After 27001 training stpe(s), validation accuracy = 0.9932
After 28001 training stpe(s), validation accuracy = 0.9926
After 29001 training stpe(s), validation accuracy = 0.993
'''