  # CNN | An introduction to the pool layer

NumPy | Python Methods and Functions | String Variables

The merge operation includes applying a two-dimensional filter on each channel of the feature map and summing the elements that lie in the area covered by the filter.
For a feature map with dimensions n h xn w xn c , the dimensions of the output received after the pool, are equal

`  (n  h  - f + 1) / sx (n  w  - f + 1) / sx n  c   `

where,

` - & gt;  n  h  -  height of feature map - & gt;  n  w  -  width of feature map - & gt;  n  c  -  number of channels in the feature map - & gt;  f -  size of filter - & gt;  s -  stride length `

The general architecture of the CNN model is to have multiple layers of convolution and join together one after the other.

#### Why use pools ?

• Pool layers are used to reduce the size of feature maps. Thus, it reduces the number of parameters learned and the amount of computation performed on the network.
• The pool layer summarizes the objects present in the area of ​​the feature map created by the convolution layer. Thus, further operations are performed on generalized features, and not on precisely positioned features generated by the convolution layer. This makes the model more resistant to changes in the position of objects in the input image.

1. ### Max pooling

Maximum pooling — it is a merge operation that selects the largest element from the area of ​​the feature map that is covered by the filter. Thus, the output after the maximum pool layer will be the featuremap containing the most visible features of the previous featuremap.

This can be achieved with the MaxPooling2D layer in keras as follows:

Code # 1: Executing the maximum pool using keras

 ` import ` ` numpy as np ` ` from ` ` keras.models ` ` import ` ` Sequential `` from keras.layers import MaxPooling2D   # define input image image = np.array ([[ 2 , 2 , 7 , 3 ], [ 9 , 4 , 6 , 1 ], [ 8 , 5 , 2 , 4  ], [ 3 , 1 , 2 , 6 ]]) image = image.reshape ( 1 , 4 , 4 , 1 )   # define a model containing only one maximum pool layer model = Sequential ( [MaxPooling2D (pool_s ize = 2 , strides = 2 )])    # generate concatenated output output = model.predict (image)   # print output image output = np.squeeze (output) print (output) `

Exit:

` [[9. 7.] [8. 6.]] `
2. ### Medium Pool

Medium Merge calculates the average of the items present in the filtered area of ​​the feature map. So while the max pool gives the most prominent feature in a particular feature map patch, the average pool gives the average of the features present in the patch.

Code # 2: Executing Medium Pool Using Keras

` `

` import numpy as np from keras.models import Sequential from keras.layers import AveragePooling2D   # define in current image image = np.array ([[ 2 , 2 , 7 , 3 ], [ 9 , 4 , 6 , 1 ], [ 8 , 5 , 2 , 4 ],   [ 3 , 1 , 2 , 6 ]]) image = image.reshape ( 1 , 4 , 4 , 1 )   # define a model containing only one middle pool layer model = Sequential (   [AveragePooling2D (pool_size =   2 , strides = 2 )])   # generate concatenated output output = model .predict (image)   # print output image output = np.squeeze (output) print (output) `

` `

Exit:

` [[4.25 4.25] [4.25 3.5]] `
3. ### Global pool

Global pooling converts each channel in the feature map to a single value. Thus, the sitemap n h xn w xn c is reduced to the sitemap 1 x 1 xn c . This is equivalent to using the dimension filter n h xn w, i.e. measurements of the object map.
Alternatively, it can be either the Global Max Pool or the Global Medium Pool.

Code # 3: Executing a Global Pool Using Keras

Output :

` gm_output: 9.0 ga_output: 4.0625 `

## Books for developers

 ` import ` ` numpy as np ` ` from ` ` keras.models ` ` import ` ` Sequential ` ` from ` ` keras.layers ` ` import ` ` GlobalMaxPooling2D ` ` from ` ` keras.layers ` ` import ` ` GlobalAveragePooling2D `   ` # define input image ` ` image ` = ` np.array ([[` ` 2 ` ` , ` ` 2 ` `, ` ` 7 ` `, ` ` 3 ` `], ` ` ` ` [` ` 9 ` `, ` ` 4 ` `, ` ` 6 ` `, ` ` 1 ` `], ` ` ` ` [` ` 8 ` `, ` ` 5 ` `, ` ` 2 ` `, ` ` 4 ` `], ` ` [` ` 3 , 1 , 2 , 6 ]]) `` image = image.reshape ( 1 , 4 , 4 , 1 )   # defne gm_model containing only one global-max pool layer gm_model = Sequential ( [GlobalMaxPooling2D ()])   # define ga_model, with holding only one global middle pool level ga_model = Sequential (   [GlobalAveragePooling2D ()])   # generate merged output gm_output = gm_model.predict (image) ga_output = ga_model.predict (image)    # print the output image gm_output = np.squeeze (gm_output) ga_output = np.squeeze (ga_output)  print ( "gm_output:" , gm_output) print ( "ga_output:" , ga_output) `