/
fam_generate.py
executable file
·253 lines (204 loc) · 8.36 KB
/
fam_generate.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
#!/usr/bin/env python2
## @file
# FAM generate file
from __future__ import division, print_function, absolute_import
import numpy as np
import tensorflow as tf
import specest
import time
import random
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import *
from keras.utils import np_utils
from keras import backend as K
from keras.regularizers import l2
from tensorflow.contrib.session_bundle import exporter
from data_generate import *
## 2xNp is the number of columns
Np = 64
## number of new items needed to calculate estimate
P = 256
L = 2
np.set_printoptions(threshold=np.nan)
## Handles flowgraph for FAM
class fam_generate(gr.top_block):
## \brief Creates flow graph
## \param modulation Modulation scheme to use
## \param sn List of SNRs
## \param sym List of symbol rates
## \param train Whether we are generating training data or testing data
def __init__(self, modulation, sn, sym, train):
self.samp_rate = samp_rate = 100e3
gr.top_block.__init__(self)
create_blocks(self, modulation, sym, sn, train)
self.blocks_add_xx_1 = blocks.add_vcc(1)
self.specest_cyclo_fam_1 = specest.cyclo_fam(Np, P, L)
self.blocks_multiply_const_vxx_3 = blocks.multiply_const_vcc(
(SNRV[sn][0], ))
self.blocks_throttle_0 = blocks.throttle(
gr.sizeof_gr_complex * 1, samp_rate, True)
self.blocks_stream_to_vector_0 = blocks.stream_to_vector(
gr.sizeof_gr_complex * 1, 1024)
self.blocks_null_sink_0 = blocks.null_sink(gr.sizeof_float * 1)
self.analog_noise_source_x_0 = analog.noise_source_c(
analog.GR_GAUSSIAN, SNRV[sn][1], np.random.randint(np.iinfo(np.int32).max))
self.analog_random_source_x_0 = blocks.vector_source_b(
map(int, np.random.randint(0, 256, 2000000)), False)
self.msgq_out = blocks_message_sink_0_msgq_out = gr.msg_queue(1)
self.blocks_message_sink_0 = blocks.message_sink(
gr.sizeof_float * 2 * Np, blocks_message_sink_0_msgq_out, False)
self.blocks_vector_to_stream_0 = blocks.vector_to_stream(
gr.sizeof_float * 1, 2 * Np)
self.blocks_stream_to_vector_0 = blocks.stream_to_vector(
gr.sizeof_float * 1, 2 * P * L * ((2 * Np) - 0))
self.blocks_probe_signal_vx_0 = blocks.probe_signal_vf(
2 * P * L * ((2 * Np) - 0))
self.connect((self.analog_noise_source_x_0, 0),
(self.blocks_add_xx_1, 1))
self.connect((self.blocks_multiply_const_vxx_3, 0),
(self.blocks_add_xx_1, 0))
if modulation == "wbfm":
self.connect((self.blocks_wavfile_source_0, 0),
(self.analog_wfm_tx_0, 0))
self.connect((self.analog_wfm_tx_0, 0),
(self.blocks_throttle_0, 0))
elif modulation == "nfm":
self.connect((self.blocks_wavfile_source_0, 0),
(self.analog_nfm_tx_0, 0))
self.connect((self.analog_nfm_tx_0, 0),
(self.blocks_throttle_0, 0))
else:
self.connect((self.analog_random_source_x_0, 0),
(self.digital_mod, 0))
self.connect((self.digital_mod, 0), (self.blocks_throttle_0, 0))
self.connect((self.blocks_throttle_0, 0),
(self.rational_resampler_xxx_0, 0))
self.connect((self.rational_resampler_xxx_0, 0),
(self.blocks_multiply_const_vxx_3, 0))
self.connect((self.blocks_add_xx_1, 0), (self.specest_cyclo_fam_1, 0))
self.connect((self.specest_cyclo_fam_1, 0),
(self.blocks_vector_to_stream_0, 0))
self.connect((self.blocks_vector_to_stream_0, 0),
(self.blocks_stream_to_vector_0, 0))
self.connect((self.blocks_stream_to_vector_0, 0),
(self.blocks_probe_signal_vx_0, 0))
## \brief Invokes flow graph and returns FAM data
## \param train Whether we are training or not
## \param m Modulation scheme
## \param sn List of SNRs
## \param z One-hot array representing modulation scheme
## \param qu Queue to return data
## \param sym List of symbol rates
def process(train, m, sn, z, qu, sym):
# Without this, multiple processes all generate exactly the same sequence of random numbers
reseed()
if train:
inp = []
out = []
else:
inp = [[] for k in range(0, len(SNR))]
out = [[] for k in range(0, len(SNR))]
tb = fam_generate(m, sn, sym, train)
tb.start()
time.sleep(3)
count = 0
while True:
floats = tb.blocks_probe_signal_vx_0.level()
if np.sum(floats) == 0:
print("Found empty FAM")
continue
floats = (floats - np.mean(floats)) / np.std(floats)
floats = np.reshape(floats, (2 * P * L, (2 * Np) - 0))
if train:
inp.append(np.array([floats]))
out.append(np.array(z))
else:
inp[sn].append(np.array([floats]))
out[sn].append(np.array(z))
if count > 30:
tb.stop()
break
count += 1
qu.put((inp, out))
## \brief Generate FAM from training data
## \param train_i Training data
## \param train_o Class for each training item
## \param test_i Testing data
## \param test_o Class for each testing item
def fam(train_i, train_o, test_i, test_o):
sess = tf.Session()
K.set_session(sess)
K.set_learning_phase(1)
batch_size = 60
nb_classes = len(MOD)
nb_epoch = 20
img_rows, img_cols = 2 * P * L, 2 * Np
nb_filters = 96
nb_pool = 2
X_train,Y_train = shuffle_in_unison_inplace( np.array(train_i) , np.array(train_o) )
model = Sequential()
model.add(Convolution2D(64, 11, 11,subsample=(2,2),
input_shape=(1, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(128, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(512,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes,init='normal'))
model.add(Activation('softmax', name="out"))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
"""
datagen = ImageDataGenerator(
#featurewise_center=True,
#featurewise_std_normalization=True,
rotation_range=20,
#width_shift_range=0.3,
#height_shift_range=0.3,
#zoom_range=[0,1.3],
horizontal_flip=True,
vertical_flip=True)
datagen.fit(X_train)
model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size,shuffle=True),
samples_per_epoch=len(X_train), nb_epoch=5,verbose=1,validation_data=(test_i[0], test_o[0]))
"""
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, shuffle=True, validation_data=(test_i[0], test_o[0]))
for s in range(len(test_i)):
if len(test_i[s]) == 0:
continue
X_test = test_i[s]
Y_test = test_o[s]
score = model.evaluate(X_test, Y_test, verbose=0)
print("SNR", SNR[s], "Test accuracy:", score[1])
K.set_learning_phase(0)
config = model.get_config()
weights = model.get_weights()
new_model = Sequential.from_config(config)
new_model.set_weights(weights)
export_path = "/tmp/fam"
export_version = 1
labels_tensor = tf.constant(MOD)
saver = tf.train.Saver(sharded=True)
model_exporter = exporter.Exporter(saver)
signature = exporter.classification_signature(
input_tensor=new_model.input,classes_tensor=labels_tensor,scores_tensor=new_model.output)
model_exporter.init(
sess.graph.as_graph_def(),
default_graph_signature=signature)
model_exporter.export(export_path, tf.constant(export_version), sess)
if __name__ == '__main__':
reseed()
test_i, test_o = getdata(range(1), [3], process)
reseed()
train_i, train_o = getdata(range(5), [3], process, True)
fam(train_i, train_o, test_i, test_o)