-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiment_methods.py
More file actions
1421 lines (1083 loc) · 51.6 KB
/
experiment_methods.py
File metadata and controls
1421 lines (1083 loc) · 51.6 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
from sklearn.svm import SVC
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import normalize
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from sklearn.manifold import TSNE
import h5py
import numpy as np
import matplotlib as plt
from cvjson.cvj import CVJ
import os
import copy
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
from collections import Counter, defaultdict
import plotting
import torch
import torch.nn as nn
import torchvision
from models import hdf_Dataset
import models
import cv2
def get_data(hdf5_path, list_of_classes_to_keep=None, semantics=False, negatives=False, list_of_ids_to_ignore=None):
"""
This method takes a path to a single hdf file and will loop through it and build the data for the file.
Parameters
----------
hdf5_path : string
The path to a single hdf file to be split in to the many classes it has stored in it.
list_of_classes: list
This is a list of classes found by the run_classifer method. These are K top classes that
have the most annotations.
semantics: bool
This variable is was says to return the semantic vectors produced by detectron is a list_of_lists of the deep features produced by detectron.
A semantic vector is also known as the softmax vector produced by detectron in this case. It is just the probabilities of each class
in vector from. This is useful since one can look at the vectors enough time to point out a pattern of probabilities that actually belong
to a specific class.
negatives: bool
This variable, if set to True, will turn every annotation this method finds to a negative class with the
ID=5000, and the class="negative"
Returns
-------
X: numpy array
Array of X values
Y: numpy array
Array of Y values
category_names: numpy array
Array of category names associated with the class IDs
"""
image_name_y_value = []
X = []
Y = []
file_ = h5py.File(hdf5_path)
for image, values in list(file_["frames"].items()):
boxes = values["bounding_boxes"]
features = values["descriptors"]
features = np.asarray(features)
scores = values["scores"]
scores = np.asarray(scores)
try:
features = features.reshape(scores.shape[0], -1)
scores = scores.reshape(scores.shape[0], scores.shape[1])
except ValueError:
print("A value error occurred in get_data() when reshaping the features. Continuing.")
if negatives == False:
categories = values["categories"]
categories = np.asarray(categories).astype(int)
else:
categories = np.asarray([5000 for i in range(features.shape[0])]) # 5000 is the id I am using for the negative class
list_of_classes_to_keep = [5000]
count_ = 0
for cat in categories:
image_name_y_value.append((image, cat, boxes[count_]))
count_ += 1
##### START#### This iterates through and makes the X and Y data
if len(X) == 0:
if list_of_classes_to_keep != None and semantics == True : # if semantics is true then you will get the semantic vector. AKA the scores that were predicted
for index, cat in enumerate(categories):
if cat in list_of_classes_to_keep:
X.append(np.copy(scores[index]))
Y.append(np.copy(categories[index]))
elif list_of_classes_to_keep == None and semantics == True:
X = np.copy(scores)
Y = np.copy(categories)
elif list_of_classes_to_keep != None and semantics == False:
for index, cat in enumerate(categories):
if cat in list_of_classes_to_keep:
X.append(np.copy(features[index]))
Y.append(np.copy(categories[index]))
else :
X = np.copy(features)
Y = np.copy(categories)
if len(X) != 0:
X = np.asarray(X)
Y = np.asarray(Y)
else:
if list_of_classes_to_keep != None and semantics == True: # if semantics is true then you will get the semantic vector. AKA the scores that were predicted
for index, cat in enumerate(categories):
if cat in list_of_classes_to_keep:
try:
Y = np.vstack((Y, cat))
X = np.vstack((X, scores[index]))
except ValueError:
try:
Y.shape[1]
except IndexError:
Y = Y[-1]
X = X[-1]
elif list_of_classes_to_keep == None and semantics == True:
for index, cat in enumerate(categories):
try:
Y = np.vstack((Y, cat))
X = np.vstack((X, scores[index]))
except ValueError:
try:
Y.shape[1]
except IndexError:
Y = Y[-1]
X = X[-1]
elif list_of_classes_to_keep != None and semantics == False:
for index, cat in enumerate(categories):
if cat in list_of_classes_to_keep:
try:
Y = np.vstack((Y, cat))
X = np.vstack((X, features[index]))
except ValueError:
try:
Y.shape[1]
except IndexError:
Y = Y[-1]
X = X[-1]
else:
for index, cat in enumerate(categories):
try:
Y = np.vstack((Y, cat))
X = np.vstack((X, features[index]))
except ValueError:
try:
Y.shape[1]
except IndexError:
Y = Y[-1]
X = X[-1]
#### END ####
# print(Y.shape)
# print(X.shape)
if list_of_ids_to_ignore != None:
for id in list_of_ids_to_ignore:
ind = np.where(Y[:] != id)[0]
Y = Y[ind, :]
X = X[ind, :]
counts = np.unique(Y, return_counts=True)
# print(counts[0])
# print(Y.shape)
# print(X.shape)
# input()
Y = Y.ravel() # Flattening to a 1-d array
category_names = np.asarray(file_["class_names"])
file_.close()
df = pd.DataFrame(image_name_y_value)
df.columns = ["image_name", "y_values", 'bbox']
df = df.set_index("image_name")
return X, Y, category_names, counts, df
#Deprecated don't use. This was for some experiments that was ill conceived.
def run_classifier(classifier, train_path=None, test_path=None, training_negatives=None, test_negatives=None, train_cvj=None,\
test_cvj=None, n_best_class=5, test_path_is_negative=False, semantics=False, norm=None):
"""
This method runs the classifier with a number of different settings. This ranges from having files fed in as only being negatives,
only negatives in the training set, only negatives in the testing set, and no negatives (which since you will most likely be using a multclass svm),
is what you want.
"""
if train_cvj != None and n_best_class != 0:
list_of_class_to_keep = get_top_count_ids(train_cvj, n_best_class)
print(list_of_class_to_keep)
input()
train_positives_count = 0
train_negatives_count = 0
if train_path != None:
X_train, Y_train, categories, train_counts = get_data(train_path, list_of_classes_to_keep=list_of_class_to_keep, semantics=semantics)
train_positives_count = X_train.shape[0]
if training_negatives != None:
for file_path in training_negatives:
try:
X_neg, Y_neg, _, _ = get_data(file_path, list_of_classes_to_keep=list_of_class_to_keep, negatives=True, semantics=semantics)
X_train = np.concatenate((X_train, X_neg))
Y_train = np.concatenate((Y_train, Y_neg))
except ValueError:
continue
train_negatives_count = X_train.shape[0] - train_positives_count
# print(X_train)
if norm:
X_train = normalize(X_train, norm=norm)
classifier.fit(X_train, Y_train)
print("\n\nTraining Positive Count = {}, Training Negative Count {}".format(train_positives_count, train_negatives_count)) # honestly don't listen to the negatives count.
print("\nTraining class counts\n")
for index, count in np.ndenumerate(train_counts[1]):
if id != 5000:
print("class = {}, count = {}".format(train_cvj.get_class_id_2_name(train_counts[0][index]), count))
else:
print("class = {}, count = {}".format("negative", count))
test_positives_count = 0
test_negatives_count = 0
if test_path != None and test_path_is_negative != True:
X_test, Y_test, _, test_counts = get_data(test_path, list_of_classes_to_keep=list_of_class_to_keep, semantics=semantics)
test_positives_count = X_test.shape[0]
if test_negatives != None:
for file_path in test_negatives:
try:
X_neg, Y_neg, _, test_counts = get_data(file_path, list_of_classes_to_keep=list_of_class_to_keep, negatives=True, semantics=semantics)
X_test = np.concatenate((X_test, X_neg))
Y_test = np.concatenate((Y_test, Y_neg))
except ValueError:
continue
test_negatives_count = test_positives_count - X_test.shape[0]
elif test_path_is_negative:
X_test, Y_test, _, test_counts = get_data(test_path, list_of_classes_to_keep=list_of_class_to_keep, negatives=True, semantics=semantics)
test_positives_count = 0
test_negatives_count = X_test.shape[0]
else:
print("No test files given, returning classifier.")
return classifier
print("\nTest class counts\n")
for index, count in np.ndenumerate(test_counts[1]):
if id != 5000:
print("class = {}, count = {}".format(train_cvj.get_class_id_2_name(test_counts[0][index]), count))
else:
print("class = {}, count = {}".format("negative", count))
if norm: # FYI normalizing the deep features will produce terrible results. ALSO normalizing args are "l2", "l1", and "max"
X_test = normalize(X_test, norm=norm)
preds = classifier.predict(X_test)
print("Test Positive Count = {}, Test Negative Count {}".format(test_positives_count, test_negatives_count))
if train_cvj != None:
list_of_names = [train_cvj.get_class_id_2_name(class_id) for class_id in list_of_class_to_keep]
else:
list_of_class_to_keep = [id for id in range(len(categories) + 1) if id != 5] # this is MARDCT specific, remove this is using a different dataset
list_of_names = categories
report = classification_report( Y_test, preds, target_names=list_of_names, labels=list_of_class_to_keep)
print(report)
print("\n\naccuracy = {}".format(accuracy_score(Y_test, preds, normalize=True)))
if test_negatives or training_negatives or test_path_is_negative:
list_of_names.append("negative")
list_of_class_to_keep.append(5000) # ID I use for negatives
cm = confusion_matrix(Y_test, preds, labels=list_of_class_to_keep) # create a confustion matrix to be visualized by cm_heatmap
print_cm(cm, list_of_names) # a Pretty print for the confusion matrix
return classifier, np.asarray(preds), cm, report, list_of_names
def get_top_count_ids(cvj_obj, top_count=5):
"""
This method will look in the CVJ object and return the classes with the most
annotations
Honestly, this method could be integrated in to the CVJSON library
Parameters
----------
cvj_obj : CVJ object
The CVJ object comes from the CVJSON library, this object must hold you
top_count: int
This variable contains the number of classes you would like returned.
Returns
-------
list_of_classes : list
The list of classes with the most annotations in the CVJ object
"""
class_2_anns = copy.deepcopy(cvj_obj.get_class_id_2_anns())
list_of_classes = []
for i in range(top_count):
key = max(class_2_anns, key= lambda x: len(class_2_anns[x]))
list_of_classes.append(key)
del class_2_anns[key]
return list_of_classes
def print_cm(cm, labels):
"""
This method prints the confusion matrix in a way that is readable.
Parameters
----------
cm : numpy array
This confusion matrix is assumed to be from scikit-learn.
labels: list
This is a list of strings that are the labels for the confusion matrix. These
will replace the rows and columns with the names of the labels
Returns
-------
cm : numpy array
This confusion matrix is assumed to be from scikit-learn.
"""
columnwidth = max([len(x) for x in labels] + [5]) # 5 is value length
empty_cell = " " * columnwidth
# Begin CHANGES
fst_empty_cell = (columnwidth-3)//2 * " " + "t/p" + (columnwidth-3)//2 * " "
if len(fst_empty_cell) < len(empty_cell):
fst_empty_cell = " " * (len(empty_cell) - len(fst_empty_cell)) + fst_empty_cell
# Print header
print(" " + fst_empty_cell, end=" ")
# End CHANGES
for label in labels:
print("%{0}s".format(columnwidth) % label, end=" ")
print()
# Print rows
for i, label1 in enumerate(labels):
print(" %{0}s".format(columnwidth) % label1, end=" ")
for j in range(len(labels)):
cell = "%{0}.d".format(columnwidth) % cm[i, j]
print(cell, end=" ")
print()
return cm
def cm_heatmap(cm, labels, file_name, show=False):
"""
This method shows/saves a heatmap of the confusion matrix with
seaborn.
Parameters
----------
cm : numpy array
This confusion matrix is assumed to be from scikit-learn.
labels: list
This is a list of strings that are the labels for the confusion matrix. These
will replace the rows and columns with the names of the labels
file_name: string
This is the file_name or file_path where the plot will be saved.
show: bool
If this is True then the plot will be shown to the user of this method
Returns
-------
plt : plot object
This is the plot of the heatmap generated by seaborn. This is a matplotlib
plt object
"""
df_cm = pd.DataFrame(cm, index = [i for i in labels],
columns = [i for i in labels])
plt.figure(figsize = (10,7))
sn.heatmap(df_cm, annot=True, fmt="d")
if show:
plt.show()
input()
plt.savefig(file_name)
return plt
def report2dict(cr):
"""
This method converts the classification report from scikit-learn
to a dictionary.
Parameters
----------
D_class_data : dictionary
This is the classification report as a dictionary.
"""
# Parse rows
tmp = list()
for row in cr.split("\n"):
parsed_row = [x for x in row.split(" ") if len(x) > 0]
if len(parsed_row) > 0:
tmp.append(parsed_row)
# Store in dictionary
measures = tmp[0]
D_class_data = defaultdict(dict)
for row in tmp[1:]:
class_label = row[0]
for j, m in enumerate(measures):
D_class_data[class_label][m.strip()] = float(row[j + 1].strip())
return D_class_data
def group_categories(X_data, y_data, list_of_lists_of_ids, df, noise=False, noise_id=24):
"""
This method receives a list of lists of category ids and the
data from the get_data() method to group them. Each list of ids
will be a group. The features from the X
data will be associated with the new labels automatically because the
features are associated with the labels based on position.
x[0] is associated with y[0]. So if y[0] changes it's label then
x[0] is now assocatied with that label.
However, X_data needs to be sent so it can be reduced the dimensions that
the y_data will be.
This is done, by refactoring the ids to associate with those ids.
e.g. The first list of ids will be group 25, their class id will then be 25.
This is incremented. Group 25, group 26, group K for K lists of ids found
in list_of_list_of_ids. So the ORDER will matter if you want a particular
class to be in a particular group.
As the user of this method, it is your responsibility to match these ids to
their appropriate names outside of this method. This method ONLY reclassifies
the data.
Parameters
----------
X_data : numpy array
This is the sample data normally known as X_train or X_test
y_data: numpy array
This is the label data normally known as y_train or y_test
list_of_lists_of_ids: list
This is a list of lists. This is used for grouping specific id's together to form
a singular class out of many classes in the dataset
noise: bool
If this is True then this will use the noise id specifed as the class that
is used for noise. This will make the noise class have the highest ID
noise_id: int
see the definition of noise above
Returns
-------
X_data : numpy array
This is the sample data that has been refactored
y_data : numpy array
This is the label data that has been refactored
"""
group_id = max(max([max(list_) for list_ in list_of_lists_of_ids]), 25) # 24 is the highest id of the MARDCT dataset so 25 should be the max. However, I wanted to attempt at
# making the code reusable.
start = group_id
for list_of_ids in list_of_lists_of_ids:
mask = np.isin(y_data, list_of_ids) # generates a mask of indices, this should work on any dimension
y_data[mask] = group_id # this sets the data found by the mask to the group number, aka the new class id
# print(df[df["y_values"].isin(list_of_ids)]["y_values"]) #= group_id
df.loc[df["y_values"].isin(list_of_ids), "y_values"] = group_id
group_id += 1
if noise:
df.loc[df["y_values"] == noise_id, "y_values"] = group_id
df = df.loc[df["y_values"] >= start, :]
y_data[y_data == noise_id] = group_id # group_id will be up by one and no other class should have it
X_data = X_data[y_data >= start]
y_data = y_data[y_data >= start]
else:
X_data = X_data[y_data >= start]
y_data = y_data[y_data >= start]
df = df.loc[df["y_values"] >= start, :]
# Ridiculuous, if you have any ids that skip increments then you will run in to "src/THCUNN/ClassNLLCriterion.cu:105:"
# type errors. This happens because you need the minimum amount of output neurons as there are classes
# if you have a class id of "25" and just one other, you will still need 26 neurons to satisfy the torch model.
# so refactoring is necessary.
unique_ids = sorted(np.unique(y_data).tolist())
count = 0
for i in unique_ids:
y_data[y_data == i] = count
df.loc[df["y_values"] == i, "y_values"] = count
count += 1
# print(np.unique(y_data))
return X_data, y_data, df
def keras_logic(X_train, y_train, X_test, y_test, input_shape_tup, class_num):
"""
so far it is broken
"""
# Import `Sequential` from `keras.models`
from keras.models import Sequential
# Import `Dense` from `keras.layers`
from keras.layers import Dense, Flatten
from keras import metrics
from keras import losses
from keras.optimizers import SGD, RMSprop
print(X_train[X_train == np.nan])
print(X_train[X_train == np.inf])
# Initialize the constructor
model = Sequential()
print(input_shape_tup[1:])
# Add an input layer
model.add(Dense(class_num, activation='softmax', input_shape=input_shape_tup[1:]))
# Add an output layer
# model.add(Dense(class_num, activation='softmax'))
model.compile(optimizer=RMSprop(lr=.000001), loss=losses.sparse_categorical_crossentropy, metrics=[metrics.categorical_accuracy])
print(model.summary())
model.fit(X_train, y_train, batch_size=1, epochs=2, shuffle=False, verbose=2)
return model
def train_torch(model_class, X_train, y_train, batch_size=30, input_neurons=None, hidden_neurons=500, \
output_neurons=None, learning_rate=.01, epochs=400, verbose=True):
"""
This method is the driving method for training a pytorch model.
Parameters
----------
X_train : numpy array
This is the sample data
y_train: numpy array
This is the label data
batch_size: int
This is the size of how many samples each iteration will use
input_neurons: int
This is the size the of the features
hidden_neurons: int
This is the size the of layers in between the input and output layers
output_neurons: int
This is the size of the output neurons
learning_rate: float
This is the learning rate of the neural network. This handles how fast
the network will converge to a solution
epochs: int
This is the amount of times the network will iterate through the ENTIRE
data set.
Returns
-------
model : nn.Module
this is the model of the trained network
"""
train = hdf_Dataset(X_train, y_train)
train_loader = torch.utils.data.DataLoader(dataset=train, batch_size=batch_size, shuffle=True)
# interestingly if the output neurons are at 10 even though there
# are only 5 classes, errors occur in the loss. Cuda assert NLLCriterion error.
if input_neurons == None:
input_neurons = X_train.shape[1]
if output_neurons == None:
output_neurons = len(np.unique(y_train).tolist())
model = model_class(input_neurons, hidden_neurons, output_neurons)
# print(model.fc1.weight)# to make sure the weights are always the same for real reproducibility and debugging.
# print(model.fc2.weight)
# print(model.fc3.weight)
# input()
gpu_available = torch.cuda.is_available()
if gpu_available:
model = model.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
correct_during_training = 0
total_during_training = 0
for i in range(epochs):
for k, (arrays, labels) in enumerate(train_loader):
arrays = torch.autograd.Variable(arrays)
labels = torch.autograd.Variable(labels)
if gpu_available:
arrays = arrays.cuda()
labels = labels.cuda()
outputs = model(arrays)
pred = torch.max(outputs, 1)[1]
if gpu_available:
correct_during_training += (pred.cpu() == labels.cpu()).sum()
total_during_training += labels.size(0)
else:
correct_during_training += (pred == labels).sum()
total_during_training += labels.size(0)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
optimizer.zero_grad()
percentage = (100 * (correct_during_training.float()/total_during_training))
if verbose:
if k % 100 == 0:
print("loss = {}, Correct = {}, Total = {}, Percentage = {}".format(loss, correct_during_training, total_during_training, percentage))
return model
def test_torch(model, X_test, y_test, batch_size=30):
"""
This method is the driving method for testing a pytorch model.
Parameters
----------
model: nn.Module
This is the model that is created when using the nn.Module class from torch.
X_test : numpy array
This is the sample data
y_test: numpy array
This is the label data
batch_size: int
This is the size of how many samples each iteration will use
Returns
-------
model : nn.Module
This is the model of the trained network
y_pred: numpy array
This is the predictions of the network in the order of the X_test set.
"""
test = hdf_Dataset(X_test, y_test)
test_loader = torch.utils.data.DataLoader(dataset=test, batch_size=batch_size)
gpu_available = torch.cuda.is_available()
if gpu_available:
model = model.cuda()
for i in range(1):
y_pred = []
for k, (arrays, labels) in enumerate(test_loader):
arrays = torch.autograd.Variable(arrays)
labels = torch.autograd.Variable(labels)
if gpu_available:
arrays = arrays.cuda()
labels = labels.cuda()
outputs = model(arrays)
pred = torch.max(outputs, 1)[1]
if gpu_available:
if len(y_pred) == 0:
y_pred = pred.cpu().numpy()
y_scores = outputs.data.cpu().numpy()
else:
y_pred = np.hstack((y_pred, pred.cpu().numpy()))
y_scores = np.vstack((y_scores, outputs.data.cpu().numpy()))
return model, y_pred, y_scores
def generate_noise(hdf_file_path, semantics=False):
"""
This method needs to be given the hdf file that you wish to be noise.
Regardless of what classes are in the file this will label them all as 5000
which is an arbitrarily defined noise class
Parameters
----------
hdf_file_path: string
This is the file path to the hdf5 file that should be considered noise
semantics : bool
If true this will use the scores in the hdf5 file as the features.
"""
file_ = h5py.File(hdf_file_path)
noise = []
for image, values in list(file_["frames"].items()):
# boxes = values["bounding_boxes"]
features = values["descriptors"]
features = np.asarray(features)
scores = values["scores"]
scores = np.asarray(scores)
try:
features = features.reshape(scores.shape[0], -1)
scores = scores.reshape(scores.shape[0], scores.shape[1])
except ValueError:
print(features.shape)
print(features)
if len(noise) == 0:
if not semantics:
noise = np.copy(features)
else:
noise = np.copy(scores)
if len(noise) != 0:
noise = np.asarray(noise)
else:
if not semantics:
for index in range(features.shape[0]):
noise = np.vstack((noise, features[index]))
else:
for index in range(features.shape[0]):
noise = np.vstack((noise, scores[index]))
labels = np.zeros((noise.shape[0],))
labels[:] = 5000 # using 5000 as my negatives/noise class. The noise data should be any detections that did not correspond to
# a ground truth label. This is the noise that is produced by the model.
labels = labels.ravel()
return noise, labels, "noise" # noise is the label for this data.
def get_grouped_data(name_list, train_labeled_features_path, test_labeled_features_path, train_cvj,\
verbose=True, save_plot=None, list_of_ids_to_ignore=None, sample_threshold=50, noise_id=24):
"""
This method needs to be given the hdf file that you wish to be noise.
Regardless of what classes are in the file this will label them all as 5000
which is an arbitrarily defined noise class
Parameters
----------
name_list: list
This is a list of lists. These are the names of the classes to be grouped together.
train_labeled_features_path : str
This is the file path to the hdf5 file created when using the map_labels.py file.
Speficially this is the file path to the training set of the file created with the labels.py file
test_labeled_features_path: str
This is the file path to the hdf5 file created when using the map_labels.py file.
Speficially this is the file path to the test set of the file created with the labels.py file
train_cvj: CVJ object
This is CVJ object created using the training set. The CVJ object requires the JSON file
of the training set.
verbose: bool
If this is true then this method will ouput to the console what it removed, what the ids were,
what is left and what the ids are. Sometimes not every class makes it in to the list due to the
sample threshold
save_plot: dict
If the plots are wanted then this will use TSNE to map the dimensions to a lower dimension.
The keys that must be set are "dimension" and "file_name" which indicates which dimension to map to and
where to save the plot respectively.
list_of_ids_to_ignore: list
The ids of the data inside this list will be used to completely ignore them during training and testing.
sample_threshold: int
The sample threshold works by removing any classes that do not have a sample size of K found in the training
set. This means that if there are samples_amount[class] > K, the test set could be less than K, but the
class still appear in the reports
noise_id: int
This is used to identify the noise class if there is one. In Mardct the noise class is 24 for "other" this class
is also referred to as the confusion class.
Returns
-------
X_train : numpy array
This is the refactored sample data from the training set.
y_train : numpy array
This is the refactored label data from the training set.
X_test : numpy array
This is the refactored sample data from the test set.
y_test : numpy array
This is the refactored label data from the test set.
labels: numpy array
These are the unique labels found in the refactored training set.
"""
id_list = []
removed_list = []
for names in name_list:
group = []
for name in names:
idx = train_cvj.get_class_name_2_id()[name.lower()]
if len(train_cvj.get_class_id_2_anns(idx)) > sample_threshold:
group.append(idx)
else:
removed_list.append(name)
id_list.append(group)
id_list = [list_ for list_ in id_list if len(list_) > 0]
new_list= []
for i in id_list:
new_list.extend(i)
class_list = [train_cvj.get_class_id_2_name(i) for i in new_list]
if verbose:
print("The ID's of the groups are as follows:")
print(id_list)
print("\nThe following classes were removed due to having"\
" a TRAINING sample size less than {}".format(sample_threshold))
print(removed_list)
print("\nThe classes left are as follows:")
print(class_list)
print(new_list)
dif = set(train_cvj.get_category_ids()) - set(new_list)
if noise_id != None:
dif.remove(noise_id)
X_train, y_train, cats, counts, df_train = get_data(train_labeled_features_path, list_of_ids_to_ignore=list_of_ids_to_ignore)
X_test, y_test, cats, count_, df_test = get_data(test_labeled_features_path, list_of_ids_to_ignore=list_of_ids_to_ignore)
classes, counts = counts
dif = list(dif)
for index, class_ in np.ndenumerate(classes):
if counts[index] < sample_threshold:
if class_ not in dif:
dif.append(class_)
df_train = df_train[~df_train["y_values"].isin(dif)]
df_test = df_test[~df_test["y_values"].isin(dif)]
X_train = X_train[np.isin(y_train, dif, invert=True)]
y_train = y_train[np.isin(y_train, dif, invert=True)]
X_test = X_test[np.isin(y_test, dif, invert=True)]
y_test = y_test[np.isin(y_test, dif, invert=True)]
if save_plot != None:
try:
dimension = save_plot["dimension"]
if dimension == 3:
threeD_model = TSNE(n_components=3, random_state=0)
threeD_tsne_matrix = threeD_model.fit_transform(X_train)
threeD_df = plotting.threeD_data(threeD_tsne_matrix, y_train)
plotting.plot_3d(threeD_df, file_name=save_plot["file_name"], jupyter=False)
elif dimension == 2:
twoD_model = TSNE(n_components=2, random_state=0)
twoD_tsne_matrix = twoD_model.fit_transform(X_train)
twoD_df = plotting.twoD_data(twoD_tsne_matrix, y_train)
plotting.plot_3d(twoD_df, file_name=save_plot["file_name"], jupyter=False)
except KeyError:
print("save_plot variable needs to have keys \'dimension\' and \'file_name\'.")
X_train, y_train, df_train = group_categories(X_train, y_train, id_list, df_train, noise=True)
X_test, y_test, df_test = group_categories(X_test, y_test, id_list, df_test, noise=True)
labels = np.unique(y_train)
return X_train, y_train, X_test, y_test, labels, df_train, df_test
def run_scikit_pipeline(classifier, X_train, y_train, X_test, y_test,\
labels=[], save_report={}, console_report=True, grid_search_params=None):
"""
This method needs to be drives the classification experiments using scikit-learn. It is a modular
piece of code in that it should be able to take any classifier from scikit-learn as long as it fits
the correct parameters.
Parameters
----------
classifier: estimator
This is a list of lists. These are the names of the classes to be grouped together.
X_train: numpy array
This is the sample data of the training set
y_train : numpy array
This is the label data of the training set
X_test: numpy array
This is the sample data of the test set
y_test: numpy array
This is label data of the test set
labels: list
This is the list of unique labels that are in the dataset
save_report: dict
If the report is to be saved this dictionary must contain the key "file_name"
along with the file_name/file_path as the value
console_report: bool
If this is True then a report will be generated to the console.
grid_search_params: dict
This is used for searching for the best possible parameters using gridsearch. Right now the classifier's name will be
clf due to standard naming conventions. So if you wanted to search the gamma values of an SVM in the dictionary you would put
{'clf__gamma': [.1, .2, .3]}. Continue adding any parameters you wish to search. Just remember the double underscore.
This also is used to set parameters for something other than default. As long as the variable to have a custom parameter
isn't given more than one, that parameter passed is the one that will be used. Therefore making this pipeline modular
and able to fit most classifiers by scikit-learn.
Returns
-------
pipe: estimator
This is technically a pipe object, but is also the classifier
preds: numpy array
These are the predictions made by the classifier
"""
# Sci-kit pipelines take a list of named estimators (just means dictionaries), in pytorch this would be known as transforms, and then performs the estimator
# functions. An estimator can be PCA or it could be a model.
estimators = [('clf', classifier)]
from sklearn.model_selection import GridSearchCV
pipe = Pipeline(estimators)
if grid_search_params != None:
pipe = GridSearchCV(pipe, grid_search_params)
pipe.fit(X_train, y_train)
preds = pipe.predict(X_test)