Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
4from collections import OrderedDict
6import torch
7import torch.nn
9from .backbones.vgg import vgg16_bn_for_segmentation
10from .make_layers import UpsampleCropBlock, conv_with_kaiming_uniform
13class ConcatFuseBlock(torch.nn.Module):
14 """
15 Takes in four feature maps with 16 channels each, concatenates them
16 and applies a 1x1 convolution with 1 output channel.
17 """
19 def __init__(self):
20 super().__init__()
21 self.conv = torch.nn.Sequential(
22 conv_with_kaiming_uniform(4 * 16, 1, 1, 1, 0),
23 torch.nn.BatchNorm2d(1),
24 )
26 def forward(self, x1, x2, x3, x4):
27 x_cat = torch.cat([x1, x2, x3, x4], dim=1)
28 x = self.conv(x_cat)
29 return x
32class DRIUBN(torch.nn.Module):
33 """
34 DRIU with Batch-Normalization head module
36 Based on paper by [MANINIS-2016]_.
38 Parameters
39 ----------
40 in_channels_list : list
41 number of channels for each feature map that is returned from backbone
42 """
44 def __init__(self, in_channels_list=None):
45 super(DRIUBN, self).__init__()
46 (
47 in_conv_1_2_16,
48 in_upsample2,
49 in_upsample_4,
50 in_upsample_8,
51 ) = in_channels_list
53 self.conv1_2_16 = torch.nn.Conv2d(in_conv_1_2_16, 16, 3, 1, 1)
54 # Upsample layers
55 self.upsample2 = UpsampleCropBlock(in_upsample2, 16, 4, 2, 0)
56 self.upsample4 = UpsampleCropBlock(in_upsample_4, 16, 8, 4, 0)
57 self.upsample8 = UpsampleCropBlock(in_upsample_8, 16, 16, 8, 0)
59 # Concat and Fuse
60 self.concatfuse = ConcatFuseBlock()
62 def forward(self, x):
63 """
64 Parameters
65 ----------
66 x : list
67 list of tensors as returned from the backbone network.
68 First element: height and width of input image.
69 Remaining elements: feature maps for each feature level.
71 Returns
72 -------
73 :py:class:`torch.Tensor`
74 """
75 hw = x[0]
76 conv1_2_16 = self.conv1_2_16(x[1]) # conv1_2_16
77 upsample2 = self.upsample2(x[2], hw) # side-multi2-up
78 upsample4 = self.upsample4(x[3], hw) # side-multi3-up
79 upsample8 = self.upsample8(x[4], hw) # side-multi4-up
80 out = self.concatfuse(conv1_2_16, upsample2, upsample4, upsample8)
81 return out
84def driu_bn(pretrained_backbone=True, progress=True):
85 """Builds DRIU with batch-normalization by adding backbone and head together
87 Parameters
88 ----------
90 pretrained_backbone : :py:class:`bool`, Optional
91 If set to ``True``, then loads a pre-trained version of the backbone
92 (not the head) for the DRIU network using VGG-16 trained for ImageNet
93 classification.
95 progress : :py:class:`bool`, Optional
96 If set to ``True``, and you decided to use a ``pretrained_backbone``,
97 then, shows a progress bar of the backbone model downloading if
98 download is necesssary.
101 Returns
102 -------
104 module : :py:class:`torch.nn.Module`
105 Network model for DRIU (vessel segmentation) using batch normalization
107 """
109 backbone = vgg16_bn_for_segmentation(
110 pretrained=False, return_features=[5, 12, 19, 29]
111 )
112 head = DRIUBN([64, 128, 256, 512])
114 order = [("backbone", backbone), ("head", head)]
115 if pretrained_backbone:
116 from .normalizer import TorchVisionNormalizer
118 order = [("normalizer", TorchVisionNormalizer())] + order
120 model = torch.nn.Sequential(OrderedDict(order))
121 model.name = "driu-bn"
122 return model