-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_attention.py
More file actions
33 lines (28 loc) · 1.08 KB
/
Copy pathmodel_attention.py
File metadata and controls
33 lines (28 loc) · 1.08 KB
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
import torch
import torch.nn as nn
import torchvision.models as models
from attention_blocks import ChannelAttention, SpatialAttention
class SkinAttentionModel(nn.Module):
def __init__(self, num_classes=7):
super(SkinAttentionModel, self).__init__()
# 1. The Body: Using ResNet50
resnet = models.resnet50(weights='DEFAULT')
# Remove the last two layers (avgpool and fc)
self.features = nn.Sequential(*list(resnet.children())[:-2])
# 2. CBAM Attention Mechanism
self.ca = ChannelAttention(2048)
self.sa = SpatialAttention()
# 3. Classifier
self.avgpool = nn.AdaptiveAvgPool2d(1)
self.classifier = nn.Sequential(
nn.Dropout(0.3),
nn.Linear(2048, num_classes)
)
def forward(self, x):
x = self.features(x) # Shape: [batch, 2048, 7, 7]
x = self.ca(x) * x # Channel Attention
x = self.sa(x) * x # Spatial Attention
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x