| import torch |
| from torch import nn |
|
|
|
|
| class ConvBlock(nn.Module): |
| def __init__(self, n_stages, n_filters_in, n_filters_out, normalization='none'): |
| super(ConvBlock, self).__init__() |
|
|
| ops = [] |
| for i in range(n_stages): |
| if i==0: |
| input_channel = n_filters_in |
| else: |
| input_channel = n_filters_out |
|
|
| ops.append(nn.Conv3d(input_channel, n_filters_out, 3, padding=1)) |
| if normalization == 'batchnorm': |
| ops.append(nn.BatchNorm3d(n_filters_out)) |
| elif normalization == 'groupnorm': |
| ops.append(nn.GroupNorm(num_groups=16, num_channels=n_filters_out)) |
| elif normalization == 'instancenorm': |
| ops.append(nn.InstanceNorm3d(n_filters_out)) |
| elif normalization != 'none': |
| assert False |
| ops.append(nn.ReLU(inplace=True)) |
|
|
| self.conv = nn.Sequential(*ops) |
|
|
| def forward(self, x): |
| x = self.conv(x) |
| return x |
|
|
|
|
| class ResidualConvBlock(nn.Module): |
| def __init__(self, n_stages, n_filters_in, n_filters_out, normalization='none'): |
| super(ResidualConvBlock, self).__init__() |
|
|
| ops = [] |
| for i in range(n_stages): |
| if i == 0: |
| input_channel = n_filters_in |
| else: |
| input_channel = n_filters_out |
|
|
| ops.append(nn.Conv3d(input_channel, n_filters_out, 3, padding=1)) |
| if normalization == 'batchnorm': |
| ops.append(nn.BatchNorm3d(n_filters_out)) |
| elif normalization == 'groupnorm': |
| ops.append(nn.GroupNorm(num_groups=16, num_channels=n_filters_out)) |
| elif normalization == 'instancenorm': |
| ops.append(nn.InstanceNorm3d(n_filters_out)) |
| elif normalization != 'none': |
| assert False |
|
|
| if i != n_stages-1: |
| ops.append(nn.ReLU(inplace=True)) |
|
|
| self.conv = nn.Sequential(*ops) |
| self.relu = nn.ReLU(inplace=True) |
|
|
| def forward(self, x): |
| x = (self.conv(x) + x) |
| x = self.relu(x) |
| return x |
|
|
|
|
| class DownsamplingConvBlock(nn.Module): |
| def __init__(self, n_filters_in, n_filters_out, stride=2, normalization='none'): |
| super(DownsamplingConvBlock, self).__init__() |
|
|
| ops = [] |
| if normalization != 'none': |
| ops.append(nn.Conv3d(n_filters_in, n_filters_out, stride, padding=0, stride=stride)) |
| if normalization == 'batchnorm': |
| ops.append(nn.BatchNorm3d(n_filters_out)) |
| elif normalization == 'groupnorm': |
| ops.append(nn.GroupNorm(num_groups=16, num_channels=n_filters_out)) |
| elif normalization == 'instancenorm': |
| ops.append(nn.InstanceNorm3d(n_filters_out)) |
| else: |
| assert False |
| else: |
| ops.append(nn.Conv3d(n_filters_in, n_filters_out, stride, padding=0, stride=stride)) |
|
|
| ops.append(nn.ReLU(inplace=True)) |
|
|
| self.conv = nn.Sequential(*ops) |
|
|
| def forward(self, x): |
| x = self.conv(x) |
| return x |
|
|
|
|
| class Upsampling_function(nn.Module): |
| def __init__(self, n_filters_in, n_filters_out, stride=2, normalization='none', mode_upsampling = 1): |
| super(Upsampling_function, self).__init__() |
|
|
| ops = [] |
| if mode_upsampling == 0: |
| ops.append(nn.ConvTranspose3d(n_filters_in, n_filters_out, stride, padding=0, stride=stride)) |
| if mode_upsampling == 1: |
| ops.append(nn.Upsample(scale_factor=stride, mode="trilinear", align_corners=True)) |
| ops.append(nn.Conv3d(n_filters_in, n_filters_out, kernel_size=3, padding=1)) |
| elif mode_upsampling == 2: |
| ops.append(nn.Upsample(scale_factor=stride, mode="nearest")) |
| ops.append(nn.Conv3d(n_filters_in, n_filters_out, kernel_size=3, padding=1)) |
|
|
| if normalization == 'batchnorm': |
| ops.append(nn.BatchNorm3d(n_filters_out)) |
| elif normalization == 'groupnorm': |
| ops.append(nn.GroupNorm(num_groups=16, num_channels=n_filters_out)) |
| elif normalization == 'instancenorm': |
| ops.append(nn.InstanceNorm3d(n_filters_out)) |
| elif normalization != 'none': |
| assert False |
| ops.append(nn.ReLU(inplace=True)) |
|
|
| self.conv = nn.Sequential(*ops) |
|
|
| def forward(self, x): |
| x = self.conv(x) |
| return x |
| |
| class Encoder(nn.Module): |
| def __init__(self, n_channels=3, n_classes=2, n_filters=16, normalization='none', has_dropout=False, has_residual=False): |
| super(Encoder, self).__init__() |
| self.has_dropout = has_dropout |
| convBlock = ConvBlock if not has_residual else ResidualConvBlock |
|
|
| self.block_one = convBlock(1, n_channels, n_filters, normalization=normalization) |
| self.block_one_dw = DownsamplingConvBlock(n_filters, 2 * n_filters, normalization=normalization) |
|
|
| self.block_two = convBlock(2, n_filters * 2, n_filters * 2, normalization=normalization) |
| self.block_two_dw = DownsamplingConvBlock(n_filters * 2, n_filters * 4, normalization=normalization) |
|
|
| self.block_three = convBlock(3, n_filters * 4, n_filters * 4, normalization=normalization) |
| self.block_three_dw = DownsamplingConvBlock(n_filters * 4, n_filters * 8, normalization=normalization) |
|
|
| self.block_four = convBlock(3, n_filters * 8, n_filters * 8, normalization=normalization) |
| self.block_four_dw = DownsamplingConvBlock(n_filters * 8, n_filters * 16, normalization=normalization) |
|
|
| self.block_five = convBlock(3, n_filters * 16, n_filters * 16, normalization=normalization) |
| |
| self.dropout = nn.Dropout3d(p=0.5, inplace=False) |
|
|
| def forward(self, input): |
| x1 = self.block_one(input) |
| x1_dw = self.block_one_dw(x1) |
|
|
| x2 = self.block_two(x1_dw) |
| x2_dw = self.block_two_dw(x2) |
|
|
| x3 = self.block_three(x2_dw) |
| x3_dw = self.block_three_dw(x3) |
|
|
| x4 = self.block_four(x3_dw) |
| x4_dw = self.block_four_dw(x4) |
|
|
| x5 = self.block_five(x4_dw) |
|
|
| if self.has_dropout: |
| x5 = self.dropout(x5) |
|
|
| res = [x1, x2, x3, x4, x5] |
| return res |
| |
| |
| class Decoder(nn.Module): |
| def __init__(self, n_channels=3, n_classes=2, n_filters=16, normalization='none', has_dropout=False, has_residual=False, up_type=0): |
| super(Decoder, self).__init__() |
| self.has_dropout = has_dropout |
|
|
| convBlock = ConvBlock if not has_residual else ResidualConvBlock |
|
|
| self.block_five_up = Upsampling_function(n_filters * 16, n_filters * 8, normalization=normalization, mode_upsampling=up_type) |
|
|
| self.block_six = convBlock(3, n_filters * 8, n_filters * 8, normalization=normalization) |
| self.block_six_up = Upsampling_function(n_filters * 8, n_filters * 4, normalization=normalization, mode_upsampling=up_type) |
|
|
| self.block_seven = convBlock(3, n_filters * 4, n_filters * 4, normalization=normalization) |
| self.block_seven_up = Upsampling_function(n_filters * 4, n_filters * 2, normalization=normalization, mode_upsampling=up_type) |
|
|
| self.block_eight = convBlock(2, n_filters * 2, n_filters * 2, normalization=normalization) |
| self.block_eight_up = Upsampling_function(n_filters * 2, n_filters, normalization=normalization, mode_upsampling=up_type) |
|
|
| self.block_nine_1 = convBlock(1, n_filters, n_filters, normalization=normalization) |
| self.block_nine_2 = convBlock(1, n_filters, n_filters, normalization=normalization) |
| self.block_nine_3 = convBlock(1, n_filters, n_filters, normalization=normalization) |
|
|
| self.block_c2f = convBlock(1, n_filters*3, n_filters, normalization=normalization) |
|
|
| self.out_conv_1 = nn.Conv3d(n_filters, n_classes, 1, padding=0) |
| self.out_conv_2 = nn.Conv3d(n_filters, n_classes, 1, padding=0) |
| self.out_conv_3 = nn.Conv3d(n_filters, n_classes, 1, padding=0) |
| self.dropout = nn.Dropout3d(p=0.5, inplace=False) |
|
|
| def forward(self, features): |
| x1 = features[0] |
| x2 = features[1] |
| x3 = features[2] |
| x4 = features[3] |
| x5 = features[4] |
| |
| x5_up = self.block_five_up(x5) |
| x5_up = x5_up + x4 |
|
|
| x6 = self.block_six(x5_up) |
| x6_up = self.block_six_up(x6) |
| x6_up = x6_up + x3 |
|
|
| x7 = self.block_seven(x6_up) |
| x7_up = self.block_seven_up(x7) |
| x7_up = x7_up + x2 |
|
|
| x8 = self.block_eight(x7_up) |
| x8_up = self.block_eight_up(x8) |
| x8_up = x8_up + x1 |
|
|
| x91 = self.block_nine_1(x8_up) |
| if self.has_dropout: |
| x91 = self.dropout(x91) |
| out_seg_1 = self.out_conv_1(x91) |
|
|
| x92 = self.block_nine_2(x8_up) |
| if self.has_dropout: |
| x92 = self.dropout(x92) |
| out_seg_2 = self.out_conv_2(x92) |
|
|
| x93 = self.block_nine_3(x8_up) |
| if self.has_dropout: |
| x93 = self.dropout(x93) |
|
|
| out_seg_diff = torch.cat(((x92 - x91), x91, x93), dim=1) |
| out_seg_3 = self.block_c2f(out_seg_diff) |
| out_seg_3 = self.out_conv_3(out_seg_3) |
| |
| return out_seg_1, out_seg_2, out_seg_3 |
| |
| class VNet(nn.Module): |
| def __init__(self, n_channels=3, n_classes=2, n_filters=16, normalization='none', has_dropout=False, has_residual=False): |
| super(VNet, self).__init__() |
|
|
| self.encoder = Encoder(n_channels, n_classes, n_filters, normalization, has_dropout, has_residual) |
| self.decoder = Decoder(n_channels, n_classes, n_filters, normalization, has_dropout, has_residual, 0) |
| |
| def forward(self, input): |
| features = self.encoder(input) |
| out_seg_1, out_seg_2, out_seg_3 = self.decoder(features) |
| return out_seg_1, out_seg_2, out_seg_3 |
|
|
| if __name__ == '__main__': |
| |
| from ptflops import get_model_complexity_info |
| model = VNet(n_channels=3, n_classes=2, normalization='batchnorm', has_dropout=True) |
| with torch.cuda.device(0): |
| macs, params = get_model_complexity_info(model, (3, 80, 80, 80), as_strings=True, |
| print_per_layer_stat=True, verbose=True) |
| print('{:<30} {:<8}'.format('Computational complexity: ', macs)) |
| print('{:<30} {:<8}'.format('Number of parameters: ', params)) |
| with torch.cuda.device(0): |
| macs, params = get_model_complexity_info(model, (3, 80, 80, 80), as_strings=True, |
| print_per_layer_stat=True, verbose=True) |
| print('{:<30} {:<8}'.format('Computational complexity: ', macs)) |
| print('{:<30} {:<8}'.format('Number of parameters: ', params)) |
| import ipdb; ipdb.set_trace() |
|
|