Effacer les filtres
Effacer les filtres

how to use stride in customized pooling layer?

3 vues (au cours des 30 derniers jours)
nayab thind
nayab thind le 28 Août 2021
I am trying to write a Customized pooling layer. I am stuck at stride.
Here is my code
classdef meanPoolingLayer < nnet.layer.Layer
properties (Learnable)
end
methods
function layer = meanPoolingLayer(name,stride,padding,poolsize,paddingmode)
% (Optional) Create a myLayer.
% This function must have the same name as the class.
layer.Name = name;
layer.Stride=stride;
layer.Padding=padding;
layer.PoolSize=poolsize;
layer.PaddingMode=paddingmode;
% Layer constructor function goes here.
end
function [Z1] = predict(layer, X1)
X=double(X1);
% Z1=mean(mean(X1));
end
end
end
How to slide window over entire data for this pooling.
Thanks in advance

Réponses (1)

Ayush Aniket
Ayush Aniket le 15 Mai 2024
Hi Nayab,
To implement the striding functionality, you need to manually iterate over the input feature map and apply the mean pooling operation to each window defined by your stride and pool size. This involves a bit of manual calculation to determine how to move the window across the input data, taking into account the stride, pool size, and optionally, padding.
Here's an example of mean pooling layer with stride and padding (assuming 'valid' padding for simplicity):
function Z = predict(layer, X)
% Predict function for performing mean pooling.
[h, w, c, n] = size(X); % Assuming 'X' is a 4-D array: height, width, channels, batch size
% Calculate output dimensions
outHeight = floor((h - layer.PoolSize(1)) / layer.Stride(1)) + 1;
outWidth = floor((w - layer.PoolSize(2)) / layer.Stride(2)) + 1;
% Initialize output
Z = zeros(outHeight, outWidth, c, n, 'like', X);
% Apply mean pooling
for i = 1:outHeight
for j = 1:outWidth
for k = 1:c
for l = 1:n
% Define window boundaries
rowStart = (i-1)*layer.Stride(1) + 1;
rowEnd = rowStart + layer.PoolSize(1) - 1;
colStart = (j-1)*layer.Stride(2) + 1;
colEnd = colStart + layer.PoolSize(2) - 1;
% Extract window and compute mean
window = X(rowStart:rowEnd, colStart:colEnd, k, l);
Z(i, j, k, l) = mean(window, 'all');
end
end
end
end
The method calculates the output size based on the input dimensions, pool size, and stride, then iterates over the input to apply mean pooling to each window.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by