UNet: Conv1d modification

Hi @ptrblck,

I am using UNET architecture and using the below code:

class DoubleConv(nn.Module):
“”“(convolution => [BN] => ReLU) * 2"”"

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)

class Down(nn.Module):
“”“Downscaling with maxpool then double conv”“”

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)

class Up(nn.Module):
“”“Upscaling then double conv”“”

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)

class OutConv(nn.Module):
def init(self, in_channels, out_channels):
super(OutConv, self).init()
self.conv = nn.Conv1d(in_channels, out_channels, kernel_size=1)

def forward(self, x):
    return self.conv(x)

class UNet1D(nn.Module):
def init(self, n_channels = 1, n_classes = 1, channel_sizes = [8,16,32,64,128], linear=True):
super(UNet1D, self).init()
self.n_channels = n_channels
self.n_classes = n_classes
self.linear = linear
print(“##”)
##channel sizes are specified in main program
print(self.n_channels,self.n_classes,channel_sizes)#3 3 [32,64,128,256,512]
#x1
self.inc = DoubleConv(n_channels, channel_sizes[0])
#x2
self.down1 = Down(channel_sizes[0], channel_sizes[1])
#x3
self.down2 = Down(channel_sizes[1], channel_sizes[2])
#x4
self.down3 = Down(channel_sizes[2], channel_sizes[3])
factor = 2 if linear else 1
self.down4 = Down(channel_sizes[3], channel_sizes[4] // factor)
self.up1 = Up(channel_sizes[4], channel_sizes[3] // factor, linear)
self.up2 = Up(channel_sizes[3], channel_sizes[2] // factor, linear)
self.up3 = Up(channel_sizes[2], channel_sizes[1] // factor, linear)
self.up4 = Up(channel_sizes[1], channel_sizes[0], linear)
self.outc = OutConv(channel_sizes[0], n_classes)

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

When my input is N=32, num_channels = 3 and number_class = 3 , this architecture is running fine
In other words:
I have 3 X 400 sized input and I am comparing them with target with user defined loss and everything is fine.

However I want to change it to 400x3 input, so my num_channels = 400 and and number_class = 400.
But the above architecture is not supporting it.
Can you please help me to gain insight and let me know how should I change my conv1d architecture to support my input 32x400x3.

It’s unclear what exactly is failing so you would need to explain the issues a bit more.
I would also not recommend tagging specific users as it could demotivate others to post valid answers.

You can also post code snippets by wrapping them into three backticks ```, which would make debugging easier.