-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPhysNetDil.py
158 lines (132 loc) · 5.59 KB
/
PhysNetDil.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
"""
PhysNet model with dilated convolution.
PhysNet implementation comes from:
'Remote Photoplethysmograph Signal Measurement from Facial Videos Using Spatio-Temporal Networks'
By Zitong Yu, 2019/05/05
Only for research purpose, and commercial use is not allowed.
MIT License
Copyright (c) 2019
"""
import torch.nn as nn
class PhysNet(nn.Module):
"""
PhysNet with 3D convolution model using dilated convolution
"""
def __init__(self, frames=128):
"""
Initialise PhysNet model
:param frames: length of sequence to process
"""
super(PhysNet, self).__init__()
self.ConvBlock1 = nn.Sequential(
nn.Conv3d(3, 16, [1, 5, 5], stride=1, padding=[0, 2, 2]),
nn.BatchNorm3d(16),
nn.ReLU(inplace=True),
)
self.ConvBlock2 = nn.Sequential(
nn.Conv3d(16, 32, [3, 3, 3], stride=1, padding=1),
nn.BatchNorm3d(32),
nn.ReLU(inplace=True),
)
self.ConvBlock3 = nn.Sequential(
nn.Conv3d(32, 64, [3, 3, 3], stride=1, padding=1, dilation=(1, 2, 2)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
)
self.ConvBlock4 = nn.Sequential(
nn.Conv3d(64, 64, [3, 3, 3], stride=1, padding=1, dilation=(1, 2, 2)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
)
self.ConvBlock5 = nn.Sequential(
nn.Conv3d(64, 64, [3, 3, 3], stride=1, padding=1, dilation=(1, 2, 2)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
)
self.ConvBlock6 = nn.Sequential(
nn.Conv3d(64, 64, [3, 3, 3], stride=1, padding=1, dilation=(1, 2, 2)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
)
self.ConvBlock7 = nn.Sequential(
nn.Conv3d(64, 64, [3, 3, 3], stride=1, padding=1, dilation=(1, 2, 2)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
)
self.ConvBlock8 = nn.Sequential(
nn.Conv3d(64, 64, [3, 3, 3], stride=1, padding=1, dilation=(1, 2, 2)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
)
self.ConvBlock9 = nn.Sequential(
nn.Conv3d(64, 64, [3, 3, 3], stride=1, padding=(1, 2, 2)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
)
self.upsample = nn.Sequential(
nn.ConvTranspose3d(in_channels=64, out_channels=64, kernel_size=[4, 1, 1], stride=[2, 1, 1],
padding=[1, 0, 0]), # [1, 128, 32]
nn.BatchNorm3d(64),
nn.ELU(),
)
self.upsample2 = nn.Sequential(
nn.ConvTranspose3d(in_channels=64, out_channels=64, kernel_size=[4, 1, 1], stride=[2, 1, 1],
padding=[1, 0, 0]), # [1, 128, 32]
nn.BatchNorm3d(64),
nn.ELU(),
)
self.ConvBlock10 = nn.Conv3d(64, 1, [1, 1, 1], stride=1, padding=0)
self.MaxpoolSpa = nn.MaxPool3d((1, 2, 2), stride=(1, 2, 2))
self.MaxpoolSpaTem = nn.MaxPool3d((2, 2, 2), stride=2)
# self.poolspa = nn.AdaptiveMaxPool3d((frames,1,1)) # pool only spatial space
self.poolspa = nn.AdaptiveAvgPool3d((frames, 1, 1)) # selects one from every frame of input
def forward(self, x): # x [3, T, 128,128]
x_visual = x
[batch, channel, length, width, height] = x.shape
x = self.ConvBlock1(x) # x [3, T, 128,128]
# print(x.size())
x = self.MaxpoolSpa(x) # x [16, T, 64,64]
x = self.ConvBlock2(x) # x [32, T, 64,64]
x = self.ConvBlock3(x) # x [32, T, 64,64]
# print(x.size())
x = self.MaxpoolSpaTem(x) # x [32, T/2, 32,32] Temporal halve
x = self.ConvBlock4(x) # x [64, T/2, 32,32]
x = self.ConvBlock5(x) # x [64, T/2, 32,32]
# print(x.size())
x = self.MaxpoolSpaTem(x) # x [64, T/4, 16,16]
x = self.ConvBlock6(x) # x [64, T/4, 16,16]
x_visual1616 = self.ConvBlock7(x) # x [64, T/4, 16,16]
# print(x.size())
x = self.MaxpoolSpa(x_visual1616) # x [64, T/4, 8,8]
x = self.ConvBlock8(x) # x [64, T/4, 8, 8]
x = self.ConvBlock9(x) # x [64, T/4, 8, 8]
# print(x.size())
x = self.upsample(x) # x [64, T/2, 8, 8]
x = self.upsample2(x) # x [64, T, 8, 8]
# h = x.register_hook(self.activations_hook)
# x = nn.ELU(inplace=True)(x)
x = self.poolspa(x) # x [64, T, 1, 1] --> groundtruth left and right - 7
x = self.ConvBlock10(x) # x [1, T, 1,1]
rPPG = x.view(-1, length)
return rPPG, x_visual, x, x_visual1616
def activations_hook(self, grad):
self.gradients = grad
def get_activations_gradient(self):
return self.gradients
def get_activations(self, x):
x = self.ConvBlock1(x) # x [3, T, 128,128]
x = self.MaxpoolSpa(x) # x [16, T, 64,64]
x = self.ConvBlock2(x) # x [32, T, 64,64]
x = self.ConvBlock3(x) # x [32, T, 64,64]
x = self.MaxpoolSpaTem(x) # x [32, T/2, 32,32] Temporal halve
x = self.ConvBlock4(x) # x [64, T/2, 32,32]
x = self.ConvBlock5(x) # x [64, T/2, 32,32]
x = self.MaxpoolSpaTem(x) # x [64, T/4, 16,16]
x = self.ConvBlock6(x) # x [64, T/4, 16,16]
x = self.ConvBlock7(x) # x [64, T/4, 16,16]
x = self.MaxpoolSpa(x) # x [64, T/4, 8,8]
x = self.ConvBlock8(x) # x [64, T/4, 8, 8]
x = self.ConvBlock9(x) # x [64, T/4, 8, 8]
x = self.upsample(x) # x [64, T/2, 8, 8]
x = self.upsample2(x) # x [64, T, 8, 8]
return x