Spaces:
Sleeping
Sleeping
File size: 11,752 Bytes
8f7598e c9e9eb6 8f7598e c9e9eb6 8f7598e c9e9eb6 8f7598e c9e9eb6 8f7598e c9e9eb6 8f7598e c9e9eb6 8f7598e c9e9eb6 f5faf92 c9e9eb6 f5faf92 c9e9eb6 f5faf92 c9e9eb6 f5faf92 c9e9eb6 f5faf92 c9e9eb6 f5faf92 c9e9eb6 f5faf92 c9e9eb6 f5faf92 c9e9eb6 f5faf92 c9e9eb6 f5faf92 c9e9eb6 8f7598e c9e9eb6 8f7598e c9e9eb6 8f7598e c9e9eb6 8f7598e c9e9eb6 f5faf92 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
import torch
import torch.nn as nn
import torch.nn.functional as F
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(
in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False
)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(
planes, planes, kernel_size=3, stride=1, padding=1, bias=False
)
self.bn2 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion * planes:
self.shortcut = nn.Sequential(
nn.Conv2d(
in_planes,
self.expansion * planes,
kernel_size=1,
stride=stride,
bias=False,
),
nn.BatchNorm2d(self.expansion * planes),
)
def forward(self, x):
out = torch.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = torch.relu(out)
return out
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_planes, planes, stride=1):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(
planes, planes, kernel_size=3, stride=stride, padding=1, bias=False
)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(
planes, self.expansion * planes, kernel_size=1, bias=False
)
self.bn3 = nn.BatchNorm2d(self.expansion * planes)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion * planes:
self.shortcut = nn.Sequential(
nn.Conv2d(
in_planes,
self.expansion * planes,
kernel_size=1,
stride=stride,
bias=False,
),
nn.BatchNorm2d(self.expansion * planes),
)
def forward(self, x):
out = torch.relu(self.bn1(self.conv1(x)))
out = torch.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
out += self.shortcut(x)
out = torch.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, num_blocks, num_classes=1000):
super(ResNet, self).__init__()
self.in_planes = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
def _make_layer(self, block, planes, num_blocks, stride):
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
def forward(self, x):
out = torch.relu(self.bn1(self.conv1(x)))
out = self.maxpool(out)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.avgpool(out)
out = torch.flatten(out, 1)
out = self.fc(out)
return out
def ResNet18(num_classes=1000):
return ResNet(BasicBlock, [2, 2, 2, 2], num_classes)
def ResNet34(num_classes=1000):
return ResNet(BasicBlock, [3, 4, 6, 3], num_classes)
def ResNet50(num_classes=1000):
return ResNet(Bottleneck, [3, 4, 6, 3], num_classes)
def ResNet101(num_classes=1000):
return ResNet(Bottleneck, [3, 4, 23, 3], num_classes)
def ResNet152(num_classes=1000):
return ResNet(Bottleneck, [3, 8, 36, 3], num_classes)
import torch
import torch.nn as nn
import torch.nn.functional as F
class SAM(nn.Module):
def __init__(self, bias=False):
super(SAM, self).__init__()
self.bias = bias
self.conv = nn.Conv2d(
in_channels=2,
out_channels=1,
kernel_size=7,
stride=1,
padding=3,
dilation=1,
bias=self.bias,
)
def forward(self, x):
max = torch.max(x, 1)[0].unsqueeze(1)
avg = torch.mean(x, 1).unsqueeze(1)
concat = torch.cat((max, avg), dim=1)
output = self.conv(concat)
output = F.sigmoid(output) * x
return output
class CAM(nn.Module):
def __init__(self, channels, r):
super(CAM, self).__init__()
self.channels = channels
self.r = r
self.linear = nn.Sequential(
nn.Linear(
in_features=self.channels,
out_features=self.channels // self.r,
bias=True,
),
nn.ReLU(inplace=True),
nn.Linear(
in_features=self.channels // self.r,
out_features=self.channels,
bias=True,
),
)
def forward(self, x):
max = F.adaptive_max_pool2d(x, output_size=1)
avg = F.adaptive_avg_pool2d(x, output_size=1)
b, c, _, _ = x.size()
linear_max = self.linear(max.view(b, c)).view(b, c, 1, 1)
linear_avg = self.linear(avg.view(b, c)).view(b, c, 1, 1)
output = linear_max + linear_avg
output = F.sigmoid(output) * x
return output
class CBAM(nn.Module):
def __init__(self, channels, r):
super(CBAM, self).__init__()
self.channels = channels
self.r = r
self.sam = SAM(bias=False)
self.cam = CAM(channels=self.channels, r=self.r)
def forward(self, x):
output = self.cam(x)
output = self.sam(output)
return output + x
class ClassifierHead(nn.Module):
def __init__(self, in_features, num_classes):
super().__init__()
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.max_pool = nn.AdaptiveMaxPool2d((1, 1))
self.classifier = nn.Sequential(
nn.Linear(in_features * 2, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(1024, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, num_classes),
)
def forward(self, x):
avg_pooled = self.avg_pool(x).flatten(1)
max_pooled = self.max_pool(x).flatten(1)
features = torch.cat([avg_pooled, max_pooled], dim=1)
return self.classifier(features)
class ResNetUNet(ResNet):
def __init__(self, block, num_blocks, num_classes=1000):
super().__init__(block, num_blocks, num_classes)
# Get the expansion factor
expansion = block.expansion
# Calculate encoder channel sizes
self.enc_channels = [
64,
64 * block.expansion,
128 * block.expansion,
256 * block.expansion,
512 * block.expansion,
]
in_features = 512 * block.expansion
self.classifier_head = ClassifierHead(in_features, num_classes)
self.cbam = CBAM(channels=512 * block.expansion, r=16)
# Calculate encoder channel sizes
self.decoder5 = nn.Sequential(
nn.Conv2d((512 * expansion) + (256 * expansion), 512, 3, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.Conv2d(512, 256, 3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True),
)
self.decoder4 = nn.Sequential(
nn.Conv2d(256 + (128 * expansion), 256, 3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 128, 3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True),
)
self.decoder3 = nn.Sequential(
nn.Conv2d(128 + (64 * expansion), 128, 3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Conv2d(128, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True),
)
self.decoder2 = nn.Sequential(
nn.Conv2d(64 + 64, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True),
)
self.final_conv = nn.Sequential(
nn.Conv2d(64, 32, 3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 1, 1),
nn.Sigmoid(),
)
def forward(self, x):
input_size = x.shape[-2:]
# Encoder path
x = torch.relu(self.bn1(self.conv1(x)))
e1 = self.maxpool(x)
e2 = self.layer1(e1)
e3 = self.layer2(e2)
e4 = self.layer3(e3)
e5 = self.layer4(e4)
# Get segmentation first
e4_resized = F.interpolate(
e4, size=e5.shape[-2:], mode="bilinear", align_corners=True
)
d5 = self.decoder5(torch.cat([e5, e4_resized], dim=1))
e3_resized = F.interpolate(
e3, size=d5.shape[-2:], mode="bilinear", align_corners=True
)
d4 = self.decoder4(torch.cat([d5, e3_resized], dim=1))
e2_resized = F.interpolate(
e2, size=d4.shape[-2:], mode="bilinear", align_corners=True
)
d3 = self.decoder3(torch.cat([d4, e2_resized], dim=1))
e1_resized = F.interpolate(
e1, size=d3.shape[-2:], mode="bilinear", align_corners=True
)
d2 = self.decoder2(torch.cat([d3, e1_resized], dim=1))
seg_out = self.final_conv(d2)
seg_out = F.interpolate(
seg_out, size=input_size, mode="bilinear", align_corners=True
)
attended_features = self.cbam(e5)
# Use segmentation to mask features before classification
# Upsample segmentation mask to match feature size
attention_mask = F.interpolate(
seg_out, size=e5.shape[2:], mode="bilinear", align_corners=True
)
# Apply attention mask to features
attended_features = attended_features * (0.25 + attention_mask)
cls_out = self.classifier_head(attended_features)
return cls_out, seg_out
def ResNet18UNet(num_classes=1000):
return ResNetUNet(BasicBlock, [2, 2, 2, 2], num_classes)
def ResNet34UNet(num_classes=1000):
return ResNetUNet(BasicBlock, [3, 4, 6, 3], num_classes)
def ResNet50UNet(num_classes=1000):
return ResNetUNet(Bottleneck, [3, 4, 6, 3], num_classes)
def ResNet101UNet(num_classes=1000):
return ResNetUNet(Bottleneck, [3, 4, 23, 3], num_classes)
def ResNet152UNet(num_classes=1000):
return ResNetUNet(Bottleneck, [3, 8, 36, 3], num_classes) |