-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_opp.py
134 lines (103 loc) · 4.49 KB
/
model_opp.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
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 10 15:26:09 2020
@author: Shenghuan Miao
"""
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from modelsummary import summary
""" SALIENCE """
class FeatureExtracter(nn.Module):
def __init__(self):
super( FeatureExtracter, self ).__init__()
self.conv1 = nn.Conv1d(3, 16, kernel_size=5, stride=1)
self.bn1 = nn.BatchNorm1d(16)
self.pool = nn.MaxPool1d(kernel_size=2, stride=None, padding=0,dilation=1, return_indices=False,
ceil_mode=False)
self.conv2 = nn.Conv1d(16, 16, kernel_size=5, stride=1)
self.bn2 = nn.BatchNorm1d(16)
self.conv3 = nn.Conv1d(16, 16, kernel_size=5, stride=1)
self.bn3 = nn.BatchNorm1d(16)
def forward( self, x):
x = x.permute(0, 2, 1)
x = F.relu(self.bn1(self.conv1(x)))
x = self.pool(x)
x = F.relu(self.bn2(self.conv2(x)))
x = self.pool(x)
x = F.relu(self.bn3(self.conv3(x)))
x = self.pool(x)
x = x.permute(0, 2, 1)
return x
class GlobalDiscriminator(nn.Module):
def __init__(self, prob=0.5):
super( GlobalDiscriminator, self ).__init__()
self.prob = prob
self.lstm1 = nn.LSTM(input_size=16, hidden_size=64, bidirectional=True, num_layers=1)
self.lstm2 = nn.LSTM(input_size=128, hidden_size=64, bidirectional=True, num_layers=1)
self.out = nn.Linear(128, 2)
def forward(self, x):
x = x.permute(1, 0, 2)
x = F.dropout(x, p=self.prob, training=self.training)
x, (h_n,c_n) = self.lstm1(x)
x = F.dropout(x, p=self.prob, training=self.training)
x, (h_n,c_n) = self.lstm2(x)
x = x[-1]
x = F.dropout(x, p=self.prob, training=self.training)
x = self.out(x)
return x
class LocalDiscriminator(nn.Module):
def __init__(self, prob=0.5):
super( LocalDiscriminator, self ).__init__()
self.prob = prob
self.lstm1 = nn.LSTM(input_size=16, hidden_size=64, bidirectional=True, num_layers=1)
self.out = nn.Linear(128, 2)
def forward(self, x):
x = x.permute(1, 0, 2)
x = F.dropout(x, p=self.prob, training=self.training)
x, (h_n,c_n) = self.lstm1(x)
x = x[-1]
x = F.dropout(x, p=self.prob, training=self.training)
x = self.out(x)
return x
class ActivityClassifier(nn.Module):
def __init__(self, prob=0.5, dataset='pamap'):
super( ActivityClassifier, self ).__init__()
self.prob = prob
self.lstm1 = nn.LSTM(input_size=16, hidden_size=64, bidirectional=True, num_layers=1)
self.lstm2 = nn.LSTM(input_size=128, hidden_size=64, bidirectional=True, num_layers=1)
if dataset == 'pamap':
self.out = nn.Linear(128, 12)
else:
self.out = nn.Linear(128, 5)
def forward(self, x):
x = x.permute(1, 0, 2)
x = F.dropout(x, p=self.prob, training=self.training)
x, (h_n,c_n) = self.lstm1(x)
x = F.dropout(x, p=self.prob, training=self.training)
x, (h_n,c_n) = self.lstm2(x)
x = x[-1]
x = F.dropout(x, p=self.prob, training=self.training)
x = self.out(x)
return x
class AttentionNetwork(nn.Module):
def __init__(self, dataset='pamap'):
super(AttentionNetwork, self ).__init__()
if dataset == 'pamap':
self.Q = nn.Linear(24, 24)
self.K = nn.Linear(336, 24)
self.num = 12
else:
self.Q = nn.Linear(78, 78)
self.K = nn.Linear(544, 78)
self.num = 39
def forward(self, local_features, concat_local_preds):
input_feature = torch.stack([local_features[i].reshape(-1, local_features[i].shape[1]*local_features[i].shape[2]) for i in range(self.num)], 1)
local_preds = concat_local_preds
query = self.Q(local_preds)
key = self.K(input_feature)
score = torch.matmul(key, query.reshape(-1, query.shape[1], 1))
score = F.softmax(score / math.sqrt(query.size(-1)), dim = 1)
score = score.reshape(-1, score.shape[1] * score.shape[2])
return score