Custom pooling layer

reza_yazdi: <p>Hello everyone.<br>
I read some similar topics about creating a custom pooling layer. but I could not implement what I want whit torch.unfold.<br>
I wanna create a simple pooling layer that if sum of patch values was greater than the special value(i.e. 5)  it returns the sum, otherwise returns 0.</p>
<p>thanks a lot.</p>

KFrank: <p>Hi Reza!</p>
<aside class="quote no-group" data-username="reza_yazdi" data-post="1" data-topic="151997" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/reza_yazdi/48/29968_2.png" class="avatar"> reza_yazdi:</div>
<blockquote>
<p>I wanna create a simple pooling layer that if sum of patch values was greater than the special value(i.e. 5)  it returns the sum, otherwise returns 0.</p>
</blockquote>
</aside>
<p>One approach would be to use something like <code>AvgPool2d</code>, rather than<br>
write a full custom layer:</p>
<pre data-code-wrap="python"><code class="lang-python">sum_pool = size_of_patch * AvgPool2d (kernel_size) (input)
threshold_sum_pool = (sum_pool &gt; threshold) * sum_pool
</code></pre>
<p>(The purpose of the factor <code>size_of_patch</code> is to convert the average<br>
over the patch to your desired sum.)</p>
<p>Best.</p>
<p>K. Frank</p>

reza_yazdi: <p>thank you, K. Frank. frankly, I want to implement something like the below, actually applying moor cellular automata to every patch. but I can’t return the true shape.</p>
<p>import torch as th<br>
import torch.nn as thNn<br>
import torch.nn.functional as thFn</p>
<p>import math</p>
<p>def unpack_param_2d(param):<br>
try:<br>
p_H, p_W = param[0], param[1]<br>
except:<br>
p_H, p_W = param, param</p>
<pre><code>return p_H, p_W
</code></pre>
<p>def mooreCA_pool_2d(inputs, kernel_size, stride, padding, dilation):</p>
<pre><code>#Input should be 4D (BCHW)
assert(inputs.dim() == 4)    

#Get input dimensions
b_size, c_size, h_size, w_size = inputs.size()

#Get input parameters
k_H, k_W = unpack_param_2d(kernel_size)
s_H, s_W = unpack_param_2d(     stride)
p_H, p_W = unpack_param_2d(    padding)
d_H, d_W = unpack_param_2d(   dilation)

#First we unfold all the (kernel_size x kernel_size)  patches
unf_input = thFn.unfold(inputs, kernel_size, dilation, padding, stride)    
    
for i in range(unf_input.shape[2]):       
    sum_col=unf_input[c_size-1,:,i].sum()        
    t=unf_input[ c_size-1,4,i]        
    if(t==0):
        if(sum_col==5): 
            unf_input[ c_size-1,4,i]=1                
        elif(sum_col==1 or sum_col==2 or sum_col==8):
            unf_input[ c_size-1,4,i]=0            
    else:
        if(sum_col==6): 
            unf_input[ c_size-1,4,i]=1
        elif(sum_col==2 or sum_col==3 or sum_col==9):
            unf_input[ c_size-1,4,i]=0            

out_W = math.floor(((w_size + (2 * p_W) - (d_W * (k_W - 1)) - 1) / s_W) + 1)
out_H = math.floor(((h_size + (2 * p_H) - (d_H * (k_H - 1)) - 1) / s_H) + 1)
output=unf_input.view(b_size, c_size, out_H ,out_W )   #here is my wrong          
return output
</code></pre>
<p>for the input img=th.tensor([[0,1,1,1],[0,1,1,1],[0,1,1,1]]).float()<br>
i got the RuntimeError: shape ‘[1, 1, 1, 2]’ is invalid for input of size 18</p>

KFrank: <p>Hi Reza!</p>
<aside class="quote no-group quote-modified" data-username="reza_yazdi" data-post="3" data-topic="151997" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/reza_yazdi/48/29968_2.png" class="avatar"> reza_yazdi:</div>
<blockquote>
<pre><code>out_W = math.floor(((w_size + (2 * p_W) - (d_W * (k_W - 1)) - 1) / s_W) + 1)
out_H = math.floor(((h_size + (2 * p_H) - (d_H * (k_H - 1)) - 1) / s_H) + 1)
output=unf_input.view(b_size, c_size, out_H ,out_W )   #here is my wrong          
</code></pre>
<p>…</p>
<p>for the input img=th.tensor([[0,1,1,1],[0,1,1,1],[0,1,1,1]]).float()<br>
i got the RuntimeError: shape ‘[1, 1, 1, 2]’ is invalid for input of size 18</p>
</blockquote>
</aside>
<p>I believe that your error message is saying that <code>unf_input</code> consists of<br>
18 elements, but that you are trying to view it as a shape of <code>[1, 1, 1, 2]</code>,<br>
which would only contain 2 elements.</p>
<p>This suggests that your calculation of <code>out_W</code> and / or <code>out_H</code> might be<br>
incorrect.</p>
<p>For debugging, I would suggest that you go through and print out the<br>
values of the various intermediate expressions that go into computing<br>
the dimensions passed to <code>.view()</code>.  Do you really expect <code>.view()</code> to<br>
only contain 2 elements?</p>
<p>Best.</p>
<p>K. Frank</p>