-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_node.py
More file actions
1175 lines (1013 loc) · 54.7 KB
/
new_node.py
File metadata and controls
1175 lines (1013 loc) · 54.7 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
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
import torch
import torch.nn as nn
from cnn import CNN
''' # comment this line if want to use big_MLP architecture instead of small_MLP
from mlp import MLP
'''
from mlp_small import MLP
# '''
import time
from sklearn.cluster import KMeans
from sklearn.cluster import MiniBatchKMeans
import numpy as np
import smote_variants as sv
import random
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
from getOptions import options
from torchsummary import summary
# this isn't used currently : kmeans function
def kmeans_output(all_images_flat, device, num_clusters=2):
cluster_ids_x, cluster_centers = kmeans(X=all_images_flat, num_clusters=num_clusters, distance='euclidean', device=device)
return cluster_ids_x, cluster_centers
# class of Node
class myNode:
# constructor of node, having parent id, own id, bool to tell to train or not, level of node, append itself in parent node
def __init__(self, parentId, nodeId, device, isTrain, level, parentNode=None):
self.parentId = parentId
self.nodeId = nodeId
self.device = device
self.isTrain = isTrain
self.level = level
self.children = []
if parentNode:
parentNode.children.append(self)
def __str__(self):
return str(self.nodeId)
# set inputs of node as train/test dictionaries, validation input(for test it will be 0 tensor), num of classes of data in this node,
# ginivalue of parent, if node is leaf or noe, left and right child node ids respectively
def setInput(self, trainInputDict, valInputDict, numClasses, giniValue, isLeaf, leafClass, lchildId, rchildId):
self.trainInputDict = trainInputDict
self.valInputDict = valInputDict
imgSize = trainInputDict["data"][0].shape[2]
inChannels = trainInputDict["data"][0].shape[0]
if options.verbose > 0:
print("Setting Input for nodeId: "+str(self.nodeId)+"...")
if options.verbose > 1:
print("imgTensorShape : ", trainInputDict["data"].shape)
outChannels = options.cnnOut
outChannels = options.cnnOut + 8*(self.level)
kernel = 5
# CNN architecture is selected accoding to the input channels
self.cnnModel = CNN(img_size=imgSize, in_channels=inChannels, out_channels=outChannels, num_class=numClasses, kernel=kernel, use_bn=False)
numFeatures = self.cnnModel.features
# mlp model is similarly constructed
self.mlpModel = MLP(numFeatures, use_bn=False)
## self.dtModel = DecisionTreeClassifier(random_state=0, criterion='gini', splitter='best', max_depth=1, max_features=None, min_samples_split=2, min_samples_leaf=1)
self.dtModel = DecisionTreeRegressor(random_state=0, criterion='mse', splitter='best', max_depth=2, max_features=None, min_samples_split=2, min_samples_leaf=1)
## self.rfModel = RandomForestClassifier(random_state=0, criterion='gini', n_estimators=100, max_depth=1, max_features='auto', min_samples_split=2, min_samples_leaf=1)
self.rfModel = RandomForestRegressor(random_state=0, criterion='mse', n_estimators=5, max_depth=2, max_features='auto', min_samples_split=2, min_samples_leaf=1)
self.numClasses = numClasses
self.giniValue = giniValue
self.giniGain = 0.0
self.splitAcc = 0.0
self.isLeaf = isLeaf
self.leafClass = leafClass
self.lchildId = lchildId
self.rchildId = rchildId
self.numData = trainInputDict["data"].shape[0]
self.nodeAcc = [0,0,0.0]
self.classLabels = { l:cnt for l,cnt in zip(*np.unique(trainInputDict["label"].numpy(),return_counts=True))}
if options.verbose > 1:
print("nodeId:", self.nodeId, ", parentId:", self.parentId, ", level:", self.level, ", lchildId:", self.lchildId, ", rchildId:", self.rchildId, ", isLeaf:", self.isLeaf, ", leafClass:", self.leafClass, ", numClasses:", self.numClasses, ", numData:", self.numData)
# function to train CNN
def trainCNN(self, labelMap, reverseLabelMap):
loss_fn = nn.CrossEntropyLoss() # used cross entropy loss function
## loss_fn_mse = nn.MSELoss()
optimizer = torch.optim.Adam(self.cnnModel.parameters(),lr=options.cnnLR) # adam optimiser used
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, options.cnnSchEpochs, options.cnnSchFactor) # used step LR
## scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=1, factor=0.2, mode='max',verbose=True)
self.cnnModel.to(self.device)
trainLabels = self.trainInputDict["label"]
trainInputs = self.trainInputDict["data"]
# set number of batches and get batch size accordingly
numBatches = options.cnnBatches
batchSize = int((len(trainInputs))/(numBatches-1))
if(batchSize<3):
batchSize=int(len(trainInputs))
numBatches=1
# set number of epochs
numEpochs = options.cnnEpochs - 10*(self.level)
# numEpochs = max(30, options.cnnEpochs - 10*(self.level))
# numEpochs = options.cnnEpochs
st_btch = 0
batch_sep = [] # batch_sep stores the pairs of (st_btch,end_btch), and is randomly shuffled to reduce chances of overfitting
for i in range(numBatches):
end_btch = min(st_btch + batchSize, len(trainInputs))
if end_btch == st_btch:
numBatches-=1
break
else:
batch_sep.append([st_btch, end_btch])
st_btch = end_btch
train_loss = 0
for epoch in range(numEpochs):
self.cnnModel.train() # sets CNN to training mode
total = 0
correct = 0
train_loss = 0
random.shuffle(batch_sep)
for batch in range(numBatches):
st_btch, end_btch = batch_sep[batch]
optimizer.zero_grad() # reset
_, _, est_labels, feat_same = self.cnnModel(trainInputs[st_btch:end_btch].to(self.device)) # CNN model is trained
batch_loss_label = loss_fn(est_labels, trainLabels[st_btch:end_btch].to(self.device))
## batch_loss_featr = loss_fn_mse(feat_same, trainInputs[st_btch:end_btch])
## batch_loss = batch_loss_featr + batch_loss_label
batch_loss = batch_loss_label
batch_loss.backward()
optimizer.step()
## if options.verbose > 1:
## print(batch_loss_featr.item(), batch_loss_label.item())
## if batch == 0:
## train_loss_tensor = batch_loss
## else:
## train_loss_tensor += batch_loss
train_loss += batch_loss.item()
_, predicted = est_labels.max(1)
total += end_btch - st_btch
correct += predicted.eq(trainLabels[st_btch:end_btch].to(self.device)).sum().item()
scheduler.step()
# scheduler.step(train_loss_tensor)
# train_loss = train_loss_tensor.item()
''' ## If using validation, just comment this line by prepending with a #, else remove any hashes present in the beginning
if (epoch%options.cnnSchEpochs == options.cnnSchEpochs-1) or (epoch%options.cnnSchEpochs == 0):
self.cnnModel.eval()
_, _, est_labels, _ = self.cnnModel(self.valInputDict["data"].to(self.device))
val_loss = loss_fn(est_labels, self.valInputDict["label"].to(self.device))
_, predicted = est_labels.max(1)
valTotalSize = float(len(self.valInputDict["data"]))
valCorrect = predicted.eq(self.valInputDict["label"].to(self.device)).sum().item()
scheduler.step(valCorrect)
if options.verbose > 1:
print(epoch, 'Train Loss: %.3f | Train Acc: %.3f | Val Loss: %.3f | Val Accuracy: %.3f'% (train_loss, 100.*correct/total, val_loss.item(), 100.*float(valCorrect)/valTotalSize))
# '''
train_loss /= numBatches
if total != 0:
if options.verbose > 1:
print(epoch, 'Train Loss: %.3f | Train Acc: %.3f '% (train_loss, 100.*correct/total))
epoch = numEpochs
torch.save({
'epoch':epoch,
'model_state_dict':self.cnnModel.state_dict(),
'optimizer_state_dict':optimizer.state_dict(),
'train_loss':train_loss,
'labelMap':labelMap,
'reverseLabelMap':reverseLabelMap,
}, options.ckptDir+'/node_cnn_'+str(self.nodeId)+'.pth')
# function to train MLP
def trainMLP(self, trainInputs, trainTargets, weightVector):
# used BCE loss function and adam optimiser with step learning rate
loss_fn = nn.BCELoss(reduction='none')
optimizer = torch.optim.Adam(self.mlpModel.parameters(),lr=options.mlpLR)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, options.mlpSchEpochs, options.mlpSchFactor)
weightVector = weightVector.to(self.device)
self.mlpModel.to(self.device)
# set number of batches and get batch size accordingly
numBatches = options.mlpBatches
batchSize = int((len(trainInputs))/(numBatches-1))
if(batchSize<3):
batchSize=int(len(trainInputs))
numBatches=1
st_btch = 0
batch_sep = []
# construct baches start and end number and store them in batch_sep
for i in range(numBatches):
end_btch = min(st_btch + batchSize, len(trainInputs))
if end_btch == st_btch:
numBatches-=1
break
else:
batch_sep.append([st_btch, end_btch])
st_btch = end_btch
# set number of epochs
numEpochs = options.mlpEpochs
## numEpochs = max(30, options.mlpEpochs-10*(self.level))
self.mlpModel.train()
train_loss=0
# for each epoch, train MLP in batches
for epoch in range(numEpochs):
train_loss = 0
correct = 0
total = 0
random.shuffle(batch_sep)
for batch in range(numBatches):
st_btch, end_btch = batch_sep[batch]
optimizer.zero_grad()
est_labels = self.mlpModel(trainInputs[st_btch:end_btch].to(self.device))
est_labels = est_labels.view(-1)
## batch_loss = loss_fn(est_labels, trainTargets[st_btch:end_btch]) #if crossentropy
# weighted loss
batch_loss = weightVector[st_btch:end_btch] * loss_fn(est_labels, trainTargets[st_btch:end_btch].float().to(self.device)) #if bce
batch_loss = batch_loss.mean()
batch_loss.backward()
optimizer.step()
train_loss += batch_loss.item()
## _, predicted = est_labels.max(1) # if cross entropy
predicted = est_labels.detach() #if bce
predicted += 0.5
predicted = predicted.long()
## total += trainTargets.size(0)
total += end_btch - st_btch
correct += predicted.eq(trainTargets[st_btch:end_btch].to(self.device)).sum().item()
scheduler.step()
if total != 0:
if options.verbose > 1:
print(epoch, 'Loss: %.3f | Acc: %.3f'% (train_loss, 100.*correct/total))
epoch = numEpochs
torch.save({
'epoch':epoch,
'model_state_dict':self.mlpModel.state_dict(),
'optimizer_state_dict':optimizer.state_dict(),
'train_loss':train_loss,
}, options.ckptDir+'/node_mlp_'+str(self.nodeId)+'.pth')
# function to train Decision Tree (DT)
def trainDT(self, trainInputs, trainTargets, weightVector):
self.dtModel = self.dtModel.fit(trainInputs.numpy(), trainTargets.numpy(), weightVector.numpy())
if options.verbose > 1:
print(' n_leaves:', self.dtModel.get_n_leaves(), ' depth:', self.dtModel.get_depth())
torch.save({
'n_leaves': self.dtModel.get_n_leaves(),
'depth': self.dtModel.get_depth(),
'params': self.dtModel.get_params(),
'model_state_dict': self.dtModel,
}, options.ckptDir+'/node_dt_'+str(self.nodeId)+'.pth')
# function to train Random Forest (RF)
def trainRF(self, trainInputs, trainTargets, weightVector):
self.rfModel = self.rfModel.fit(trainInputs.numpy(), trainTargets.numpy(), weightVector.numpy())
if options.verbose > 1:
print('params:', self.rfModel.get_params())
torch.save({
'params': self.rfModel.get_params(),
'model_state_dict': self.rfModel,
}, options.ckptDir+'/node_rf_'+str(self.nodeId)+'.pth')
# this is required if input data doesn't have same data points in each class while training the node
# (but this is not the case in our current implementation when using {options.caseNum as 1 or 2})
def balanceData(self):
shape=self.trainInputDict["data"].shape
if options.verbose > 1:
print("trainInputDict[data].shape : ", shape)
copy = self.trainInputDict["data"]
copy = copy.reshape(shape[0], -1)
if options.verbose > 1:
print("copy.shape : ", copy.shape)
npDict = copy.numpy()
copyLabel = self.trainInputDict["label"]
if options.verbose > 1:
print("copyLabel.shape : ", copyLabel.shape)
## copyLabel = copyLabel.view(-1)
npLabel = copyLabel.numpy()
## X_resampled, y_resampled = kmeans_smote.fit_sample(npDict, npLabel)
## if options.verbose > 1:
## print(sv.get_all_oversamplers_multiclass())
oversampler= sv.MulticlassOversampling(sv.SMOTE(n_jobs=6))
X_resampled, y_resampled = oversampler.sample(npDict, npLabel)
if options.verbose > 1:
[print('Class {} has {} instances after oversampling'.format(label, count)) for label, count in zip(*np.unique(y_resampled, return_counts=True))]
newData = torch.from_numpy(X_resampled.reshape(len(X_resampled), shape[1], shape[2], shape[3]))
newLabel = torch.from_numpy(y_resampled)
newData = newData.float()
return newData, newLabel
# makes labels map and reverse label map, which is required because CNN output labels should be 0 indexed
# but the labels of input data can be 4,2,5,6, which will be mapped to 0,1,2,3 for CNN training and stored in labelMap,
# reverse of above will be stores in reverseLabelMap, which will be 0,1,2,3 -> 4,2,5,6
# hence accordingly input labels are changed to required labels
def make_labels_list(self):
labelsList = []
labelMap = {}
reverseLabelMap = {}
for i in self.trainInputDict["label"]:
if (i.item() not in labelsList):
labelsList.append(i.item())
if len(labelsList) == self.numClasses:
break
for i, val in enumerate(sorted(labelsList)):
labelMap[val] = i
reverseLabelMap[i] = val
for i, val in enumerate(self.trainInputDict["label"]):
self.trainInputDict["label"][i] = labelMap[val.item()]
for i, val in enumerate(self.valInputDict["label"]):
self.valInputDict["label"][i] = labelMap[val.item()]
''' # if we have to balance the data, then comment the current line by prepending a hash(#)
newData, newLabel = self.balanceData();
self.trainInputDict["data"] = newData
self.trainInputDict["label"] = newLabel
# '''
return labelMap, reverseLabelMap
# loads CNN model from stored dictionary
def loadCNNModel(self):
ckpt = torch.load(options.ckptDir+'/node_cnn_'+str(self.nodeId)+'.pth')
self.cnnModel.load_state_dict(ckpt['model_state_dict'])
self.cnnModel.eval()
self.cnnModel.to(self.device)
return ckpt['reverseLabelMap'], ckpt['labelMap']
# loads MLP model from stored dictionary
def loadMLPModel(self):
ckpt = torch.load(options.ckptDir+'/node_mlp_'+str(self.nodeId)+'.pth')
self.mlpModel.load_state_dict(ckpt['model_state_dict'])
self.mlpModel.eval()
self.mlpModel.to(self.device)
# loads DT model from stored dictionary
def loadDTModel(self):
ckpt = torch.load(options.ckptDir+'/node_dt_'+str(self.nodeId)+'.pth')
self.dtModel = ckpt['model_state_dict']
# loads RF model from stored dictionary
def loadRFModel(self):
ckpt = torch.load(options.ckptDir+'/node_rf_'+str(self.nodeId)+'.pth')
self.rfModel = ckpt['model_state_dict']
# This constructs the dictionaries where data points of each class will go on 2-means clustering output
def separateLabels(self, cluster_ids):
# first we count for each class how many data points belong to left cluster and right cluster respectively and
# we store them in leftCnt and rightCnt respectively
leftCnt = {}
rightCnt = {}
for i in range(len(self.trainInputDict["data"])):
label = self.trainInputDict["label"][i].item() # 0-indexed according to self.trainInputDict["label"]
if cluster_ids[i] == 0:
if label in leftCnt:
leftCnt[label]+=1
else:
leftCnt[label] = 1
else:
if label in rightCnt:
rightCnt[label]+=1
else:
rightCnt[label] = 1
expected_dict = {}
lCnt=0
rCnt=0
maxLeftRatio=0.0
maxRightRatio=0.0
lIndx=-1
rIndx=-1
# traversing leftCnt dict. and seeing its intersections with rightCnt dict.
for label, count in leftCnt.items():
lRatio=1.0
if label in rightCnt:
lRatio = float(count)/float(count+rightCnt[label])
if lRatio>maxLeftRatio:
maxLeftRatio = lRatio
lIndx = label
## rRatio = float(rightCnt[label])/float(count+rightCnt[label])
rRatio = 1.0 - lRatio
if rRatio>maxRightRatio:
maxRightRatio = rRatio
rIndx = label
if count >= rightCnt[label]:
expected_dict[label] = 0
lCnt+=1
else:
expected_dict[label] = 1
rCnt+=1
else:
if lRatio>maxLeftRatio:
maxLeftRatio = lRatio
lIndx = label
expected_dict[label] = 0
lCnt+=1
# traversing rightCnt dict. and seeing its difference with leftCnt dict.
for label, count in rightCnt.items():
if not (label in expected_dict):
rRatio=1.0
if rRatio>maxRightRatio:
maxRightRatio = rRatio
rIndx = label
expected_dict[label] = 1
rCnt+=1
# if all samples went to the right side, forcefully send the class having maximum lRatio (maxLeftRatio) to the left side
if lCnt==0:
if options.verbose > 0:
print("L_CNT=0 detected")
expected_dict[lIndx] = 0
# if all samples went to the left side, forcefully send the class having maximum rRatio (maxRightRatio) to the right side
if rCnt==0:
if options.verbose > 0:
print("R_CNT=0 detected")
expected_dict[rIndx] = 1
if options.verbose > 1:
print("printing expected split from k means")
print(expected_dict)
# created and return a sorted list of tuples to be used later while making final_dict
leftSortedListOfTuples = sorted(leftCnt.items(), reverse=True, key=lambda x: x[1])
rightSortedListOfTuples = sorted(rightCnt.items(), reverse=True, key=lambda x: x[1])
return leftSortedListOfTuples, rightSortedListOfTuples, expected_dict
# creates the final dictionary to be used
def makeFinalDict(self, leftSortedListOfTuples, rightSortedListOfTuples, expected_dict):
final_dict = {}
# use the expected dict. as it is
if options.caseNum == 1:
final_dict = expected_dict
# create a final dict. having half classes(along with their data points) in both children sides when the parent node contains even no. of diff. classes
# while, when the parent node contains an odd no. of classes, then the right child gets one extra class data points' as compared to the left one
elif options.caseNum == 2:
fullDict = {}
for ind, element in enumerate(leftSortedListOfTuples):
fullDict[element[0]] = element[1]
if ind >= self.numClasses/2:
final_dict[element[0]] = 1
else:
final_dict[element[0]] = 0
for ind, element in enumerate(rightSortedListOfTuples):
if element[0] not in fullDict:
fullDict[element[0]] = -1*element[1]
if ind >= self.numClasses/2:
final_dict[element[0]] = 0
else:
final_dict[element[0]] = 1
fullSortedListOfTuples = sorted(fullDict.items(), reverse=True, key=lambda x: (x[1],x[0]))
# This below code should be used only if we want both the children nodes to contain exactly same no. of classes
# even in case of odd no. of classes present in the parent node. Here, we distribute half-half elements of a single particular class to both children
## if (self.numClasses%2 == 1):
## final_dict[fullSortedListOfTuples[int(self.numClasses/2)][0]] = -1
if options.verbose > 1:
print("Printing final_dict items...")
print(final_dict)
# save the final dict. in a file for using it later
torch.save({
'splittingDict':final_dict,
}, options.ckptDir+'/node_split_'+str(self.nodeId)+'.pth')
return final_dict
# returns the saved final dict.
def getSavedFinalSplit(self):
ckpt = torch.load(options.ckptDir+'/node_split_'+str(self.nodeId)+'.pth')
return ckpt['splittingDict']
# saves the pred and act labels in a file along with thier indices
def setFinalPredictions(self, predicted):
ckpt = torch.load(options.ckptDir+'/testPred.pth')
testPredDict = ckpt['testPredDict']
testPredDict['actual'] = testPredDict['actual'].to(self.device)
testPredDict['pred'] = testPredDict['pred'].to(self.device)
testPredDict['index'] = testPredDict['index'].to(self.device)
testPredDict['actual'] = torch.cat((testPredDict['actual'],self.trainInputDict["label"].to(self.device)),0)
testPredDict['pred'] = torch.cat((testPredDict['pred'],predicted),0)
testPredDict['index'] = torch.cat((testPredDict['index'],self.trainInputDict["index"].to(self.device)),0)
torch.save({
'testPredDict':testPredDict,
}, options.ckptDir+'/testPred.pth')
# this function compares the output we got with the actual labels
def checkTestPreds(self, reverseLabelMap, est_labels, nodeProb, oneHotTensors):
_, predicted = est_labels.max(1) # gets the predicted label corresponding to the max probability value (per sample)
predicted = predicted.to(self.device)
# updates predicted labels to now store the corresponding labels according to reverseLabelMap
for i, val in enumerate(predicted):
predicted[i] = reverseLabelMap[val.item()]
# calculates the no. of correct predicted labels
correct = predicted.eq(self.trainInputDict["label"].to(self.device)).sum().item()
total = len(est_labels)
# nodeAcc (format) :: (correct, total, 100.*correct/total)
self.nodeAcc[2] = 0.0
if total != 0:
self.nodeAcc[2] = round(100.*correct/total,3)
self.nodeAcc[1] = total
self.nodeAcc[0] = correct
if options.verbose > 1:
print('Node %d Acc: %.3f'% ( self.nodeId, self.nodeAcc[2]))
# updates the nodeAcc in the file, to be used later while printing tree
NodeDict = torch.load(options.ckptDir+'/node_'+str(self.nodeId)+'.pth')['nodeDict']
NodeDict['nodeAcc'] = self.nodeAcc[:]
torch.save({
'nodeDict':NodeDict,
}, options.ckptDir+'/node_'+str(self.nodeId)+'.pth')
# when not using probabilistic method
if not options.probabilistic:
# Level Dict. stores the level-wise accuracies and for calculating this, it uses all the leaf accuracies that come across by this time too
LevelDict = torch.load(options.ckptDir+'/level.pth')['levelDict']
LevelDict['levelAcc'][self.level][0] += correct
LevelDict['levelAcc'][self.level][1] += total
if(self.isLeaf):
LevelDict['leafAcc'][0] += correct
LevelDict['leafAcc'][1] += total
torch.save({
'levelDict':LevelDict,
}, options.ckptDir+'/level.pth')
# if a leaf node
if self.isLeaf:
if self.level != 0:
# calculate geometric mean of nodeProb seen so far from root to this leaf node
nodeProb = nodeProb.pow(1/self.level)
if options.probabilistic:
# if probabilistic method is used, update the oneHotTensors corresponding to the predicted labels with their respective nodeProb
oneHotTensors[torch.arange(len(oneHotTensors)), predicted.long()] += nodeProb
else:
self.setFinalPredictions(predicted)
# returns the indices of class labels going to left and right side
def doLabelCounting(self, modelPrediction):
lclasses = [0]*10 # 0-indexed according to self.trainInputDict["label"]
rclasses = [0]*10
for i, val in enumerate(modelPrediction):
if val<=0.5:
lclasses[self.trainInputDict["label"][i].item()]+=1
else:
rclasses[self.trainInputDict["label"][i].item()]+=1
return lclasses, rclasses
# used for calculating split accuracy and could also be used to remove redundant classes from a node
def countBalanceAndThreshold(self, modelPrediction, labelMap):
final_dict = self.getSavedFinalSplit() # loading saved final_dict from file
lclasses, rclasses = self.doLabelCounting(modelPrediction)
# totalLeftImages = 0.0
# totalRightImages = 0.0
maxLeftClasses = 0.0
maxRightClasses = 0.0
testCorrectResults = 0.0 # testCorrectResults stores the no. of test samples going to the correct side as was declared while training
totalL = 0
totalR = 0
for i, val in enumerate(lclasses):
# totalLeftImages += val
if not self.isTrain and (i in labelMap):
totalL+=val
if(final_dict[labelMap[i]] == 0 or final_dict[labelMap[i]] == -1):
testCorrectResults += val
maxLeftClasses = max(maxLeftClasses, val)
for i, val in enumerate(rclasses):
# totalRightImages += val
if not self.isTrain and (i in labelMap):
totalR+=val
if (final_dict[labelMap[i]] == 1 or final_dict[labelMap[i]] == -1):
testCorrectResults += val
maxRightClasses = max(maxRightClasses, val)
if not self.isTrain:
# total = float(len(self.trainInputDict["label"])) # all test samples at the current node
total = float(totalL+totalR) # all those samples at the current node whose classes participated during training at this numbered/indexed train_node only
splitAcc = 0
if total != 0:
splitAcc = float(100.*testCorrectResults/total)
if options.verbose > 1:
print('Split Acc: %.3f'% (splitAcc))
NodeDict = torch.load(options.ckptDir+'/node_'+str(self.nodeId)+'.pth')['nodeDict']
NodeDict['splitAcc'] = splitAcc
# saving the split acc to be used later while printing tree
torch.save({
'nodeDict':NodeDict,
}, options.ckptDir+'/node_'+str(self.nodeId)+'.pth')
leftClassesToBeRemoved = []
rightClassesToBeRemoved = []
''' comment this line, in order to use (NOT USED CURRENTLY) this code which removes those classes from a child node whose percantage in a node is less than the threshold value
threshold = 15.0
for i, val in enumerate(lclasses):
if float(100*val)/maxLeftClasses < threshold:
leftClassesToBeRemoved.append(i)
for i, val in enumerate(rclasses):
if float(100*val)/maxRightClasses < threshold:
rightClassesToBeRemoved.append(i)
#'''
return maxLeftClasses, maxRightClasses, testCorrectResults, leftClassesToBeRemoved, rightClassesToBeRemoved
# calculates the Gini_Gain for the current node (in relation to its children nodes) as well as gini values for both its children nodes
def getPredictionAnalysis(self, totalLeftImages, totalRightImages, lclasses, rclasses):
giniLeftRatio = 0.0
giniRightRatio = 0.0
lcheck = 0.0
rcheck = 0.0
if options.verbose > 1:
print("# of Left images: ", totalLeftImages)
print("# of Right images: ", totalRightImages)
noOfLeftClasses = 0
noOfRightClasses = 0
for i in lclasses:
if i != 0:
noOfLeftClasses += 1
pi=0
if totalLeftImages != 0:
pi = float(i)/totalLeftImages
lcheck += pi
giniLeftRatio += pi*(1-pi)
for i in rclasses:
if i != 0:
noOfRightClasses += 1
pi=0
if totalRightImages != 0:
pi = float(i)/totalRightImages
rcheck += pi
giniRightRatio += pi*(1-pi)
if options.verbose > 1:
print("giniRightRatio: ", giniRightRatio)
print("giniLeftRatio: ", giniLeftRatio)
leftChildrenRatio = 0
if totalRightImages != 0:
leftChildrenRatio = totalLeftImages/totalRightImages
impurity = leftChildrenRatio*float(giniLeftRatio) + (1-leftChildrenRatio)*float(giniRightRatio)
giniGain = self.giniValue - impurity
if options.verbose > 1:
print("impurity: ", impurity)
print("giniGain: ", giniGain)
print("lclasses: ", lclasses)
print("rclasses: ", rclasses)
print("noOfLeftClasses: ", noOfLeftClasses)
print("noOfRightClasses: ", noOfRightClasses)
return giniLeftRatio, giniRightRatio, noOfLeftClasses, noOfRightClasses, giniGain
# separates the data to be send to left and right children nodes of the current node
def classifyLabels(self, modelPrediction, reverseLabelMap, labelMap, lChildProb, rChildProb, oneHotTensors):
totalLeftImages = 0.0
totalRightImages = 0.0
leftChildIndexList = [] # leftChildIndexList/rightChildIndexList store the indices of the data samples to the left/right side resp.
rightChildIndexList = []
leftValIndexList = [] # leftValIndexList/rightValIndexList store the indices of the valid. data samples to the left/right side resp.
rightValIndexList = []
lclasses = [0]*10 # 0-indexed in Train according to self.trainInputDict["label"]
rclasses = [0]*10 # lclasses/rclasses store the no. of input samples per class going to their resp. sides
maxLeft=0
maxRight=0
maxLeftClassIndex=-1 # maxLeftClassIndex/maxRightClassIndex stores the index corresponding to class that has the max. samples (among all) that are going to the left/right side resp.
maxRightClassIndex=-1
splitClassIndx = -1 # index of the splitted class (only matters if this option is used above)
splitClassCnt = -1
leftSplitClassCnt = 0
rightSplitClassCnt = 0
lmaxSplitClassCnt = 0
rmaxSplitClassCnt = 0
final_dict = {}
lTrainDict = {}
rTrainDict = {}
lValDict={}
rValDict={}
## resDict = dict(zip(self.trainInputDict["label"].tolist(), modelPrediction.tolist()))
if options.caseNum==1 or options.caseNum==2:
maxLeftClasses, maxRightClasses, testCorrectResults, leftClassesToBeRemoved, rightClassesToBeRemoved = self.countBalanceAndThreshold(modelPrediction, labelMap)
final_dict = self.getSavedFinalSplit()
# used only when we divide a parent having odd numbered classes into 2 EXACTLY same sized children nodes as was explained earlier
for k,v in final_dict.items():
if v == -1:
splitClassIndx = k
splitClassCnt = (self.trainInputDict["label"].tolist()).count(splitClassIndx)
lmaxSplitClassCnt = int((splitClassCnt+1)/2)
rmaxSplitClassCnt = int(splitClassCnt/2)
break
for i, val in enumerate(modelPrediction):
label = self.trainInputDict["label"][i].item()
if self.isTrain:
self.trainInputDict["label"][i] = reverseLabelMap[self.trainInputDict["label"][i].item()]
if val<=0.5:
if (self.isTrain) and (options.caseNum==1 or options.caseNum==2):
#LEFT SIDE
### if not (label in leftClassesToBeRemoved):
if (final_dict[label] == 0) or ((final_dict[label] == -1) and leftSplitClassCnt<lmaxSplitClassCnt):
lclasses[label]+=1
leftChildIndexList.append(i)
if (final_dict[label] == -1):
leftSplitClassCnt += 1
if lclasses[label] > maxLeft:
maxLeft = lclasses[label]
maxLeftClassIndex = reverseLabelMap[label]
#RIGHT SIDE
else:
rclasses[label]+=1
rightChildIndexList.append(i)
if (final_dict[label] == -1):
rightSplitClassCnt += 1
if rclasses[label] > maxRight:
maxRight = rclasses[label]
maxRightClassIndex = reverseLabelMap[label]
#LEFT SIDE
elif (options.caseNum==3) or (not self.isTrain):
lclasses[label]+=1
leftChildIndexList.append(i)
if self.isTrain:
if lclasses[label] > maxLeft:
maxLeft = lclasses[label]
maxLeftClassIndex = reverseLabelMap[label]
else:
#RIGHT SIDE
if (self.isTrain) and (options.caseNum==1 or options.caseNum==2):
### if not (label in rightClassesToBeRemoved):
if (final_dict[label] == 1) or ((final_dict[label] == -1) and leftSplitClassCnt>=lmaxSplitClassCnt):
rclasses[label]+=1
rightChildIndexList.append(i)
if (final_dict[label] == -1):
rightSplitClassCnt += 1
if rclasses[label] > maxRight:
maxRight = rclasses[label]
maxRightClassIndex = reverseLabelMap[label]
#LEFT SIDE
else:
lclasses[label]+=1
leftChildIndexList.append(i)
if (final_dict[label] == -1):
leftSplitClassCnt += 1
if lclasses[label] > maxLeft:
maxLeft = lclasses[label]
maxLeftClassIndex = reverseLabelMap[label]
#RIGHT SIDE
elif (options.caseNum==3) or (not self.isTrain):
rclasses[label]+=1
rightChildIndexList.append(i)
if self.isTrain:
if rclasses[label] > maxRight:
maxRight = rclasses[label]
maxRightClassIndex = reverseLabelMap[label]
totalLeftImages += float(len(leftChildIndexList))
totalRightImages += float(len(rightChildIndexList))
if (not self.isTrain) and (options.probabilistic): # sending all the parent data to both children nodes
lTrainDict = {"data":self.trainInputDict["data"], "label":self.trainInputDict["label"], "index":self.trainInputDict["index"]}
rTrainDict = {"data":self.trainInputDict["data"], "label":self.trainInputDict["label"], "index":self.trainInputDict["index"]}
else:
lTrainDict = {"data":self.trainInputDict["data"][leftChildIndexList], "label":self.trainInputDict["label"][leftChildIndexList], "index":self.trainInputDict["index"][leftChildIndexList]}
rTrainDict = {"data":self.trainInputDict["data"][rightChildIndexList], "label":self.trainInputDict["label"][rightChildIndexList], "index":self.trainInputDict["index"][rightChildIndexList]}
if options.verbose > 1:
print("lTrainDict[data].shape: ", lTrainDict["data"].shape, " lTrainDict[label].shape: ", lTrainDict["label"].shape, " lTrainDict[index].shape: ", lTrainDict["index"].shape)
print("rTrainDict[data].shape: ", rTrainDict["data"].shape, " rTrainDict[label].shape: ", rTrainDict["label"].shape, " rTrainDict[index].shape: ", rTrainDict["index"].shape)
# Handling Validation Data
if self.isTrain:
total = totalLeftImages + totalRightImages
if total != 0:
left_count = (totalLeftImages * len(self.valInputDict["label"])) // (total)
for i, val in enumerate(self.valInputDict["label"]):
label = self.valInputDict["label"][i].item()
if options.caseNum==1 or options.caseNum==2:
if (final_dict[label] == 0):
leftValIndexList.append(i)
elif (final_dict[label] == 1):
rightValIndexList.append(i)
#TODO: Needs to modified appropriately for this case
elif options.caseNum==3:
if i < left_count:
leftValIndexList.append(i)
else:
rightValIndexList.append(i)
self.valInputDict["label"][i] = reverseLabelMap[val.item()]
lValDict = {"data":self.valInputDict["data"][leftValIndexList], "label":self.valInputDict["label"][leftValIndexList], "index":self.valInputDict["index"][leftValIndexList]}
rValDict = {"data":self.valInputDict["data"][rightValIndexList], "label":self.valInputDict["label"][rightValIndexList], "index":self.valInputDict["index"][rightValIndexList]}
if options.verbose > 1:
print("lValDict[data].shape: ", lValDict["data"].shape, " lValDict[label].shape: ", lValDict["label"].shape, " lValDict[index].shape: ", lValDict["index"].shape)
print("rValDict[data].shape: ", rValDict["data"].shape, " rValDict[label].shape: ", rValDict["label"].shape, " rValDict[index].shape: ", rValDict["index"].shape)
giniLeftRatio, giniRightRatio, noOfLeftClasses, noOfRightClasses, giniGain = self.getPredictionAnalysis(totalLeftImages, totalRightImages, lclasses, rclasses)
leftDataNum = int(totalLeftImages)
rightDataNum = int(totalRightImages)
if not (len(leftChildIndexList) == 0):
maxLeft = float(float(maxLeft)/float(len(leftChildIndexList)))
if not (len(rightChildIndexList) == 0):
maxRight = float(float(maxRight)/float(len(rightChildIndexList)))
# stores all the important/relevant things needed for specifying whether the left and right children nodes are leaf or node and also, their no. of classes and gini gain of the current node resp.
handleLeafDict = {"lvl":self.level+1,"noOfLeftClasses":noOfLeftClasses, "noOfRightClasses":noOfRightClasses, "maxLeft":maxLeft, "maxRight":maxRight, "leftDataNum":leftDataNum, "rightDataNum":rightDataNum,"maxLeftClassIndex":maxLeftClassIndex,"maxRightClassIndex":maxRightClassIndex, "giniGain":giniGain}
if options.verbose > 0:
print("RETURNING FROM WORK...")
if self.isTrain and not self.isLeaf:
return lTrainDict, lValDict, rTrainDict, rValDict, giniLeftRatio, giniRightRatio, handleLeafDict
elif not self.isTrain and not self.isLeaf:
return lTrainDict, rTrainDict, giniLeftRatio, giniRightRatio, noOfLeftClasses, noOfRightClasses, lChildProb, rChildProb
elif self.isTrain and self.isLeaf:
return
else:
return
# this is function that runs for testing
def workTest(self, nodeProb, oneHotTensors, resume=False):
# if we have a single class in this node which will be given by leafClass atrribute
# we don't need to fetched trained CNN, as we just have to compare with the class present here
if (not self.isTrain) and (not (self.leafClass == -1)):
x=torch.Tensor(1,1).long()
x[0] = 1
est_labels = torch.cat(len(self.trainInputDict["label"].to(self.device))*[x])
reverseLabelMap = {}
reverseLabelMap[0] = self.leafClass
self.checkTestPreds(reverseLabelMap, est_labels, nodeProb, oneHotTensors)
# used when resume Training is called
elif (self.isTrain) and (not (self.leafClass == -1)):
return
# else we load the CNN model and get the predicted output on the trained CNN
else:
reverseLabelMap, labelMap = self.loadCNNModel()
# used when resume Training is called
if resume:
# correctly maps all the inputs as should be done while <make_labels_list()> call in Training
for i, val in enumerate(self.trainInputDict["label"]):
self.trainInputDict["label"][i] = labelMap[val.item()]
for i, val in enumerate(self.valInputDict["label"]):
self.valInputDict["label"][i] = labelMap[val.item()]
trainInputs = self.trainInputDict["data"]
numBatches = 5
batchSize = int((len(trainInputs))/(numBatches-1))
if(batchSize<3):
batchSize=int(len(trainInputs))
numBatches=1
st_btch = 0
batch_sep = []
for i in range(numBatches):
end_btch = min(st_btch + batchSize, len(trainInputs))
if end_btch == st_btch:
numBatches-=1
break
else:
batch_sep.append([st_btch, end_btch])
st_btch = end_btch
imgNextFlat=[]
estLabels=[]
# getting outputs batch-wise, else GPU goes out of memory :p
for batch in range(numBatches):
st_btch, end_btch = batch_sep[batch]
_, image_next_flat, est_labels, _ = self.cnnModel(self.trainInputDict["data"][st_btch:end_btch].to(self.device))
if batch == 0:
imgNextFlat = image_next_flat.detach().cpu()
estLabels = est_labels.detach().cpu()
else:
imgNextFlat = torch.cat((imgNextFlat,image_next_flat.detach().cpu()))
estLabels = torch.cat((estLabels,est_labels.detach().cpu()))
# if current node is a test node
if not self.isTrain:
self.checkTestPreds(reverseLabelMap, estLabels.to(self.device), nodeProb, oneHotTensors)
# if node is leaf node, we don't need to go further
if self.isLeaf:
return
# else load the Model_to_Train
if options.optionNum == 1:
self.loadMLPModel()
elif options.optionNum == 2:
self.loadDTModel()
elif options.optionNum == 3:
self.loadRFModel()
# pass the input through Model_to_Train and get the predictions from it
estLabels = []
for batch in range(numBatches):
st_btch, end_btch = batch_sep[batch]
if options.optionNum == 1:
est_labels = self.mlpModel(imgNextFlat[st_btch:end_btch].to(self.device))
est_labels = est_labels.detach().cpu() # since already a tensor
elif options.optionNum == 2:
est_labels = self.dtModel.predict(imgNextFlat[st_btch:end_btch].to("cpu"))
est_labels = torch.from_numpy(est_labels)
elif options.optionNum == 3:
est_labels = self.rfModel.predict(imgNextFlat[st_btch:end_btch].to("cpu"))
est_labels = torch.from_numpy(est_labels)
if batch == 0:
estLabels = est_labels
else:
estLabels = torch.cat((estLabels, est_labels))
estLabels = estLabels.view(-1)
modelPrediction = estLabels.detach()
modelPrediction = modelPrediction.double()
rChildProb = modelPrediction.clone()
lChildProb = 1.0 - rChildProb
# if probabilistic is used, update lChildProb, rChildProb for its corresponding left and right children nodes
if options.probabilistic:
lChildProb = lChildProb*nodeProb
rChildProb = rChildProb*nodeProb
modelPrediction += 0.5
modelPrediction = modelPrediction.long()
return self.classifyLabels(modelPrediction, reverseLabelMap, labelMap, lChildProb, rChildProb, oneHotTensors)
# this fucntion does 2-means clustering and give the ouput to train Model_to_Train on
def getTrainPredictionsNotLeaf(self):
self.loadCNNModel()
# first we get the output from CNN on the input data
trainInputs = self.trainInputDict["data"]
numBatches = 5
batchSize = int((len(trainInputs))/(numBatches-1))
if(batchSize<3):
batchSize=int(len(trainInputs))
numBatches=1
st_btch = 0
batch_sep = []
for i in range(numBatches):
end_btch = min(st_btch + batchSize, len(trainInputs))
if end_btch == st_btch:
numBatches-=1
break
else:
batch_sep.append([st_btch, end_btch])