-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhr_cnn.py
172 lines (131 loc) · 6.09 KB
/
hr_cnn.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
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
"""
HR-CNN model implementation from
`Visual Heart Rate Estimation with Convolutional Neural Network`
by Spetlik, R., Franc, V., Cech, J. and Matas, J.
2018
SelfAttention and GlobalContext blocks from
"""
import torch.nn as nn
import torch.nn.functional as F
import torch
import math
class HrCNN(nn.Module):
def __init__(self, rgb=3):
super(HrCNN, self).__init__()
self.rgb = rgb
self.ada_avg_pool2d = nn.AdaptiveAvgPool2d(output_size=(192, 128))
conv_init_mean = 0
conv_init_std = .1
xavier_normal_gain = 1
input_count = rgb
self.bn_input = nn.BatchNorm2d(input_count)
nn.init.normal_(self.bn_input.weight, conv_init_mean, conv_init_std)
output_count = 64
self.conv_00 = nn.Conv2d(input_count, output_count, kernel_size=(15, 10), stride=1, padding=0)
nn.init.xavier_normal_(self.conv_00.weight, gain=xavier_normal_gain)
self.max_pool2d_00 = nn.MaxPool2d(kernel_size=(15, 10), stride=(2, 2), )
self.bn_00 = nn.BatchNorm2d(output_count)
nn.init.normal_(self.bn_00.weight, conv_init_mean, conv_init_std)
input_count = 64
self.conv_01 = nn.Conv2d(input_count, output_count, kernel_size=(15, 10), stride=1, padding=0)
nn.init.xavier_normal_(self.conv_01.weight, gain=xavier_normal_gain)
self.max_pool2d_01 = nn.MaxPool2d(kernel_size=(15, 10), stride=(1, 1))
self.bn_01 = nn.BatchNorm2d(output_count)
nn.init.normal_(self.bn_01.weight, conv_init_mean, conv_init_std)
output_count = 128
self.conv_10 = nn.Conv2d(input_count, output_count, kernel_size=(15, 10), stride=1, padding=0)
nn.init.xavier_normal_(self.conv_10.weight, gain=xavier_normal_gain)
self.max_pool2d_10 = nn.MaxPool2d(kernel_size=(15, 10), stride=(1, 1))
self.bn_10 = nn.BatchNorm2d(output_count)
nn.init.normal_(self.bn_10.weight, conv_init_mean, conv_init_std)
input_count = 128
output_count = 128
self.gcb = GCBlock(output_count)
self.conv_20 = nn.Conv2d(input_count, output_count, kernel_size=(12, 10), stride=1, padding=0)
self.max_pool2d_20 = nn.MaxPool2d(kernel_size=(15, 10), stride=(1, 1))
self.bn_20 = nn.BatchNorm2d(output_count)
input_count = 128
self.conv_last = nn.Conv2d(input_count, 1, kernel_size=1, stride=1, padding=0)
nn.init.xavier_normal_(self.conv_last.weight, gain=xavier_normal_gain)
self.gradients = None
def forward(self, x):
nonlin = F.elu
x = self.ada_avg_pool2d(x)
x = self.bn_input(x)
x = nonlin(self.bn_00(self.max_pool2d_00(self.conv_00(F.dropout2d(x, p=0.0, training=self.training)))))
x = nonlin(self.bn_01(self.max_pool2d_01(self.conv_01(F.dropout(x, p=0.0, training=self.training)))))
x = nonlin(self.bn_10(self.max_pool2d_10(self.conv_10(F.dropout(x, p=0.0, training=self.training)))))
# h = x.register_hook(self.activations_hook)
x = self.gcb(x) # <--------- global convolution block
x_features = self.conv_20(F.dropout2d(x, p=0.2, training=self.training))
x = nonlin(self.bn_20(self.max_pool2d_20(x_features)))
x = self.conv_last(F.dropout(x, p=0.5, training=self.training))
if sum(x.size()[1:]) > x.dim() - 1:
raise ValueError('Check your network!')
return x
def activations_hook(self, grad):
self.gradients = grad
def get_activations_gradient(self):
return self.gradients
def get_activations(self, x):
nonlin = F.elu
x = self.ada_avg_pool2d(x)
x = self.bn_input(x)
x = nonlin(self.bn_00(self.max_pool2d_00(self.conv_00(F.dropout2d(x, p=0.0, training=self.training)))))
x = nonlin(self.bn_01(self.max_pool2d_01(self.conv_01(F.dropout(x, p=0.0, training=self.training)))))
x = nonlin(self.bn_10(self.max_pool2d_10(self.conv_10(F.dropout(x, p=0.0, training=self.training)))))
return x
class GCBlock(nn.Module):
"""
Global Context block
"""
def __init__(self, c, reduction_ratio=16):
"""
Initialize global context layer
:param c: number of input channels
:param reduction_ratio: reduction ratio, default 16
"""
super(GCBlock, self).__init__()
self.attention = nn.Conv2d(c, out_channels=1, kernel_size=1)
self.c12 = nn.Conv2d(c, math.ceil(c / reduction_ratio), kernel_size=1)
self.c15 = nn.Conv2d(math.ceil(c / reduction_ratio), c, kernel_size=1)
self.relu = nn.ReLU(inplace=True)
def forward(self, block_input):
N = block_input.size()[0]
C = block_input.size()[1]
attention = self.attention(block_input)
block_input = nn.functional.softmax(block_input)
block_input_flattened = torch.reshape(block_input, [N, C, -1])
attention = torch.squeeze(attention, dim=3)
attention_flattened = torch.reshape(attention, [N, -1])
c11 = torch.einsum('bcf,bf->bc', block_input_flattened,
attention_flattened)
c11 = torch.reshape(c11, (N, C, 1, 1))
c12 = self.c12(c11)
c15 = self.c15(self.relu(torch.layer_norm(c12, c12.size()[1:])))
cnn = torch.add(block_input, c15)
return cnn
class SelfAttention(nn.Module):
"""
SelfAttention block
"""
def __init__(self, c, reduction_ratio=16):
"""
Initialize SelfAttention layer
:param c: number of channels
:param reduction_ratio: reduction ratio, default 16
"""
super(SelfAttention, self).__init__()
self.pooling = nn.AdaptiveAvgPool2d((1, 1))
self.decoded = nn.Conv2d(c, math.ceil(c / reduction_ratio), kernel_size=1)
self.encoded = nn.Conv2d(math.ceil(c / reduction_ratio), c, kernel_size=1)
self.relu = nn.ReLU()
def forward(self, x):
N = x.size()[0]
C = x.size()[1]
xx = self.pooling(x)
decoded = self.decoded(xx)
encoded = self.encoded(self.relu(torch.layer_norm(decoded, decoded.size()[1:])))
encoded = nn.functional.softmax(encoded)
cnn = x * encoded
return cnn