Fixing number of filters in Conv2ds

Idrees11: <p>Hi,</p>
<p>I am designing encoder decoder network. In which number of convolutional layers are four and number of Transposed convolutional layers are four. My image size is : 8x 1x 256x256. 8 is batch size, 1 is number of channels ( gray image). Size is H = 256 W is 256. Kernel size is 3 x 3. Stride is 2 x2. Padding 1.  However, I have to fix number of filters as 96 each layer…I don’t understand how to do that .</p>

Aknw_Fen: <p>96 filters will always make 96 channels.</p>
<p>The first time though, the input channels will be 3, output 96. Then, it will be input 96, output 96.</p>
<p>You should try, <a href="https://github.com/TylerYep/torchinfo" rel="noopener nofollow ugc">and use <code>torchinfo</code></a> to understand the dimensionality changes.</p>

Idrees11: <p>first convltional layer will be having in_channels =1, out_channels = 96 . why since image is gray<br>
second convolutional layer will be having in_channles = 96, out_channels= ?<br>
<span class="hashtag-raw">#-----------------------------------------------------------------------------------------------------</span>-#<br>
I am having four convolutional layers… And each layer is having 96 filters…<br>
<span class="hashtag-raw">#-----------------------------------------------------------------------------------------------------</span>---#<br>
I am not understanding, how It can be done ?</p>

Idrees11: <aside class="quote no-group" data-username="Idrees11" data-post="1" data-topic="214033" 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/idrees11/48/74448_2.png" class="avatar"> Idrees11:</div>
<blockquote>
<p>Hi,</p>
<p>I am designing encoder decoder network. In which number of convolutional layers are four and number of Transposed convolutional layers are four. My image size is : 8x 1x 256x256. 8 is batch size, 1 is number of channels ( gray image). Size is H = 256 W is 256. Kernel size is 3 x 3. Stride is 2 x2. Padding 1.  However, I have to fix number of filters as 96 each layer…I don’t understand how to do that .</p>
</blockquote>
</aside>
<p>import cv2<br>
import torch<br>
from torchvision import transforms<br>
<span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#<br>
<span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#<br>
class conv_block(nn.Module):</p>
<pre><code>def __init__(self, in_c, out_c):
    super().__init__()
    self.conv1 = nn.Conv2d(in_c, out_c, kernel_size=3, padding=1)
    self.bn1   = nn.InstanceNorm2d(out_c)
    self.relu  = nn.ReLU(inplace=True)
    
def forward(self, inputs):
    x = self.conv1(inputs)
    x = self.bn1(x)
    x = self.relu(x)
    return x
</code></pre>
<p>class encoder_block(nn.Module):</p>
<pre><code>def __init__(self, in_c, out_c):
    super().__init__()
    self.conv = conv_block(in_c, out_c)
    self.pool = nn.MaxPool2d((2, 2))
    
def forward(self, inputs):
    x = self.conv(inputs)
    p = self.pool(x)
    return x, p
</code></pre>
<p>class decoder_block(nn.Module):</p>
<pre><code>def __init__(self, in_c, out_c):
    super().__init__()
    self.up = nn.ConvTranspose2d(in_c, out_c, kernel_size=3, stride=2, padding=1)
    self.conv = conv_block(out_c+ out_c, out_c)
    
def forward(self, inputs, skip):
    x = self.up(inputs)
    x = torch.cat([x, skip], axis=1)
    x = self.conv(x)
    return x
</code></pre>
<p>class build_unet(nn.Module):</p>
<pre><code>def __init__(self):
    super().__init__()
</code></pre>
<p><span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#</p>
<h1><a name="p-461203-encoder-1" class="anchor" href="#p-461203-encoder-1"></a>ENCODER</h1>
<p><span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#<br>
self.e1 = encoder_block(1,96)<br>
self.e2 = encoder_block(96,96)<br>
self.e3 = encoder_block(96,96)<br>
self.e4 = encoder_block(96,96)<br>
<span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#</p>
<h1><a name="p-461203-bottleneck-2" class="anchor" href="#p-461203-bottleneck-2"></a>BOTTLENECK</h1>
<p><span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#<br>
self.b = conv_block(96, 96)<br>
<span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#</p>
<h1><a name="p-461203-decoder-3" class="anchor" href="#p-461203-decoder-3"></a>DECODER</h1>
<p><span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#<br>
self.d1 = decoder_block(96,96)<br>
self.d2 = decoder_block(96,96)<br>
self.d3 = decoder_block(96,96)<br>
self.d4 = decoder_block(96, 3)<br>
<span class="hashtag-raw">#----------------------------------------------------</span>#</p>
<h1><a name="p-461203-classifier-4" class="anchor" href="#p-461203-classifier-4"></a>“”" Classifier “”"</h1>
<p><span class="hashtag-raw">#----------------------------------------------------</span>#<br>
self.outputs = nn.Conv2d(96, 1, kernel_size=1, padding=0)<br>
<span class="hashtag-raw">#------------------------------------------------------------------------------</span>#</p>
<h1><a name="p-461203-encoder-5" class="anchor" href="#p-461203-encoder-5"></a>“”" Encoder “”"</h1>
<p><span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#<br>
def forward(self, inputs):<br>
s1, p1 = self.e1(inputs)<br>
s2, p2 = self.e2(p1)<br>
s3, p3 = self.e3(p2)<br>
s4, p4 = self.e4(p3)<br>
<span class="hashtag-raw">#------------------------------------------------------------------------------</span>#</p>
<h1><a name="p-461203-bottleneck-6" class="anchor" href="#p-461203-bottleneck-6"></a>“”" Bottleneck “”"</h1>
<p><span class="hashtag-raw">#-----------------------------------------------------------------------------</span>#<br>
b = self.b(p4)<br>
<span class="hashtag-raw">#------------------------------------------------------------------------------</span>#</p>
<h1><a name="p-461203-decoder-7" class="anchor" href="#p-461203-decoder-7"></a>“”" Decoder “”"</h1>
<p><span class="hashtag-raw">#------------------------------------------------------------------------------</span>#<br>
d1 = self.d1(b, s4)<br>
d2 = self.d2(d1, s3)<br>
d3 = self.d3(d2, s2)<br>
d4 = self.d4(d3, s1)<br>
<span class="hashtag-raw">#-------------------------------------------</span>#</p>
<h1><a name="p-461203-classifier-8" class="anchor" href="#p-461203-classifier-8"></a>“”" Classifier “”"</h1>
<p><span class="hashtag-raw">#-------------------------------------------</span>#<br>
outputs = self.outputs(d4)<br>
return outputs<br>
<span class="hashtag-raw">#-------------------------------------------------------------------------------</span>#</p>
<p>if <strong>name</strong> == “<strong>main</strong>”:<br>
# inputs = torch.randn([1,1,256,256])<br>
image = cv2.imread (r’C:\Users\Idrees Bhat\Desktop\Research\Insha\Dataset\o_gray\1.png’,0)<br>
convert_tensor = transforms.ToTensor()<br>
inputs=convert_tensor(image)<br>
# print (inputs.shape)<br>
# print (type(inputs))<br>
model = build_unet()<br>
y= model (inputs)<br>
print(y.shape)<br>
``<br>
Error : RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 31 but got size 32 for tensor number 1 in the list.</p>
<p>Note: Image size gray and size is [256 256]… I want fix filters per layer i.e. 96 filters per layer</p>