-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
51 lines (44 loc) · 1.69 KB
/
model.py
File metadata and controls
51 lines (44 loc) · 1.69 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import torch
import torch.nn as nn
import torch.nn.functional as F
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, downsample=False):
super().__init__()
stride = 2 if downsample else 1
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.skip = nn.Sequential()
if downsample or in_channels != out_channels:
self.skip = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1, stride=stride),
nn.BatchNorm2d(out_channels),
)
def forward(self, x):
identity = self.skip(x)
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += identity
return F.relu(out)
class ArchitectureClassifier(nn.Module):
def __init__(self, num_classes=5):
super().__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(3, 32, 3, 1, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2),
)
self.layer2 = ResidualBlock(32, 64, downsample=True)
self.layer3 = ResidualBlock(64, 128, downsample=True)
self.layer4 = ResidualBlock(128, 128)
self.global_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Linear(128, num_classes)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.global_pool(x).squeeze(-1).squeeze(-1)
return self.fc(x)