メインコンテンツへスキップ
見出し画像

高度な深層学習の概念+CNN第2部 (Advanced Deep Learning Concepts + CNN Part 2)

    In this section, I'd like to discuss potential issues with deep networks, specifically vanishing gradients. I will also be mentioning the possible solutions for this, residual connections and batch normalization. Afterwards, I'll be touching on extra concepts of CNN that I didn't get to cover in my previous article. 

    Vanishing Gradients

    In order to understand vanishing gradients, let's take a look at the architecture of the pre-trained model VGG16.

    画像
    VGG16 Architecture

    The VGG16 architecture is like a pyramid. 
    In fact, most large pre-trained models adopt a pyramid structure like this, where the earlier layers are larger (smaller depth). The more downstream you go, the layer size reduces but depth increases. 
    These deeper stack of narrow layers perform better than shallow stack of large layers. 

    But a problem with this structure is that it causes vanishing gradients. 
    Vanishing gradients are when back propagation stops working and the model does not train. 

    So why would this happen?
    In a network like this, different layers are on top of each other.
    Computing output Y (Ypred) would look something like the following:

    画像
    Calculating Ypred

    Each layer in the network introduces some noise. This noise coming from hundreds to thousands of layers aggregate and overwhelm the gradient information, causing the back propagation to stop working. 

    There are two techniques that could help with vanishing gradients:

    Solution 1: Residual Connections

    画像
    Residual Connections

    For this diagram, you could think of "layers" as some random block of layers (e.g., 2 convolutional layers + 1 max pooling layer). 
    The big idea for residual connections is taking the input and adding it to the output of the block of layers. 

    Let's look at how this technique is implemented in practice:

    画像
    Residual Connections Example 1

    First, we define the input shape of the model (32 x 32 with depth of 3). 
    Add a convolution layer (chained to input layer). There are 32 3x3 filters so the depth becomes 32. 
    The output of this convolution layer will be saved in a variable called "residual."
    Add a second convolution layer (chained to first convolution layer). There are 64 3x3 filters so the new depth is 64. There is padding so the shape of the output will be same as the input (30 x 30). 

    What is important is the next step: we need to convert the depth of the data stored in "residual."
    For residual connections, the input is added to the output of the block of layers. However, the shapes are not the same (input is 30 x 30 x 32, output of last layer is 30 x 30 x 64.
    So, the shape of "residual" needs to be converted to 30 x 30 x 64 first. 
    This will be done using 64 1x1 filters (changes depth to 64 while maintaining shape). 
    Now, the residual can be added to x (2nd convolution layer output). 

    Let's look at another quick example. This time, the model has maxpool layers. 

    画像
    Residual Connections Example 2

    The only difference from the previous example is that this model has a maxpool layer after the second convolution layer. 
    As I explained in previous articles, maxpooling descales the input image (by a lot). 
    The maxpooling layer in the example above takes an input shape (30 x 30 x 64) and outputs (15 x 15 x 64). The depth stays the same since there is padding. 
    The input stored in the variable "residual" has a shape of 30 x 30 x 32. 
    We need this shape to match the output shape (15 x 15 x 64).
    So once again, we apply a convolution layer with 64 1x1 filters. Strides = 2 to descale the shape aswell. 
    Then the input can be added to the output of the layers. 

    Solution 2: Batch Normalization

    What is normalization in the first place?
    Normalization is scaling the data, which is done in the preprocessing data step of building the model.
    For example, the input values can be scaled: X/255.0 to make pixel values in range (0, 1). 
    Another way to normalize data

    画像
    Normalization

    Batch normalization is the same thing just done in batches.
    It adaptively normalizes data even as mean & variance changes during training.
    The reason as to why batch normalization alleviates vanishing gradients is very complicated. The effects are just observed. 

    Other concepts in Convolutional Neural Networks (CNN)

    Depthwise Separable Convolution Layers

    These type of convolution layers are helpful when creating efficient models that are smaller in size and trains faster while maintaining performance. 
    This is possible since depthwise separable convolution layers requires a smaller number of weights to be trained. 
    Lets compare this with a regular convolution layer:

    画像
    Regular Convolution Layer

    The depth of the filters are the same as the input data shape. 
    What about depthwise separable convolutional layers?

    画像
    Depthwise Separable Convolutional Layer

    These type of layers require two steps to do the same task a regular convolutional layer can do in one step.
    Step 1: Depthwise Convolution 
    The filter is still 3x3, but the depth is just 1 (does not match input shape). 
    This filter is placed on one surface of the 3 RGB surfaces, performs convolutional operation and generates a 2D shape output. 
    It repeats this for the other 2 surfaces, generating a 3D shape output. 
    Step 2: Pointwise Convolution 
    Now, 10 1x1 filters are used with a depth of 3 (matches the input shape).
    It will perform convolutional operation. 
    The depth will be 10 as there are 10 filters. 

    With depthwise separable convolutional layers, the number of parameters needed to be trained are far less, making the model more efficient. 

    Convolution 2D Transpose Layers

    A simple definition of these type of layers would be: the opposite of regular convolutional layers. 
    Regular convolutional layers descale the input image.
    Convolution 2D transpose layers upscales the input image. 
    Let's see what this means.

    画像
    Example: Stride = 1

    Let's say there is a 2x2 input and a 2x2 filter (stride = 1). 
    Each element in the input will be multiplied to all of the weights in the filter to generate a box.
    For example:
    1 * 1 = 1
    1 * 0 = 0
    1 * 0 = 0
    1 * 1 = 1
    So the upper-left box will be (1, 0, 0 1). This is done for all the input elements. 
    Afterwards, the yellow elements will be added together to generate a 3x3 output.
    The input shape (2x2) has been upscaled to an output shape of 3x3. 

    画像
    Example: Stride = 2

    Same operations happen for stride = 2. 
    The difference is, no elements need to be added up and the output will be 4x4. 
    For stride =3, the filter will move down 3 units and will have extra zeros populated in the output (the output will be even bigger).

    Purpose of Convolutional Transpose Layers:
    → Upscale (increase spatial dimensions of input feature map)
    → Balance out downscaling done by earlier layers (e.g., max-pooling, stride > 1 in convolutional layers) to restore the original spatial resolution of the input.
    Some applications: Semantic segmentation, GAN's, decoder networks etc. 

    Different computer vision tasks

    画像
    Different computer vision tasks

    There are different things you could make your model do.
    Single-label multi-class classification - classifies image as on output
    Multi-label classification - image can belong to multiple classes
    Image segmentation - segments image into different portions
    Object detection - recognizes different objects in an image (self driving cars would use this to identify obstacles, other cars, pedestrians etc). 


    This wraps up everything I want to talk about for CNNs. I'll come back with an article on recurrent neural networks (RNNs), which handles sequence data and is the basis for the AI models we have today

    あなたへのおすすめ