卷积运算基本概念
程序11-1:
import numpy as np
dateMat = np.ones((7,7))
kernel = np.array([[2,1,1],[3,0,1],[1,1,0]])#卷积核
def convolve(dateMat,kernel):
m,n = dateMat.shape
km,kn = kernel.shape
newMat = np.ones(((m - km + 1),(n - kn + 1)))
tempMat = np.ones(((km),(kn)))
for row in range(m - km + 1):#从左到右
for col in range(n - kn + 1):#从上到下
for m_k in range(km):
for n_k in range(kn):
tempMat[m_k,n_k] = dateMat[(row + m_k),(col + n_k)] * kernel[m_k,n_k]
newMat[row,col] = np.sum(tempMat)
return newMat
程序11-2:
import tensorflow as tf
input = tf.Variable(tf.random_normal([1, 3, 3, 1]))
filter = tf.Variable(tf.ones([1, 1, 1, 1]))#卷积核
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
conv2d = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
print(sess.run(conv2d))
结果:
[[[[-0.6038895 ]
[ 1.3880277 ]
[-0.55250233]]
[[-1.5142695 ]
[ 1.3565125 ]
[-1.6141095 ]]
[[ 1.1786922 ]
[ 1.062755 ]
[-0.60056657]]]]
程序11-3:
import tensorflow as tf
input = tf.Variable(tf.random_normal([1,5,5,5]))#图片5*5,5通道
filter = tf.Variable(tf.ones([1,1,5,1]))#卷积核1*1
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
conv2d = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='VALID')
print(sess.run(conv2d))
结果:
[[[[ 2.1486616 ]
[-1.6250083 ]
[ 1.0605603 ]
[ 0.753872 ]
[-3.1178155 ]]
[[ 1.023294 ]
[-0.9213852 ]
[ 3.0557642 ]
[-0.05864181]
[ 1.683459 ]]
[[ 2.4802954 ]
[ 1.2296188 ]
[ 0.9990875 ]
[-0.5168697 ]
[-1.4944886 ]]
[[ 1.5258802 ]
[-0.45413285]
[-1.7837254 ]
[-3.3443706 ]
[-2.4967175 ]]
[[-3.5222106 ]
[ 3.950252 ]
[-1.177776 ]
[-2.2402415 ]
[-4.414331 ]]]]
程序11-4:
import tensorflow as tf
input = tf.Variable(tf.random_normal([1, 5, 5, 5]))
filter = tf.Variable(tf.ones([1, 1, 5, 1]))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
conv2d = tf.nn.conv2d(input, filter, strides=[1, 1, 1, 1], padding='SAME')#补0
print(sess.run(conv2d))
结果:
[[[[-3.111798 ]
[-1.3990246 ]
[ 1.080067 ]
[ 0.42263 ]
[-2.0002058 ]]
[[-1.1735396 ]
[ 1.4240425 ]
[ 0.428392 ]
[-1.0098026 ]
[-2.6465251 ]]
[[-2.1096897 ]
[-1.2200532 ]
[ 5.0949807 ]
[ 1.7395508 ]
[-0.31153533]]
[[ 3.7215576 ]
[-2.8665297 ]
[-0.17784858]
[ 3.4722195 ]
[ 1.2791455 ]]
[[-2.967908 ]
[-1.3078861 ]
[-0.2683595 ]
[-1.6859698 ]
[ 0.64759237]]]]
程序11-5:
import tensorflow as tf
input = tf.Variable(tf.random_normal([1, 5, 5, 5]))
filter = tf.Variable(tf.ones([3, 3, 5, 1]))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
conv2d = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
print(sess.run(conv2d))
结果报错:
tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
(0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[node Conv2D (defined at E:/PycharmProjects/test20190712/test0712.py:10) ]]
(1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[node Conv2D (defined at E:/PycharmProjects/test20190712/test0712.py:10) ]]
[[Conv2D/_1]]
0 successful operations.
0 derived errors ignored.
Errors may have originated from an input operation.
Input Source operations connected to node Conv2D:
Variable_1/read (defined at E:/PycharmProjects/test20190712/test0712.py:4)
Variable/read (defined at E:/PycharmProjects/test20190712/test0712.py:3)
Input Source operations connected to node Conv2D:
Variable_1/read (defined at E:/PycharmProjects/test20190712/test0712.py:4)
Variable/read (defined at E:/PycharmProjects/test20190712/test0712.py:3)
Original stack trace for 'Conv2D':
File "E:/PycharmProjects/test20190712/test0712.py", line 10, in <module>
conv2d = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
使用卷积函数对图像感兴趣区域进行标注
程序11-6:
import tensorflow as tf
import cv2
import numpy as np
img = cv2.imread("lena.jpg")
img = np.array(img,dtype=np.float32)
img=img.resize()
x_image=tf.reshape(img,[1, 512,512,3])
filter = tf.Variable(tf.ones([7, 7, 3, 1]))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
res = tf.nn.conv2d(x_image, filter, strides=[1, 2, 2, 1], padding='SAME')
res_image = sess.run(tf.reshape(res,[256,256]))/128 + 1
cv2.imshow("lena",res_image.astype('uint8'))
cv2.waitKey()
结果报错:
Original stack trace for 'Conv2D':
File "E:/PycharmProjects/test20190712/test0712.py", line 13, in <module>
res = tf.nn.conv2d(x_image, filter, strides=[1, 2, 2, 1], padding='SAME')
程序11-7:
import tensorflow as tf
import cv2
import numpy as np
img = cv2.imread("lena.jpg")
img = np.array(img,dtype=np.float32)
x_image=tf.reshape(img,[1,512,512,3])
filter = tf.Variable(tf.ones([11, 11, 3, 1]))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
res = tf.nn.conv2d(x_image, filter, strides=[1, 2, 2, 1], padding='SAME')
res_image = sess.run(tf.reshape(res,[256,256]))/128 + 1
cv2.imshow("lena",res_image.astype('uint8'))
cv2.waitKey()
结果报错:
Traceback (most recent call last):
File "D:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1864, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot reshape a tensor with 196608 elements to shape [1,512,512,3] (786432 elements) for 'Reshape' (op: 'Reshape') with input shapes: [256,256,3], [4] and with input tensors computed as partial shapes: input[1] = [1,512,512,3].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/PycharmProjects/test20190712/test0712.py", line 7, in <module>
x_image=tf.reshape(img,[1,512,512,3])
池化运算
程序11-8:
import tensorflow as tf
data=tf.constant([
[[3.0,2.0,3.0,4.0],
[2.0,6.0,2.0,4.0],
[1.0,2.0,1.0,5.0],
[4.0,3.0,2.0,1.0]]
])
data = tf.reshape(data,[1,4,4,1])
maxPooling=tf.nn.max_pool(data, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')
with tf.Session() as sess:
print(sess.run(maxPooling))
结果:
[[[[6.]
[4.]]
[[4.]
[5.]]]]
使用池化运算加强卷积特征提取
程序11-9:
import tensorflow as tf
import cv2
import numpy as np
img = cv2.imread("lena.jpg")
img = np.array(img,dtype=np.float32)
x_image=tf.reshape(img,[1,512,512,3])
filter = tf.Variable(tf.ones([7, 7, 3, 1]))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
res = tf.nn.conv2d(x_image, filter, strides=[1, 2, 2, 1], padding='SAME')
res = tf.nn.max_pool(res, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')
res_image = sess.run(tf.reshape(res,[128,128]))/128 + 1
cv2.imshow("lena",res_image.astype('uint8'))
cv2.waitKey()
结果报错
TensorFlow实现LeNet实例
程序11-10:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time
# 声明输入图片数据,类别
x = tf.placeholder('float', [None, 784])#minist数据集以[None, 784]数据格式存放
y_ = tf.placeholder('float', [None, 10])
# 输入图片数据转化 一维数组转化为二维图像矩阵
x_image = tf.reshape(x, [-1, 28, 28, 1])
#第一层卷积层,初始化卷积核参数、偏置值,该卷积层5*5大小,一个通道,共有6个不同卷积核
filter1 = tf.Variable(tf.truncated_normal([5, 5, 1, 6]))
bias1 = tf.Variable(tf.truncated_normal([6]))
conv1 = tf.nn.conv2d(x_image, filter1, strides=[1, 1, 1, 1], padding='SAME')
h_conv1 = tf.nn.sigmoid(conv1 + bias1)
maxPool2 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
filter2 = tf.Variable(tf.truncated_normal([5, 5, 6, 16]))
bias2 = tf.Variable(tf.truncated_normal([16]))
conv2 = tf.nn.conv2d(maxPool2, filter2, strides=[1, 1, 1, 1], padding='SAME')
h_conv2 = tf.nn.sigmoid(conv2 + bias2)
maxPool3 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
filter3 = tf.Variable(tf.truncated_normal([5, 5, 16, 120]))
bias3 = tf.Variable(tf.truncated_normal([120]))
conv3 = tf.nn.conv2d(maxPool3, filter3, strides=[1, 1, 1, 1], padding='SAME')
h_conv3 = tf.nn.sigmoid(conv3 + bias3)
# 全连接层
# 权值参数
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 120, 80]))
# 偏置值
b_fc1 = tf.Variable(tf.truncated_normal([80]))
# 将卷积的产出展开
h_pool2_flat = tf.reshape(h_conv3, [-1, 7 * 7 * 120])
# 神经网络计算,并添加sigmoid激活函数
h_fc1 = tf.nn.sigmoid(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# 输出层,使用softmax进行多分类
W_fc2 = tf.Variable(tf.truncated_normal([80, 10]))
b_fc2 = tf.Variable(tf.truncated_normal([10]))
y_conv = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
# 损失函数
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
# 使用GDO优化算法来调整参数
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
sess = tf.InteractiveSession()
# 测试正确率
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
# 所有变量进行初始化
sess.run(tf.global_variables_initializer())
# 获取mnist数据
mnist_data_set = input_data.read_data_sets('MNIST_data', one_hot=True)
# 进行训练
start_time = time.time()
for i in range(20000):
# 获取训练数据
batch_xs, batch_ys = mnist_data_set.train.next_batch(200)
# 每迭代100个 batch,对当前训练数据进行测试,输出当前预测准确率
if i % 2 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys})
print("step %d, training accuracy %g" % (i, train_accuracy))
# 计算间隔时间
end_time = time.time()
print('time: ', (end_time - start_time))
start_time = end_time
# 训练数据
train_step.run(feed_dict={x: batch_xs, y_: batch_ys})
# 关闭会话
sess.close()
结果:
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data\train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
step 0, training accuracy 0.095
time: 1.8945338726043701
step 2, training accuracy 0.105
time: 0.10003161430358887
step 4, training accuracy 0.14
time: 0.009999513626098633
step 6, training accuracy 0.08
time: 0.020000934600830078
...
time: 0.02003312110900879
step 19992, training accuracy 0.985
time: 0.009978294372558594
step 19994, training accuracy 0.98
time: 0.01999044418334961
step 19996, training accuracy 0.99
time: 0.01002955436706543
step 19998, training accuracy 0.97
time: 0.01999664306640625
程序11-11:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time
# 声明输入图片数据,类别
x = tf.placeholder('float', [None, 784])
y_ = tf.placeholder('float', [None, 10])
# 输入图片数据转化
x_image = tf.reshape(x, [-1, 28, 28, 1])
#第一层卷积层,初始化卷积核参数、偏置值,该卷积层5*5大小,一个通道,共有6个不同卷积核
filter1 = tf.Variable(tf.truncated_normal([5, 5, 1, 6]))
bias1 = tf.Variable(tf.truncated_normal([6]))
conv1 = tf.nn.conv2d(x_image, filter1, strides=[1, 1, 1, 1], padding='SAME')
h_conv1 = tf.nn.relu(conv1 + bias1)
maxPool2 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
filter2 = tf.Variable(tf.truncated_normal([5, 5, 6, 16]))
bias2 = tf.Variable(tf.truncated_normal([16]))
conv2 = tf.nn.conv2d(maxPool2, filter2, strides=[1, 1, 1, 1], padding='SAME')
h_conv2 = tf.nn.relu(conv2 + bias2)#激活函数换为relu
maxPool3 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
filter3 = tf.Variable(tf.truncated_normal([5, 5, 16, 120]))
bias3 = tf.Variable(tf.truncated_normal([120]))
conv3 = tf.nn.conv2d(maxPool3, filter3, strides=[1, 1, 1, 1], padding='SAME')
h_conv3 = tf.nn.relu(conv3 + bias3)
# 全连接层
# 权值参数
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 120, 80]))
# 偏置值
b_fc1 = tf.Variable(tf.truncated_normal([80]))
# 将卷积的产出展开
h_pool2_flat = tf.reshape(h_conv3, [-1, 7 * 7 * 120])
# 神经网络计算,并添加relu激活函数
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# 输出层,使用softmax进行多分类
W_fc2 = tf.Variable(tf.truncated_normal([80, 10]))
b_fc2 = tf.Variable(tf.truncated_normal([10]))
y_conv = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
# 损失函数
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
# 使用GDO优化算法来调整参数
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
sess = tf.InteractiveSession()
# 测试正确率
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
# 所有变量进行初始化
sess.run(tf.global_variables_initializer())
# 获取mnist数据
mnist_data_set = input_data.read_data_sets('D:/python_models/MNIST_data/', one_hot=True)
# 进行训练
start_time = time.time()
for i in range(20000):
# 获取训练数据
batch_xs, batch_ys = mnist_data_set.train.next_batch(200)
# 每迭代100个 batch,对当前训练数据进行测试,输出当前预测准确率
if i % 2 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys})
print("step %d, training accuracy %g" % (i, train_accuracy))
# 计算间隔时间
end_time = time.time()
print('time: ', (end_time - start_time))
start_time = end_time
# 训练数据
train_step.run(feed_dict={x: batch_xs, y_: batch_ys})
# 关闭会话
sess.close()
结果:
Extracting D:/python_models/MNIST_data/train-images-idx3-ubyte.gz
Extracting D:/python_models/MNIST_data/train-labels-idx1-ubyte.gz
Extracting D:/python_models/MNIST_data/t10k-images-idx3-ubyte.gz
Extracting D:/python_models/MNIST_data/t10k-labels-idx1-ubyte.gz
step 0, training accuracy 0.19
time: 4.418234586715698
step 2, training accuracy 0.065
time: 0.10991549491882324
step 4, training accuracy 0.085
time: 0.010030031204223633
step 6, training accuracy 0.15
time: 0.020003080368041992
...
step 19990, training accuracy 0.095
time: 0.01299428939819336
step 19992, training accuracy 0.085
time: 0.010038614273071289
step 19994, training accuracy 0.06
time: 0.019990205764770508
step 19996, training accuracy 0.1
time: 0.009974241256713867
step 19998, training accuracy 0.1
time: 0.01999974250793457
程序11-12:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time
import matplotlib.pyplot as plt
def weight_variable(shape):#根据传递进来的矩阵元素个数生成一个标准差为0.1的矩阵
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
#初始化单个卷积核上的偏置值
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)#生成一个值为0.1的矩阵
return tf.Variable(initial)
#输入特征x,用卷积核W进行卷积运算,strides为卷积核移动步长,
#padding表示是否需要补齐边缘像素使输出图像大小不变
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')#步长和边距都相同,保证输入和输出同样大小
#对x进行最大池化操作,ksize进行池化的范围,
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')#池化模板大小2*2
sess = tf.InteractiveSession()
# 声明输入图片数据、类别
x = tf.placeholder('float32', [None, 784])
y_ = tf.placeholder('float32', [None, 10])
# 输入图片数据转化
x_image = tf.reshape(x, [-1, 28, 28, 1])
W_conv1 = weight_variable([5, 5, 1, 6])#patch大小,输入、输出通道数目 5*5的patch算出6个特征
b_conv1 = bias_variable([6])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 6, 16])#5*5的patch算出16个特征
b_conv2 = bias_variable([16])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7*7*16,120])
# 偏置值
b_fc1 = bias_variable([120])
# 将卷积的产出展开
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 16])
# 神经网络计算,并添加relu激活函数
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
W_fc2 = weight_variable([120,10])
b_fc2 = bias_variable([10])
#softmax函数用于计算输出的数据对应于分类概率的大小
y_conv = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
# 代价函数
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
# 使用Adam优化算法来调整参数
train_step = tf.train.GradientDescentOptimizer(1e-4).minimize(cross_entropy)
# 测试正确率
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float32"))
# 所有变量进行初始化
sess.run(tf.global_variables_initializer())
# 获取mnist数据
mnist_data_set = input_data.read_data_sets('D:/python_models/MNIST_data/', one_hot=True)
c = []
# 进行训练
start_time = time.time()
for i in range(1000):
# 获取训练数据
batch_xs, batch_ys = mnist_data_set.train.next_batch(200)
# 每迭代10个 batch,对当前训练数据进行测试,输出当前预测准确率
if i % 2 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys})
c.append(train_accuracy)
print("step %d, training accuracy %g" % (i, train_accuracy))
# 计算间隔时间
end_time = time.time()
print('time: ', (end_time - start_time))
start_time = end_time
# 训练数据
train_step.run(feed_dict={x: batch_xs, y_: batch_ys})
sess.close()
plt.plot(c)
plt.tight_layout()
plt.savefig('cnn-tf-cifar10-2.png', dpi=200)
结果:
....
step 990, training accuracy 0.975
time: 0.008978605270385742
step 992, training accuracy 0.97
time: 0.009010791778564453
step 994, training accuracy 0.955
time: 0.008938074111938477
step 996, training accuracy 0.955
time: 0.008009672164916992
step 998, training accuracy 0.965
time: 0.008960485458374023
代码同级目录下生成cnn-tf-cifar10-2.png图片:
程序11-13:
#卷积核和隐藏层参数的修改
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time
import matplotlib.pyplot as plt
def weight_variable(shape):#根据传递进来的矩阵元素个数生成一个标准差为0.1的矩阵
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
#初始化单个卷积核上的偏置值
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)#生成一个值为0.1的矩阵
return tf.Variable(initial)
#输入特征x,用卷积核W进行卷积运算,strides为卷积核移动步长,
#padding表示是否需要补齐边缘像素使输出图像大小不变
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')#步长和边距都相同,保证输入和输出同样大小
#对x进行最大池化操作,ksize进行池化的范围,
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')#池化模板大小2*2
sess = tf.InteractiveSession()
# 声明输入图片数据、类别
x = tf.placeholder('float32', [None, 784])
y_ = tf.placeholder('float32', [None, 10])
# 输入图片数据转化
x_image = tf.reshape(x, [-1, 28, 28, 1])
W_conv1 = weight_variable([5, 5, 1, 32])#patch大小,输入、输出通道数目 5*5的patch算出6个特征
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])#5*5的patch算出16个特征
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7*7*64,1024])
# 偏置值
b_fc1 = bias_variable([1024])
# 将卷积的产出展开
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
# 神经网络计算,并添加relu激活函数
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
W_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])
#softmax函数用于计算输出的数据对应于分类概率的大小
y_conv = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
# 代价函数
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
# 使用Adam优化算法来调整参数
train_step = tf.train.GradientDescentOptimizer(1e-4).minimize(cross_entropy)
# 测试正确率
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float32"))
# 所有变量进行初始化
sess.run(tf.global_variables_initializer())
# 获取mnist数据
mnist_data_set = input_data.read_data_sets('MNIST_data', one_hot=True)
c = []
# 进行训练
start_time = time.time()
for i in range(1000):
# 获取训练数据
batch_xs, batch_ys = mnist_data_set.train.next_batch(200)
# 每迭代10个 batch,对当前训练数据进行测试,输出当前预测准确率
if i % 2 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys})
c.append(train_accuracy)
print("step %d, training accuracy %g" % (i, train_accuracy))
# 计算间隔时间
end_time = time.time()
print('time: ', (end_time - start_time))
start_time = end_time
# 训练数据
train_step.run(feed_dict={x: batch_xs, y_: batch_ys})
sess.close()
plt.plot(c)
plt.tight_layout()
plt.savefig('cnn-tf-cifar10-1.png', dpi=200)
结果:
...
step 990, training accuracy 0.13
time: 0.029993772506713867
step 992, training accuracy 0.11
time: 0.020006656646728516
step 994, training accuracy 0.09
time: 0.029967308044433594
step 996, training accuracy 0.085
time: 0.02003169059753418
step 998, training accuracy 0.115
time: 0.020000219345092773
代码同级目录下生成cnn-tf-cifar10-1.png图片:
程序11-14:
#全连接层的数目增加一层
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time
import matplotlib.pyplot as plt
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
#初始化单个卷积核上的偏置值
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
#输入特征x,用卷积核W进行卷积运算,strides为卷积核移动步长,
#padding表示是否需要补齐边缘像素使输出图像大小不变
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
#对x进行最大池化操作,ksize进行池化的范围,
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
sess = tf.InteractiveSession()
# 声明输入图片数据、类别
x = tf.placeholder('float32', [None, 784])
y_ = tf.placeholder('float32', [None, 10])
# 输入图片数据转化
x_image = tf.reshape(x, [-1, 28, 28, 1])
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7*7*64,1024])
# 偏置值
b_fc1 = bias_variable([1024])
# 将卷积的产出展开
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
# 神经网络计算,并添加relu激活函数
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
W_fc2 = weight_variable([1024,128])
b_fc2 = bias_variable([128])
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2)
W_fc3 = weight_variable([128,10])
b_fc3 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc2, W_fc3) + b_fc3)
# 代价函数
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
# 使用Adam优化算法来调整参数
train_step = tf.train.GradientDescentOptimizer(1e-5).minimize(cross_entropy)
# 测试正确率
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float32"))
# 所有变量进行初始化
sess.run(tf.global_variables_initializer())
# 获取mnist数据
mnist_data_set = input_data.read_data_sets('MNIST_data', one_hot=True)
c = []
# 进行训练
start_time = time.time()
for i in range(1000):
# 获取训练数据
batch_xs, batch_ys = mnist_data_set.train.next_batch(200)
# 每迭代10个 batch,对当前训练数据进行测试,输出当前预测准确率
if i % 2 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys})
c.append(train_accuracy)
print("step %d, training accuracy %g" % (i, train_accuracy))
# 计算间隔时间
end_time = time.time()
print('time: ', (end_time - start_time))
start_time = end_time
# 训练数据
train_step.run(feed_dict={x: batch_xs, y_: batch_ys})
sess.close()
plt.plot(c)
plt.tight_layout()
plt.savefig('cnn-tf-cifar10-11.png', dpi=200)
结果:
...
step 988, training accuracy 0.925
time: 0.024901151657104492
step 990, training accuracy 0.97
time: 0.025961637496948242
step 992, training accuracy 0.925
time: 0.025899887084960938
step 994, training accuracy 0.925
time: 0.025962352752685547
step 996, training accuracy 0.91
time: 0.02593207359313965
step 998, training accuracy 0.925
time: 0.02592921257019043
代码同级目录下生成cnn-tf-cifar10-11.png图片: