<jupyter_start><jupyter_text>Goals 1. Experiment with different parameters of pooling layer  Table of contents [Install Monk](0) [Parameter - Type of Pooling](1) [Parameter - Kernel Size](2) [Parameter - Stride](3) [Parameter - Padding](4)  [Parameter - Dilation](5)  Install Monk  Install Monk Manually  Step 1: Clone the library - git clone https://github.com/Tessellate-Imaging/monk_v1.git     Step 2: Install requirements  - Linux     - Cuda 9.0         - `cd monk_v1/installation/Linux && pip install -r requirements_cu90.txt`     - Cuda 9.2         - `cd monk_v1/installation/Linux && pip install -r requirements_cu92.txt`     - Cuda 10.0         - `cd monk_v1/installation/Linux && pip install -r requirements_cu100.txt`     - Cuda 10.1         - `cd monk_v1/installation/Linux && pip install -r requirements_cu101.txt`     - Cuda 10.2         - `cd monk_v1/installation/Linux && pip install -r requirements_cu102.txt`     - CPU (Non gpu system)         - `cd monk_v1/installation/Linux && pip install -r requirements_cpu.txt`    - Windows     - Cuda 9.0 (Experimental support)         - `cd monk_v1/installation/Windows && pip install -r requirements_cu90.txt`     - Cuda 9.2 (Experimental support)         - `cd monk_v1/installation/Windows && pip install -r requirements_cu92.txt`     - Cuda 10.0 (Experimental support)         - `cd monk_v1/installation/Windows && pip install -r requirements_cu100.txt`     - Cuda 10.1 (Experimental support)         - `cd monk_v1/installation/Windows && pip install -r requirements_cu101.txt`     - Cuda 10.2 (Experimental support)         - `cd monk_v1/installation/Windows && pip install -r requirements_cu102.txt`     - CPU (Non gpu system)         - `cd monk_v1/installation/Windows && pip install -r requirements_cpu.txt`                    - Mac     - CPU (Non gpu system)         - `cd monk_v1/installation/Mac && pip install -r requirements_cpu.txt`                    - Misc      - Colab (GPU)          - `cd monk_v1/installation/Misc && pip install -r requirements_colab.txt`      - Kaggle (GPU)          - `cd monk_v1/installation/Misc && pip install -r requirements_kaggle.txt`    Step 3: Add to system path (Required for every terminal or kernel run) - `import sys` - `sys.path.append("monk_v1/");`  Visualization functions   - Can be ignored<jupyter_code>import warnings
warnings.filterwarnings('ignore')
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import cv2
import numpy as np


