Let’s begin the journey!! Let’s start by familiarizing ourselves with the meaning of CNN (Convolutional Neural Network) along with its significance and the concept of convolution.
Convolutional Neural Network is a specialized neural network designed for visual data, such as images & videos. But CNNs also work well for non-image data (especially in NLP & text classification).
Its concept is similar to that of a vanilla neural network (multilayer perceptron) – It follows the same general principle of forwarding & backward propagation.
Why do we need Convolutional Neural Network?
Although vanilla neural networks (MLPs) can learn highly complex functions, their architecture does not exploit what we know about how the brain reads & processes images.
The architecture of Convolutional Neural Network uses many of the working principles of the animal visual system & it has been able to achieve extraordinary results in image-related learning tasks.
For this reason, MLPs haven’t been able to achieve any significant breakthroughs in the image processing domain.
What is Convolution?
Mathematically, convolution is the summation of the element-wise product of 2 matrices.
Let us consider an image ‘X’ & a filter ‘Y’ (More about filter will be covered later). Both of them, i.e. X & Y, are matrices (image X is being expressed in the state of pixels). When we convolve the image ‘X’ using filter ‘Y’, we produce the output in a matrix, say’ Z’.
Source: Author
Finally, we compute the sum of all the elements in ‘Z’ to get a scalar number, i.e. 3+4+0+6+0+0+0+45+2 = 60
Now that we are familiar with the idea behind CNN let us dig deeper into the topic to understand the building blocks of CNN. Following is the outline of our journey:
What are Filters/Kernels?
A filter provides a measure for how close a patch or a region of the input resembles a feature. A feature may be any prominent aspect – a vertical edge, a horizontal edge, an arch, a diagonal, etc.
A filter acts as a single template or pattern, which, when convolved across the input, finds similarities between the stored template & different locations/regions in the input image.
Let us consider an example of detecting a vertical edge in the input image.
Each column of the 4×4 output matrix looks at exactly three columns & three rows (the coloured boxes show the output of the filter as it moves over the input image). The values in the output matrix represent the change in the intensity along the horizontal direction w.r.t the columns in the input image.
The output image has the value 0 in the 1st & last column. It means there is no change in intensity in the first three columns & the previous three columns of the input image. On the other hand, the output is 30 in the 2nd & 3rd column, indicating a change in the intensity of the corresponding columns of the input image.
Dimensions of the Convolved Output?
If the input image size is ‘n x n’ & filter size is ‘f x f ‘, then after convolution, the size of the output image is: (Size of input image – filter size + 1) x (Size of input image – filter size + 1). Let us refer to the below image:
Source: Author
Why do we do Padding?
Every time we apply a convolution operator, our image shrinks (in the above example, our vision shrunk from 6 x 6 to 4 x 4). If we convolve the output again with a filter, our image shrinks.
If we continue this process, we lose a lot of information because of image shrinking, which is one of the downsides of convolution.
During convolution, the pixels in the corners & the edges are considered only once. This is the 2nd downside of convolution. If we consider any pixel in the middle, many (fxf) regions overlap the pixel (we shift the filter & observe the image through it, i.e. convolve). Thus, the pixels on the corners or the edges are used much less in the output. So, we throw away a lot of information near the edge of the image.
So to fix both of these problems, we can ‘pad’ the image.
Let P be padding. In this example, p = 1 because we padded all around the input image with an extra border of 1 pixel.
⸫ Output Size = (n + 2p –f +1) x (n + 2p –f +1)
= (6 + 2×1 – 3 +1) x (6 + 2×1 – 3 +1)
= 6 x 6
Types of Padding
There are two common choices for padding: Valid convolutions & the Same convolutions.
Valid convolutions: This Means no padding. Thus, in this case, we might have (nxn) image convolve with (fxf) filter & this would give us an output (n-f+1) x (n-f+1) dimensional output.
Same convolutions: In this case, padding is such that the output size is the same as the input image size. When we do padding by ‘p’ pixels then, size of the input image changes from (nxn) to (n + 2p –f +1) x (n + 2p –f +1). The amount of padding to be done should be such that the output image after convolution matches the size of the input image.
Let,
n x n = Original input image size
p = Padding
(n+2p) x (n+2p) = Size of padded input image
(n+2p–f+1) x (n+2p-f+1) = Size of output image after convolving padded image
To avoid shrinkage of the original input image, we calculate ‘p = padding size’. The output image size achieved after convolving the padded input image is equal to that of the original input image size.
⸫ Output size after convolving padded image = Original input image size
How is the Filter Size Decided?
By convention, the value of ‘f,’ i.e. filter size, is usually odd in computer vision. This might be because of 2 reasons:
If the value of ‘f’ is even, we may need asymmetric padding (Please refer above eqn. 1 ). Let us say that the size of the filter i.e. ‘f’ is 6. Then by using equation 1, we get a padding size of 2.5, which does not make sense.
The 2nd reason for choosing an odd size filter such as a 3×3 or a 5×5 filter is we get a central position & at times it is nice to have a distinguisher.
What is Stride?
The stride indicates the pace by which the filter moves horizontally & vertically over the pixels of the input image during convolution.
Stride depends on what we expect in our output image. We prefer a smaller stride size if we expect several fine-grained features to reflect in our output. On the other hand, if we are only interested in the macro-level of features, we choose a larger stride size.
To understand the concept of stride in more detail, let’s consider an example. Let’s say we are interested in classifying the input image between landscape & portrait. We do not need minute details for this task, such as the number of mountain peaks, trees, etc. So we can choose a higher value for stride. On the other hand, if we want to classify an image between dog & cat, we need to focus or capture very minute details or features of the input image to type the image correctly. In this case, we prefer a smaller stride size.
Let,
S = Stride size
⸫ Size of the output image is given by :
Convolutions over RGB images
Consider an RGB image of size 6×6. Since it’s an RGB image, its dimension is 6x6x3, where the three corresponds to the three colour channels: Red, Green & Blue. We can imagine this as a 3-D image with a stack of 3 six by six shots.
For 3-D images, we need 3D filters, i.e. the filter itself will also have three layers corresponding to the red, green & blue channels, similar to that of the input RGB image.
Convolution operator functions in a similar fashion in both 2D (greyscale) & 3D (RGB) images. We 1st place the 3x3x3 filter in the upper left most position. This filter has 27 (9 parameters in each channel) or numbers. We take each of these 27 numbers & multiply them with the corresponding numbers from the image’s red, green & blue channels. Then we add up all those numbers & this gives us the 1st number in the output image.
To compute the following output, we take this filter & slide/stride it over by 1 (or whatever stride number we consider) & again, due to 27 multiplications, add up the 27 numbers.
Multiple Filters for Multiple Features
We can use multiple filters to detect various features simultaneously. Let us consider the following example in which we see vertical edge & curve in the input RGB image. We will have to use two different filters for this task, and the output image will thus have two feature maps.
Let us understand the dimensions mathematically,
Some important terms
The filters are learned during training (i.e. during backpropagation). Hence, the individual values of the filters are often called the weights of CNN.
A neuron is a filter whose weights are learned during training. E.g., a (3,3,3) filter (or neuron) has 27 units. Each neuron looks at a particular region in the output (i.e. its ‘receptive field’)
A feature map is a collection of multiple neurons, each looking at different inputs with the same weights. All neurons in a feature map extract the same feature (but from other input regions). It is called a ‘feature map’ because it maps where a particular part is found in the image.
What is Pooling?
A pooling layer is another essential building block of CNN. It tries to figure out whether a particular region in the image has the feature we are interested in or not.
The actual dictionary meaning of pooling is the act of sharing or combining two or more things. In CNN, the pooling layer does a similar job. It summarizes the featured map so that the model will not need to be trained on precisely positioned features, making a model more reliable and robust.
The pooling layer looks at more significant regions (having multiple patches) of the image & captures aggregate statistics (min, max, average & global). In other words, it makes the network invariant to local transformations.
The two most popular aggregate functions used in pooling are ‘max’ & ‘average’:
Max pooling – If any of the patches say something firmly about the presence of a particular feature, then the pooling layer counts that feature as ‘detected’.
Average pooling – If one patch says something very firmly, but the other ones disagree, the average pooling takes the average to find out.
Source: Author
Pooling has the advantage of making the representation more compact by reducing the spatial size of the feature maps, thereby reducing the number of parameters to be learnt. Pooling reduces only the height & width of the feature map, not the (i.e. number of channels). For example, if we have ‘m’ feature maps each of size (c,c), the pooling operation will produce ‘m’ outputs (c/2,c/2).
On the other hand, pooling also loses a lot of information, which is often considered a potential disadvantage.
The pooling layer has ‘NO PARAMETERS’ i.e. ‘ZERO TRAINABLE PARAMETERS’. The pooling layer computes the aggregate of the input. E.g. in max pooling, it takes a maximum over a group of pixels. We do not need any adjustments in any parameters.
The following image explains when a MAX pooling works and when AVG pooling works:
A typical CNN has the following sequence of CNN layers
We have an input image using multiple filters to create various feature maps.
Each feature map of size (C, C) is pooled to generate a (C/2, C/2) output (for a standard 2×2 pooling)
The above pattern is referred to as one Convolutional Neural Network layer or one unit. Multiple such CNN layers are stacked on top of each other to create deep Convolutional Neural Network networks.
The output of the convolution layer contains features, and these features are fed into a dense neural network.
Read more articles about the convolutional neural network on our blog.
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.
We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.
Show details
Powered By
Cookies
This site uses cookies to ensure that you get the best experience possible. To learn more about how we use cookies, please refer to our Privacy Policy & Cookies Policy.
brahmaid
It is needed for personalizing the website.
csrftoken
This cookie is used to prevent Cross-site request forgery (often abbreviated as CSRF) attacks of the website
Identityid
Preserves the login/logout state of users across the whole site.
sessionid
Preserves users' states across page requests.
g_state
Google One-Tap login adds this g_state cookie to set the user status on how they interact with the One-Tap modal.
MUID
Used by Microsoft Clarity, to store and track visits across websites.
_clck
Used by Microsoft Clarity, Persists the Clarity User ID and preferences, unique to that site, on the browser. This ensures that behavior in subsequent visits to the same site will be attributed to the same user ID.
_clsk
Used by Microsoft Clarity, Connects multiple page views by a user into a single Clarity session recording.
SRM_I
Collects user data is specifically adapted to the user or device. The user can also be followed outside of the loaded website, creating a picture of the visitor's behavior.
SM
Use to measure the use of the website for internal analytics
CLID
The cookie is set by embedded Microsoft Clarity scripts. The purpose of this cookie is for heatmap and session recording.
SRM_B
Collected user data is specifically adapted to the user or device. The user can also be followed outside of the loaded website, creating a picture of the visitor's behavior.
_gid
This cookie is installed by Google Analytics. The cookie is used to store information of how visitors use a website and helps in creating an analytics report of how the website is doing. The data collected includes the number of visitors, the source where they have come from, and the pages visited in an anonymous form.
_ga_#
Used by Google Analytics, to store and count pageviews.
_gat_#
Used by Google Analytics to collect data on the number of times a user has visited the website as well as dates for the first and most recent visit.
collect
Used to send data to Google Analytics about the visitor's device and behavior. Tracks the visitor across devices and marketing channels.
AEC
cookies ensure that requests within a browsing session are made by the user, and not by other sites.
G_ENABLED_IDPS
use the cookie when customers want to make a referral from their gmail contacts; it helps auth the gmail account.
test_cookie
This cookie is set by DoubleClick (which is owned by Google) to determine if the website visitor's browser supports cookies.
_we_us
this is used to send push notification using webengage.
WebKlipperAuth
used by webenage to track auth of webenagage.
ln_or
Linkedin sets this cookie to registers statistical data on users' behavior on the website for internal analytics.
JSESSIONID
Use to maintain an anonymous user session by the server.
li_rm
Used as part of the LinkedIn Remember Me feature and is set when a user clicks Remember Me on the device to make it easier for him or her to sign in to that device.
AnalyticsSyncHistory
Used to store information about the time a sync with the lms_analytics cookie took place for users in the Designated Countries.
lms_analytics
Used to store information about the time a sync with the AnalyticsSyncHistory cookie took place for users in the Designated Countries.
liap
Cookie used for Sign-in with Linkedin and/or to allow for the Linkedin follow feature.
visit
allow for the Linkedin follow feature.
li_at
often used to identify you, including your name, interests, and previous activity.
s_plt
Tracks the time that the previous page took to load
lang
Used to remember a user's language setting to ensure LinkedIn.com displays in the language selected by the user in their settings
s_tp
Tracks percent of page viewed
AMCV_14215E3D5995C57C0A495C55%40AdobeOrg
Indicates the start of a session for Adobe Experience Cloud
s_pltp
Provides page name value (URL) for use by Adobe Analytics
s_tslv
Used to retain and fetch time since last visit in Adobe Analytics
li_theme
Remembers a user's display preference/theme setting
li_theme_set
Remembers which users have updated their display / theme preferences
We do not use cookies of this type.
_gcl_au
Used by Google Adsense, to store and track conversions.
SID
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
SAPISID
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
__Secure-#
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
APISID
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
SSID
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
HSID
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
DV
These cookies are used for the purpose of targeted advertising.
NID
These cookies are used for the purpose of targeted advertising.
1P_JAR
These cookies are used to gather website statistics, and track conversion rates.
OTZ
Aggregate analysis of website visitors
_fbp
This cookie is set by Facebook to deliver advertisements when they are on Facebook or a digital platform powered by Facebook advertising after visiting this website.
fr
Contains a unique browser and user ID, used for targeted advertising.
bscookie
Used by LinkedIn to track the use of embedded services.
lidc
Used by LinkedIn for tracking the use of embedded services.
bcookie
Used by LinkedIn to track the use of embedded services.
aam_uuid
Use these cookies to assign a unique ID when users visit a website.
UserMatchHistory
These cookies are set by LinkedIn for advertising purposes, including: tracking visitors so that more relevant ads can be presented, allowing users to use the 'Apply with LinkedIn' or the 'Sign-in with LinkedIn' functions, collecting information about how visitors use the site, etc.
li_sugr
Used to make a probabilistic match of a user's identity outside the Designated Countries
MR
Used to collect information for analytics purposes.
ANONCHK
Used to store session ID for a users session to ensure that clicks from adverts on the Bing search engine are verified for reporting purposes and for personalisation
We do not use cookies of this type.
Cookie declaration last updated on 24/03/2023 by Analytics Vidhya.
Cookies are small text files that can be used by websites to make a user's experience more efficient. The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies, we need your permission. This site uses different types of cookies. Some cookies are placed by third-party services that appear on our pages. Learn more about who we are, how you can contact us, and how we process personal data in our Privacy Policy.
Shaily, good to read this detail Article from you. Keep it up. It reflects your hardwork and grip on the topic.
It's very very helpful thanks