-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcnn6.py
97 lines (77 loc) · 3.38 KB
/
cnn6.py
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
import torch
import torch.nn as nn
import torch.nn.functional as F
def init_layer(layer):
"""Initialize a Linear or Convolutional layer. """
nn.init.xavier_uniform_(layer.weight)
if hasattr(layer, 'bias'):
if layer.bias is not None:
layer.bias.data.fill_(0.)
def init_bn(bn):
"""Initialize a Batchnorm layer. """
bn.bias.data.fill_(0.)
bn.weight.data.fill_(1.)
class ConvBlock5x5(nn.Module): #for CNN6
def __init__(self, in_channels, out_channels, stride=(1,1)):
super(ConvBlock5x5, self).__init__()
self.conv1 = nn.Conv2d(in_channels=in_channels,
out_channels=out_channels,
kernel_size=(5, 5), stride=stride,
padding=(2, 2), bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.init_weight()
def init_weight(self):
init_layer(self.conv1)
init_bn(self.bn1)
def forward(self, input, pool_size=(2, 2), pool_type='avg'):
x = input
x = F.relu_(self.bn1(self.conv1(x)))
if pool_type == 'max':
x = F.max_pool2d(x, kernel_size=pool_size)
elif pool_type == 'avg':
x = F.avg_pool2d(x, kernel_size=pool_size)
elif pool_type == 'avg+max':
x1 = F.avg_pool2d(x, kernel_size=pool_size)
x2 = F.max_pool2d(x, kernel_size=pool_size)
x = x1 + x2
else:
raise Exception('Incorrect argument!')
return x
class CNN6(nn.Module):
def __init__(self):
super(CNN6, self).__init__()
self.final_feat_dim = 512
self.do_dropout = False
self.conv_block1 = ConvBlock5x5(in_channels=1, out_channels=64, stride=(1,1))
self.conv_block2 = ConvBlock5x5(in_channels=64, out_channels=128, stride=(1,1))
self.conv_block3 = ConvBlock5x5(in_channels=128, out_channels=256, stride=(1,1))
self.conv_block4 = ConvBlock5x5(in_channels=256, out_channels=512, stride=(1,1))
self.dropout = nn.Dropout(0.2)
# self.linear = nn.Linear(512, num_classes, bias=True)
def load_sl_official_weights(self):
""" download AudioSet pretrained CNN6 in https://zenodo.org/record/3960586#.Y8dz8y_kEiY
"""
weights = torch.load('pretrained_models/Cnn6_mAP=0.343.pth')['model']
state_dict = {k: v for k, v in weights.items() if k in self.state_dict().keys()}
missing, unexpected = self.load_state_dict(state_dict, strict=False)
def forward(self, x):
x = self.conv_block1(x, pool_size=(2, 2), pool_type='avg')
if self.do_dropout:
x = self.dropout(x)
x = self.conv_block2(x, pool_size=(2, 2), pool_type='avg')
if self.do_dropout:
x = self.dropout(x)
x = self.conv_block3(x, pool_size=(2, 2), pool_type='avg')
if self.do_dropout:
x = self.dropout(x)
x = self.conv_block4(x, pool_size=(2, 2), pool_type='avg')
if self.do_dropout:
x = self.dropout(x)
x = torch.mean(x, dim=3) #mean over time dim
(x1, _) = torch.max(x, dim=2) #max over freq dim
x2 = torch.mean(x, dim=2) #mean over freq dim (after mean over time)
x = x1 + x2
# if self.embed_only:
# return x
# return self.linear(x)
return x