|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +from resnet import resnet50 |
| 4 | +import numpy as np |
| 5 | +import cv2 |
| 6 | + |
| 7 | + |
| 8 | +def save_feats_mean(x, size=(256, 256)): |
| 9 | + b, c, h, w = x.shape |
| 10 | + with torch.no_grad(): |
| 11 | + x = x.detach().cpu().numpy() |
| 12 | + x = np.transpose(x[0], (1, 2, 0)) |
| 13 | + x = np.mean(x, axis=-1) |
| 14 | + x = x/np.max(x) |
| 15 | + x = x * 255.0 |
| 16 | + x = x.astype(np.uint8) |
| 17 | + if h != size[1]: |
| 18 | + x = cv2.resize(x, size) |
| 19 | + x = cv2.applyColorMap(x, cv2.COLORMAP_JET) |
| 20 | + x = np.array(x, dtype=np.uint8) |
| 21 | + return x |
| 22 | + |
| 23 | + |
| 24 | +def get_mean_attention_map(x): |
| 25 | + x = torch.mean(x, axis=1) |
| 26 | + x = torch.unsqueeze(x, 1) |
| 27 | + x = x / torch.max(x) |
| 28 | + return x |
| 29 | + |
| 30 | + |
| 31 | +class ResidualBlock(nn.Module): |
| 32 | + def __init__(self, in_c, out_c): |
| 33 | + super().__init__() |
| 34 | + self.relu = nn.ReLU() |
| 35 | + self.conv = nn.Sequential( |
| 36 | + nn.Conv2d(in_c, out_c, kernel_size=3, padding=1), |
| 37 | + nn.BatchNorm2d(out_c), |
| 38 | + nn.ReLU(), |
| 39 | + nn.Conv2d(out_c, out_c, kernel_size=3, padding=1), |
| 40 | + nn.BatchNorm2d(out_c) |
| 41 | + ) |
| 42 | + self.shortcut = nn.Sequential( |
| 43 | + nn.Conv2d(in_c, out_c, kernel_size=1, padding=0), |
| 44 | + nn.BatchNorm2d(out_c) |
| 45 | + ) |
| 46 | + |
| 47 | + def forward(self, inputs): |
| 48 | + x1 = self.conv(inputs) |
| 49 | + x2 = self.shortcut(inputs) |
| 50 | + x = self.relu(x1 + x2) |
| 51 | + return x |
| 52 | + |
| 53 | + |
| 54 | +class DilatedConv(nn.Module): |
| 55 | + def __init__(self, in_c, out_c): |
| 56 | + super().__init__() |
| 57 | + self.c1 = nn.Sequential( |
| 58 | + nn.Conv2d(in_c, out_c, kernel_size=3, padding=1, dilation=1), |
| 59 | + nn.BatchNorm2d(out_c), |
| 60 | + nn.ReLU() |
| 61 | + ) |
| 62 | + self.c2 = nn.Sequential( |
| 63 | + nn.Conv2d(in_c, out_c, kernel_size=3, padding=3, dilation=3), |
| 64 | + nn.BatchNorm2d(out_c), |
| 65 | + nn.ReLU() |
| 66 | + ) |
| 67 | + self.c3 = nn.Sequential( |
| 68 | + nn.Conv2d(in_c, out_c, kernel_size=3, padding=6, dilation=6), |
| 69 | + nn.BatchNorm2d(out_c), |
| 70 | + nn.ReLU() |
| 71 | + ) |
| 72 | + self.c4 = nn.Sequential( |
| 73 | + nn.Conv2d(in_c, out_c, kernel_size=3, padding=9, dilation=9), |
| 74 | + nn.BatchNorm2d(out_c), |
| 75 | + nn.ReLU() |
| 76 | + ) |
| 77 | + self.c5 = nn.Sequential( |
| 78 | + nn.Conv2d(out_c*4, out_c, kernel_size=1, padding=0), |
| 79 | + nn.BatchNorm2d(out_c), |
| 80 | + nn.ReLU() |
| 81 | + ) |
| 82 | + |
| 83 | + def forward(self, inputs): |
| 84 | + x1 = self.c1(inputs) |
| 85 | + x2 = self.c2(inputs) |
| 86 | + x3 = self.c3(inputs) |
| 87 | + x4 = self.c4(inputs) |
| 88 | + x = torch.cat([x1, x2, x3, x4], axis=1) |
| 89 | + x = self.c5(x) |
| 90 | + return x |
| 91 | + |
| 92 | + |
| 93 | +class ChannelAttention(nn.Module): |
| 94 | + def __init__(self, in_planes, ratio=16): |
| 95 | + super(ChannelAttention, self).__init__() |
| 96 | + self.avg_pool = nn.AdaptiveAvgPool2d(1) |
| 97 | + self.max_pool = nn.AdaptiveMaxPool2d(1) |
| 98 | + self.fc1 = nn.Conv2d(in_planes, in_planes // 16, 1, bias=False) |
| 99 | + self.relu1 = nn.ReLU() |
| 100 | + self.fc2 = nn.Conv2d(in_planes // 16, in_planes, 1, bias=False) |
| 101 | + self.sigmoid = nn.Sigmoid() |
| 102 | + |
| 103 | + def forward(self, x): |
| 104 | + x0 = x |
| 105 | + avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x)))) |
| 106 | + max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x)))) |
| 107 | + out = avg_out + max_out |
| 108 | + return x0 * self.sigmoid(out) |
| 109 | + |
| 110 | + |
| 111 | +class SpatialAttention(nn.Module): |
| 112 | + def __init__(self, kernel_size=7): |
| 113 | + super(SpatialAttention, self).__init__() |
| 114 | + assert kernel_size in (3, 7), 'kernel size must be 3 or 7' |
| 115 | + padding = 3 if kernel_size == 7 else 1 |
| 116 | + self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False) |
| 117 | + self.sigmoid = nn.Sigmoid() |
| 118 | + |
| 119 | + def forward(self, x): |
| 120 | + x0 = x |
| 121 | + avg_out = torch.mean(x, dim=1, keepdim=True) |
| 122 | + max_out, _ = torch.max(x, dim=1, keepdim=True) |
| 123 | + x = torch.cat([avg_out, max_out], dim=1) |
| 124 | + x = self.conv1(x) |
| 125 | + return x0 * self.sigmoid(x) |
| 126 | + |
| 127 | + |
| 128 | +class DecoderBlock(nn.Module): |
| 129 | + def __init__(self, in_c, out_c): |
| 130 | + super().__init__() |
| 131 | + self.up = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True) |
| 132 | + self.r1 = ResidualBlock(in_c[0]+in_c[1], out_c) |
| 133 | + self.r2 = ResidualBlock(out_c, out_c) |
| 134 | + self.ca = ChannelAttention(out_c) |
| 135 | + self.sa = SpatialAttention() |
| 136 | + |
| 137 | + def forward(self, x, s): |
| 138 | + x = self.up(x) |
| 139 | + x = torch.cat([x, s], axis=1) |
| 140 | + x = self.r1(x) |
| 141 | + x = self.r2(x) |
| 142 | + x = self.ca(x) |
| 143 | + x = self.sa(x) |
| 144 | + return x |
| 145 | + |
| 146 | + |
| 147 | +class RUPNet(nn.Module): |
| 148 | + def __init__(self): |
| 149 | + super().__init__() |
| 150 | + backbone = resnet50(pretrained=False) |
| 151 | + self.layer0 = nn.Sequential(backbone.conv1, backbone.bn1, backbone.relu) |
| 152 | + self.layer1 = nn.Sequential(backbone.maxpool, backbone.layer1) |
| 153 | + self.layer2 = backbone.layer2 |
| 154 | + self.layer3 = backbone.layer3 |
| 155 | + self.r1 = nn.Sequential(DilatedConv(64, 64), nn.MaxPool2d((8, 8))) |
| 156 | + self.r2 = nn.Sequential(DilatedConv(256, 64), nn.MaxPool2d((4, 4))) |
| 157 | + self.r3 = nn.Sequential(DilatedConv(512, 64), nn.MaxPool2d((2, 2))) |
| 158 | + self.r4 = DilatedConv(1024, 64) |
| 159 | + self.d1 = DecoderBlock([256, 512], 256) |
| 160 | + self.d2 = DecoderBlock([256, 256], 128) |
| 161 | + self.d3 = DecoderBlock([128, 64], 64) |
| 162 | + self.d4 = DecoderBlock([64, 3], 32) |
| 163 | + self.y = nn.Conv2d(32, 1, kernel_size=1, padding=0) |
| 164 | + |
| 165 | + def forward(self, x, heatmap=None): |
| 166 | + s0 = x |
| 167 | + s1 = self.layer0(s0) |
| 168 | + s2 = self.layer1(s1) |
| 169 | + s3 = self.layer2(s2) |
| 170 | + s4 = self.layer3(s3) |
| 171 | + r1 = self.r1(s1) |
| 172 | + r2 = self.r2(s2) |
| 173 | + r3 = self.r3(s3) |
| 174 | + r4 = self.r4(s4) |
| 175 | + rx = torch.cat([r1, r2, r3, r4], axis=1) |
| 176 | + d1 = self.d1(rx, s3) |
| 177 | + d2 = self.d2(d1, s2) |
| 178 | + d3 = self.d3(d2, s1) |
| 179 | + d4 = self.d4(d3, s0) |
| 180 | + y = self.y(d4) |
| 181 | + if heatmap is not None: |
| 182 | + hmap = save_feats_mean(d4) |
| 183 | + return hmap, y |
| 184 | + else: |
| 185 | + return y |
0 commit comments