-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
328 lines (255 loc) · 10.8 KB
/
example.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
# Ensure that the least loaded GPU is used
#import setGPU
# Plotting Includes
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")
# External Includes
import numpy as np
from pprint import pprint
from torch.autograd import Variable
import torch.nn as nn
from torch.optim import Adam
from torch.utils.data import DataLoader
# Internal Includes
from rfml.data import Dataset, Encoder
from rfml.data.converters import load_RML201610A_dataset
from rfml.nbutils import plot_acc_vs_snr, plot_confusion, plot_convergence, plot_IQ
from rfml.nn.eval import compute_accuracy, compute_accuracy_on_cross_sections, compute_confusion
from rfml.nn.model import Model
import time
gpu = True # Set to True to use a GPU for training
fig_dir = None # Set to a file path if you'd like to save the plots generated
data_path = './data/RML2016.10a_dict.pkl' # Set to a file path if you've downloaded RML2016.10A locally (e.g. path/to/file/filename.pkl
#Data Structure
# 11 Modulation classes
# - 20 SNR sets per (-20db - 18dB, increments of 2dB)
# - 1000 samples per each Mod/SNR
# - 2 components in each sample (I/Q)
start_time = time.time()
dataset = load_RML201610A_dataset(path=data_path)
print(len(dataset))
pprint(dataset.get_examples_per_class())
print(dataset.df.head().to_string())
#train, test2 = dataset.split(frac=0.9, on=["Modulation", "SNR"])
train, test = dataset.split(frac=0.3, on=["Modulation", "SNR"])
train, val = train.split(frac=0.05, on=["Modulation", "SNR"])
print("Dataset Length after split")
print("=================")
print(len(dataset))
print("=================")
print("Training Examples")
print("=================")
pprint(train.get_examples_per_class())
print("=================")
print()
print("Validation Examples")
print("=================")
pprint(val.get_examples_per_class())
print("=================")
print()
print("Testing Examples")
print("=================")
pprint(test.get_examples_per_class())
print("=================")
print("Label Encoder")
print("=================")
le = Encoder(["WBFM",
"AM-DSB",
"AM-SSB",
"CPFSK",
"GFSK",
"BPSK",
"QPSK",
"8PSK",
"PAM4",
"QAM16",
"QAM64"],
label_name="Modulation")
print(le)
## ======================================= PLOT ONE Signal =======================================================
# Plot a sample of the data
# You can choose a different sample by changing
# idx = 999 #0-1000
# snr = 18.0
# modulation = "BPSK"
# #Plots 128 datapoints per set above
#
# mask = (dataset.df["SNR"] == snr) & (dataset.df["Modulation"] == modulation)
# sample = dataset.as_numpy(mask=mask, le=le)[0][idx,0,:]
# t = np.arange(sample.shape[1])
#
# title = "{modulation} Sample at {snr:.0f} dB SNR".format(modulation=modulation, snr=snr)
# fig = plot_IQ(iq=sample, title=title)
# plt.show()
## ======================================= PLOT ONE Signal =======================================================
## ======================================= MODEL CLASS =======================================================
class MyCNN(Model):
def __init__(self, input_samples: int, n_classes: int):
super().__init__(input_samples=input_samples, n_classes=n_classes)
# Batch x 1-channel x IQ x input_samples
# Modifying the first convolutional layer to not use a bias term is a
# modification made by Bryse Flowers due to the observation of vanishing
# gradients during training when ported to PyTorch (other authors used
# Keras).
self.conv1 = nn.Conv2d(
in_channels=1,
out_channels=256,
kernel_size=(1, 7),
padding=(0, 3),
bias=False,
)
self.a1 = nn.ReLU()
self.n1 = nn.BatchNorm2d(256)
self.conv2 = nn.Conv2d(
in_channels=256,
out_channels=80,
kernel_size=(2, 7),
padding=(0, 3),
bias=True,
)
self.a2 = nn.ReLU()
self.n2 = nn.BatchNorm2d(80)
# Batch x Features
self.dense1 = nn.Linear(80 * 1 * input_samples, 256)
self.a3 = nn.ReLU()
self.n3 = nn.BatchNorm1d(256)
self.dense2 = nn.Linear(256, n_classes)
def forward(self, x):
x = self.conv1(x)
x = self.a1(x)
x = self.n1(x)
x = self.conv2(x)
x = self.a2(x)
x = self.n2(x)
# Flatten the input layer down to 1-d by using Tensor operations
x = x.contiguous()
x = x.view(x.size()[0], -1)
x = self.dense1(x)
x = self.a3(x)
x = self.n3(x)
x = self.dense2(x)
return x
## ======================================= MODEL CLASS =======================================================
model = MyCNN(input_samples=128, n_classes=11)
print(model)
## ======================================= TRAINING LOOP CLASS =======================================================
class MyTrainingStrategy(object):
def __init__(self, lr: float = 10e-4, n_epochs: int = 3, gpu: bool = True):
self.lr = lr
self.n_epochs = n_epochs
self.gpu = gpu
def __repr__(self):
ret = self.__class__.__name__
ret += "(lr={}, n_epochs={}, gpu={})".format(self.lr, self.n_epochs, self.gpu)
return ret
def __call__(
self, model: nn.Module, training: Dataset, validation: Dataset, le: Encoder
):
criterion = nn.CrossEntropyLoss()
if self.gpu:
model.to("mps")
criterion.to("mps")
optimizer = Adam(model.parameters(), lr=self.lr)
train_data = DataLoader(
training.as_torch(le=le), shuffle=True, batch_size=512
)
val_data = DataLoader(
validation.as_torch(le=le), shuffle=True, batch_size=512
)
# Save two lists for plotting a convergence graph at the end
ret_train_loss = list()
ret_val_loss = list()
for epoch in range(self.n_epochs):
train_loss = self._train_one_epoch(
model=model, data=train_data, loss_fn=criterion, optimizer=optimizer
)
print("On Epoch {} the training loss was {}".format(epoch, train_loss))
ret_train_loss.append(train_loss)
val_loss = self._validate_once(
model=model, data=val_data, loss_fn=criterion
)
print("---- validation loss was {}".format(val_loss))
ret_val_loss.append(val_loss)
return ret_train_loss, ret_val_loss
def _train_one_epoch(
self, model: nn.Module, data: DataLoader, loss_fn: nn.CrossEntropyLoss, optimizer: Adam
) -> float:
total_loss = 0.0
# Switch the model mode so it remembers gradients, induces dropout, etc.
model.train()
for i, batch in enumerate(data):
x, y = batch
# Push data to GPU if necessary
if self.gpu:
x = Variable(x.to("mps"))
y = Variable(y.to("mps"))
else:
x = Variable(x)
y = Variable(y)
# Forward pass of prediction
outputs = model(x)
# Zero out the parameter gradients, because they are cumulative,
# compute loss, compute gradients (backward), update weights
loss = loss_fn(outputs, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
mean_loss = total_loss / (i + 1.0)
return mean_loss
def _validate_once(
self, model: nn.Module, data: DataLoader, loss_fn: nn.CrossEntropyLoss
) -> float:
total_loss = 0.0
# Switch the model back to test mode (so that batch norm/dropout doesn't
# take effect)
model.eval()
for i, batch in enumerate(data):
x, y = batch
if self.gpu:
x = x.to("mps")
y = y.to("mps")
outputs = model(x)
loss = loss_fn(outputs, y)
total_loss += loss.item()
mean_loss = total_loss / (i + 1.0)
return mean_loss
## ======================================= TRAINING LOOP CLASS =======================================================
## ======================================= TRAINING =======================================================
trainer = MyTrainingStrategy(gpu=gpu)
print(trainer)
train_loss, val_loss = trainer(model=model,
training=train,
validation=val,
le=le)
title = "Training Results of {model_name} on {dataset_name}".format(model_name="MyCNN", dataset_name="RML2016.10A")
fig = plot_convergence(train_loss=train_loss, val_loss=val_loss, title=title)
#plt.show()
## ======================================= TRAINING =======================================================
## ======================================= TESTING =======================================================
## ======================================= Overall Accuracy
acc = compute_accuracy(model=model, data=test, le=le)
print("Overall Testing Accuracy: {:.4f}".format(acc))
## ======================================= Accuracy vs SNR
acc_vs_snr, snr = compute_accuracy_on_cross_sections(model=model,
data=test,
le=le,
column="SNR")
title = "Accuracy vs SNR of {model_name} on {dataset_name}".format(model_name="MyCNN", dataset_name="RML2016.10A")
fig = plot_acc_vs_snr(acc_vs_snr=acc_vs_snr, snr=snr, title=title)
#plt.show()
## ======================================= Confusion Matrix
cmn = compute_confusion(model=model, data=test, le=le)
title = "Confusion Matrix of {model_name} on {dataset_name}".format(model_name="MyCNN", dataset_name="RML2016.10A")
fig = plot_confusion(cm=cmn, labels=le.labels, title=title)
#plt.show()
## ======================================= TESTING =======================================================
print("\n--- Elapsed Time: %s s ---" % (time.time() - start_time))
# WARNING: The scripts convert-caffe2-to-onnx, convert-onnx-to-caffe2 and torchrun are installed in '/Users/pg/.local/bin' which is not on PATH.
# Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
# WARNING: The script gpustat is installed in '/Users/pg/.local/bin' which is not on PATH.
# Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
#
# ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
# dask-expr 1.1.0 requires pandas>=2, but you have pandas 1.4.4 which is incompatible.