What is the difference between the terms "kernel" and "filter" in the context of convolutional neural networks?
-
9$\begingroup$ They are the same! Filter or kernel is simple group of weights shared all over the input space. $\endgroup$– yasin.yaziciMay 31, 2015 at 12:08
-
$\begingroup$ is a filter the weight in a cnn? i see always weights in decritptions of neuronal networks, but i was wondering where the weights of a cnn are: $\endgroup$– KhanMay 10, 2020 at 12:50
6 Answers
In the context of convolutional neural networks, kernel = filter = feature detector.
Here is a great illustration from Stanford's deep learning tutorial (also nicely explained by Denny Britz).
The filter is the yellow sliding window, and its value is:
\begin{bmatrix} 1 & 0 & 1 \\ 0 & 1 & 0 \\ 1 & 0 & 1 \end{bmatrix}
-
3$\begingroup$ Is there a specific name for the filter? Like a "Blur" or an "Edge detect" $\endgroup$ Aug 5, 2018 at 18:57
-
5$\begingroup$ @PratikKhadloya no, those names are given subjectively by researchers based on what the filter appears to be detecting. $\endgroup$ Aug 30, 2018 at 22:25
-
3$\begingroup$ @Frank Dernoncourt according to this link towardsdatascience.com/… they are different when we have a couple of channels. actually when there are multiple channel Each filter actually happens to be a collection of kernels, with there being one kernel for every single input channel to the layer, and each kernel being unique. I would be happy to have your idea $\endgroup$– sariiiOct 8, 2018 at 2:18
How about we use the term "kernel" for a 2D array of weights, and the term "filter" for the 3D structure of multiple kernels stacked together? The dimension of a filter is $k \times k \times C$ (assuming square kernels). Each one of the $C$ kernels that compose a filter will be convolved with one of the $C$ channels of the input (input dimensions $H_{in} \times H_{in} \times C$, for example a $32 \times 32$ RGB image). It makes sense to use a different word to describe a 2D array of weights and a different for the 3D structure of the weights, since the multiplication happens between 2D arrays and then the results are summed to calculate the 3D operation.
Currently there is a problem with the nomenclature in this field. There are many terms describing the same thing and even terms used interchangeably for different concepts! Take as an example the terminology used to describe the output of a convolution layer: feature maps, channels, activations, tensors, planes, etc...
Based on wikipedia, "In image processing, a kernel, is a small matrix".
Based on wikipedia, "A matrix is a rectangular array arranged in rows and columns".
If a kernel is a rectangular array, then it cannot be the 3D structure of the weights, which in general is of $k_1 \times k_2 \times C$ dimensions.
Well, I cant argue that this is the best terminology, but it is better than just use the terms "kernel" and "filter" interchangeably. Moreover, we do need a word to describe the concept of the distinct 2D arrays that form a filter.
Filter consists of kernels. This means, in 2D convolutional neural network, filter is 3D. Check this gif from CS231n Convolutional Neural Networks for Visual Recognition:
Those three 3x3 kernels in second column of this gif form a filter. So as in the third column. The number of filters always equal to the number of feature maps in next layer. While the number of kernels in each filter will always equal to the number of feature maps in this layer.
-
2$\begingroup$ please add the source of the image you're using (Stanford CS 231): cs231n.github.io/convolutional-networks $\endgroup$– AntoineMar 25, 2020 at 9:42
-
-
A feature map is the same as a filter or "kernel" in this particular context. The weights of the filter determine what specific features are detected.
So for example, Franck has provided a great visual. Notice that his filter/feature-detector has x1 along the diagonal elements and x0 along all the other elements. This kernel weighting would thus detect pixels in the image that have a value of 1 along the image's diagonals.
Observe that the resulting convolved feature shows values of 4 wherever the image has a "1" along the diagonal values of the 3x3 filter (thus detecting the filter in that specific 3x3 section of the image), and lower values of 2 in the areas of the image where that filter didn't match as strongly.
The existing answers are excellent and comprehensively answer the question. Just want to add that filters in Convolutional networks are shared across the entire image (i.e., the input is convolved with the filter, as visualized in Franck's answer). The receptive field of a particular neuron are all input units that affect the neuron in question. The receptive field of a neuron in a Convolutional network is generally smaller than the receptive field of a neuron in a Dense network courtesy of shared filters(also called parameter sharing).
Parameter sharing confers a certain benefit on CNNs, namely a property termed equivariance to translation. This is to say that if the input is perturbed or translated, the output is also modified in the same manner. Ian Goodfellow provides a great example in the Deep Learning Book regarding how practitioners can capitalize on equivariance in CNNs:
When processing time-series data, this means that convolution produces a sort of timeline that shows when different features appear in the input.If we move an event later in time in the input, the exact same representation of it will appear in the output, just later. Similarly with images, convolution creates a 2-D map of where certain features appear in the input. If we move the object in the input, its representation will move the same amount in the output. This is useful for when we know that some function of a small number of neighboring pixels is useful when applied to multiple input locations. For example, when processing images, it is useful to detect edges in the first layer of a convolutional network. The same edges appear more or less everywhere in the image, so it is practical to share parameters across the entire image.
To be straightforward: A filter is a collection of kernels, although we use filter and kernel interchangeably.
Example: Let's say you want to apply P 3x3xN filter to a K x K x N input with stride =1 and pad = 0. So each of the 3 x 3 matrix in 3 x 3 x N filter is a kernel. And your output will be K-2 x K-2 x P .