# How do I compute the maxpool of a image? Let us say stride of 2,2 on a mxn matrix?

13 vues (au cours des 30 derniers jours)
Kannan U V le 5 Juil 2018
Commenté : suWits Mohrarsii le 26 Fév 2020
If I were to implement just the max pooling operation on an image as mentioned in the following page https://www.quora.com/What-is-max-pooling-in-convolutional-neural-networks What is the most efficient way of computing it without going into for loops
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Kannan U V le 6 Juil 2018
The following does the trick
fun = @(block_struct) max(block_struct.data(:));
b = blockproc (a, [X Y], fun);
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Matt J le 6 Juil 2018
Modifié(e) : Matt J le 6 Juil 2018
But it is not very efficient. Compare:
a=rand(5000);
X=4; Y=4; %window sizes
tic
fun = @(block_struct) max(block_struct.data(:));
b = blockproc (a, [X Y], fun);
toc
%Elapsed time is 19.764354 seconds.
tic
b=sepblockfun(a,[X,Y],'max');
toc
%Elapsed time is 0.092457 seconds.
It is probably in fact the least efficient approach you could use. Even a double for-loop is faster:
tic;
[m,n]=size(a);
ex=ones(1,m/X)*X;
ey=ones(1,n/Y)*Y;
ac=mat2cell(a,ex,ey);
for i=1:m/X
for j=1:n/Y
ac{i,j}=max(ac{i,j}(:));
end
end
b=cell2mat(ac);
toc
%Elapsed time is 6.203763 seconds.

Connectez-vous pour commenter.

### Plus de réponses (2)

Matt J le 6 Juil 2018
Modifié(e) : Matt J le 6 Juil 2018
What is the most efficient way of computing it without going into for loops
The most efficient way in the entire universe is to use SEPBLOCKFUN (Download) as follows,
X=2; Y=2; %window sizes
maxpool=sepblockfun(yourImage,[X,Y],'max');
This assumes the image dimensions m,n are evenly divisible by X,Y respectively. Otherwise, you must pad the image to make it so.
##### 2 commentairesAfficher AucuneMasquer Aucune
Kannan U V le 6 Juil 2018
Thanks for your time and answer. It looks like the following does the trick
fun = @(block_struct) max(block_struct.data(:));
b = blockproc (a, [X Y], fun);
suWits Mohrarsii le 26 Fév 2020
thank you for this function

Connectez-vous pour commenter.

Anton Semechko le 5 Juil 2018
Modifié(e) : Anton Semechko le 5 Juil 2018
Here is an example:
% Sample image
% 4 pixels comprising non-overlapping 2-by-2 neighbourhoods
im_nw=im(1:2:end,1:2:end);
im_sw=im(2:2:end,1:2:end);
im_se=im(2:2:end,2:2:end);
im_ne=im(1:2:end,2:2:end);
% Select pixel with maximum intensity
im_max=max(cat(3,im_nw,im_sw,im_se,im_ne),[],3);
% Visualize
figure('color','w')
ha=subplot(1,2,1);
imshow(im,imref2d(size(im)))
title(ha,'original','FontSize',20)
ha=subplot(1,2,2);
imshow(im_max,imref2d(size(im_max)))
title(ha,'2x2 max-pool','FontSize',20)
Note that even though two images appear to have the same size when visualized using 'imshow', the dimensions of im_max are half that of im. Recursive application of 2-by-2 max-pool will result in downsampled images with sizes 1/2, 1/4, 1/8, etc. of the original image.
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Kannan U V le 5 Juil 2018
Thanks for your time and answer. I am looking for variable X, Y window sizes too.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Neighborhood and Block Processing dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by