Change language

CNN | An introduction to the pool layer

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,

 -"  n  h  -  height of feature map -"  n  w  -  width of feature map -"  n  c  -  number of channels in the feature map -"  f -  size of filter -"  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.

Types of merging layers

  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 

    Shop

    Gifts for programmers

    Learn programming in R: courses

    $FREE
    Gifts for programmers

    Best Python online courses for 2022

    $FREE
    Gifts for programmers

    Best laptop for Fortnite

    $399+
    Gifts for programmers

    Best laptop for Excel

    $
    Gifts for programmers

    Best laptop for Solidworks

    $399+
    Gifts for programmers

    Best laptop for Roblox

    $399+
    Gifts for programmers

    Best computer for crypto mining

    $499+
    Gifts for programmers

    Best laptop for Sims 4

    $

    Latest questions

    PythonStackOverflow

    Common xlabel/ylabel for matplotlib subplots

    1947 answers

    PythonStackOverflow

    Check if one list is a subset of another in Python

    1173 answers

    PythonStackOverflow

    How to specify multiple return types using type-hints

    1002 answers

    PythonStackOverflow

    Printing words vertically in Python

    909 answers

    PythonStackOverflow

    Python Extract words from a given string

    798 answers

    PythonStackOverflow

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    606 answers

    PythonStackOverflow

    Python os.path.join () method

    384 answers

    PythonStackOverflow

    Flake8: Ignore specific warning for entire file

    360 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    How to specify multiple return types using type-hints

    Printing words vertically in Python

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically

    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)