-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
464 lines (373 loc) · 16.1 KB
/
model.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import math
from tensorflow.keras import layers, Model
import numpy as np
import tensorflow as tf
from config import parse_args
import glob
# ResBlk -> Tx
class ResidualBlockTx(layers.Layer):
def __init__(self, out_channel, strides: int=1, downsample: bool=False, name: str=None, **kwargs) -> None:
super().__init__(name=name, **kwargs)
self.out_channel = out_channel
self.strides = strides
self.downsample = downsample
if self.downsample:
self.Conv2D_0 = layers.Conv2D(filters=self.out_channel, kernel_size=1,
strides=self.strides, padding='same', activation=None)
self.BatchNorm_0 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5)
self.Conv2D_1 = layers.Conv2D(filters=self.out_channel, kernel_size=3,
strides=self.strides, padding='same', activation=None)
self.Conv2D_2 = layers.Conv2D(filters=self.out_channel, kernel_size=3,
strides=1, padding='same',activation=None)
self.BatchNorm_1 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5)
self.BatchNorm_2 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5)
self.ELU = layers.ELU()
self.Add = layers.Add()
def call(self, inputs, *args, **kwargs):
residual = inputs
if self.downsample:
residual = self.Conv2D_0(inputs)
residual = self.BatchNorm_0(residual)
x = self.Conv2D_1(inputs)
x = self.BatchNorm_1(x)
x = self.ELU(x)
x = self.Conv2D_2(x)
x = self.BatchNorm_2(x)
x = self.Add([x, residual])
x = self.ELU(x)
return x
def get_config(self):
base_config = super().get_config()
base_config['out_channel'] = self.out_channel
base_config['strides'] = self.strides
base_config['downsample'] = self.downsample
return base_config
# ResBlk -> R
class ResidualBlock(layers.Layer):
def __init__(self, out_channel, strides: int=1, downsample: bool=False, name: str=None, **kwargs) -> None:
super().__init__(name=name, **kwargs)
self.out_channel = out_channel
self.strides = strides
self.downsample = downsample
if self.downsample:
self.Conv1D_0 = layers.Conv1D(filters=self.out_channel, kernel_size=1,
strides=self.strides, padding='same', activation=None)
self.BatchNorm_0 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5)
self.Conv1D_1 = layers.Conv1D(filters=self.out_channel, kernel_size=3,
strides=self.strides, padding='same', activation=None)
self.Conv1D_2 = layers.Conv1D(filters=self.out_channel, kernel_size=3,
strides=1, padding='same', activation=None)
self.BatchNorm_1 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5)
self.BatchNorm_2 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5)
self.ELU = layers.ELU()
self.Add = layers.Add()
def call(self, inputs, *args, **kwargs):
residual = inputs
if self.downsample:
residual = self.Conv1D_0(inputs)
residual = self.BatchNorm_0(residual)
x = self.Conv1D_1(inputs)
x = self.BatchNorm_1(x)
x = self.ELU(x)
x = self.Conv1D_2(x)
x = self.BatchNorm_2(x)
x = self.Add([x, residual])
x = self.ELU(x)
return x
def get_config(self):
base_config = super().get_config()
base_config['out_channel'] = self.out_channel
base_config['strides'] = self.strides
base_config['downsample'] = self.downsample
return base_config
# ResBlk -> Rx
class ResidualBlockRx(layers.Layer):
def __init__(self, out_channel, strides: int=1, upsample: bool=False, name: str=None, **kwargs) -> None:
super().__init__(name=name, **kwargs)
self.out_channel = out_channel
self.strides = strides
self.upsample = upsample
if self.upsample:
self.Conv2DTrans_0 = layers.Conv2DTranspose(filters=self.out_channel, kernel_size=1,
strides=self.strides, padding='same', activation=None)
self.BatchNorm_0 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5)
self.Conv2DTrans_1 = layers.Conv2DTranspose(filters=self.out_channel, kernel_size=3,
strides=self.strides, padding='same', activation=None)
self.Conv2DTrans_2 = layers.Conv2DTranspose(filters=self.out_channel, kernel_size=3,
strides=1, padding='same', activation=None)
self.BatchNorm_1 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5)
self.BatchNorm_2 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5)
self.ELU = layers.ELU()
self.Add = layers.Add()
def call(self, inputs, *args, **kwargs):
residual = inputs
if self.upsample:
residual = self.Conv2DTrans_0(inputs)
residual = self.BatchNorm_0(residual)
x = self.Conv2DTrans_1(inputs)
x = self.BatchNorm_1(x)
x = self.ELU(x)
x = self.Conv2DTrans_2(x)
x = self.BatchNorm_2(x)
x = self.Add([x, residual])
x = self.ELU(x)
return x
def get_config(self):
base_config = super().get_config()
base_config['out_channel'] = self.out_channel
base_config['strides'] = self.strides
base_config['upsample'] = self.upsample
return base_config
class generator_conditional(layers.Layer):
def __init__(self, name: str=None):
super(generator_conditional, self).__init__(name=None)
self.Conv2D_1 = layers.Conv1D(filters=256, kernel_size=5, padding='same', activation='leaky_relu')
self.Conv2D_2 = layers.Conv1D(filters=128, kernel_size=3, padding='same', activation='leaky_relu')
self.Conv2D_3 = layers.Conv1D(filters=64, kernel_size=3, padding='same', activation='leaky_relu')
self.Conv2D_4 = layers.Conv1D(filters=2, kernel_size=3, padding='same')
def call(self, inputs):
z = inputs[0]
conditioning = inputs[1]
z_conbine = tf.concat([z, conditioning], -1)
outputs = self.Conv2D_1(z_conbine)
outputs = self.Conv2D_2(outputs)
outputs = self.Conv2D_3(outputs)
outputs = self.Conv2D_4(outputs)
return outputs
class discriminator_conditional(layers.Layer):
def __init__(self, name: str=None):
super(discriminator_conditional, self).__init__(name=None)
self.Conv2D_1 = layers.Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')
self.Conv2D_2 = layers.Conv1D(filters=128, kernel_size=3, padding='same', activation='relu')
self.Conv2D_3 = layers.Conv1D(filters=64, kernel_size=3, padding='same', activation='relu')
self.Conv2D_4 = layers.Conv1D(filters=16, kernel_size=3, padding='same')
self.Flatten = layers.Flatten()
self.Dence = layers.Dense(units=100, activation=None)
#self.FC = tf.nn.relu(layers.Dense(units=100, activation=None))
self.D_logit = layers.Dense(units=1, activation=None)
#self.D_prob = tf.nn.sigmoid()
def call(self, inputs):
x = inputs[0]
conditioning = inputs[1]
z_conbine = tf.concat([x, conditioning], -1)
z = self.Conv2D_1(z_conbine)
z = tf.reduce_mean(z, axis=0, keepdims=True)
z = self.Conv2D_2(z)
z = self.Conv2D_3(z)
z = self.Conv2D_4(z)
z = self.Flatten(z)
z = self.Dence(z)
z = tf.nn.relu(z)
D_logit = self.D_logit(z)
#D_prob = tf.nn.sigmoid(D_logit)
return D_logit#, D_prob
# semantic encoder
class SemanticEncoder(layers.Layer):
def __init__(self, name=None, **kwargs):
super(SemanticEncoder, self).__init__(name=name, **kwargs)
self.Conv2D = layers.Conv2D(filters=4, kernel_size=3, strides=1, padding='same', activation='elu')
self.ResBlk_1 = ResidualBlockTx(out_channel=8, strides=2, downsample=True)
self.ResBlk_2 = ResidualBlockTx(out_channel=16, strides=2, downsample=True)
def call(self, inputs, *args, **kwargs):
x = self.Conv2D(inputs)
x = self.ResBlk_1(x)
x = self.ResBlk_2(x)
return x
# channel encoder
class ChannelEncoder(layers.Layer):
def __init__(self, num_symbol, name=None, **kwargs):
super(ChannelEncoder, self).__init__(name=name, **kwargs)
self.ResBlk_1 = ResidualBlockTx(out_channel=32, strides=1, downsample=True)
self.ResBlk_2 = ResidualBlockTx(out_channel=32, strides=1, downsample=False)
self.Flatten = layers.Flatten()
self.Dense = layers.Dense(units=2*num_symbol, activation=None, use_bias=True)
self.Reshape = layers.Reshape((-1, 2))
def call(self, inputs, *args, **kwargs):
x = self.ResBlk_1(inputs)
x = self.ResBlk_2(x)
x = self.Flatten(x)
x = self.Dense(x)
x = self.Reshape(x)
# power norm
x_norm = tf.math.sqrt(tf.cast(x.shape[1], tf.float32) / 2.0) * tf.math.l2_normalize(x, axis=1)
return x_norm
class ChannelLayer(layers.Layer):
def __init__(self, snr_db, channel_type: str='AWGN', name: str=None, **kwargs):
super().__init__(name=name, **kwargs)
self.snr_db = snr_db
self.channel_type = channel_type
def call(self, inputs, *args, **kwargs):
# noise std
Es = 1
EsN0 = 10 ** (self.snr_db / 10)
N0 = Es / EsN0
sigma = math.sqrt(N0 / 2)
std = tf.constant(value=sigma, dtype=tf.float32)
# signal
inputs_real = inputs[:, :, 0]
inputs_imag = inputs[:, :, 1]
inputs_complex = tf.complex(real=inputs_real, imag=inputs_imag)
# AWGN channel
if self.channel_type == 'AWGN':
h_complex = tf.complex(real=1., imag=0.)
# Rayleigh channel
elif self.channel_type == 'Rayleigh':
h_real = tf.divide(
tf.random.normal(shape=tf.shape(inputs_complex), mean=0.0, stddev=1.0, dtype=tf.float32),
tf.sqrt(2.))
h_imag = tf.divide(
tf.random.normal(shape=tf.shape(inputs_complex), mean=0.0, stddev=1.0, dtype=tf.float32),
tf.sqrt(2.))
h_complex = tf.complex(real=h_real, imag=h_imag)
# noise
n_real = tf.random.normal(shape=tf.shape(inputs_complex), mean=0.0, stddev=std, dtype=tf.float32)
n_imag = tf.random.normal(shape=tf.shape(inputs_complex), mean=0.0, stddev=std, dtype=tf.float32)
noise = tf.complex(real=n_real, imag=n_imag)
# received signal y
hx = tf.multiply(h_complex, inputs_complex)
y_complex = tf.add(hx, noise)
# reshape
x_hat_complex = tf.math.divide_no_nan(y_complex, h_complex)
x_hat_real = tf.math.real(x_hat_complex)
x_hat_imag = tf.math.imag(x_hat_complex)
x_hat_real = tf.expand_dims(x_hat_real, axis=-1)
x_hat_imag = tf.expand_dims(x_hat_imag, axis=-1)
x_hat = tf.concat([x_hat_real, x_hat_imag], axis=-1)
return x_hat
def get_config(self):
base_config = super().get_config()
base_config['snr_db'] = self.snr_db
base_config['channel_type'] = self.channel_type
return base_config
# channel decoder
class ChannelDecoder(layers.Layer):
def __init__(self, name=None, **kwargs):
super(ChannelDecoder, self).__init__(name=name, **kwargs)
self.Concatenate = layers.Concatenate(axis=1)
self.Flatten = layers.Flatten()
self.Dense_1 = layers.Dense(units=7 * 7 * 16, activation=None, use_bias=True)
self.Reshape = layers.Reshape((7, 7, 16))
self.ResBlk_1 = ResidualBlockRx(out_channel=32, strides=1, upsample=True)
self.ResBlk_2 = ResidualBlockRx(out_channel=16, strides=1, upsample=True)
def call(self, inputs, *args, **kwargs):
#oncat = self.Concatenate(inputs)
x = self.Flatten(inputs)
x = self.Dense_1(x)
# x = self.Dense_2(x)
x = self.Reshape(x)
x = self.ResBlk_1(x)
x = self.ResBlk_2(x)
return x
# semantic decoder
class SemanticDecoder(layers.Layer):
def __init__(self, name=None, **kwargs):
super(SemanticDecoder, self).__init__(name=name, **kwargs)
self.ResBlk_1 = ResidualBlockRx(out_channel=8, strides=2, upsample=True)
self.ResBlk_2 = ResidualBlockRx(out_channel=4, strides=2, upsample=True)
self.Flatten = layers.Flatten()
self.TransConv2D = layers.Conv2DTranspose(filters=1, kernel_size=3,
strides=1, padding='same', activation=None)
self.Sigmoid = layers.Activation('sigmoid')
self.drop = layers.Activation('Softmax')
def call(self, inputs, *args, **kwargs):
x = self.ResBlk_1(inputs)
x = self.ResBlk_2(x)
x = self.TransConv2D(x)
x = self.Sigmoid(x)
return x
# class SemanticTransmitter(Model):
# def __init__(self, args):
# super(SemanticTransmitter, self).__init__()
#
# self.SE = SemanticEncoder(name='SE')
#
# self.CE = ChannelEncoder(num_symbol=args.num_symbol_node, name='CE')
#
# def call(self, inputs):
# x = self.SE(inputs[0])
# x = self.CE(x)
# return x
#
#
# class SemanticReceiver(Model):
# def __init__(self, args):
# super(SemanticReceiver, self).__init__()
#
# self.SD = SemanticDecoder(name='SD')
#
# self.CD = ChannelDecoder(name='CD')
#
# def call(self, y):
# rec = self.SD(y)
# rec = self.CD(rec)
#
# return rec
class SemanticComm(Model):
def __init__(self, args, train_channel='real_channel', **kwargs):
super().__init__(**kwargs)
self.train_channel = train_channel
# transmitter
self.SE_A = SemanticEncoder(name='SE_A')
self.CE_A = ChannelEncoder(num_symbol=args.num_symbol_node, name='CE_A')
self.SE_B = SemanticEncoder(name='SE_B')
self.CE_B = ChannelEncoder(num_symbol=args.num_symbol_node, name='CE_B')
if train_channel == 'real_channel':
self.channel_A = ChannelLayer(snr_db=args.snr_train_dB_down, channel_type=args.channel_type, name='channel_A')
self.channel_B = ChannelLayer(snr_db=args.snr_train_dB_down, channel_type=args.channel_type,
name='channel_B')
# receiver
self.SD_A = SemanticDecoder(name='SD_A')
self.CD_A = ChannelDecoder(name='CD_A')
self.SD_B = SemanticDecoder(name='SD_B')
self.CD_B = ChannelDecoder(name='CD_B')
# Tx_A
def transmitter_A(self, x):
x = self.SE_A(x)
x = self.CE_A(x)
return x
# Tx_B
def transmitter_B(self, x):
x = self.SE_B(x)
x = self.CE_B(x)
return x
# Rx_A
def receiver_A(self, x):
x = self.CD_A(x)
x = self.SD_A(x)
return x
# Rx_B
def receiver_B(self, x):
x = self.CD_B(x)
x = self.SD_B(x)
return x
def call(self, inputs, training=None):
x_A = self.transmitter_A(inputs[0])
x_B = self.transmitter_B(inputs[1])
y_A = self.channel_A(x_A)
y_B = self.channel_B(x_B)
y_A = self.receiver_A(y_A)
y_B = self.receiver_B(y_B)
return y_A, y_B
class _Generator_(Model):
def __init__(self):
super().__init__()
self.gen_A = generator_conditional(name='gen_A')
def call(self, inputs):
rec_A = self.gen_A(inputs)
return rec_A
class _Discriminator_(Model):
def __init__(self):
super().__init__()
self.disc_A = discriminator_conditional(name='disc_A')
def call(self, inputs):
rec_A = self.disc_A(inputs)
return rec_A
def semantic_autoencoder(args, **kwargs):
model = SemanticComm(args)
return model
def generator_model():
model = _Generator_()
return model
def discirminator_model():
model = _Discriminator_()
return model