Understand and implement ResNet (Keras)

13894005-2261b2f2ba8c527c
image

This article is a compilation of AI Yanxishe technology blog, the original title:

Understanding and Coding a ResNet in Keras

作者 | * Priya Dwivedi @ Deep Learning Analytics *

Translation | linlh, all night long edit | • Jeff Demps, Pita

Original link:

https://towardsdatascience.com/understanding-and-coding-a-resnet-in-keras-446d7ff84d33

ResNet is an abbreviation of residual network (Residual Network), is a computer vision tasks as the backbone of many classic neural network. This model is ImageNet Challenge 2015 winner, ResNet most fundamental breakthrough is that it allows us to successfully trained very deep neural networks, such as the 150+ network layer. Before ResNet, since the gradient disappears (vanishing gradients) problem, very deep neural network training is very difficult.

AlexNet, 2012 Nian ImageNet winner, this model is obviously concerned about solving convolution only eight layers deep learning, VGG network has 19 floors, Inception or GoogleNet have 22 floors, ResNet 152 152 layer. In this article, we will write a ResNet-50's network, ResNet smaller version of the 152, often beginning with a study on migration.

13894005-d1f42c4cecce9b74
image

However, to enhance the depth of the network is not a simple network layer stack up. The reason underlying network training is difficult, because the gradient disappears very annoying problem - as the gradient back-propagation network back to the previous layer and repeat the operation of the product will make the gradient becomes very small. The result, as the network more and more, its performance becomes saturated, and began to decline rapidly.

I was learning to write about the content of the ResNet, highly recommend you watch this program on DeepLearning.AI Andrew Ng's courses.

On my Github repo, I shared two Jupyter Notebook, as one of the DeepLearning.AI, the coding from scratch ResNet, another pre-trained in the use of models in Keras. I hope you can download the code and try it for yourself.

Residual connection (Skip Connection) - ResNet strengths

ResNet was the first to propose the concept of residual connections. The following figure illustrates the residual connections. The left figure shows a stack of the network layer, layer by layer. In the diagram on the right, we still saw a stack before the network layer, but we will also be added to the original input layer unit of output volume.

13894005-b408967e2cf11268
image

Residual connection diagram (from DeepLearning.AI)

The following two lines of code can be written as:

X_shortcut = X # Store the initial value of X in a variable
## Perform convolution + batch norm operations on X

X = Add()([X, X_shortcut]) # SKIP Connection

Code is very simple, but there is a very important consideration - above X, X_shortcut two matrices, they are only in the same shape, you can add. Thus, if the specification convolution + batch (batch norm) operates to output the same shape is accomplished, then we can simply add them, as shown below.

13894005-d2b0fdb3eff06139
image

When x is the same shape and x_shortcut

Otherwise, x_shortcut selected by convolution layer, so that it is the same as the output of the convolution block, as follows:

13894005-bdd33ab498ad57bf
image

By convolution unit X_shortcut

Github on the Notebook, identity_block convolution_block two functions and achieve the above content. These functions are implemented using Keras Convolution and Batch Norm layer having ReLU activation function. Residual connection is achieved on this line of code: X = Add () ([X, X_shortcut]).

Note here that an important thing is that the residual connection is applied before ReLU activation function, as shown on FIG. The researchers found that so you can get the best results.

** Why do you want to skip the connection? **

This is an interesting question. I think here there are two reasons to skip the connection:

  1. They disappear gradient to alleviate the problem by optional shortcut this by allowing the gradient

  2. They allow a model to learn the identity function, which ensure high level of performance at least as good as low-level, rather than worse.

In fact, since the connection is skipped ResNet more models for architecture, such as the full convolution network (the FCN) and U-Net. They are used to pass information from the earlier model layer to the layer later. In these architectures, they are used to pass information from the lower layer of the sample to the sample layer.

** Our test model built ResNet **

The notebook is then encoded and convolutional identity blocks are combined to create a ResNet-50 model, its structure is as follows:

13894005-a115df26d0f3bc43
image

* ResNet-50 model *

ResNet-50 model consists of five stages, and each stage has a convolution of identical blocks. Each block has three convolution convolution layer of each unit block has three layers convolution. ResNet-50 has more than 23 million trainable parameters.

I tested this model on signs the data set already included in my Github repo. This data set corresponding to the class of manual image 6. We have a 1080 train images and 120 test images.

13894005-4e62074a46152959
image

* * Symbol data sets

Our ResNet-50 through 25 stages of training, test accuracy of 86%. Yes!

ResNet constructed using pre-trained library in Keras

I like to write your own ResNet model, because it makes me a better understanding of the network and I often use in image classification, object location, and other related division of the migration of many learning tasks.

However, the more common practice, ResNet-50 model in Keras pre-training faster. Keras has a backbone of many of these models, which are available in libraries Imagenet weight.

13894005-8f89895b43270001
image

Keras pre-trained model

I uploaded a Notebook on Github, using a model Keras to load a pre-trained ResNet-50. You can use a line of code to load the model:

base_model = applications.resnet50.ResNet50(weights= None, include_top=False, input_shape= (img_height,img_width,3))

Here weights = None, because I want to re-initialize the model with random right, just like I did when ResNet-50 I coding. Right or you can load pre-trained ImageNet weight. Provided include_top = False, it indicates the original model is not included in the final cell layer (Pooling) and the fully connected layers (fully connected). I added the global average cell layer (global average pooling) and dense output layer (dense output) in ResNet50 model.

x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.7)(x)
predictions = Dense(num_classes, activation= 'softmax')(x)
model = Model(inputs = base_model.input, outputs = predictions)

Can be seen from the above, Keras provides a very convenient interface to load pre-training model, it is important that at least once for ResNet own coding, so you can understand this concept, and this study can be applied to your being created another new architecture.

The Keras ResNet model to get the right rate of 75% after using Adam optimizer and learning rate of 0.0001, trained 100 epoch. The correct rate is lower than my own coded model number, I think it should be related to the weight initialization.

Keras also provides a very simple data enhancement (data augmentation) interface, so if you have the opportunity to try on the enhanced data set, look at the results if you can get better performance.

** to sum up**

  • ResNet is a very powerful model backbone (backbone model), often used in many computer vision tasks

  • ResNet using a residual connection (skip connection) to add the output of the earlier network layer back to a more network layer. This helps alleviate the problem of disappearing gradient

  • You can use the pre-loaded Keras training ResNet-50 model or use the code I have written to share ResNet model.

I have my own consulting work depth study, like the study of interesting questions. I have helped many start-up companies to deploy innovative solutions based on AI. Please visit http://deeplearninganalytics.org/ see us.

You can also see my other articles on the medium:

https://medium.com/@priya.dwivedi

reference

  • DeepLearning.AI

  • Hard

  • ReNet Paper

I want to continue to view the articles Related links and references?

Click understood and ResNet (Keras) to access!

Recommended resources today: top computer vision will CVPR 2019 to be held in June in the United States Long Beach, the General Assembly this year received a total of more than 5165 Paper Submission, as ultimate collection of papers 1299. As the meeting approaches, either academia or industry have ushered in a wave CVPR 2019 selected papers interpreted boom. This collection of papers from nearly 300 CVPR 2019 Oral papers featured dozens of papers, including the popular areas of the field of face detection, object detection, gesture recognition, robotics, for you developers reading and academic research youth.

https://ai.yanxishe.com/page/resourceDetail/819

Reproduced in: https: //www.jianshu.com/p/28a8fb144635

Guess you like

Origin blog.csdn.net/weixin_34375233/article/details/91170289