-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqmodels.py
161 lines (127 loc) · 4.59 KB
/
qmodels.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
'''
File: Contains the code of QCNN models.
'''
import torch
import torch.nn as nn
from qconv import QCONV2d
# A basic QCNN network
class Basic_QCNN(nn.Module):
def __init__(self,
num_blocks,
width,
expected_n_channels,
batch_size):
super(Basic_QCNN, self).__init__()
self.num_blocks = num_blocks
self.width = width
self.expected_n_channels = expected_n_channels
self.batch_size = batch_size
self.qconv_list = []
self.bn_list = []
for _ in range(self.num_blocks):
self.qconv_list.append(QCONV2d(width, 3, padding="same", batch_size=self.batch_size))
self.bn_list.append(nn.BatchNorm(width))
self.conv = nn.Conv2d(width, 1, 3, padding=1)
self.relu = nn.ReLU()
def forward(self, x):
x = x
for i in range(self.num_blocks):
x = self.qconv_list[i](x)
x = self.bn_list(x)
x = self.relu(x)
x = self.conv(x)
x = self.relu(x)
return x
# Architecture QCNN_RDD (original)
class QCNN_RDD(nn.Module):
def __init__(self, num_blocks, width, expected_n_channels, batch_size):
super(QCNN_RDD, self).__init__()
self.num_blocks = num_blocks
self.width = width
self.expected_n_channels = expected_n_channels
self.batch_size = batch_size
self.dropout_value = 0.3
self.d_rate = 1
self.bn1 = nn.BatchNorm2d(expected_n_channels)
self.relu = nn.ReLU()
self.conv1 = nn.Conv2d(expected_n_channels, width, 3, padding=1)
self.dropout = nn.Dropout(self.dropout_value)
self.bn2 = nn.BatchNorm2d(width)
self.conv2 = nn.Conv2d(width, 3, 3, padding=1)
self.sigmoid = nn.Sigmoid()
self.qconv_list = []
self.dconv_list = []
self.bn_list = []
for _ in range(self.num_blocks):
self.qconv_list.append(QCONV2d(width, 3, padding="same", batch_size=self.batch_size))
self.dconv_list.append(nn.Conv2d(width, width, 3, padding=1, dilation=self.d_rate))
if self.d_rate == 1:
self.d_rate = 2
elif self.d_rate == 2:
self.d_rate = 4
else:
self.d_rate = 1
self.bn_list.append(nn.BatchNorm2d(width))
def forward(self, x):
x = self.bn1(x)
x = self.relu(x)
x = self.conv1(x)
for i in range(self.num_blocks):
x_in = self.bn_list[i](x)
x_in = self.relu(x_in)
x_in = self.qconv_list[i](x_in)
x_in = self.dropout(x_in)
x_in = self.relu(x_in)
x_in = self.dconv_list[i](x_in)
x = x_in + x
x = self.bn2(x)
x = self.relu(x)
x = self.conv2(x)
x = self.sigmoid(x)
return x
# Architecture QCNN_RDD_Distances
class QCNN_RDD_Distances(nn.Module):
def __init__(self, num_blocks, width, expected_n_channels, batch_size):
super(QCNN_RDD_Distances, self).__init__()
self.num_blocks = num_blocks
self.width = width
self.expected_n_channels = expected_n_channels
self.batch_size = batch_size
self.dropout_value = 0.3
self.d_rate = 1
self.bn1 = nn.BatchNorm2d(expected_n_channels)
self.relu = nn.ReLU()
self.conv1 = nn.Conv2d(expected_n_channels, width, 3, padding=1)
self.dropout = nn.Dropout(self.dropout_value)
self.bn2 = nn.BatchNorm2d(width)
self.conv2 = nn.Conv2d(width, 1, 3, padding=1)
self.qconv_list = []
self.dconv_list = []
self.bn_list = []
for _ in range(self.num_blocks):
self.qconv_list.append(QCONV2d(width, 3, padding="same", batch_size=self.batch_size))
self.dconv_list.append(nn.Conv2d(width, width, 3, padding=1, dilation=self.d_rate))
if self.d_rate == 1:
self.d_rate = 2
elif self.d_rate == 2:
self.d_rate = 4
else:
self.d_rate = 1
self.bn_list.append(nn.BatchNorm2d(width))
def forward(self, x):
x = self.bn1(x)
x = self.relu(x)
x = self.conv1(x)
for i in range(self.num_blocks):
x_in = self.bn_list[i](x)
x_in = self.relu(x_in)
x_in = self.qconv_list[i](x_in)
x_in = self.dropout(x_in)
x_in = self.relu(x_in)
x_in = self.dconv_list[i](x_in)
x = x_in + x
x = self.bn2(x)
x = self.relu(x)
x = self.conv2(x)
x = self.relu(x)
return x