UNet: Conv1d modification

Abhishek_Das: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>I am using UNET architecture and using the below code:</p>
<p>class DoubleConv(nn.Module):<br>
“”“(convolution =&gt; [BN] =&gt; ReLU) * 2"”"</p>
<pre><code>def __init__(self, in_channels, out_channels, mid_channels=None):
    super().__init__()
    if not mid_channels:
        mid_channels = out_channels
    self.double_conv = nn.Sequential(
        nn.Conv1d(in_channels, mid_channels, kernel_size=3, padding=1, bias=False),           
        nn.BatchNorm1d(mid_channels),          
        nn.ReLU(inplace=True),
        nn.Conv1d(mid_channels, out_channels, kernel_size=3, padding=1, bias=False),         
        nn.BatchNorm1d(mid_channels),           
        nn.ReLU(inplace=True)
    )

def forward(self, x):
    return self.double_conv(x)
</code></pre>
<p>class Down(nn.Module):<br>
“”“Downscaling with maxpool then double conv”“”</p>
<pre><code>def __init__(self, in_channels, out_channels):
    super().__init__()
    self.maxpool_conv = nn.Sequential(
        nn.MaxPool1d(2),
        DoubleConv(in_channels, out_channels)
    )

def forward(self, x):
    return self.maxpool_conv(x)
</code></pre>
<p>class Up(nn.Module):<br>
“”“Upscaling then double conv”“”</p>
<pre><code>def __init__(self, in_channels, out_channels, linear=True):
    super().__init__()

    # if linear, use the normal convolutions to reduce the number of channels
    if linear:
        self.up = nn.Upsample(scale_factor=2, mode='linear', align_corners=True)
        self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
    else:
        self.up = nn.ConvTranspose1d(in_channels, in_channels // 2, kernel_size=2, stride=2)
        self.conv = DoubleConv(in_channels, out_channels)

def forward(self, x1, x2):
    x1 = self.up(x1)
    # input is CHW
    diffY = x2.size()[2] - x1.size()[2]

    x1 = F.pad(x1, [diffY // 2, diffY - diffY // 2])
    # if you have padding issues, see
    # https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a
    # https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd
    x = torch.cat([x2, x1], dim=1)
    return self.conv(x)
</code></pre>
<p>class OutConv(nn.Module):<br>
def <strong>init</strong>(self, in_channels, out_channels):<br>
super(OutConv, self).<strong>init</strong>()<br>
self.conv = nn.Conv1d(in_channels, out_channels, kernel_size=1)</p>
<pre><code>def forward(self, x):
    return self.conv(x)
</code></pre>
<p>class UNet1D(nn.Module):<br>
def <strong>init</strong>(self, n_channels = 1, n_classes = 1, channel_sizes = [8,16,32,64,128], linear=True):<br>
super(UNet1D, self).<strong>init</strong>()<br>
self.n_channels = n_channels<br>
self.n_classes = n_classes<br>
self.linear = linear<br>
print(“##”)<br>
#<span class="hashtag">#channel</span> sizes are specified in main program<br>
print(self.n_channels,self.n_classes,channel_sizes)<span class="hashtag">#3</span> 3 [32,64,128,256,512]<br>
<span class="hashtag">#x1</span><br>
self.inc = DoubleConv(n_channels, channel_sizes[0])<br>
<span class="hashtag">#x2</span><br>
self.down1 = Down(channel_sizes[0], channel_sizes[1])<br>
<span class="hashtag">#x3</span><br>
self.down2 = Down(channel_sizes[1], channel_sizes[2])<br>
<span class="hashtag">#x4</span><br>
self.down3 = Down(channel_sizes[2], channel_sizes[3])<br>
factor = 2 if linear else 1<br>
self.down4 = Down(channel_sizes[3], channel_sizes[4] // factor)<br>
self.up1 = Up(channel_sizes[4], channel_sizes[3] // factor, linear)<br>
self.up2 = Up(channel_sizes[3], channel_sizes[2] // factor, linear)<br>
self.up3 = Up(channel_sizes[2], channel_sizes[1] // factor, linear)<br>
self.up4 = Up(channel_sizes[1], channel_sizes[0], linear)<br>
self.outc = OutConv(channel_sizes[0], n_classes)</p>
<pre><code>def forward(self, x):
    print("----")
    print(x.shape)#torch.Size([32, 3, 400])
    x1 = self.inc(x)
    print(x1.shape)#torch.Size([32, 32, 400])
    x2 = self.down1(x1)
    print(x2.shape)#torch.Size([32, 64, 200])
    x3 = self.down2(x2)
    #print(x3.shape)#torch.Size([32, 128, 100])
    x4 = self.down3(x3)
    #print(x4.shape)#torch.Size([32, 256, 50])
    x5 = self.down4(x4)
    #print(x5.shape)#torch.Size([32, 256, 25])
    x = self.up1(x5, x4)
    #print(x.shape)#torch.Size([32, 128, 50])
    x = self.up2(x, x3)
    #print(x.shape)#torch.Size([32, 64, 100])
    x = self.up3(x, x2)
    #print(x.shape)#torch.Size([32, 32, 200])
    x = self.up4(x, x1)
    #print(x.shape)#torch.Size([32, 32, 400])
    logits = self.outc(x)
    #print(logits.shape)#torch.Size([32, 3, 400])
    return logits
</code></pre>
<p>When my input is N=32, num_channels = 3 and number_class = 3 , this architecture is running fine<br>
In other words:<br>
I have 3 X 400 sized input and I am comparing them with target with user defined loss and everything is fine.</p>
<p>However I want to change it to 400x3 input, so my  num_channels = 400 and  and number_class = 400.<br>
But the above architecture is not supporting it.<br>
Can you please help me to gain insight and let me know how should I change my conv1d architecture to support my input 32x400x3.</p>

ptrblck: <aside class="quote no-group" data-username="Abhishek_Das" data-post="1" data-topic="181332">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/9e8a1a/48.png" class="avatar"> Abhishek_Das:</div>
<blockquote>
<p>But the above architecture is not supporting it.</p>
</blockquote>
</aside>
<p>It’s unclear what exactly is failing so you would need to explain the issues a bit more.<br>
I would also not recommend tagging specific users as it could demotivate others to post valid answers.</p>
<p>You can also post code snippets by wrapping them into three backticks ```, which would make debugging easier.</p>