def showFiltersWithKernels(list_of_files, col=10, wSize=5, hSize=5, mypath='.'):   
    
    from matplotlib import pyplot as plt

    from matplotlib.pyplot import figure, imshow, axis
    from matplotlib.image import imread
    
    plt.ion();
    fig = figure(figsize=(wSize, hSize))
    number_of_files = len(list_of_files)
    row = number_of_files / col
    if (number_of_files % col != 0):
        row += 1
    new_list = [];
    for i in range(len(list_of_files)//2):
        new_list.append("tmp/test" + str(i) + ".jpg")
        new_list.append("tmp/test_filter" + str(i) + ".jpg")
    
    for i in range(len(new_list)):
        a=fig.add_subplot(row, col, i + 1)
        if(i%2 == 0):
            a.set_title('feature_' + str(i//2+1));
        else:
            a.set_title('kernel_' + str(i//2+1));
        image = imread(mypath + '/' + new_list[i])
        imshow(image, cmap='Greys_r')
        axis('off')
        


        
        
        
def showFilters(list_of_files, col=10, wSize=5, hSize=5, mypath='.'):

    from matplotlib import pyplot as plt

    from matplotlib.pyplot import figure, imshow, axis
    from matplotlib.image import imread
    
    plt.ion();
    
    fig = figure(figsize=(wSize, hSize))
    number_of_files = len(list_of_files)
    row = number_of_files / col
    if (number_of_files % col != 0):
        row += 1
    new_list = [];
    for i in range(len(list_of_files)//2):
        new_list.append("tmp/test" + str(i) + ".jpg")
        
    for i in range(len(new_list)):
        a=fig.add_subplot(row, col, i + 1)
        image = imread(mypath + '/' + new_list[i])
        imshow(image, cmap='Greys_r')
        axis('off')

import os
import sys
sys.path.append("monk_v1")
## Import Monk
#Using mxnet-gluon backend 

# When installed using pip
from monk.gluon_prototype import prototype
# Create a sample project
gtf = prototype(verbose=0);
gtf.Prototype("Project", "Study-Pooling");<jupyter_output><empty_output><jupyter_text>Max Pooling vs Avg Pooling  Study Material  - As stated in https://computersciencewiki.org/index.php/Max-pooling_/_Pooling - "Max pooling is a sample-based discretization process. The objective is to down-sample an input representation (image, hidden-layer output matrix, etc.), reducing its dimensionality and allowing for assumptions to be made about features contained in the sub-regions binned."       - As described here https://machinelearningmastery.com/pooling-layers-for-convolutional-neural-networks/ - "Average pooling involves calculating the average for each patch of the feature map."    - This blog - https://medium.com/@bdhuma/which-pooling-method-is-better-maxpooling-vs-minpooling-vs-average-pooling-95fb03f45a9 - on medium tells what difference lies between average and max pooling and when to use what      - "Average pooling method smooths out the image and hence the sharp features may not be identified when this pooling method is used."      - "Max pooling selects the brighter pixels from the image. It is useful when the background of the image is dark and we are interested in only the lighter pixels of the image."<jupyter_code># kernel_size=2, stride=None, padding=0, dilation=1
# Pooling
output = gtf.study_pooling_type("test.jpg", type="avg");
print("");

# Display
%matplotlib inline
from glob import glob
list_of_files=glob('tmp/test*.jpg')
print("Output is scaled from to a value of 255 for visibility")
showFilters(list_of_files, col=4, wSize=20, hSize=10)<jupyter_output>Input shape - (1, 3, 224, 224)
Output shape - (1, 3, 112, 112)

Output is scaled from to a value of 255 for visibility<jupyter_text>Interactive demo Toggle type of pooling<jupyter_code>sl1 = widgets.Dropdown(options=['max', 'avg'], value='max')



def f(factor):
    
    ############################# MAIN FUNCTION###########################
    gtf.study_pooling_type("test.jpg", type=factor);
    ######################################################################

    
    
    
    ############################# FOR DISPLAYING OUTPUT ##################
    from IPython.display import Image, HTML, display
    from glob import glob
    list_of_files=glob('tmp/test*.jpg')

    %matplotlib inline
    from matplotlib.pyplot import figure, imshow, axis
    from matplotlib.image import imread
    showFilters(list_of_files, col=4, wSize=20, hSize=10)
    #######################################################################
    
    
interact(f, factor=sl1);<jupyter_output><empty_output><jupyter_text>Effect of kernel size on pooling  Study Material  - As per the answers in https://stats.stackexchange.com/questions/296679/what-does-kernel-size-mean/339265 - "The kernel size here refers to the widthxheight of the filter mask."    - Larger kernel sizes usually tend to higher feature loss. As per answers here https://www.quora.com/Are-maxpooling-layer-kernel-sizes-in-CNNs-generally-smaller-than-convolutional-layer-kernel-sizes-Why - "If the kernel of the max pooling layer is too high you are throwing away too much valuable information."<jupyter_code># Pooling
output = gtf.study_pooling_kernels("test.jpg", kernel_size=4, type="avg");
print("");

# Display
%matplotlib inline
from glob import glob
list_of_files=glob('tmp/test*.jpg')
print("Output is scaled from to a value of 255 for visibility")
showFilters(list_of_files, col=4, wSize=20, hSize=10)<jupyter_output>Input shape - (1, 3, 224, 224)
Output shape - (1, 3, 56, 56)

Output is scaled from to a value of 255 for visibility<jupyter_text>Interactive demo Understanding effect of kernel size<jupyter_code>sl1 = widgets.IntSlider(value=2, min=2, max=10, step=2);


def f(factor):
    
    ############################# MAIN FUNCTION###########################
    gtf.study_pooling_kernels("test.jpg", kernel_size=factor, type="avg");
    ######################################################################

    
    
    
    ############################# FOR DISPLAYING OUTPUT ##################
    from IPython.display import Image, HTML, display
    from glob import glob
    list_of_files=glob('tmp/test*.jpg')

    %matplotlib inline
    from matplotlib.pyplot import figure, imshow, axis
    from matplotlib.image import imread
    showFilters(list_of_files, col=4, wSize=20, hSize=10)
    #######################################################################
    
    
interact(f, factor=sl1);<jupyter_output><empty_output><jupyter_text>Effect of kernel stride on pooling  Study Material  - The question to use strides while pooling or while convolutional layers is an old one. An analysis mentioned here https://stats.stackexchange.com/questions/387482/pooling-vs-stride-for-downsampling, describes when to use what kind of strides.    - Pooling combined with strides results in higher feature loss<jupyter_code># Pooling
output = gtf.study_pooling_stride("test.jpg", stride=1, type="avg");
print("");

# Display
%matplotlib inline
from glob import glob
list_of_files=glob('tmp/test*.jpg')
print("Output is scaled from to a value of 255 for visibility")
showFilters(list_of_files, col=4, wSize=20, hSize=10)<jupyter_output>Input shape - (1, 3, 224, 224)
Output shape - (1, 3, 223, 223)

Output is scaled from to a value of 255 for visibility<jupyter_text>Interactive demo Understanding effect of strides<jupyter_code>sl1 = widgets.IntSlider(value=1, min=1, max=10, step=1);


def f(factor):
    
    ############################# MAIN FUNCTION###########################
    gtf.study_pooling_stride("test.jpg", stride=factor, type="avg");
    ######################################################################

    
    
    
    ############################# FOR DISPLAYING OUTPUT ##################
    from IPython.display import Image, HTML, display
    from glob import glob
    list_of_files=glob('tmp/test*.jpg')

    %matplotlib inline
    from matplotlib.pyplot import figure, imshow, axis
    from matplotlib.image import imread
    showFilters(list_of_files, col=4, wSize=20, hSize=10)
    #######################################################################
    
    
interact(f, factor=sl1);<jupyter_output><empty_output><jupyter_text>Effect of kernel padding on pooling  Study Material  - https://www.geeksforgeeks.org/cnn-introduction-to-padding/ defines Padding as simply a process of adding layers of zeros to our input images     - As per the blog here https://machinelearningmastery.com/padding-and-stride-for-convolutional-neural-networks/             - Padding solves the border exffect problem.       - The pixels on the edge of the input are only ever exposed to the edge of the filter.       - By starting the filter outside the frame of the image, it gives the pixels on the border of the image more of an opportunity for interacting with the filter, more of an opportunity for features to be detected by the filter, and in turn, an output feature map that has the same shape as the input image.              - As per the article here https://medium.com/@ayeshmanthaperera/what-is-padding-in-cnns-71b21fb0dd7      - Convolution involves two problems - Shrinking outputs & Loosing information on corners of the image      - And both are solved using padding     - Helps to remove inconsistencies with data shapes in hidden layers<jupyter_code># Pooling
output = gtf.study_pooling_padding("test.jpg", padding=4, type="avg");
print("");

# Display
%matplotlib inline
from glob import glob
list_of_files=glob('tmp/test*.jpg')
print("Output is scaled from to a value of 255 for visibility")
showFilters(list_of_files, col=4, wSize=20, hSize=10)<jupyter_output>Input shape - (1, 3, 224, 224)
Output shape - (1, 3, 116, 116)

Output is scaled from to a value of 255 for visibility<jupyter_text>Interactive demo Understanding effect of padding<jupyter_code>sl1 = widgets.IntSlider(value=0, min=0, max=10, step=2);


def f(factor):
    
    ############################# MAIN FUNCTION###########################
    gtf.study_pooling_padding("test.jpg", padding=factor, type="avg");
    ######################################################################

    
    
    
    ############################# FOR DISPLAYING OUTPUT ##################
    from IPython.display import Image, HTML, display
    from glob import glob
    list_of_files=glob('tmp/test*.jpg')

    %matplotlib inline
    from matplotlib.pyplot import figure, imshow, axis
    from matplotlib.image import imread
    showFilters(list_of_files, col=4, wSize=20, hSize=10)
    #######################################################################
    
    
interact(f, factor=sl1);<jupyter_output><empty_output><jupyter_text>Effect of kernel dilation on pooling  Study Material  - As per one of the answers here https://datascience.stackexchange.com/questions/28881/what-is-dilated-pooling-and-how-it-works-mathematically - "Dilated max-pooling is simply regular max-pooling but the pixels/voxels you use in each "application" of the max-pooling operation are exactly the same pixels/voxels you would select with dilated convolution."      - As per the blog here https://towardsdatascience.com/review-dilated-convolution-semantic-segmentation-9d5a5bd768f5    - The idea of Dilated Convolution is come from the wavelet decomposition.     - It is also called “atrous convolution”, “algorithme à trous” and “hole algorithm”.    - With dilation the receptive field is larger compared with the standard one.        - An answer here https://ai-pool.com/d/what-is-dilated-convolution- mentions    - Dilated or atrous convolution is the same convolution, only with pixel skipping.    - If you remember how regular convolution works, then dilated convolution skips pixels with 2 diraction: width and hight and as a result it takes much bigger part of the image for every step of the convolution.     - In addition to the benefits you already mentioned such as larger receptive field, efficient computation and lesser memory consumption, the dilated causal convolutions also has the following benefits:      - it preserves the resolution/dimensions of data at the output layer. This is because the layers are dilated instead of pooling, hence the name dilated causal convolutions.      - it maintains the ordering of data. For example, in 1D dilated causal convolutions when the prediction of output depends on previous inputs then the structure of convolution helps in maintaining the ordering of data.<jupyter_code># Pooling
output = gtf.study_pooling_dilation("test.jpg", dilation=4, type="avg");
print("");

# Display
%matplotlib inline
from glob import glob
list_of_files=glob('tmp/test*.jpg')
print("Output is scaled from to a value of 255 for visibility")
showFilters(list_of_files, col=4, wSize=20, hSize=10)<jupyter_output>Input shape - (1, 3, 224, 224)
Output shape - (1, 3, 112, 112)

Output is scaled from to a value of 255 for visibility<jupyter_text>Interactive demo Understanding effect of dilated kernels<jupyter_code>sl1 = widgets.IntSlider(value=1, min=1, max=15, step=2);


def f(factor):
    
    ############################# MAIN FUNCTION###########################
    gtf.study_pooling_dilation("test.jpg", dilation=factor, type="avg");
    ######################################################################

    
    
    
    ############################# FOR DISPLAYING OUTPUT ##################
    from IPython.display import Image, HTML, display
    from glob import glob
    list_of_files=glob('tmp/test*.jpg')

    %matplotlib inline
    from matplotlib.pyplot import figure, imshow, axis
    from matplotlib.image import imread
    showFilters(list_of_files, col=4, wSize=20, hSize=10)
    #######################################################################
    
    
interact(f, factor=sl1);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16328544120367516, 'fraction_numerical': 0.021212308071776532, 'mean_word_length': 4.022607618457727, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 13, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '483153', 'license': 'Apache-2.0', 'n_tokens_mistral': 5065, 'n_tokens_neox': 4689, 'n_words': 1486, 'path': 'study_roadmaps/3_image_processing_deep_learning_roadmap/2_deep_learning_basics/1_layers/2) Study Pooling.ipynb', 'repo_name': 'Tessellate-Imaging/monk_v1', 'size': '16217'}