首先思考一个问题:计算机如何模拟人的行为进行识别?
每个像素块用RGB色彩模式表示。RGB色彩模式是工业界的一种颜色标准,是通过对红®、绿(G)、蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,RGB即是代表红、绿、蓝三个通道的颜色,这个标准几乎包括了人类视力所能感知的所有颜色,是目前运用最广的颜色系统之一。
轮廓特征:就是看这个人的脸型,五官的比例等
常用的轮廓特征检测方法叫做:边缘检测
局部特征就是:眼睛,鼻子,嘴
通过把图像的某一部分分裂出来,得到一个个的子图,就是局部特征。
神经网络和卷积神经网络的联系:神经网络也是一样, 组合局部特征,得到全局图像
卷积神经网络的原理
卷积神经网络的基础 – 卷积运算
卷积运算: 运算的规则就是把filter铺到原矩阵中,然后把filter与原矩阵中对应的数相乘再加和。
卷积神经网络的原理 - 边缘检测
代码实现:
"""
Date: 2019--18 11:27
User: yz
Email: 1147570523@qq.com
Desc:
"""
import numpy as np
from sklearn.datasets import load_sample_images
import tensorflow as tf
import matplotlib.pyplot as plt
dataset=np.array(load_sample_images().images,dtype=np.float32)
batch_size,height,width,channels=dataset.shape
print(batch_size,height,width,channels)
filters_test=np.zeros(shape=(7,7,channels,2),dtype=np.float32)
filters_test[:,3,:,0]=1
filters_test[:,1,:,0]=1
filters_test[3,:,:,1]=1
filters_test[1,:,:,1]=1
X=tf.placeholder(tf.float32,shape=(None,height,width,channels))
strides=[1,2,2,1]
convolution=tf.nn.conv2d(X,filter=filters_test,strides=[1,2,2,1],padding='SAME')
with tf.Session() as sess:
output=sess.run(convolution,feed_dict={X:dataset})
plt.imshow(load_sample_images().images[0])
plt.show()
plt.imshow(output[0,:,:,0])
plt.show()
plt.imshow(output[0,:,:,1])
plt.show()
卷积运算的缺点:
每次做卷积的时候,图像都会缩小
角落的像素点只会被使用一次,而中间的像素点会用到很多次,这意味着你丢掉了很多图像边缘的信息。
卷积神经网络的示例
池化层
池化层是用来缩减模型,提高计算速度,同时提高所取得特征的鲁棒性
池化层是用来降采样subsample, shrink, 减少计算负荷,内存使用,参数数量(防止过拟合)
正如卷积神经网络一样,在池化层中的每个神经元被连接到上面一层输出的神经元,只对应一小块感受野的区域。 我们必须定义大小,步长,padding类型
池化层没有权重值,他只是聚合输入根据取最大值或者求均值
池化层的例子: 2x2的池化核,步长为2,没有填充,只有最大值往下传,其他输入被丢弃掉了; 直观的理解说就是,因为取最大值,所以较小值就会被忽略,也就是不重要的特征会被忽略。
最大池化
卷积神经网络示例2 – 基于经典模型LeNet5
tensorflow_cnn_mnist执行逻辑
代码实现
"""
Date: 2019--18 14:26
User: yz
Email: 1147570523@qq.com
Desc:
"""
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets('MNIST_data_bak/',one_hot=True)
sess=tf.InteractiveSession()
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)
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding="SAME")
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
x=tf.placeholder(tf.float32,[None,784])
y_=tf.placeholder(tf.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])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prod=tf.placeholder(tf.float32)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prod)
W_fc2=weight_variable([1024,10])
b_fc2=bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2))
cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),reduction_indices=[1]))
train_step=tf.train.AdamOptimizer(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,tf.float32))
tf.global_variables_initializer().run()
for i in range(20000):
batch=mnist.train.next_batch(50)
if i %100==0:
train_accuracy=accuracy.eval(feed_dict={x:batch[0],y_:batch[1],keep_prod:1.0})
print("step %d,training accuracy %g"%(i,train_accuracy))
train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prod:0.5})
print("test accuracy %g "%accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prod: 1.0}))