Scientific knowledge
NIPS (NeurIPS), the full name of the Conference and Workshop on Neural Information Processing Systems (Neural Information Processing Systems), is an international conference on machine learning and computational neuroscience. The conference is held in December every year and is sponsored by the NIPS Foundation. NIPS is the top conference in machine learning. In the ranking of international academic conferences of the China Computer Federation, NIPS is a Class A conference in the field of artificial intelligence.
# Preface
SEP.
In the previous article of the theoretical chapter, we learned about the residual network (ResNet). Its core idea is to build a residual structure through skip connections, so that the network can break through the depth limit and build a deeper network.
TensorFlow's ResNet combat
In this issue of practical sharing, we mainly practice the code Tensorflow of ResNet-18, which is commonly used in the ResNet network structure. The code this time is very concise, and I hope you can keep up with it all the way.
1. Data preparation
This time the data uses the RAFDB facial expression dataset, which contains seven categories: peaceful, happy, sad,
Surprised, disgusted, angry, scared. Similar to the previous dataset, this facial expression dataset also contains
Training set and test set, each set does not contain 7 folders (expressions). with the previous dataset
Same, the data set contains training set and test set, each set contains seven folders (expressions) and some samples show
2. Network structure
The above picture contains a common version of the ResNet network. This sharing takes the 18-layer ResNet-18 as an example. The network structure is:
# 残差块构建
def ResBlock(name, num_blocks, input, inchannel, outchannel, stride):
conv_input = input
conv_inchannel = inchannel
conv_stride = stride
for i in range(num_blocks):
out = Conv_layer(names = '{}{}'.format(name, i), input = conv_input , w_shape = [3, 3, conv_inchannel, outchannel], b_shape = [outchannel], strid = [conv_stride, conv_stride])
conv_input = out
conv_inchannel = outchannel
conv_stride = 1
# 残差
if stride > 1:
shortcut = Conv_layer(names = '{}_{}'.format(name,i), input = input , w_shape = [1, 1, inchannel, outchannel], b_shape = [outchannel], strid = [stride, stride])
out = out + shortcut
return out
# 残差网络构建
def inference(images, batch_size, n_classes,drop_rate):
print("******** images {} ".format(images.shape))
#第一层预处理卷积
pre_conv1 = Conv_layer(names = 'pre_conv', input = images , w_shape = [7, 7, 3, 64], b_shape = [64], strid = [2, 2])
print("******** pre_conv1 {} ".format(pre_conv1.shape))
# 池化层
pool_1 = Max_pool_lrn(names = 'pooling1', input = pre_conv1 , ksize = [1, 3, 3, 1], is_lrn = False)
# print("******** pool_1 {} ".format(pool_1.shape))
# 第一个卷积块(layer1)
layer1 = ResBlock('Resblock1', 2, pool_1, 64, 64, 1)
print("******** layer1 {} ".format(layer1.shape))
# 第二个卷积块(layer2)
layer2 = ResBlock('Resblock2', 2, layer1, 64, 128, 2)
print("******** layer2 {} ".format(layer2.shape))
# 第三个卷积块(layer3)
layer3 = ResBlock('Resblock3', 2, layer2, 128, 256, 2)
print("******** layer3 {} ".format(layer3.shape))
# 第四个卷积块(layer4)
layer4 = ResBlock('Resblock4', 2, layer3, 256, 512, 2)
print("******** layer4 {} ".format(layer4.shape))
# 全局平均池化
global_avg = tf.nn.avg_pool(layer4, ksize=[1,7,7,1],strides=[1,7,7,1],padding='SAME')
print("******** global_avg {} ".format(global_avg.shape))
reshape = tf.reshape(global_avg, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value
with tf.variable_scope('softmax_linear') as scope:
weights = tf.Variable(tf.truncated_normal(shape=[dim, n_classes], stddev=0.005, dtype=tf.float32),
name='softmax_linear', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[n_classes]),
name='biases', dtype=tf.float32)
resnet18_out = tf.add(tf.matmul(reshape, weights), biases, name='softmax_linear')
print("---------resnet18_out:{}".format(resnet18_out))
return resnet18_out
3. Training process
Source code acquisition: https://gitee.com/fengyuxiexie/res-net-18
END
epilogue
This concludes the sharing of this issue. The residual network is loved by many researchers because of its excellent performance. Therefore, the residual network has become a baseline model on many data sets.
Due to the simple implementation of the residual network structure, the editor hopes that you can implement it yourself after understanding the network structure, deeply understand the dimension transformation of images in the residual network, and learn the use of convolution step size and padding in Tensorflow. Further enhance the actual combat capability of Tensorflow.
In addition, due to the popularity of the Pytorch framework in the academic world, we may no longer use Tensorflow in the following articles, but directly use Pytorch to implement it. I hope you will continue to like it.
Editor: Layman Yueyi|Review: Layman Xiaoquanquan
Advanced IT Tour
Past review
Deep Learning Practical Chapter (15) -- GoogLeNet of TensorFlow
Deep Learning Practical Chapter (14) -- VGG16 of TensorFlow
Deep Learning Practical Chapter (13) -- AlexNet of TensorFlow
What have we done in the past year:
[Year-end Summary] Saying goodbye to the old and welcoming the new, 2020, let's start again
[Year-end summary] 2021, bid farewell to the old and welcome the new
Click "Like" and let's go~