-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_modRec.py
More file actions
70 lines (54 loc) · 2.63 KB
/
Copy pathexample_modRec.py
File metadata and controls
70 lines (54 loc) · 2.63 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 29 19:38:54 2017
@author: mkulin
"""
# import the necessary packages
from networks.modnet import ModNet2
import numpy as np
import argparse
from datasets.load_datasets import load_modRec_rth, load_modRec_fft, load_modRec_iq
from evaluation.evaluate import calc_predictions, calc_acc_per_snr
"""
Main driver program for training and collecting results
"""
if __name__ == '__main__':
#Construct the argument parse
arg = argparse.ArgumentParser()
arg.add_argument("-l", "--load-model", type=int, default=-1, help="(optional) whether or not pre-trained model should be loaded", dest="load_model")
arg.add_argument("-w", "--weights", type=str, help="(optional) path to weights file or folder", dest="weights")
arg.add_argument("-d", "--load_data", type=int, help="1-iq, 2-rth, 3-fft", dest="load_data")
args = vars(arg.parse_args())
#D:\mkulin\Documents\Software\ipython\radioml_paper\output\
data=""
#Load data
print("[INFO] loading datasets...")
if args["load_data"]==1:
X_train, X_test, Y_train, Y_test, test_SNRs = load_modRec_iq()
data="iq"
elif args["load_data"]==2:
X_train, X_test, Y_train, Y_test, test_SNRs = load_modRec_rth()
data="rth"
elif args["load_data"]==3:
X_train, X_test, Y_train, Y_test, test_SNRs = load_modRec_fft()
data="fft"
#Constants
snrs=[-20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
classes=['8PSK', 'BPSK', 'CPFSK', 'GFSK', 'PAM4', 'QAM16', 'QAM64', 'QPSK']
path=args["weights"] + 'CNN2' + '_' + data
#Initialize the optimizer and model
print("[INFO] compiling model...")
cnn= ModNet2(0.0001, 0.5)
cnn.build(list(X_train.shape[1:]), classes, weightsPath=args["weights"] if args["load_model"] > 0 else None)
#Train the model if a pre-existing model is not loaded
if args["load_model"] < 0:
print("[INFO] training...")
cnn.train(X_train, Y_train, X_test, Y_test, nb_epoch=50, batch_size=1024, basepath=path)
# show the accuracy on the testing set
print("[INFO] evaluating...")
loss = cnn.model.evaluate(X_test, Y_test, batch_size=1024, verbose=1)
#print("[INFO] accuracy: {:.2f}%".format(accuracy * 100))
print("[INFO] loss: {:.2f}%".format(loss * 100))
#Collect results for performance evaluation
Y_truth, Y_pred=calc_predictions(cnn.model, classes, X_test, Y_test, path=path)
calc_acc_per_snr(cnn.model, snrs, classes, test_SNRs, X_test, Y_test, path=path)