| import torch |
| import torch.nn as nn |
| from torch.quantization import fuse_modules |
| from torch.nn import init |
| import torch.functional as F |
| class DoubleConv(nn.Module): |
| def __init__(self, in_ch, out_ch): |
| super(DoubleConv, self).__init__() |
| self.conv = nn.Sequential( |
| nn.Conv1d(in_ch, out_ch, 3, padding=1), |
| nn.BatchNorm1d(out_ch), |
| nn.ReLU(inplace=True), |
| nn.Conv1d(out_ch, out_ch, 3, padding=1), |
| nn.BatchNorm1d(out_ch), |
| nn.ReLU(inplace=True) |
| ) |
|
|
| def forward(self, input): |
| return self.conv(input) |
|
|
| class Upsampling(nn.Module): |
| def __init__(self, scale_factor) -> None: |
| super().__init__() |
| self.up = nn.UpsamplingBilinear2d(scale_factor=scale_factor) |
| def forward(self, x): |
| x = x.unsqueeze(3) |
| x = self.up(x) |
| x = x.squeeze() |
| return x |
|
|
| class UNetpp(nn.Module): |
| def __init__(self, in_channel=3, out_channel=3): |
| super().__init__() |
| nb_filter = [32, 64, 128, 256, 512] |
|
|
| self.pool = nn.MaxPool1d(2, 2) |
| self.up = nn.Upsample(scale_factor=2, mode='nearest') |
|
|
| self.conv0_0 = DoubleConv(in_channel, nb_filter[0]) |
| self.conv1_0 = DoubleConv(nb_filter[0], nb_filter[1]) |
| self.conv2_0 = DoubleConv(nb_filter[1], nb_filter[2]) |
| self.conv3_0 = DoubleConv(nb_filter[2], nb_filter[3]) |
| self.conv4_0 = DoubleConv(nb_filter[3], nb_filter[4]) |
|
|
| self.conv0_1 = DoubleConv(nb_filter[0]+nb_filter[1], nb_filter[0]) |
| self.conv1_1 = DoubleConv(nb_filter[1]+nb_filter[2], nb_filter[1]) |
| self.conv2_1 = DoubleConv(nb_filter[2]+nb_filter[3], nb_filter[2]) |
| self.conv3_1 = DoubleConv(nb_filter[3]+nb_filter[4], nb_filter[3]) |
|
|
| self.conv0_2 = DoubleConv(nb_filter[0]*2+nb_filter[1], nb_filter[0]) |
| self.conv1_2 = DoubleConv(nb_filter[1]*2+nb_filter[2], nb_filter[1]) |
| self.conv2_2 = DoubleConv(nb_filter[2]*2+nb_filter[3], nb_filter[2]) |
|
|
| self.conv0_3 = DoubleConv(nb_filter[0]*3+nb_filter[1], nb_filter[0]) |
| self.conv1_3 = DoubleConv(nb_filter[1]*3+nb_filter[2], nb_filter[1]) |
|
|
| self.conv0_4 = DoubleConv(nb_filter[0]*4+nb_filter[1], nb_filter[0]) |
| self.sigmoid = nn.Softmax(dim=1) |
| self.final = nn.Conv1d(nb_filter[0], out_channel, kernel_size=1) |
|
|
|
|
| def forward(self, input): |
| x0_0 = self.conv0_0(input) |
| x1_0 = self.conv1_0(self.pool(x0_0)) |
| x0_1 = self.conv0_1(torch.cat([x0_0, self.up(x1_0)], 1)) |
|
|
| x2_0 = self.conv2_0(self.pool(x1_0)) |
| x1_1 = self.conv1_1(torch.cat([x1_0, self.up(x2_0)], 1)) |
| x0_2 = self.conv0_2(torch.cat([x0_0, x0_1, self.up(x1_1)], 1)) |
|
|
| x3_0 = self.conv3_0(self.pool(x2_0)) |
| x2_1 = self.conv2_1(torch.cat([x2_0, self.up(x3_0)], 1)) |
| x1_2 = self.conv1_2(torch.cat([x1_0, x1_1, self.up(x2_1)], 1)) |
| x0_3 = self.conv0_3(torch.cat([x0_0, x0_1, x0_2, self.up(x1_2)], 1)) |
|
|
| x4_0 = self.conv4_0(self.pool(x3_0)) |
| x3_1 = self.conv3_1(torch.cat([x3_0, self.up(x4_0)], 1)) |
| x2_2 = self.conv2_2(torch.cat([x2_0, x2_1, self.up(x3_1)], 1)) |
| x1_3 = self.conv1_3(torch.cat([x1_0, x1_1, x1_2, self.up(x2_2)], 1)) |
| x0_4 = self.conv0_4(torch.cat([x0_0, x0_1, x0_2, x0_3, self.up(x1_3)], 1)) |
|
|
| output = self.final(x0_4) |
| output = self.sigmoid(output) |
| return output |
| class Loss(nn.Module): |
| def __init__(self) -> None: |
| super().__init__() |
| def forward(self, x, d): |
| loss = - (d * torch.log(x+1e-9)).sum() |
| return loss |
| if __name__ == "__main__": |
| x = torch.randn([10, 3, 6144]) |
| model = NestedUNet() |
| x = model(x) |
| print(x.shape) |