|
| 1 | +# Copyright (c) Open-CD. All rights reserved. |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | +import numpy as np |
| 5 | +from mmcv.ops import CrissCrossAttention |
| 6 | + |
| 7 | +from mmseg.models.utils import LayerNorm2d |
| 8 | +from opencd.registry import MODELS |
| 9 | + |
| 10 | + |
| 11 | +class CCA(nn.Module): |
| 12 | + """Criss-Cross Attention for Semantic Segmentation. |
| 13 | +
|
| 14 | + This head is the implementation of `CCNet |
| 15 | + <https://arxiv.org/abs/1811.11721>`_. |
| 16 | +
|
| 17 | + Args: |
| 18 | + recurrence (int): Number of recurrence of Criss Cross Attention |
| 19 | + module. Default: 2. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, channels, recurrence=2): |
| 23 | + super(CCA, self).__init__() |
| 24 | + self.recurrence = recurrence |
| 25 | + self.cca = CrissCrossAttention(channels) |
| 26 | + |
| 27 | + def forward(self, x): |
| 28 | + for _ in range(self.recurrence): |
| 29 | + x = self.cca(x) |
| 30 | + return x |
| 31 | + |
| 32 | + |
| 33 | +def channel_shuffle(x, groups=2): |
| 34 | + bat_size, channels, w, h = x.shape |
| 35 | + group_c = channels // groups |
| 36 | + x = x.view(bat_size, groups, group_c, w, h) |
| 37 | + x = torch.transpose(x, 1, 2).contiguous() |
| 38 | + x = x.view(bat_size, -1, w, h) |
| 39 | + return x |
| 40 | + |
| 41 | + |
| 42 | +class ShuffleBlock(nn.Module): |
| 43 | + |
| 44 | + def __init__(self, in_c, out_c, downsample=False): |
| 45 | + super(ShuffleBlock, self).__init__() |
| 46 | + self.downsample = downsample |
| 47 | + half_c = out_c // 2 |
| 48 | + if downsample: |
| 49 | + self.branch1 = nn.Sequential( |
| 50 | + # 3*3 dw conv, stride = 2 |
| 51 | + nn.Conv2d(in_c, in_c, 3, 2, 1, groups=in_c, bias=False), |
| 52 | + nn.BatchNorm2d(in_c), |
| 53 | + # 1*1 pw conv |
| 54 | + nn.Conv2d(in_c, half_c, 1, 1, 0, bias=False), |
| 55 | + nn.BatchNorm2d(half_c), |
| 56 | + nn.ReLU(True)) |
| 57 | + |
| 58 | + self.branch2 = nn.Sequential( |
| 59 | + # 1*1 pw conv |
| 60 | + nn.Conv2d(in_c, half_c, 1, 1, 0, bias=False), |
| 61 | + nn.BatchNorm2d(half_c), |
| 62 | + nn.ReLU(True), |
| 63 | + # 3*3 dw conv, stride = 2 |
| 64 | + nn.Conv2d(half_c, half_c, 3, 2, 1, groups=half_c, bias=False), |
| 65 | + nn.BatchNorm2d(half_c), |
| 66 | + # 1*1 pw conv |
| 67 | + nn.Conv2d(half_c, half_c, 1, 1, 0, bias=False), |
| 68 | + nn.BatchNorm2d(half_c), |
| 69 | + nn.ReLU(True)) |
| 70 | + |
| 71 | + else: |
| 72 | + assert in_c == out_c |
| 73 | + |
| 74 | + self.branch2 = nn.Sequential( |
| 75 | + # 1*1 pw conv |
| 76 | + nn.Conv2d(half_c, half_c, 1, 1, 0, bias=False), |
| 77 | + nn.BatchNorm2d(half_c), |
| 78 | + nn.ReLU(True), |
| 79 | + # 3*3 dw conv, stride = 1 |
| 80 | + nn.Conv2d(half_c, half_c, 3, 1, 1, groups=half_c, bias=False), |
| 81 | + nn.BatchNorm2d(half_c), |
| 82 | + # 1*1 pw conv |
| 83 | + nn.Conv2d(half_c, half_c, 1, 1, 0, bias=False), |
| 84 | + nn.BatchNorm2d(half_c), |
| 85 | + nn.ReLU(True)) |
| 86 | + |
| 87 | + def forward(self, x): |
| 88 | + out = None |
| 89 | + if self.downsample: |
| 90 | + # if it is downsampling, we don't need to do channel split |
| 91 | + out = torch.cat((self.branch1(x), self.branch2(x)), 1) |
| 92 | + else: |
| 93 | + # channel split |
| 94 | + channels = x.shape[1] |
| 95 | + c = channels // 2 |
| 96 | + x1 = x[:, :c, :, :] |
| 97 | + x2 = x[:, c:, :, :] |
| 98 | + out = torch.cat((x1, self.branch2(x2)), 1) |
| 99 | + |
| 100 | + return channel_shuffle(out, 2) |
| 101 | + |
| 102 | + |
| 103 | +class TimeAttention(nn.Module): |
| 104 | + |
| 105 | + def __init__(self, channels): |
| 106 | + super(TimeAttention, self).__init__() |
| 107 | + self.avg_pool = nn.AdaptiveAvgPool2d(1) |
| 108 | + attn_channels = channels // 16 |
| 109 | + attn_channels = max(attn_channels, 8) |
| 110 | + self.mlp = nn.Sequential( |
| 111 | + nn.Conv2d(channels * 2, attn_channels, kernel_size=1, bias=False), |
| 112 | + nn.BatchNorm2d(attn_channels), |
| 113 | + nn.ReLU(), |
| 114 | + nn.Conv2d(attn_channels, channels * 2, kernel_size=1, bias=False), |
| 115 | + ) |
| 116 | + |
| 117 | + def forward(self, x1, x2): |
| 118 | + x = torch.cat((x1, x2), dim=1) |
| 119 | + x = self.avg_pool(x) |
| 120 | + y = self.mlp(x) |
| 121 | + B, C, H, W = y.size() |
| 122 | + x1_attn, x2_attn = y.reshape(B, 2, C // 2, H, W).transpose(0, 1) |
| 123 | + x1_attn = torch.sigmoid(x1_attn) |
| 124 | + x2_attn = torch.sigmoid(x2_attn) |
| 125 | + x1 = x1 * x1_attn + x1 |
| 126 | + x2 = x2 * x2_attn + x2 |
| 127 | + return x1, x2 |
| 128 | + |
| 129 | + |
| 130 | +class shuffle_fusion(nn.Module): |
| 131 | + |
| 132 | + def __init__(self, channels, block_num=2): |
| 133 | + super().__init__() |
| 134 | + |
| 135 | + self.stages = [] |
| 136 | + self.stages.append( |
| 137 | + nn.Sequential( |
| 138 | + nn.Conv2d(channels, channels * 4, kernel_size=1, bias=False), |
| 139 | + nn.BatchNorm2d(channels * 4), nn.ReLU())) |
| 140 | + for i in range(block_num): |
| 141 | + self.stages.append( |
| 142 | + ShuffleBlock(channels * 4, channels * 4, downsample=False)) |
| 143 | + |
| 144 | + self.stages = nn.Sequential(*self.stages) |
| 145 | + |
| 146 | + self.single_conv = nn.Sequential( |
| 147 | + nn.Conv2d(channels * 4, channels, kernel_size=1, bias=False), |
| 148 | + nn.BatchNorm2d(channels), nn.ReLU()) |
| 149 | + |
| 150 | + self.time_attn = TimeAttention(channels) |
| 151 | + |
| 152 | + self.final_conv = nn.Sequential( |
| 153 | + nn.Conv2d(channels * 2, channels, kernel_size=1, bias=False), |
| 154 | + nn.BatchNorm2d(channels), nn.ReLU()) |
| 155 | + |
| 156 | + def forward_single(self, x): |
| 157 | + identity = x |
| 158 | + x = self.stages(x) |
| 159 | + x = self.single_conv(x) |
| 160 | + x = identity + x |
| 161 | + return x |
| 162 | + |
| 163 | + def forward(self, x1, x2): |
| 164 | + x1 = self.forward_single(x1) |
| 165 | + x2 = self.forward_single(x2) |
| 166 | + x1, x2 = self.time_attn(x1, x2) |
| 167 | + x = self.final_conv(channel_shuffle(torch.cat((x1, x2), dim=1))) |
| 168 | + return x |
| 169 | + |
| 170 | + |
| 171 | +@MODELS.register_module() |
| 172 | +class LightCDNet(nn.Module): |
| 173 | + |
| 174 | + def __init__(self, stage_repeat_num, net_type="small"): |
| 175 | + super(LightCDNet, self).__init__() |
| 176 | + |
| 177 | + index_list = stage_repeat_num.copy() |
| 178 | + index_list[0] = index_list[0] - 1 |
| 179 | + self.index_list = list(np.cumsum(index_list)) |
| 180 | + if net_type == "small": |
| 181 | + self.out_channels = [24, 48, 96, 192] |
| 182 | + self.block_num = 4 |
| 183 | + elif net_type == "base": |
| 184 | + self.out_channels = [24, 116, 232, 464] |
| 185 | + self.block_num = 8 |
| 186 | + elif net_type == "large": |
| 187 | + self.out_channels = [24, 176, 352, 704] |
| 188 | + self.block_num = 16 |
| 189 | + else: |
| 190 | + print("the model type is error!") |
| 191 | + |
| 192 | + self.conv1 = nn.Sequential( |
| 193 | + nn.Conv2d(3, self.out_channels[0], 3, 2, 1, bias=False), |
| 194 | + LayerNorm2d(self.out_channels[0]), nn.GELU()) |
| 195 | + |
| 196 | + self.fusion_conv = shuffle_fusion( |
| 197 | + self.out_channels[0], block_num=self.block_num) |
| 198 | + |
| 199 | + in_c = self.out_channels[0] |
| 200 | + |
| 201 | + self.stages = [] |
| 202 | + for stage_idx in range(len(stage_repeat_num)): |
| 203 | + out_c = self.out_channels[1 + stage_idx] |
| 204 | + repeat_num = stage_repeat_num[stage_idx] |
| 205 | + for i in range(repeat_num): |
| 206 | + if i == 0: |
| 207 | + self.stages.append( |
| 208 | + ShuffleBlock(in_c, out_c, downsample=True)) |
| 209 | + else: |
| 210 | + self.stages.append( |
| 211 | + ShuffleBlock(in_c, in_c, downsample=False)) |
| 212 | + in_c = out_c |
| 213 | + self.stages.append(CCA(channels=out_c, recurrence=2)) |
| 214 | + |
| 215 | + self.stages = nn.Sequential(*self.stages) |
| 216 | + |
| 217 | + def forward(self, x1, x2): |
| 218 | + x1 = self.conv1(x1) |
| 219 | + x2 = self.conv1(x2) |
| 220 | + x = self.fusion_conv(x1, x2) |
| 221 | + outs = [x] |
| 222 | + |
| 223 | + for i in range(len(self.stages)): |
| 224 | + x = self.stages[i](x) |
| 225 | + if i in self.index_list: |
| 226 | + outs.append(x) |
| 227 | + return outs |
0 commit comments