Machine Learning - Max & Average Pooling
Tags: machinelearning, beginners, computerscience
Published: 2020-03-08 09:38:14

---
title: Machine Learning - Max & Average Pooling
published: true
description: Machine Learning studies
cover_image: https://i.ibb.co/gyDsXzP/0-hzqhs-KFIZNWJKhvq.jpg
tags: machinelearning, Beginners, Computer Science
series: Making Of Terminator
---
Hey there, 

How is the sunday going there? Corona there yet? 
This is going to be the 10th post in the series. 
Lets get to the topics without furthor ado. 

There are two types of Pooling: <code>Max Pooling and Average Pooling</code>. Max Pooling returns the maximum value from the portion of the image covered by the Kernel. On the other hand, Average Pooling returns the average of all the values from the portion of the image covered by the Kernel.


We'll now take a look at the other key concept in  convolutional neural networks called max pooling.  <code>In short, max pooling is just the process of  reducing the size of an input image by summarizing regions</code>.  Let's see how this works concretely with an example. 

<code>In order to perform max pooling,  we need to select 2 things, 
a grid,  which is the pool size, and a stride.</code>

For this example, we're going to use  a two-by-two pixel grid that we can see here in orange. 

![details](https://i.ibb.co/R7QbpQK/maxpooling.png)

We then look at the pixels inside our orange grid, and select the pixel with the greatest value. 

For example, here we have the pixel values 22,  27, 91, and 110.  In this case, we select the value 110 since this is the greatest pixel value among these.  

<code>The other parameter we use is called the stride</code>.  The stride determines <code>the number of pixels to slide the window across the image</code>.  

<ul>
<li>In this example, we use a stride of two,  which means we will slide the orange grid two pixels at each step.</li>
<li>So we slide our orange grid 2 pixels to the right.  Now, in this new position,  we again select the pixel with the greatest value.  We now have pixel values of 36,  313, 120, and 522.</li>
<li>So now, we select the value of 522,  since this is the greatest pixel value among these.  We now add this pixel value of 522 to the corresponding pixel in the new image,  and we continue this process until we cover the entire convoluted image.</li>
</ul>

The result will be a new image that's smaller than the original image,  and you can say we've down-sampled the original image.  In this particular example,  we end up with a new image that's half the size of the original image.  The size of this new image will vary  depending on your choice of the grid size and the stride.

![details](https://i.ibb.co/XFVy9hj/maxcomplete.png)

<h1>Recap</h1>


<code>A convolution is the process of applying a filter (“kernel”) to an image. Max pooling is the process of reducing the size of the image through downsampling.</code>

Convolutional layers can be added to the neural network model using the Conv2D layer type in Keras. This layer is similar to the Dense layer, and has weights and biases that need to be tuned to the right values. The Conv2D layer also has kernels (filters) whose values need to be tuned as well. So, in a Conv2D layer the values inside the filter matrix are the variables that get tuned in order to produce the right output.

Here are some of terms that were introduced in this lesson:


<code>CNNs</code>: Convolutional neural network. That is, a network which has at least one convolutional layer. A typical CNN also includes other types of layers, such as pooling layers and dense layers.

<code>Convolution</code>: The process of applying a kernel (filter) to an image
Kernel / filter: A matrix which is smaller than the input, used to transform the input into chunks

<code>Padding</code>: Adding pixels of some value, usually 0, around the input image
Pooling The process of reducing the size of an image through downsampling.There are several types of pooling layers. For example, average pooling converts many values into a single value by taking the average. However, maxpooling is the most common.

<code>Maxpooling</code>: A pooling process in which many values are converted into a single value by taking the maximum value from among them.

<code>Stride</code>: the number of pixels to slide the kernel (filter) across the image.

<code>Downsampling</code>: The act of reducing the size of an image


If you want to know more details about how CNNs works make sure to check out this <a href="https://towardsdatascience.com/a-comprehensive-guide-to-convolutional-neural-networks-the-eli5-way-3bd2b1164a53">Comprehensive Guide to Convolutional Neural Networks</a>.