这里以mnist(Lenet5)为例,提前训练循环一万次,并保存模型后,二次运行就直接调用保存好的模型,就不用再次训练,以减少时间。
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import os
import time
tic = time.time()
np.set_printoptions(suppress=True,threshold=1e6)
# 读取数据
mnist = input_data.read_data_sets('../../datas/MNIST.data',one_hot=True)
tf.set_random_seed(1)
# 站位
x = tf.placeholder(tf.float32,[None,784])
x_img = tf.reshape(x,[-1,28,28,1]) #把像素转换为图片28x28x1
y = tf.placeholder(tf.float32,[None,10])
# 比率站位
keep_prod = tf.placeholder(tf.float32)
# 第一层卷积核3x3x1 32个
w1 = tf.Variable(tf.random_normal([3,3,1,32],stddev=0.01))
L1 = tf.nn.conv2d(x_img,w1,strides=[1,1,1,1],padding='SAME') #步长1 same卷积
L1 = tf.nn.relu(L1)
L1 = tf.nn.max_pool(L1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') #大小2x2x1 步长2
L1 = tf.nn.dropout(L1,keep_prob=keep_prod) #去除部分连接
# Conv -> (?, 28, 28, 32)
# Pool -> (?, 14, 14, 32)
# 第二层卷积核3x3x32 64个
w2 = tf.Variable(tf.random_normal([3,3,32,64],stddev=0.01))
L2 = tf.nn.conv2d(L1,w2,strides=[1,1,1,1],padding='SAME')
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
L2 = tf.nn.dropout(L2,keep_prob=keep_prod)
# Conv ->(?, 14, 14, 64)
# Pool ->(?, 7, 7, 64)
# 第三层卷积核3x3x64 128个
w3 = tf.Variable(tf.random_normal([3,3,64,128],stddev=0.01))
L3 = tf.nn.conv2d(L2,w3,strides=[1,1,1,1],padding='SAME')
L3 = tf.nn.relu(L3)
L3 = tf.nn.max_pool(L3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
L3 = tf.nn.dropout(L3,keep_prob=keep_prod)
L3_flat = tf.reshape(L3,[-1,4*4*128]) #压平 成向量 全连接
# Conv ->(?, 7, 7, 128)
# Pool ->(?, 4, 4, 128)
# Reshape ->(?, 4 * 4 * 128) # Flatten them for FC
# L4 FC 4x4x128输入-> 625输出
w4 = tf.get_variable('w4',shape=[128*4*4,625],initializer=tf.contrib.layers.xavier_initializer())
b4 = tf.Variable(tf.random_normal([625]))
L4 = tf.nn.relu(tf.matmul(L3_flat,w4) + b4)
L4 = tf.nn.dropout(L4,keep_prob=keep_prod)
# 最后的FC 625输入-> 10输出
w5 = tf.get_variable('w5',shape=[625,10],initializer=tf.contrib.layers.xavier_initializer())
b5 = tf.Variable(tf.random_normal([10]))
a5 = tf.nn.softmax(tf.matmul(L4,w5) + b5)
# 代价
cost = -tf.reduce_mean(tf.reduce_sum(y*tf.log(a5),axis=1))
cost_history = []
# 优化器
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
# 开启会话
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
model_path = './bottleneck1'
training = 15 #周期
batch_size = 100 #批次
if not os.path.exists(model_path):
os.mkdir(model_path)
for i in range(1, 10001):
batch_x, batch_y = mnist.train.next_batch(batch_size)
c, _ = sess.run([cost, optimizer], feed_dict={x: batch_x, y: batch_y, keep_prod: 0.7})
if i % 1000 == 0:
print(i,c)
#保存模型
saver.save(sess, './bottleneck1/', global_step=i)
print('model save over')
else:
print('Start training model')
#读取模型
saver.restore(sess, tf.train.latest_checkpoint('./bottleneck1/'))
# 准确率
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(a5,1),tf.argmax(y,1)),tf.float32))
print('测试集准确率:',sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prod:1}))
import random
r = random.randint(0,mnist.test.num_examples-1)
# 标签下标
print(sess.run(tf.argmax(mnist.test.labels[r:r+1],1)))
# 预测下标
print(sess.run(tf.argmax(a5,1),feed_dict={x:mnist.test.images[r:r+1],keep_prod:1}))
toc = time.time()
#计算运行时间
print('used{:.5}s'.format(toc-tic))
Start training model
测试集准确率: 0.9943
[3]
[3]
used7.5436s
这里可以看到在保存模型后,一万次运行只需7.5s,大大缩短了运行时间