forked from rpp0/lora-phy-fingerprinting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_train.py
executable file
·1616 lines (1329 loc) · 71.8 KB
/
tf_train.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python2
# tf_train.py
#
# Collection of ML algorithms to fingerprint radio devices using Tensorflow.
# A high level overview of the functionality provided by this code is given in
# the paper entitled "Physical-Layer Fingerprinting of LoRa devices using
# Supervised and Zero-Shot Learning", which was presented at WiSec 2017. A VM
# containing the training data and scripts required to reproduce the results
# from our paper will be published on Zenodo. Please contact one of the authors
# for more information.
#
# The code provides an abstraction layer on top of Tensorflow, consisting of
# "Models" and "Layers", in order to build a "Classifier" for raw radio signals.
# If you plan on using this framework for your research, I would recommend using
# the library "Keras" to build the models instead of "raw" Tensorflow. Keras was
# developed concurrently with this work, and provides a more concise and mature
# implementation for the same types of models that are used here.
#
# Author: Pieter Robyns
# Contact: [email protected]
import tensorflow as tf
import colorama
import random
import numpy as np
import scipy.io as sio
import os
import configparser
import argparse
import preprocessing
import visualization
import pickle
import json
import sklearn
import utilities
from colorama import Fore,Back,Style
from pymongo import MongoClient
from pymongo.errors import OperationFailure, AutoReconnect
from scipy import stats
from sklearn.manifold import TSNE
from sklearn.svm import SVC
from mapping import Mapping
from cache import GenericCache
from datetime import datetime
from random import randint
from tensorflow.contrib.tensorboard.plugins import projector
from sklearn.cluster import DBSCAN
from itertools import combinations
from collections import defaultdict
# ----------------------------------------------------
# Globals
# ----------------------------------------------------
colorama.init(autoreset=True)
EPSILON = 0.00000000001
defaults = {
'exclude_classes': '',
'epochs': -1,
'num_zs_test_samples': 40,
}
cp = configparser.RawConfigParser(defaults)
flags = tf.app.flags
FLAGS = flags.FLAGS
# ----------------------------------------------------
# Static functions
# ----------------------------------------------------
def load_conf(conf): # Configure the classifier using settings from conf file
cp.read(conf)
# Flags
flags.DEFINE_string('logdir', '/tmp/tensorboard', 'Tensorboard summaries directory')
flags.DEFINE_string('trainedmodelsdir', cp.get("DEFAULT", "trained_models_path"), 'Trained models directory')
flags.DEFINE_string('dataset', cp.get("DEFAULT", "dataset"), 'Dataset type (mongo or matlab)')
flags.DEFINE_string('classifier', cp.get("DEFAULT", "classifier"), 'Type of classifier to use')
flags.DEFINE_string('clustering', cp.get("DEFAULT", "clustering"), 'Type of clustering to use if doing open set classification')
flags.DEFINE_string('model_name', cp.get("DEFAULT", "model_name"), 'Name of the experiment / model. Used for saving it')
flags.DEFINE_integer('limit', cp.getint("DEFAULT", "limit"), 'Limit input tensor to n samples')
flags.DEFINE_integer('num_train_samples', cp.getint("DEFAULT", "num_train_samples"), 'Number of training samples')
flags.DEFINE_integer('num_test_samples', cp.getint("DEFAULT", "num_test_samples"), 'Number of test samples')
flags.DEFINE_integer('num_zs_test_samples', cp.getint("DEFAULT", "num_zs_test_samples"), 'Number of zero shot test samples')
flags.DEFINE_integer('batch_size', cp.getint("DEFAULT", "batch_size"), 'Training batch size')
flags.DEFINE_integer('print_step', cp.getint("DEFAULT", "print_step"), 'Print step')
flags.DEFINE_integer('epochs', cp.getint("DEFAULT", "epochs"), 'Epochs to train')
flags.DEFINE_integer('sampling_freq', cp.getint("DEFAULT", "sampling_freq"), 'Sampling frequency')
flags.DEFINE_string('mode', cp.get("DEFAULT", "mode"), 'Analysis mode (ifreq, iphase, or fft)')
flags.DEFINE_float('keep_prob', cp.getfloat("DEFAULT", "keep_prob"), 'Probability to keep neuron when using CNN')
flags.DEFINE_integer('retrain_batch', cp.getint("DEFAULT", "retrain_batch"), 'Number of times to retrain the same batch (speeds up, but also overfits)')
flags.DEFINE_string('exclude_classes', cp.get("DEFAULT", "exclude_classes"), 'Classes to exclude from training')
# Mode specific options
if cp.get("DEFAULT", "dataset") == 'matlab': # TODO: Bug in Tensorflow: once FLAGS.dataset is accessed it's no longer possible to define new strings
flags.DEFINE_string('matlabfile', cp.get("matlab", "matlabfile"), 'MATLAB LoRa database')
flags.DEFINE_integer('chirp_length', cp.getint("matlab", "chirp_length"), 'Length of a single chirp')
elif cp.get("DEFAULT", "dataset") == 'mongo':
flags.DEFINE_string ('ip', cp.get("mongo", "ip"), 'MongoDB server IP')
flags.DEFINE_integer('port', cp.get("mongo", "port"), 'MongoDB server port')
flags.DEFINE_string ('db', cp.get("mongo", "db"), 'MongoDB database name')
flags.DEFINE_string ('collection', cp.get("mongo", "collection"), 'MongoDB chirp collection name')
flags.DEFINE_string ('test_collection', cp.get("mongo", "test_collection"), 'MongoDB test chirp collection name')
flags.DEFINE_integer ('random_mode', RandomMode.s2e(cp.get("mongo", "random_mode")), 'Data randomization approach')
flags.DEFINE_string ('random_date', cp.get("mongo", "random_date"), 'Date for split date mode')
flags.DEFINE_string ('filter', cp.get("mongo", "filter"), 'Query filter for "find" queries')
elif cp.get("DEFAULT", "dataset") == 'random':
flags.DEFINE_integer('num_classes', cp.get("random", "num_classes"), 'Number of random classes')
flags.DEFINE_integer('num_samples', cp.get("random", "num_samples"), 'Number of random samples')
# Classifier specific options
if cp.get("DEFAULT", "classifier") == 'mlp':
flags.DEFINE_integer('num_hidden_layers', cp.getint("mlp", "num_hidden_layers"), 'Number of hidden layers')
flags.DEFINE_integer('num_hidden_neurons', cp.getint("mlp", "num_hidden_neurons"), 'Number of hidden neurons in a hidden layer')
elif cp.get("DEFAULT", "classifier") == 'cnn':
flags.DEFINE_integer('conv_kernel_width', cp.getint("cnn", "conv_kernel_width"), 'Convolution kernel width')
flags.DEFINE_integer('pooling_kernel_width', cp.getint("cnn", "pooling_kernel_width"), 'Max pooling kernel width')
elif cp.get("DEFAULT", "classifier") == 'mdn':
flags.DEFINE_integer('num_hidden_layers', cp.getint("mdn", "num_hidden_layers"), 'Number of hidden layers')
flags.DEFINE_integer('num_hidden_neurons', cp.getint("mdn", "num_hidden_neurons"), 'Number of hidden neurons in a hidden layer')
def print_conf(cp): # Print settings to terminal
for e in cp.defaults():
print("[+] " + Fore.YELLOW + Style.BRIGHT + e + ": " + str(cp.get("DEFAULT", e)))
def select_cols(matrix, c1, c2): # Select two columns from a numpy matrix
return matrix[:, [c1, c2]]
# ----------------------------------------------------
# Dataset classes
# ----------------------------------------------------
class TensorIO():
def __init__(self, x, y):
self.x = x # Input
self.y = y # Output
class Dataset(): # Dataset base class
def __init__(self):
self.num_training_samples = FLAGS.num_train_samples
self.num_test_samples = FLAGS.num_test_samples
# Based on the tag, get the LoRa ID
def _determine_id(self, tag):
if 'lora' in tag:
lora_id = int(tag[4:])
return lora_id
print("[!] Warning: unable to determine lora_id for entry " + str(tag))
return None
# Preprocess an input so that it can be learned by Tensorflow
def _data_to_tf_record(self, lora_id, chirp, debug=False):
features = []
#visualization.dbg_plot(preprocessing.iphase(chirp), title='Preprocessed chirp')
chirp = preprocessing.roll_to_base(chirp)
for m in FLAGS.mode.split(','):
if m == 'iphase':
features.append(preprocessing.iphase(chirp))
elif m == 'fft':
features.append(preprocessing.fft(chirp))
elif m == 'ifreq':
features.append(preprocessing.ifreq(chirp, FLAGS.sampling_freq))
elif m == 'iamp':
features.append(preprocessing.iamp(chirp))
elif m == 'raw':
features.append(preprocessing.normalize(chirp))
else:
print(Fore.RED + Style.BRIGHT + "[-] Analysis mode must be configured to be either 'fft', 'iphase', 'ifreq', or a comma separated combination.")
exit(1)
if debug:
if lora_id == 1:
visualization.dbg_plot(features[0], title='First feature vector of LoRa 1 chirp')
tf_record = {"lora_id": lora_id, "iq": features}
return tf_record
class GNURadioDataset(Dataset): # Convert pmt of IQ samples to numpy complex 64
def __init__(self, pmt, symbol_length):
self.pmt = pmt
self.symbol_length = symbol_length
def get(self):
data = []
frame = np.frombuffer(self.pmt, dtype=np.complex64)
symbols = [frame[i:i+self.symbol_length] for i in range(0, len(frame), self.symbol_length)]
for symbol in symbols:
tf_record = self._data_to_tf_record(None, symbol)
data.append(tf_record)
return data
class FakeSampleDataset(Dataset):
def __init__(self, host='localhost', port=27017, name="chirps"):
Dataset.__init__(self)
self.name = name
def get(self, projection={}, num_records=500):
return [{"lora_id": 1, "iq": [0+0j] * 74200}] * num_records
class UniformRandomDataset(Dataset): # Sanity check dataset
def __init__(self):
Dataset.__init__(self)
self.num_classes = FLAGS.num_classes
self.lora_ids = set()
for i in range(1, self.num_classes+1):
self.lora_ids.add(i)
def get(self, projection={}):
data = []
for i in range(0, FLAGS.num_samples):
record = {"lora_id": random.randint(1,self.num_classes), "iq": [random.random() for x in range(0, FLAGS.limit)]}
data.append(record)
return data
class MatlabDataset(Dataset):
def __init__(self):
Dataset.__init__(self)
self.path = FLAGS.matlabfile
self.data = []
self.lora_ids = set()
# Load the file and contents
mat_contents = sio.loadmat(self.path)
self.all_samples = mat_contents['all_samples']
# Determine number of classes
for entry in self.all_samples:
entry_name = os.path.basename(entry[0][0])
lora_id = self._determine_id(entry_name)
if lora_id is None:
continue
self.lora_ids.add(lora_id)
def _determine_id(self, filename):
for elem in filename.split('-'):
if 'lora' in elem:
return Dataset._determine_id(self, elem)
def get(self, projection={}, num_records=0): # TODO: projection
data = []
# Parse class data
for entry in self.all_samples:
entry_name = os.path.basename(entry[0][0])
entry_data = entry[1]
lora_id = self._determine_id(entry_name)
if lora_id is None:
continue
print("Parsing " + entry_name + " (class " + str(lora_id) + ", " + str(len(entry_data)) + " samples)")
for record in entry_data:
for i in range(0, 8):
chirp = record[i*FLAGS.chirp_length:(i+1)*FLAGS.chirp_length]
tf_record = self._data_to_tf_record(lora_id, chirp, debug=args.debug)
data.append(tf_record)
return data
class RandomMode:
RANDOMIZE_SYMBOLS = 0
RANDOMIZE_FRAMES = 1
SPLIT_DATE = 2
SPLIT_COLLECTION = 3
_STR_RANDOMIZE_SYMBOLS = 'randomize_symbols'
_STR_RANDOMIZE_FRAMES = 'randomize_frames'
_STR_SPLIT_DATE = 'split_date'
_STR_SPLIT_COLLECTION = 'split_collection'
@staticmethod
def e2s(enum):
if enum == RandomMode.RANDOMIZE_SYMBOLS:
return RandomMode._STR_RANDOMIZE_SYMBOLS
elif enum == RandomMode.RANDOMIZE_FRAMES:
return RandomMode._STR_RANDOMIZE_FRAMES
elif enum == RandomMode.SPLIT_DATE:
return RandomMode._STR_SPLIT_DATE
elif enum == RandomMode.SPLIT_COLLECTION:
return RandomMode._STR_SPLIT_COLLECTION
else:
print(Fore.YELLOW + Style.BRIGHT + "[!] Warning: unknown enum %d. Defaulting to 0." % enum)
return 0
@staticmethod
def s2e(string):
if string == RandomMode._STR_RANDOMIZE_SYMBOLS:
return RandomMode.RANDOMIZE_SYMBOLS
elif string == RandomMode._STR_RANDOMIZE_FRAMES:
return RandomMode.RANDOMIZE_FRAMES
elif string == RandomMode._STR_SPLIT_DATE:
return RandomMode.SPLIT_DATE
elif string == RandomMode._STR_SPLIT_COLLECTION:
return RandomMode.SPLIT_COLLECTION
else:
print(Fore.YELLOW + Style.BRIGHT + "[!] Warning: unknown randomization mode '%s'. Defaulting to randomize_symbols." % string)
return RandomMode.RANDOMIZE_SYMBOLS
class MongoDataset(Dataset):
def __init__(self):
Dataset.__init__(self)
self.ip = FLAGS.ip
self.port = FLAGS.port
self.client = MongoClient(self.ip, self.port)
self.db = self.client[FLAGS.db]
self.collection = self.db[FLAGS.collection]
self.collection_test = self.db[FLAGS.test_collection]
self.lora_ids = set()
self.random_mode = FLAGS.random_mode
self.filter = json.loads(FLAGS.filter)
self.num_samples = self.collection.find(self.filter).count()
print(Fore.MAGENTA + Style.BRIGHT + "[+] Filter: %s" % str(self.filter))
self.sort = '$natural' if args.natural else 'rand'
# Randomize mongo set
self.randomize()
# Randomize all symbols and divide into training and test set
if self.random_mode == RandomMode.RANDOMIZE_SYMBOLS:
self.cursor_train = self.collection.find(self.filter).sort(self.sort, 1).skip(0).limit(self.num_training_samples)
self.cursor_test = self.collection.find(self.filter).sort(self.sort, 1).skip(self.num_training_samples).limit(self.num_test_samples)
elif self.random_mode == RandomMode.RANDOMIZE_FRAMES:
self.collection.create_index("fn")
# Find out how many test frames we need
frames_for_test = int(self.num_test_samples / 36) # 36 = number of symbols in frame
# Find highest frame number
print("[+] Finding highest frame number")
last_fn = self.collection.find(self.filter).sort("fn", -1).limit(1)[0]['fn']
# Generate list of random frame numbers to be used as test set
test_fns = []
for i in range(0, frames_for_test):
test_fns.append(randint(0, last_fn))
# Assign the cursors
train_query = self.filter.copy()
train_query["fn"] = {"$nin": test_fns}
self.cursor_train = self.collection.find(train_query).sort(self.sort, 1).limit(self.num_training_samples)
test_query = self.filter.copy()
test_query["fn"] = {"$in": test_fns}
self.cursor_test = self.collection.find(test_query).sort(self.sort, 1).limit(self.num_test_samples)
elif self.random_mode == RandomMode.SPLIT_DATE:
self.collection.create_index("date")
print("[+] Splitting test set after date: %s" % FLAGS.random_date)
the_date = datetime.strptime(FLAGS.random_date,'%Y-%m-%dT%H:%M:%SZ')
train_query = self.filter.copy()
train_query["date"] = {"$lt": the_date}
self.cursor_train = self.collection.find(train_query).sort(self.sort, 1).limit(self.num_training_samples)
test_query = self.filter.copy()
test_query["date"] = {"$gte": the_date}
self.cursor_test = self.collection.find(test_query).sort(self.sort, 1).limit(self.num_test_samples)
elif self.random_mode == RandomMode.SPLIT_COLLECTION:
self.cursor_train = self.collection.find(self.filter).sort(self.sort, 1).limit(self.num_training_samples)
self.cursor_test = self.collection_test.find(self.filter).sort(self.sort, 1).limit(self.num_test_samples)
# Determine number of classes
print("[+] Determining number of classes")
for tag in self.cursor_train.distinct('tag'):
lora_id = self._determine_id(tag)
if lora_id is None:
continue
self.lora_ids.add(lora_id)
self.cursor_train.rewind()
# Create caches
self.cache_train = GenericCache(name="train")
self.cache_test = GenericCache(name="test")
def randomize(self):
if os.path.isfile('/tmp/randomized_mongo'):
print("[+] MongoDB dataset is already randomized")
return
self._randomize(self.collection, "")
if self.random_mode == RandomMode.SPLIT_COLLECTION: # If random mode is set to split collection, also randomize this collection
self._randomize(self.collection_test, "(test set)")
with open('/tmp/randomized_mongo', "w") as f:
f.write('')
def _randomize(self, collection, label=""):
print("[+] Randomizing MongoDB dataset %s" % label)
progress = 0
for entry in collection.find(self.filter):
collection.update({"_id": entry["_id"]}, {"$set": {"rand": random.random()}}, upsert=False, multi=False)
progress += 1
print("\r[+] Progress: %d / %d (estimation) " % (progress, self.num_samples)),
print("")
print("[+] Creating index")
collection.create_index("rand")
def get(self, train=True, projection={}, num_records=1000):
data = []
set_in_memory = False
if train:
cursor = self.cursor_train
cache = self.cache_train
num_records_total = self.num_training_samples
else:
cursor = self.cursor_test
cache = self.cache_test
num_records_total = self.num_test_samples
if len(cache) == num_records_total:
set_in_memory = True
# Set is already loaded in cache memory
if set_in_memory:
for i in range(0, num_records):
try:
tf_record = cache.next()
except StopIteration:
cache.rewind()
tf_record = cache.next()
data.append(tf_record)
else: # Go through each record in the MongoDB
for i in range(0, num_records):
try:
record = cursor.next()
except StopIteration:
cursor.rewind()
record = cursor.next()
except (OperationFailure, AutoReconnect) as e:
print("[!] Warning: Got other exception than StopIteration: "),
print(e)
cursor.rewind()
record = cursor.next()
lora_id = self._determine_id(record['tag'])
if lora_id is None:
continue
tf_record = cache.get(record['_id'])
if tf_record is None:
chirp = np.frombuffer(record['chirp'], dtype=np.complex64)
tf_record = self._data_to_tf_record(lora_id, chirp, debug=args.debug)
cache.store(record['_id'], tf_record)
data.append(tf_record)
return data
# The Instances class is responsible for providing:
# - Preprocessing of the raw chirp data into features
# - Separation of dataset into training and test sets
# - Random shuffling of training and test data
class Instances():
def __init__(self, limit=None, exclude_classes=[], name="", mapping=None):
self.name = name
self.num_excluded_samples = 0
self.limit = limit
self.exclude_classes = exclude_classes
# Select dataset type
if cp.get("DEFAULT", "dataset") == 'matlab':
self.dataset = MatlabDataset()
elif cp.get("DEFAULT", "dataset") == 'mongo':
self.dataset = MongoDataset()
elif cp.get("DEFAULT", "dataset") == 'random':
self.dataset = UniformRandomDataset()
else:
print(Fore.RED + Style.BRIGHT + "[-] Unknown dataset type '" + cp.get("DEFAULT", "dataset") + "'. Exiting")
exit(1)
# Make sure we don't underestimate available data
print("[+] Got " + Fore.GREEN + Style.BRIGHT + str(self.dataset.num_samples) + Style.RESET_ALL + " samples")
if self.dataset.num_test_samples + self.dataset.num_training_samples > self.dataset.num_samples:
print(Fore.RED + Style.BRIGHT + "[-] Sum of training and test samples exceeds available samples. Exiting")
exit(1)
# Get length of input samples (= number of features) and configure limit
print("[+] Getting number of features (1 record get from test set)")
self.num_features = self._get_num_features(self.dataset.get(train=False, num_records=1))
if self.limit == -1 or self.limit is None:
self.limit = self.num_features
print("[+] First sample contains %d features (limited to %d)" % (self.num_features, self.limit))
# Create mapping from LoRa ID to One Hot Vector if necessary
if mapping is None:
self.mapping = Mapping(self.dataset.lora_ids, exclude_classes=self.exclude_classes)
self.mapping.display()
else: # Update existing map with any new entries found
self.mapping = mapping
self.mapping.update(self.dataset.lora_ids, exclude_classes=self.exclude_classes)
self.mapping.display()
def next_batch(self, train, size):
temp = list(self.dataset.get(train=train, num_records=size))
if len(temp) > 0:
# Randomize (already done in Mongo, but not for other datasets)
random.shuffle(temp)
# Create instances
instances_x = []
instances_y = []
for i in range(0, size):
processed_record = self.process_record(temp[i])
if not (processed_record is None):
instances_x.append(processed_record.x[0:self.limit])
instances_y.append(processed_record.y)
instances_x = np.array(instances_x, dtype=np.float32)
instances_y = np.array(instances_y, dtype=np.float32)
# Done!
#if len(self.exclude_classes) > 0:
# print(Fore.GREEN + Style.BRIGHT + "[+] EXCLUDING %d samples" % self.num_excluded_samples)
else:
print("[-] No samples found in dataset. Exiting")
exit(1)
if len(instances_x) == 0:
raise Exception
return instances_x, instances_y
def _get_num_features(self, x):
return len(np.array(x[0]["iq"]).flatten())
def process_record(self, record):
# Do some preprocessing on the records here
if record["lora_id"] in self.exclude_classes:
self.num_excluded_samples += 1
return None
one_hot_vector = self.mapping.lora_id_to_oh(record["lora_id"])
features = np.array(record["iq"]).flatten()
return TensorIO(features, one_hot_vector)
# ----------------------------------------------------
# ML models
# Some of these models are based on the reference im-
# plementations provided by Aymeric Damien. See
# https://github.com/aymericdamien/TensorFlow-Examples
# for more information.
# ----------------------------------------------------
class MLModel(): # Base class for ML models
def __init__(self):
self.learning_rate = None
self.layers = []
self.output_layer = None
self.cost_function = None
self.correct_prediction = None
class MLPModel(MLModel):
def __init__(self, x, num_inputs, y, num_classes, hidden_layers=0, hidden_neurons=0, name='mlp'):
MLModel.__init__(self)
self.learning_rate = 0.0001 #0.001 works pretty good too
next_layer = x
next_layer_size = num_inputs
for i in range(0, hidden_layers):
self.layers.append(LinearReluLayer(next_layer, next_layer_size, hidden_neurons, name=name+'lin' + str(i)))
self.output_layer = self.layers[-1]
next_layer = self.output_layer.h
next_layer_size = hidden_neurons
self.layers.append(LinearLayer(next_layer, next_layer_size, num_classes, name=name+'clin', init_zero=True)) # Since it will be softmaxed later, init to zero. Seems to affect training speed and making the weights align on a diagonal faster
self.output_layer = self.layers[-1]
#self.cost_function = tf.reduce_mean(-tf.reduce_sum(y * tf.log(tf.nn.softmax(self.output_layer.h)+EPSILON), reduction_indices=[1])) # Doesn't deal with edge cases so we need to add EPSILON
self.cost_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.output_layer.h, labels=y))
#self.cost_function = tf.reduce_mean(tf.reduce_sum(tf.square(y - tf.nn.softmax(self.output_layer.h)), reduction_indices=[1]))
self.correct_prediction = tf.equal(tf.argmax(self.output_layer.h,1), tf.argmax(y,1))
class ConvNeuralNetModel(MLModel):
def __init__(self, x, num_inputs, y, num_classes, keep_prob=None, name='cnn'):
MLModel.__init__(self)
self.learning_rate = 0.001 # 0.0001
# Make image
x_shaped = tf.reshape(x, shape=[-1, 1, num_inputs, 1])
# Append convolution layers
self.layers.append(NNLayer(x_shaped, [1, FLAGS.conv_kernel_width, 1, 32], [32], name=name+'wc1'))
self.output_layer = self.layers[-1]
self.layers.append(NNLayer(self.output_layer.h, [1, FLAGS.conv_kernel_width, 32, 64], [64], name=name+'wc2'))
self.output_layer = self.layers[-1]
# Reshape conv2 output to fit fully connected layer input
relu_inputs = (num_inputs/pow(FLAGS.pooling_kernel_width, 2))*64 # 64 = output channels per sample from conv. 4 = k from polling (see paper notes). Power of two because max pooling twice
relu_outputs = num_inputs
out_shaped = tf.reshape(self.output_layer.h, [-1, relu_inputs])
# Append fully connected layer
self.layers.append(LinearReluDropLayer(out_shaped, relu_inputs, relu_outputs, keep_prob))
self.output_layer = self.layers[-1]
# Output, class prediction
self.layers.append(LinearLayer(self.output_layer.h, relu_outputs, num_classes, name=name+'lin'))
self.output_layer = self.layers[-1]
self.cost_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.output_layer.h, labels=y))
self.correct_prediction = tf.equal(tf.argmax(self.output_layer.h,1), tf.argmax(y,1))
class MDNModel(MLModel):
def __init__(self, x, num_inputs, y, num_classes, hidden_layers=0, hidden_neurons=0, name='mdn'):
MLModel.__init__(self)
self.num_classes = num_classes
self.learning_rate = 0.001
next_layer = x
next_layer_size = num_inputs
# Hidden layers
for i in range(0, hidden_layers):
self.layers.append(LinearLayer(next_layer, next_layer_size, hidden_neurons, name=name+'lin' + str(i)))
self.output_layer = self.layers[-1]
next_layer = self.output_layer.h
next_layer_size = hidden_neurons
# MDN layer
self.layers.append(MixtureLayer(next_layer, next_layer_size, num_classes, name=name+"mix"))
self.output_layer = self.layers[-1]
self.pi, self.mu, self.sigma = self._get_components(self.output_layer)
self.gauss = tf.contrib.distributions.Normal(mu=self.mu, sigma=self.sigma)
# Cost function
self.cost_function = self._get_cost_function(y)
# Evaluation
self.correct_prediction = tf.equal(tf.argmax(tf.mul(self.pi,self.gauss.mean()), 1), tf.argmax(y,1))
def _get_components(self, layer):
pi = tf.placeholder("float", [None, layer.num_components])
mu = tf.placeholder("float", [None, layer.num_components])
sigma = tf.placeholder("float", [None, layer.num_components])
pi, mu, sigma = tf.split(1, layer.num_components, layer.h)
pi = tf.nn.softmax(pi)
#assert_op = tf.Assert(tf.equal(tf.reduce_sum(pi), 1.), [pi])
#pi = tf.with_dependencies([assert_op], pi)
sigma = tf.exp(sigma)
return pi, mu, sigma
def _get_cost_function(self, y):
return tf.reduce_mean(-tf.log(tf.reduce_sum(tf.mul(self.pi, self.gauss.pdf(y)), 1, keep_dims=True)))
def _sample(self, n):
# Randomly sample x times according to pi distribution
mixture_indices = tf.reshape(tf.multinomial(tf.log(self.pi), n), [-1]) # Pi must be a log probability
# Sample all gaussian distributions x times
samples = tf.reshape(self.gauss.sample(n), [-1, self.num_classes])
# Select only the one according to pi
select_gaussians = tf.reduce_sum(tf.one_hot(mixture_indices, self.num_classes) * samples, 1)
return select_gaussians
def _mean(self):
# Get the indices of the most likely mixtures beloning to each x
mixture_indices = tf.argmax(self.pi, 1)
# Get the expected values of all gaussians
exp_values = self.gauss.mean()
# Get expected value of most likely mixture
select_exp = tf.reduce_sum(tf.one_hot(mixture_indices, self.num_classes) * exp_values, 1)
return select_exp
class ModelType:
MLP = 0
CONVNET = 1
MDN = 2
@staticmethod
def str2type(string):
if string == "mlp":
return ModelType.MLP
elif string == "cnn":
return ModelType.CONVNET
elif string == "mdn":
return ModelType.MDN
else:
print(Fore.RED + Style.BRIGHT + "[-] Model type "+ string +" does not exist.")
exit(1)
# ----------------------------------------------------
# ML classifiers
# ----------------------------------------------------
class SVM():
def __init__(self, name="svc"):
print("[+] SVM Classifier")
self.m = SVC()
self.name = name
def _get_lora_id_labels(self, instances, oh_labels):
result = []
for i in range(0, len(oh_labels)):
result.append(instances.mapping.oh_to_lora_id(oh_labels[i]))
return result
def _to_vendor(self, instances, lora_id_labels):
result = []
for i in range(0, len(lora_id_labels)):
result.append(instances.mapping.lora_id_to_vendor_id(lora_id_labels[i]))
return result
def train(self, instances, batch_size=2500):
print("[+] Getting %d training samples" % batch_size)
train_samples_x, train_samples_y = instances.next_batch(True, batch_size)
train_samples_y = self._get_lora_id_labels(instances, train_samples_y)
print("[+] Training model")
self.m.fit(train_samples_x, train_samples_y)
def save(self):
path = FLAGS.trainedmodelsdir + self.name + "/"
if not os.path.exists(path):
os.makedirs(path)
# Save model
pickle.dump(self.m, open(path + 'svc_model.p', "wb"))
@staticmethod
def load():
path = FLAGS.trainedmodelsdir + FLAGS.model_name + "/"
# Set up classifier based on config and stored data
net = SVM()
net.m = pickle.load(open(path + 'svc_model.p', "rb"))
return net
def bin_class_per_sample(self, instances, limit=200, adv_detect=True, vendor_only=False):
print("[+] Getting %d test samples" % limit)
test_samples_x, test_samples_y = instances.next_batch(False, limit)
test_samples_y = self._get_lora_id_labels(instances, test_samples_y)
print("[+] Evaluating model")
predicted_y = self.m.predict(test_samples_x)
if vendor_only:
metrics = utilities.get_eval_metrics_percent(self._to_vendor(instances, test_samples_y), self._to_vendor(instances, predicted_y))
else:
metrics = utilities.get_eval_metrics_percent(test_samples_y, predicted_y)
utilities.print_metrics(metrics)
return
def visualize_embeddings(self, instances, limit=200, train=True):
print("[!] Warning: visualize_embeddings not implemented for SVM")
return
class Classifier():
# Build the classifier
def __init__(self, num_inputs, num_classes, name, modeltype=ModelType.MLP):
self.num_inputs = num_inputs
self.num_classes = num_classes
self.name = name
self.step = 0
self.modeltype = modeltype
self.expected_values = None
self.std = None
self.distance_threshold = np.zeros(num_classes)
self.sess = None
self.instances_mapping = None
model_summaries = []
self.x = tf.placeholder("float", [None, self.num_inputs], name='inputs')
self.y = tf.placeholder("float", [None, self.num_classes], name='map-id-oh')
self.keep_prob = tf.placeholder(tf.float32, name='dropout')
if modeltype == ModelType.MLP:
self.m = MLPModel(self.x, self.num_inputs, self.y, self.num_classes, hidden_layers=FLAGS.num_hidden_layers, hidden_neurons=FLAGS.num_hidden_neurons, name="mlp") # Build MLP model
elif modeltype == ModelType.CONVNET:
self.m = ConvNeuralNetModel(self.x, self.num_inputs, self.y, self.num_classes, keep_prob=self.keep_prob, name="cnn") # Build Convolutional Neural Network model
elif modeltype == ModelType.MDN:
self.m = MDNModel(self.x, self.num_inputs, self.y, self.num_classes, hidden_layers=FLAGS.num_hidden_layers, hidden_neurons=FLAGS.num_hidden_neurons, name="mdn") # Build MDN model
else:
raise Exception("No model type specified")
# Define optimizer
self.optimizer = tf.train.AdamOptimizer(learning_rate=self.m.learning_rate).minimize(self.m.cost_function)
# Define accuracy model
self.accuracy = tf.reduce_mean(tf.cast(self.m.correct_prediction, tf.float32))
# Merge TensorBoard summaries for the model
model_summaries.append(tf.summary.scalar('accuracy', self.accuracy))
model_summaries.append(tf.summary.scalar('cost', self.m.cost_function))
self.merged_model_summaries = tf.summary.merge(model_summaries, collections=None, name=None)
# Define session object and summary writers
self.sess = tf.Session()
self.train_writer = tf.summary.FileWriter(FLAGS.logdir + '/train', graph=self.sess.graph)
self.test_writer = tf.summary.FileWriter(FLAGS.logdir + '/test')
def __del__(self):
if not (self.sess is None):
self.sess.close()
self.train_writer.close()
self.test_writer.close()
# Plot sample data to Tensorboard
def _plot_samples(self, samples_x, samples_y):
# Register plot summaries
plot_summaries = []
plots_to_show = 5
learned_weights_tensor = tf.identity(self.m.output_layer.W)
learned_weights = self.sess.run(learned_weights_tensor)
plot_summaries.append(visualization.plot_values(samples_x[0], self.instances_mapping, height=500, width=self.num_inputs, tag="weights", title="Weights", label=np.argmax(samples_y[0]), backdrop=learned_weights))
for i in range(1, 6):
label = np.argmax(samples_y[i])
guess = self.get_accuracy([samples_x[i]], [samples_y[i]])
plot_summaries.append(visualization.plot_values(samples_x[i], self.instances_mapping, height=500, width=self.num_inputs, tag="trd" + str(i) + "c" + str(label) + "g" + str(guess), title="Training data", label=label))
# Merge TensorBoard summaries for plots
merged_plot_summaries = tf.summary.merge(plot_summaries, collections=None, name=None)
summary_plot = self.sess.run(merged_plot_summaries)
self.train_writer.add_summary(summary_plot)
# Plot kernel data to Tensorboard
def _plot_kernels(self):
plot_summaries = []
# TODO go through layers and check .startswith("wc")
kernels_tensor = self.m.layers[0].W
kernels_shaped_tensor = tf.reshape(kernels_tensor, [-1, FLAGS.conv_kernel_width]) # Arrange kernels so that there is one per row
kernels_shaped = self.sess.run(kernels_shaped_tensor)
plot_summaries.append(visualization.plot_kernels(kernels_shaped, FLAGS.conv_kernel_width, height=4096, width=1024, tag="kernels", title="CNN Kernels"))
# Merge TensorBoard summaries for plots TODO dup code
merged_plot_summaries = tf.summary.merge(plot_summaries, collections=None, name=None)
summary_plot = self.sess.run(merged_plot_summaries)
self.train_writer.add_summary(summary_plot)
def get_output_weights(self, samples_x):
return self.sess.run(self.m.output_layer.h, feed_dict={self.x: samples_x, self.keep_prob: 1.0})
def _plot_output_weights_2d(self, samples_x, samples_y, predictions_y, instances, metrics): # Do not use new samples from instances
plot_summaries = []
# Get the output weight values for all classes
output_weights = self.get_output_weights(samples_x)
# OLD: Get first two weights to visualize
# weights = select_cols(output_weights, 0, 1)
# Reduce dimensionality of weights to 2
tsne = TSNE(n_components=2, init='pca', n_iter=5000)
weights = tsne.fit_transform(output_weights)
#xlabel = "Weight #" + str(0) + " values"
#ylabel = "Weight #" + str(1) + " values"
xlabel = "t-SNE dimension 1"
ylabel = "t-SNE dimension 2"
plot_summaries.append(visualization.plot_weights(weights, samples_y, predictions_y, self.expected_values, self.distance_threshold, instances.mapping, tag=self.name+"-w", title="2D projection of output feature weights", xlabel=xlabel, ylabel=ylabel, metrics=metrics))
# Merge TensorBoard summaries for plots TODO dup code
merged_plot_summaries = tf.summary.merge(plot_summaries, collections=None, name=None)
summary_plot = self.sess.run(merged_plot_summaries)
self.train_writer.add_summary(summary_plot)
def train(self, instances, batch_size=2500):
# Let's go
print("[+] Training")
self.sess.run(tf.global_variables_initializer())
# Start learning weights
try:
while True:
train_batch_x, train_batch_y = instances.next_batch(True, batch_size)
test_batch_x, test_batch_y = instances.next_batch(False, batch_size)
# Execute training step(s) on batch
#print(self.sess.run(self.m.tmp, feed_dict={self.x: train_batch_x, self.y: train_batch_y, self.keep_prob: FLAGS.keep_prob})) # To test something inside model with the same data
for i in range(0, FLAGS.retrain_batch):
self.sess.run(self.optimizer, feed_dict={self.x: train_batch_x, self.y: train_batch_y, self.keep_prob: FLAGS.keep_prob})
# Print progress
if self.step % FLAGS.print_step == 0:
# Print stats about step
summary_train, c_train, a_train = self.sess.run([self.merged_model_summaries, self.m.cost_function, self.accuracy], feed_dict={self.x: train_batch_x, self.y: train_batch_y, self.keep_prob: 1.0})
summary_test = self.sess.run(self.merged_model_summaries, feed_dict={self.x: test_batch_x, self.y: test_batch_y, self.keep_prob: 1.0})
# Add summaries
self.train_writer.add_summary(summary_train, self.step)
self.test_writer.add_summary(summary_test, self.step)
# Print info about training
print("Epoch {:d}: cost={:.6f}, tr_acc={:.6f}, W0_0={:.6f}".format(self.step, c_train, a_train, self.sess.run(self.m.output_layer.W)[0][0]))
# Next step
self.step += 1
if self.step == FLAGS.epochs:
raise KeyboardInterrupt
except KeyboardInterrupt:
pass
# Save the mapping used during training from LoRa ID to Map ID
self.instances_mapping = instances.mapping
# Mixture components
self.expected_values, self.std = self.calculate_mixture_components(instances)
# Show results
print(Fore.GREEN + Style.BRIGHT + "[+] Done training!")
if self.modeltype == ModelType.MLP:
print(Fore.GREEN + Style.BRIGHT + "[+] Plotting training samples")
self._plot_samples(train_batch_x, train_batch_y)
else:
print(Fore.GREEN + Style.BRIGHT + "[+] Plotting model kernels")
self._plot_kernels()
# Evaluation
print("[+] Training set accuracy")
print(self.get_accuracy(train_batch_x, train_batch_y))
print("[+] Test set accuracy")
print(self.get_accuracy(test_batch_x, test_batch_y))
# Assert that nothing unexpected happened during the whole process
GenericCache.assert_disjunction(instances.dataset.cache_train, instances.dataset.cache_test)
print(Fore.GREEN + Style.BRIGHT + "[+] Training assertions passed")
def determine_ideal_threshold(self, map_id, samples_x, expected_values):
output_weights = self.sess.run(self.m.output_layer.h, feed_dict={self.x: samples_x, self.keep_prob: 1.0})
threshold = 0.0
for output_weight in output_weights:
#threshold = max(np.linalg.norm(output_weight - expected_values), threshold)
#threshold = (np.linalg.norm(output_weight - expected_values) + threshold) / 2.0
threshold += np.linalg.norm(output_weight - expected_values)
threshold /= len(output_weights)
return threshold
def calculate_mixture_components(self, instances, num_samples_to_use=10000):
print("[+] Determining mixture model components")
train_batch_x, train_batch_y = instances.next_batch(True, num_samples_to_use)
expected_values = np.ndarray(shape=(self.num_classes,self.num_classes), dtype=np.float32)
std = np.ndarray(shape=(self.num_classes,self.num_classes), dtype=np.float32)
for lora_id in instances.mapping.keys():
map_id = instances.mapping.lora_to_map_id(lora_id)
samples_x = []
# Collect samples belonging to class map_id
for i in range(0, len(train_batch_x)):
if np.argmax(train_batch_y[i]) == map_id:
samples_x.append(train_batch_x[i])
if len(samples_x) == 0:
print(train_batch_y)
print("[-] Error: no samples in training set for LoRa %d. Dumped y training set" % lora_id)
exit()
# Determine mean and std deviation for all features
nn_output_weights = self.sess.run(tf.identity(self.m.output_layer.h), feed_dict={self.x: samples_x, self.keep_prob: 1.0})
expected_values[map_id] = np.mean(nn_output_weights, axis=0)
std[map_id] = np.std(nn_output_weights, axis=0)
# Determine ideal threshold based on expected values
# this threshold is used when doing nearest neighbor classification
# as the outlier detection (not discussed in paper)
if args.distance_threshold == 'auto':
print("\r[+] Determining expected value distance threshold for LoRa %d " % lora_id),
self.distance_threshold[map_id] = self.determine_ideal_threshold(map_id, samples_x, expected_values[map_id])
else:
self.distance_threshold[map_id] = args.distance_threshold
# Clean up
del samples_x
print("")
return expected_values, std
# Calculates the distance between a point and a centroid
def calculate_expected_values_distance(self, samples_x):
if self.expected_values is None or self.distance_threshold is None:
raise Exception("Tried to evaluate expected value MSE without training values")
output_weights = self.sess.run(self.m.output_layer.h, feed_dict={self.x: samples_x, self.keep_prob: 1.0})
distances = []
for output_weight_v in output_weights:
distances.append(np.linalg.norm(output_weight_v - self.expected_values, axis=1)) # Distance from E(X) for each class to X
return distances
def get_accuracy(self, samples_x, samples_y):
return self.sess.run(self.accuracy, feed_dict={self.x: samples_x, self.y: samples_y, self.keep_prob: 1.0})