-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallmodels_superres_nonfeatup.py
336 lines (295 loc) · 11 KB
/
allmodels_superres_nonfeatup.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import torch
import torch.nn as nn
import torchvision.models as models
from torchvision import transforms, datasets
from torch.utils.data import DataLoader, Subset
import pennylane as qml
import argparse
from vit_pytorch import ViT
from architecture.swin_transformer import SwinTransformer
from semantic_communication.noise import add_awgn_noise, add_rayleigh_noise
# Quantum node setup
n_qubits = 10
dev = qml.device("lightning.gpu", wires=n_qubits)
@qml.qnode(dev)
def qnode(inputs, weights):
qml.AmplitudeEmbedding(inputs, wires=range(n_qubits), normalize=True, pad_with=0.0)
qml.BasicEntanglerLayers(weights, wires=range(n_qubits))
return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]
weight_shapes = {"weights": (10, n_qubits)}
# expanded_circuit = qml.transforms.broadcast_expand(qnode)
class QNet(nn.Module):
def __init__(self):
super().__init__()
self.qlayer = qml.qnn.TorchLayer(qnode, weight_shapes)
def forward(self, x):
return self.qlayer(x)
class InceptionEncoder(nn.Module):
def __init__(self):
super(InceptionEncoder, self).__init__()
self.inception = models.inception_v3(pretrained=True, aux_logits=True)
def forward(self, x):
if self.training:
x, aux = self.inception(x)
return x, aux
else:
x = self.inception(x)
return x
class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
self.fc = nn.Sequential(
nn.Linear(1000, 512 * 7 * 7), # Adjusting to match the starting feature map size
nn.ReLU()
)
self.deconv = nn.Sequential(
nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1), # 7x7 -> 14x14
nn.ReLU(),
nn.ConvTranspose2d(256, 128, kernel_size=4, stride=2, padding=1), # 14x14 -> 28x28
nn.ReLU(),
nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1), # 28x28 -> 56x56
nn.ReLU(),
nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=1), # 56x56 -> 112x112
nn.ReLU(),
nn.ConvTranspose2d(32, 3, kernel_size=4, stride=2, padding=1), # 112x112 -> 224x224
)
def forward(self, x):
x = self.fc(x)
x = x.view(x.size(0), 512, 7, 7) # Reshape to (batch_size, 512, 7, 7)
x = self.deconv(x)
return x
class DecoderInception(nn.Module):
def __init__(self):
super(DecoderInception, self).__init__()
self.fc = nn.Sequential(
nn.Linear(1000, 512 * 8 * 8),
nn.ReLU()
)
self.deconv = nn.Sequential(
nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1), # 8x8 -> 16x16
nn.ReLU(),
nn.ConvTranspose2d(256, 128, kernel_size=4, stride=2, padding=1), # 16x16 -> 32x32
nn.ReLU(),
nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1), # 32x32 -> 64x64
nn.ReLU(),
nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=1), # 64x64 -> 128x128
nn.ReLU(),
nn.ConvTranspose2d(32, 16, kernel_size=4, stride=2, padding=1), # 128x128 -> 256x256
nn.ReLU(),
nn.Conv2d(16, 3, kernel_size=3, stride=1, padding=1), # Adjust to final shape
nn.ReLU()
)
def forward(self, x):
x = self.fc(x)
x = x.view(x.size(0), 512, 8, 8) # Reshape to (batch_size, 512, 8, 8)
x = self.deconv(x)
x = nn.functional.interpolate(x, size=(299, 299), mode='bilinear', align_corners=False)
return x
class InceptionV3(nn.Module):
def __init__(self, num_classes, device):
super(InceptionV3, self).__init__()
self.encoder = InceptionEncoder().to(device)
self.decoder = DecoderInception().to(device)
self.flatten = nn.Flatten()
def forward(self, x, snr=None, noise_type=None):
if self.training:
x, aux = self.encoder(x)
else:
x = self.encoder(x)
x = self.flatten(x)
# Adding noise to the encoded features
if noise_type == 'awgn' and snr is not None:
x = add_awgn_noise(x, snr)
elif noise_type == 'rayleigh' and snr is not None:
x = add_rayleigh_noise(x, snr)
x = self.decoder(x)
return x
class QInceptionV3(nn.Module):
def __init__(self, num_classes, device):
super(QInceptionV3, self).__init__()
self.encoder = InceptionEncoder().to(device)
self.decoder = DecoderInception().to(device)
self.flatten = nn.Flatten()
self.final_layer = nn.Linear(n_qubits, num_classes)
self.qnet = QNet().to(device)
self.before_layer = nn.Sequential(
nn.Linear(1000, 256),
nn.ReLU(),
nn.Linear(256, n_qubits)
)
self.relu = nn.ReLU()
def forward(self, x, snr=None, noise_type=None):
if self.training:
x, aux = self.encoder(x)
else:
x = self.encoder(x)
x = self.flatten(x)
if noise_type == 'awgn' and snr is not None:
x = add_awgn_noise(x, snr)
elif noise_type == 'rayleigh' and snr is not None:
x = add_rayleigh_noise(x, snr)
x2 = self.before_layer(x)
x2 = self.qnet(x2)
x2 = self.final_layer(x2)
x = self.decoder(x + x2)
return x
# ResNet-50
class ResNetEncoder(nn.Module):
def __init__(self):
super(ResNetEncoder, self).__init__()
self.resnet = models.resnet50(pretrained=True)
def forward(self, x):
x = self.resnet(x)
return x
class ResNet50(nn.Module):
def __init__(self, num_classes, device):
super(ResNet50, self).__init__()
self.encoder = ResNetEncoder().to(device)
self.decoder = Decoder().to(device)
self.flatten = nn.Flatten()
def forward(self, x, snr=None, noise_type=None):
x = self.encoder(x)
x = self.flatten(x)
if noise_type == 'awgn' and snr is not None:
x = add_awgn_noise(x, snr)
elif noise_type == 'rayleigh' and snr is not None:
x = add_rayleigh_noise(x, snr)
x = self.decoder(x)
return x
class QResNet50(nn.Module):
def __init__(self, num_classes, device):
super(QResNet50, self).__init__()
self.encoder = ResNetEncoder().to(device)
self.decoder = Decoder().to(device)
self.flatten = nn.Flatten()
self.relu = nn.ReLU()
self.qnet = QNet().to(device)
self.before_layer = nn.Sequential(
nn.Linear(1000, 256),
nn.ReLU(),
nn.Linear(256, n_qubits)
)
self.final_layer = nn.Linear(n_qubits, num_classes)
def forward(self, x, snr=None, noise_type=None):
x = self.encoder(x)
x = self.flatten(x)
if noise_type == 'awgn' and snr is not None:
x = add_awgn_noise(x, snr)
elif noise_type == 'rayleigh' and snr is not None:
x = add_rayleigh_noise(x, snr)
x2 = self.before_layer(x)
x2 = self.qnet(x2)
x2 = self.final_layer(x2)
x = self.decoder(x + x2)
return x
# Vision Transformer
class ViTEncoder(nn.Module):
def __init__(self):
super(ViTEncoder, self).__init__()
self.vit = ViT(
image_size = 224,
patch_size = 32,
num_classes = 1000,
dim = 1024,
depth = 6,
heads = 16,
mlp_dim = 2048,
dropout = 0.1,
emb_dropout = 0.1
)
def forward(self, x):
return self.vit(x)
class ViT32(nn.Module):
def __init__(self, num_classes, device):
super(ViT32, self).__init__()
self.encoder = ViTEncoder().to(device)
self.decoder = Decoder().to(device)
self.flatten = nn.Flatten()
def forward(self, x, snr=None, noise_type=None):
x = self.encoder(x)
x = self.flatten(x)
if noise_type == 'awgn' and snr is not None:
x = add_awgn_noise(x, snr)
elif noise_type == 'rayleigh' and snr is not None:
x = add_rayleigh_noise(x, snr)
x = self.decoder(x)
return x
class QViT32(nn.Module):
def __init__(self, num_classes, device):
super(QViT32, self).__init__()
self.encoder = ViTEncoder().to(device)
self.decoder = Decoder().to(device)
self.flatten = nn.Flatten()
self.qnet = QNet().to(device)
self.final_layer = nn.Linear(n_qubits, num_classes)
self.relu = nn.ReLU()
self.before_layer = nn.Sequential(
nn.Linear(1000, 256),
nn.ReLU(),
nn.Linear(256, n_qubits)
)
def forward(self, x, snr=None, noise_type=None):
x = self.encoder(x)
x = self.flatten(x)
if noise_type == 'awgn' and snr is not None:
x = add_awgn_noise(x, snr)
elif noise_type == 'rayleigh' and snr is not None:
x = add_rayleigh_noise(x, snr)
x2 = self.before_layer(x)
x2 = self.qnet(x2)
x2 = self.final_layer(x2)
x = self.decoder(x + x2)
return x
# Swin Transformer
class SwinEncoder(nn.Module):
def __init__(self, device):
super(SwinEncoder, self).__init__()
self.swin = SwinTransformer(
in_chans=3,
window_size=7,
num_classes=1000,
drop_rate=0.1,
).to(device)
def forward(self, x):
return self.swin(x)
class SwinT(nn.Module):
def __init__(self, num_classes, device):
super(SwinT, self).__init__()
self.encoder = SwinEncoder(device).to(device)
self.decoder = Decoder().to(device)
self.flatten = nn.Flatten()
def forward(self, x, snr=None, noise_type=None):
x = self.encoder(x)
x = self.flatten(x)
if noise_type == 'awgn' and snr is not None:
x = add_awgn_noise(x, snr)
elif noise_type == 'rayleigh' and snr is not None:
x = add_rayleigh_noise(x, snr)
x = self.decoder(x)
return x
class QSwinT(nn.Module):
def __init__(self, num_classes, device):
super(QSwinT, self).__init__()
self.encoder = SwinEncoder(device).to(device)
self.decoder = Decoder().to(device)
self.flatten = nn.Flatten()
self.qnet = QNet().to(device)
self.final_layer = nn.Linear(n_qubits, num_classes)
self.relu = nn.ReLU()
self.before_layer = nn.Sequential(
nn.Linear(1000, 256),
nn.ReLU(),
nn.Linear(256, n_qubits)
)
def forward(self, x, snr=None, noise_type=None):
x = self.encoder(x)
x = self.flatten(x)
if noise_type == 'awgn' and snr is not None:
x = add_awgn_noise(x, snr)
elif noise_type == 'rayleigh' and snr is not None:
x = add_rayleigh_noise(x, snr)
x2 = self.before_layer(x)
x2 = self.qnet(x2)
x2 = self.final_layer(x2)
x = self.decoder(x + x2)
return x