在上一篇博客 使用Tensorflow制作球鞋识别模型(二)——图片输入处理中已经介绍了图片输入处理,至此图片准备和处理处理已经结束,接下来就是建立神经网络。
网络结构
神经网络包含3层卷积+池化层,2层全连接层,使用ReLu激活函数,Softmax函数做分类器。
定义神经网络
导入包
首先导入所需的包
import tensorflow as tf
定义函数
def weight_variable(shape, n):
# tf.truncated_normal(shape, mean, stddev)从截断的正态分布中输出随机值
# stddev是标准差,默认最大为1,最小为-1,均值为0
initial = tf.truncated_normal(shape, stddev=n, dtype=tf.float32)
return initial
def bias_variable(shape):
# 创建一个结构为shape矩阵,初始化所有值为0.1
initial = tf.constant(0.1, shape=shape, dtype=tf.float32 )
return initial
def conv2d(x, W):
# 卷积函数,步长为1,边缘自动补0
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x, name):
# 尺寸为3×3,步长为2的×2的最大池化层
return tf.nn.max_pool(x, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
卷积神经网络
def inference(images, batch_size, n_classes):
# 卷积层1
with tf.variable_scope('conv1') as scope:
w_conv1 = tf.Variable(weight_variable([3, 3, 3, 64], 1.0), name='weights', dtype=tf.float32)
b_conv1 = tf.Variable(bias_variable([64]), name='biases', dtype=tf.float32)
h_conv1 = tf.nn.relu(conv2d(images, w_conv1) + b_conv1, name='conv1')
# 池化层1
with tf.variable_scope('pooling1_lrn') as scope:
pool1 = max_pool_2x2(h_conv1, 'pooling1')
# 卷积池化后使用LRN(局部相应归一化)增强模型的泛化能力
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm1')
# 卷积层2
with tf.variable_scope('conv2') as scope:
w_conv2 = tf.Variable(weight_variable([3, 3, 64, 32], 0.1), name='weights', dtype=tf.float32)
b_conv2 = tf.Variable(bias_variable([32]), name='biases', dtype=tf.float32)
h_conv2 = tf.nn.relu(conv2d(norm1, w_conv2) + b_conv2, name='conv2')
# 池化层2
with tf.variable_scope('pooling2_lrn') as scope:
pool2 = max_pool_2x2(h_conv2, 'pooling2')
norm2 = tf.nn.lrn(pool2, depth_radius=0.75, bias=1.0, alpha=1.0/0.9, beta=0.75, name='norm2')
# 卷积层3
with tf.variable_scope('conv3') as scope:
w_conv3 = tf.Variable(weight_variable([3, 3, 32, 16], 0.1), name='weights', dtype=tf.float32)
b_conv3 = tf.Variable(bias_variable([16]), name='biases', dtype=tf.float32)
h_conv3 = tf.nn.relu(conv2d(norm2, w_conv3) + b_conv3, name='conv3')
# 池化层3
with tf.variable_scope('pooling3_lrn') as scope:
pool3 = max_pool_2x2(h_conv3, 'pooling2')
norm3 = tf.nn.lrn(pool3, depth_radius=4, bias=1.0, alpha=1.0 / 0.9, beta=0.75, name='norm3')
# 全连接层1
with tf.variable_scope('local3') as scope:
reshape = tf.reshape(norm3, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value
w_fc1 = tf.Variable(weight_variable([dim, 128], 0.005), name='weights', dtype=tf.float32)
b_fc1 = tf.Variable(bias_variable([128]), name='biases', dtype=tf.float32)
h_fc1 = tf.nn.relu(tf.matmul(reshape, w_fc1) + b_fc1, name=scope.name)
# 全连接层2
with tf.variable_scope('local4') as scope:
w_fc2 = tf.Variable(weight_variable([128, 128], 0.005), name='weights', dtype=tf.float32)
b_fc2 = tf.Variable(bias_variable([128]), name='biases', dtype=tf.float32)
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, w_fc2) + b_fc2, name=scope.name)
h_fc2_dropout = tf.nn.dropout(h_fc2, 0.5)
with tf.variable_scope('softmax_linear') as scope:
weights = tf.Variable(weight_variable([128, n_classes], 0.005), name='weights', dtype=tf.float32)
biases = tf.Variable(bias_variable([n_classes]), name='biases', dtype=tf.float32)
softmax_linear = tf.add(tf.matmul(h_fc2_dropout, weights), biases, name='softmax_linear')
return softmax_linear
loss计算和优化
def losses(logits, labels):
with tf.variable_scope('loss') as scope:
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels, name='xentropy_per_example')
loss = tf.reduce_mean(cross_entropy, name='loss')
tf.summary.scalar(scope.name + '/loss', loss)
return loss
def trainning(loss, learning_rate):
with tf.name_scope('optimizer'):
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
global_step = tf.Variable(0, name='global_step', trainable=False)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
准确率计算
def evaluation(logits, labels):
with tf.variable_scope('accuracy') as scope:
# tf.nn.in_top_k(prediction, target, K)计算预测的结果和实际结果的是否相等
correct = tf.nn.in_top_k(logits, labels, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float16))
tf.summary.scalar(scope.name + '/accuracy', accuracy)
return accuracy
全部代码
import tensorflow as tf
def weight_variable(shape, n):
initial = tf.truncated_normal(shape, stddev=n, dtype=tf.float32)
return initial
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape, dtype=tf.float32 )
return initial
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x, name):
return tf.nn.max_pool(x, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name=name)
def inference(images, batch_size, n_classes):
# 卷积层1
with tf.variable_scope('conv1') as scope:
w_conv1 = tf.Variable(weight_variable([3, 3, 3, 64], 1.0), name='weights', dtype=tf.float32)
b_conv1 = tf.Variable(bias_variable([64]), name='biases', dtype=tf.float32)
h_conv1 = tf.nn.relu(conv2d(images, w_conv1) + b_conv1, name='conv1')
# 池化层1
with tf.variable_scope('pooling1_lrn') as scope:
pool1 = max_pool_2x2(h_conv1, 'pooling1')
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm1')
# 卷积层2
with tf.variable_scope('conv2') as scope:
w_conv2 = tf.Variable(weight_variable([3, 3, 64, 32], 0.1), name='weights', dtype=tf.float32)
b_conv2 = tf.Variable(bias_variable([32]), name='biases', dtype=tf.float32)
h_conv2 = tf.nn.relu(conv2d(norm1, w_conv2) + b_conv2, name='conv2')
# 池化层2
with tf.variable_scope('pooling2_lrn') as scope:
pool2 = max_pool_2x2(h_conv2, 'pooling2')
norm2 = tf.nn.lrn(pool2, depth_radius=0.75, bias=1.0, alpha=1.0/0.9, beta=0.75, name='norm2')
# 卷积层3
with tf.variable_scope('conv3') as scope:
w_conv3 = tf.Variable(weight_variable([3, 3, 32, 16], 0.1), name='weights', dtype=tf.float32)
b_conv3 = tf.Variable(bias_variable([16]), name='biases', dtype=tf.float32)
h_conv3 = tf.nn.relu(conv2d(norm2, w_conv3) + b_conv3, name='conv3')
# 池化层3
with tf.variable_scope('pooling3_lrn') as scope:
pool3 = max_pool_2x2(h_conv3, 'pooling2')
norm3 = tf.nn.lrn(pool3, depth_radius=4, bias=1.0, alpha=1.0 / 0.9, beta=0.75, name='norm3')
# 全连接层
with tf.variable_scope('local3') as scope:
reshape = tf.reshape(norm3, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value
w_fc1 = tf.Variable(weight_variable([dim, 128], 0.005), name='weights', dtype=tf.float32)
b_fc1 = tf.Variable(bias_variable([128]), name='biases', dtype=tf.float32)
h_fc1 = tf.nn.relu(tf.matmul(reshape, w_fc1) + b_fc1, name=scope.name)
# 全连接层2
with tf.variable_scope('local4') as scope:
w_fc2 = tf.Variable(weight_variable([128, 128], 0.005), name='weights', dtype=tf.float32)
b_fc2 = tf.Variable(bias_variable([128]), name='biases', dtype=tf.float32)
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, w_fc2) + b_fc2, name=scope.name)
h_fc2_dropout = tf.nn.dropout(h_fc2, 0.5)
with tf.variable_scope('softmax_linear') as scope:
weights = tf.Variable(weight_variable([128, n_classes], 0.005), name='weights', dtype=tf.float32)
biases = tf.Variable(bias_variable([n_classes]), name='biases', dtype=tf.float32)
softmax_linear = tf.add(tf.matmul(h_fc2_dropout, weights), biases, name='softmax_linear')
return softmax_linear
def losses(logits, labels):
with tf.variable_scope('loss') as scope:
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels, name='xentropy_per_example')
loss = tf.reduce_mean(cross_entropy, name='loss')
tf.summary.scalar(scope.name + '/loss', loss)
return loss
def trainning(loss, learning_rate):
with tf.name_scope('optimizer'):
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
global_step = tf.Variable(0, name='global_step', trainable=False)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
def evaluation(logits, labels):
with tf.variable_scope('accuracy') as scope:
correct = tf.nn.in_top_k(logits, labels, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float16))
tf.summary.scalar(scope.name + '/accuracy', accuracy)
return accuracy
项目代码
GitHub地址:https://github.com/WellTung666/Tensorflow/tree/master/AJ_Recognition