-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
128 lines (110 loc) · 4.07 KB
/
model.py
File metadata and controls
128 lines (110 loc) · 4.07 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
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
import torch.nn as nn
import torch
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super().__init__()
self.conv1 = nn.Conv2d(
in_channels,
out_channels,
kernel_size=3,
stride=stride,
padding=1,
bias=False,
)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(
out_channels, out_channels, kernel_size=3, padding=1, bias=False
)
self.bn2 = nn.BatchNorm2d(out_channels)
self.shortcut = nn.Sequential()
self.use_shortcut = stride != 1 or in_channels != out_channels
if self.use_shortcut:
self.shortcut = nn.Sequential(
nn.Conv2d(
in_channels, out_channels, kernel_size=1, stride=stride, bias=False
),
nn.BatchNorm2d(out_channels),
)
def forward(self, x, fmap_dict=None, prefix=""):
out = self.conv1(x)
out = self.bn1(out)
out = torch.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = torch.relu(out)
shortcut = self.shortcut(x) if self.use_shortcut else x
out_add = out + shortcut
if fmap_dict is not None:
fmap_dict[f"{prefix}.conv"] = out_add
out = torch.relu(out_add)
if fmap_dict is not None:
fmap_dict[f"{prefix}.relu"] = out_add
return out
class AudioCNN(nn.Module):
def __init__(self, num_classes=50):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
)
self.layer1 = nn.ModuleList([ResidualBlock(64, 64) for _ in range(3)])
self.layer2 = nn.ModuleList(
[
ResidualBlock(64 if i == 0 else 128, 128, stride=2 if i == 0 else 1)
for i in range(4)
]
)
self.layer3 = nn.ModuleList(
[
ResidualBlock(128 if i == 0 else 256, 256, stride=2 if i == 0 else 1)
for i in range(6)
]
)
self.layer4 = nn.ModuleList(
[
ResidualBlock(256 if i == 0 else 512, 512, stride=2 if i == 0 else 1)
for i in range(3)
]
)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.dropout = nn.Dropout(0.5)
self.fc = nn.Linear(512, num_classes)
def forward(self, x, return_feature_maps=False):
if not return_feature_maps:
x = self.conv1(x)
for block in self.layer1:
x = block(x)
for block in self.layer2:
x = block(x)
for block in self.layer3:
x = block(x)
for block in self.layer4:
x = block(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1) # Flatten the tensor
x = self.dropout(x)
x = self.fc(x)
return x
else:
feature_maps = {}
x = self.conv1(x)
feature_maps["conv1"] = x
for ind, block in enumerate(self.layer1):
x = block(x, feature_maps, prefix=f"layer1.block{ind}")
feature_maps["layer1"] = x
for ind, block in enumerate(self.layer2):
x = block(x, feature_maps, prefix=f"layer2.block{ind}")
feature_maps["layer2"] = x
for ind, block in enumerate(self.layer3):
x = block(x, feature_maps, prefix=f"layer3.block{ind}")
feature_maps["layer3"] = x
for ind, block in enumerate(self.layer4):
x = block(x, feature_maps, prefix=f"layer4.block{ind}")
feature_maps["layer4"] = x
x = self.avgpool(x)
x = x.view(x.size(0), -1) # Flatten the tensor
x = self.dropout(x)
x = self.fc(x)
return x, feature_maps