concept CNN in category Keras

appears as: CNN, A CNN, CNN
Probabilistic Deep Learning: With Python, Keras and TensorFlow Probability

This is an excerpt from Manning's book Probabilistic Deep Learning: With Python, Keras and TensorFlow Probability.

Now let’s talk about analogies between in fcNN and CNNs. For each neuron in an fcNN, a new set of weights is learnt (the learning process is discussed in chapter 3) that combines the input of the former layer to a new value that can be seen as a feature of the image (see, for example, figure 2.8). In an fcNN, you can go deep by adding layers where all neurons of one layer are connected to all neurons in the next layer. In this sense, the number of kernel sets or activation maps in a CNN correspond to the number of neurons in one layer of an fcNN. If you want to go deep in a CNN, you need to add more convolutional layers. This means you learn kernels that are again applied to the stack of activation maps of the previous layers.

Figure 2.25 illustrates this principle. In that figure, you see a CNN with 3 convolutional layers. The convolution over a stack of activation maps isn’t different than the convolution with an input of several channels. In figure 2.23, only 6 activation maps are generated from a 3-channel input image. However, learning only 6 kernels isn’t common. A typical number to learn is 32 kernels in the first layer and even more kernels (often the number of kernels doubles when moving from layer to layer). To reduce the number of weights in a CNN, it’s also common to downsample the activation maps before doing the next round of convolution. This is often done by replacing a 2 x 2 patch of neurons in an activation map with the maximal activation. This step is called max pooling.

When adding more layers to a CNN, the area that a neuron sees in the original image gets larger. This is called a receptive field, and it’s composed of all pixels in the original image to which the neuron is connected to, through all intermediate layers. Depending on the image size and the kernel size (often after around 4-10 layers), all neurons are connected to the whole input image. Still, the complexity of image patterns that activate the neurons in different layers of the CNN get higher with each layer.

When checking which images or image parts can activate a neuron in the different layers of a CNN, layers close to the input respond to simple image patterns (like edges) and layers close to the output combine these simple patterns into more complex patterns (see figure 2.24).

Figure 2.24: Image patterns that activate neurons in different convolutional layers of a CNN show a hierarchy of increasing complexity: simple patterns (like edges) combine into local objects (like eyes or ears), which combine into higher-level concepts (such as a cat). Figure used with permission from Deep Learning with Python (Manning, 2017).

The number of convolutional layers and the numbers of kernels within each layer are tuning parameters in a CNN. When the complexity of the problem increases, you usually need more convolutional layers and more kernels per layer. In the last convolution layer of the CNN, we’ve a new representation of the input. Flattening all neurons of this layer into a vector results in a new feature representation of the image with as many features as were neurons in the last convolutional layer. We end up with the same situation as before: the input is described by a vector of image features. But this time the features are results from trained kernels. Now you can add a couple of densely connected layers to construct the prediction.

Figure 2.25: A CNN with three convolutional layers followed by three fully connected layers. The number of feature maps in each convolutional layer indicate the number of kernel sets learned. The number of elements in each layer of the fully connected part indicate the number of weight sets learned.

Let’s try a CNN on the MNIST data. In listing 2.6, you see the definition of a CNN with convolutional layers followed by fully connected layers.

Open again the MNIST notebook https://github.com/tensorchiefs/dl_book/blob/master/chapter_02/nb_ch02_02.ipynb and fit a CNN with two convolutional layers to the MNIST data (see second part of the notebook) and compare the performance to what you achieved with an fcNN. Play with the code and perform a permutation experiment to check that the order of the pixels within the images matter for the performance of the CNN.

  • You need less weight parameters in a CNN than in an fcNN.
  • A CNN is to a large extent invariant to translations within the images.
  • The convolutional part of a CNN allows the network to learn hierarchically task-specific abstract image features