This repository was archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_components.py
More file actions
111 lines (88 loc) · 3.54 KB
/
model_components.py
File metadata and controls
111 lines (88 loc) · 3.54 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
import torch
import torch.nn as nn
import torch.nn.functional as F
class classificationHybridModel(nn.Module):
"""Defines the architecture of the discriminator network.
Note: Both discriminators D_X and D_Y have the same architecture in this assignment.
"""
def __init__(self, conv_dim_in=2, conv_dim_out=128, conv_dim_lstm=1024):
super(classificationHybridModel, self).__init__()
self.out_size = conv_dim_out
self.conv1 = nn.Conv2d(conv_dim_in, 16, (3, 3), stride=(2, 2), padding=(1, 1))
self.pool1 = nn.MaxPool2d((2, 2), stride=(2, 2))
self.dense = nn.Linear(conv_dim_lstm * 4, conv_dim_out * 4)
self.fcn1 = nn.Linear(conv_dim_out * 4, conv_dim_out * 2)
self.fcn2 = nn.Linear(2 * conv_dim_out, conv_dim_out)
self.softmax = nn.Softmax(dim=1)
self.drop1 = nn.Dropout(0.2)
self.drop2 = nn.Dropout(0.5)
self.act = nn.ReLU()
def forward(self, x):
out = self.act(self.conv1(x))
out = self.pool1(out)
out = out.view(out.size(0), -1)
out = self.act(self.dense(out))
out = self.drop2(out)
out = self.act(self.fcn1(out))
out = self.drop1(out)
out = self.fcn2(out)
return out
class maskCNNModel(nn.Module):
def __init__(self, conv_dim_lstm, lstm_dim, fc1_dim, freq_size):
super(maskCNNModel, self).__init__()
self.conv = nn.Sequential(
# cnn1
nn.ZeroPad2d((3, 3, 0, 0)),
nn.Conv2d(2, 64, kernel_size=(1, 7), dilation=(1, 1)),
nn.BatchNorm2d(64), nn.ReLU(),
# cnn2
nn.ZeroPad2d((0, 0, 3, 3)),
nn.Conv2d(64, 64, kernel_size=(7, 1), dilation=(1, 1)),
nn.BatchNorm2d(64), nn.ReLU(),
# cnn3
nn.ZeroPad2d(2),
nn.Conv2d(64, 64, kernel_size=(5, 5), dilation=(1, 1)),
nn.BatchNorm2d(64), nn.ReLU(),
# cnn4
nn.ZeroPad2d((2, 2, 4, 4)),
nn.Conv2d(64, 64, kernel_size=(5, 5), dilation=(2, 1)),
nn.BatchNorm2d(64), nn.ReLU(),
# cnn5
nn.ZeroPad2d((2, 2, 8, 8)),
nn.Conv2d(64, 64, kernel_size=(5, 5), dilation=(4, 1)),
nn.BatchNorm2d(64), nn.ReLU(),
# cnn6
nn.ZeroPad2d((2, 2, 16, 16)),
nn.Conv2d(64, 64, kernel_size=(5, 5), dilation=(8, 1)),
nn.BatchNorm2d(64), nn.ReLU(),
# cnn7
nn.ZeroPad2d((2, 2, 32, 32)),
nn.Conv2d(64, 64, kernel_size=(5, 5), dilation=(16, 1)),
nn.BatchNorm2d(64), nn.ReLU(),
# cnn8
nn.Conv2d(64, 8, kernel_size=(1, 1), dilation=(1, 1)),
nn.BatchNorm2d(8), nn.ReLU(),
)
self.lstm = nn.LSTM(
conv_dim_lstm,
lstm_dim,
batch_first=True,
bidirectional=True)
self.fc1 = nn.Linear(2 * lstm_dim, fc1_dim)
self.fc2 = nn.Linear(fc1_dim, freq_size * 2)
def forward(self, x):
out = x.transpose(2, 3).contiguous()
out = self.conv(out)
out = out.transpose(1, 2).contiguous()
out = out.view(out.size(0), out.size(1), -1)
out, _ = self.lstm(out)
out = F.relu(out)
out = self.fc1(out)
out = F.relu(out)
out = self.fc2(out)
out = out.view(out.size(0), out.size(1), 2, -1)
out = torch.sigmoid(out)
out = out.transpose(1, 2).contiguous()
out = out.transpose(2, 3).contiguous()
masked = out * x # out is mask, masked is denoised
return masked