-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
1479 lines (1296 loc) · 62.8 KB
/
main.py
File metadata and controls
1479 lines (1296 loc) · 62.8 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 logging
import logging.handlers
import argparse
import os
import time
from exp_run.models_pytorch import Custom, CustomAtt
from data_loader import organiser, organiser_test
import shutil
import sys
import torch
import random
import numpy as np
import pandas as pd
import utils.utilities as util
import socket
from distutils.dir_util import copy_tree
import sklearn.metrics as metrics
from sklearn.metrics import confusion_matrix
from torch.utils.tensorboard import SummaryWriter
import torchvision.models as models
import natsort
from utils import model_utilities as mu
from exp_run.plotter import plot_graph, confusion_mat
from exp_run import dataset_processing
from configs import config, config_dataset, create_exp_folder
import pickle
EPS = 1e-12
def calculate_accuracy(target, predict, classes_num, f_score_average):
"""
Calculates accuracy, precision, recall, F1-Score, True Negative,
False Negative, True Positive, and False Positives of the output of
the model
Inputs
target: np.array() The labels for the predicted outputs from the model
predict: np.array() The batched outputs of the network
classes_num: int How many classes are in the dataset
f_score_average: str How to average the F1-Score
Outputs:
accuracy: Float Accuracy of the model outputs
p_r_f: Array of Floats Precision, Recall, and F1-Score
tn_fp_fn_tp: Array of Floats True Negative, False Positive,
False Negative, and True Positive
"""
number_samples_labels = len(target)
number_correct_predictions = np.zeros(classes_num)
total = np.zeros(classes_num)
for n in range(number_samples_labels):
total[target[n]] += 1
if target[n] == predict[n]:
number_correct_predictions[target[n]] += 1
con_matrix = confusion_matrix(target, predict)
# print(con_matrix)
cm = confusion_mat(target, predict)
# print(cm)
tn_fp_fn_tp = con_matrix.ravel()
if tn_fp_fn_tp.shape != (4,):
value = int(tn_fp_fn_tp)
if target[0][0] == 1:
tn_fp_fn_tp = np.array([0, 0, 0, value])
elif target[0][0] == 0:
tn_fp_fn_tp = np.array([value, 0, 0, 0])
else:
print('Error in the true_neg/false_pos value')
sys.exit()
if f_score_average is None:
# This code fixes the divide by zero error
accuracy = np.divide(number_correct_predictions,
total,
out=np.zeros_like(number_correct_predictions),
where=total != 0)
p_r_f = metrics.precision_recall_fscore_support(target,
predict)
elif f_score_average == 'macro':
# This code fixes the divide by zero error
accuracy = np.divide(number_correct_predictions,
total,
out=np.zeros_like(number_correct_predictions),
where=total != 0)
p_r_f = metrics.precision_recall_fscore_support(target,
predict,
average='macro')
elif f_score_average == 'micro':
# This code fixes the divide by zero error
accuracy = np.divide(np.sum(number_correct_predictions),
np.sum(total),
out=np.zeros_like(number_correct_predictions),
where=total != 0)
p_r_f = metrics.precision_recall_fscore_support(target,
predict,
average='micro')
else:
raise Exception('Incorrect average!')
if p_r_f[0].shape == (1,):
temp = np.zeros((4, 2))
position = int(target[0])
for val in range(len(p_r_f)):
temp[val][position] = float(p_r_f[val])
temp1 = (temp[0])
temp2 = (temp[1])
temp3 = (temp[2])
temp4 = (temp[3])
p_r_f = (temp1, temp2, temp3, temp4)
return accuracy, p_r_f, tn_fp_fn_tp
def forward(model, generate_dev, net_params, recurrent_out, convert_image,
data_type):
"""
Pushes the data to the model and collates the outputs
Inputs:
generate_dev: generator - holds the batches for the validation
return_target: Bool - If True, return labels and folders information
net_params: dictionary - Holds the model configurations
recurrent_out: str - Type of output of RNN layer
Output:
results_dict: dictionary - Outputs, optional - labels and folders
"""
outputs = []
folders = []
if data_type == 'dev':
targets = []
# Evaluate on mini-batch
for data in generate_dev:
if data_type == 'dev':
(batch_data, batch_label, batch_folder, batch_locator) = data
else:
(batch_data, batch_folder, batch_locator) = data
# Predict
model.eval()
# Potentially speeds up evaluation and memory usage
with torch.no_grad():
batch_output = get_output_from_model(model=model,
data=batch_data,
net_type=select_net,
net_params=net_params,
learning_procedure=learning_procedure_dev,
learning_procedure_decider=learning_procedure_decider_dev,
locator=batch_locator,
label='dev',
recurrent_out=recurrent_out,
convert_to_image=convert_image)
# Append data
outputs.append(batch_output.data.cpu().numpy())
folders.append(batch_folder)
if data_type == 'dev':
targets.append(batch_label)
results_dict = {}
outputs = np.concatenate(outputs, axis=0)
results_dict['output'] = outputs
folders = np.concatenate(folders, axis=0)
results_dict['folder'] = np.array(folders)
if data_type == 'dev':
targets = np.concatenate(targets, axis=0)
results_dict['target'] = np.array(targets)
return results_dict
def evaluate(model, generator, data_type, class_weights, comp_res, class_num,
f_score_average, net_p, recurrent_out, epochs,
convert_im, logger, gender_balance=False):
"""
Processes the validation set by creating batches, passing these through
the model and then calculating the resulting loss and accuracy metrics.
Input
generator: generator - Created to load validation data to the model
data_type: str - set to 'dev' or 'test'
class_weights: tensor - if class weights are used due to imbalanced
dataset
comp_res: dataframe - holds the complete results for training and
validation up to the current epoch
class_num: int - Number of classes in the dataset
f_score_average: str - Type of F1 Score processing
averaging: str - Geometric or arithmetic for the outputs from the model
net_p: dictionary - holds the model configurations
recurrent_out: str - If RNN used, how to process the output
epochs: int - The current epoch
Returns:
complete_results: dataframe - Updated dataframe of results
per_epoch_pred: numpy.array - collated outputs and labels from the
current validation test
"""
# Generate function
print('Generating data for evaluation')
start_time_dev = time.time()
if data_type == 'dev':
generate_dev = generator.generate_development_data(epoch=epochs)
else:
generate_dev = generator.generate_test_data()
# Forward
results_dict = forward(model=model,
generate_dev=generate_dev,
net_params=net_p,
recurrent_out=recurrent_out,
convert_image=convert_im,
data_type=data_type)
outputs = results_dict['output'] # (audios_num, classes_num)
folders = results_dict['folder']
if data_type == 'dev':
targets = results_dict['target'] # (audios_num, classes_num)
if learning_procedure_decider_dev != 'whole_file':
collected_output = {}
if data_type == 'dev':
new_targets = []
counter = {}
for p, fol in enumerate(folders):
if fol not in collected_output.keys():
# collected_output[fol] = [outputs[p][0]]
if data_type == 'dev':
new_targets.append(targets[p])
collected_output[fol] = outputs[p].copy()
counter[fol] = 1
else:
# collected_output[fol].append(outputs[p][0])
collected_output[fol] += outputs[p].copy()
counter[fol] += 1
new_outputs = []
new_folders = []
for co in collected_output:
temp = collected_output[co] / counter[co]
new_outputs.append(temp)
new_folders.append(co)
outputs = np.array(new_outputs)
folders = np.array(new_folders)
if data_type == 'dev':
targets = np.array(new_targets)
calculate_time(start_time_dev, time.time(), 'dev', logger)
if data_type == 'test':
return outputs, folders
if data_type == 'dev':
loss = mu.calculate_loss(torch.Tensor(outputs),
torch.LongTensor(targets), class_weights,
net_p, gender_balance)
if gender_balance:
targets = targets % 2
complete_results, per_epoch_pred = prediction_and_accuracy(outputs,
targets,
True,
class_num,
comp_res,
loss, 0,
config,
f_score_average)
return complete_results, per_epoch_pred
def logging_info(current_dir, current_fold, data_type=''):
"""
Sets up the logger to be used for the current experiment. This is useful
to capture relevant information during the course of the experiment.
Output
main_logger: logger - The created logger
"""
if mode == 'test':
if data_type == 'test':
log_path = os.path.join(current_dir, "test.log")
elif data_type == 'dev':
log_path = os.path.join(current_dir, 'log',
f"model_{current_fold}_test.log")
else:
log_path = os.path.join(current_dir, 'log', f"model_{current_fold}.log")
main_logger = logging.getLogger('MainLogger')
main_logger.setLevel(logging.INFO)
if os.path.exists(log_path) and mode == 'test':
os.remove(log_path)
main_handler = logging.handlers.RotatingFileHandler(log_path)
main_logger.addHandler(main_handler)
main_logger.info(config_dataset.SEPARATOR)
main_logger.info('EXPERIMENT DETAILS')
for dict_val in config.EXPERIMENT_DETAILS:
if dict_val == 'SEED':
main_logger.info(f"Starting {dict_val}:"
f" {str(config.EXPERIMENT_DETAILS[dict_val])}")
else:
main_logger.info(f"{dict_val}:"
f" {str(config.EXPERIMENT_DETAILS[dict_val])}")
main_logger.info(f"Current Seed: {chosen_seed}")
main_logger.info(f"Logged into: {socket.gethostname()}")
main_logger.info(config_dataset.SEPARATOR)
return main_logger
def create_model(main_logger):
"""
Creates the model to be used in the current experimentation
Input
main_logger: logger - Used to capture important information
Output
model: obj - The model to be used for training during experiment
"""
if select_net == 'custom':
model = Custom(main_logger, config.NETWORK_PARAMS)
elif select_net == 'custom_att':
model = CustomAtt(main_logger, config.NETWORK_PARAMS)
elif select_net == 'densenet':
model = models.densenet161(pretrained=True)
model.classifier = torch.nn.Linear(model.classifier.in_features, 2)
if cuda:
model.cuda()
else:
model.cpu()
# model.state_dict()
# list(model.parameters())
log_network_params(model, main_logger)
return model
def setup(dataset_dir, current_dir, model_dir, feature_experiment, data_mode,
gender, current_fold, data_type='', path_to_logger_for_test=None):
"""
Creates the necessary directories, data folds, logger, and model to be
used in the experiment. It also determines whether a previous checkpoint
has been saved.
Inputs
dataset_dir: str - The location of the dataset
feature_experiment: str - The type of features used in this experiment
data_mode: str - Set to sub if using some of the training data as
validation data or set to complete if using the complete
training data and no validation
audio_mode_is_concat_not_shorten: bool - Set False if the data is
shortened to the shortest clip in the
dataset
make_dataset_equal: bool - Set True if the dataset should be
subsampled in order to balance it
Outputs
main_logger: logger - The logger to be used to record information
model: obj - The model to be used for training during the experiment
checkpoint_run: str - The location of the last saved checkpoint
checkpoint: bool - True if loading from a saved checkpoint
next_fold: bool - If loading from a checkpoint is suspected but the
current fold experiment has been completed
"""
reproducibility(chosen_seed)
checkpoint_run = None
checkpoint = False
next_fold = False
next_exp = False
if not os.path.exists(features_dir):
print('There is no folder and therefore no database created. '
'Create the database first')
sys.exit()
if os.path.exists(current_dir) and os.path.exists(model_dir) and debug:
shutil.rmtree(current_dir, ignore_errors=False, onerror=None)
# THIS WILL DELETE EVERYTHING IN THE CURRENT WORKSPACE #
if not os.path.exists(data_fold_dir):
os.makedirs(data_fold_dir)
os.makedirs(data_fold_dir_equal)
dataset_processing.partition_dataset(workspace_main_dir,
feature_experiment,
features_dir,
sub_dir,
current_dir,
data_mode,
dataset_dir,
total_folds,
gender)
if os.path.exists(current_dir) and os.path.exists(model_dir):
temp_dirs = os.listdir(model_dir)
temp_dirs = natsort.natsorted(temp_dirs, reverse=True)
temp_dirs = [d for d in temp_dirs if '.pth' in d]
if len(temp_dirs) == 0:
pass
else:
if int(temp_dirs[0].split('_')[1]) == final_iteration and mode ==\
'train':
directory = model_dir.split('/')[-1]
final_directory = model_dir.replace(directory, 'Fold_'+str(total_folds))
if os.path.exists(final_directory):
temp_dirs2 = os.listdir(final_directory)
temp_dirs2 = natsort.natsorted(temp_dirs2, reverse=True)
temp_dirs2 = [d for d in temp_dirs2 if '.pth' in d]
if int(temp_dirs2[0].split('_')[1]) == final_iteration:
if i == config.EXP_RUNTHROUGH-1:
print(f"A directory at this location exists: {current_dir}")
sys.exit()
else:
next_exp = True
return None, None, None, None, next_fold, next_exp
else:
next_fold = True
return None, None, None, None, next_fold, next_exp
else:
next_fold = True
return None, None, None, None, next_fold, next_exp
else:
print(f"Current directory exists but experiment not finished")
print(f"Loading from checkpoint: {int(temp_dirs[0].split('_')[1])}")
checkpoint_run = os.path.join(model_dir, temp_dirs[0])
checkpoint = True
elif not os.path.exists(current_dir):
os.mkdir(current_dir)
util.create_directories(current_dir, config.EXP_FOLDERS)
os.mkdir(model_dir)
elif os.path.exists(current_dir) and not os.path.exists(model_dir):
os.mkdir(model_dir)
if mode == 'test' and path_to_logger_for_test is not None and data_type \
== 'test':
if os.path.exists(path_to_logger_for_test):
shutil.rmtree(path_to_logger_for_test, ignore_errors=False,
onerror=None)
os.mkdir(path_to_logger_for_test)
main_logger = logging_info(path_to_logger_for_test, current_fold,
data_type)
else:
main_logger = logging_info(current_dir, current_fold, data_type)
model = create_model(main_logger)
return main_logger, model, checkpoint_run, checkpoint, next_fold, next_exp
def log_network_params(model, main_logger):
"""
Logs the network architecture for inspection post experiment
Input
params: dictionary - The network configuration from the config file
main_logger: logger - Used to record the network architecture
"""
main_logger.info('List of Network Parameters')
conv_count = 1
fc_count = 1
for name, param in model.named_parameters():
if param.requires_grad:
main_logger.info(f"Name: {name}, \nParam.data: {param.data.shape}")
if model.named_children():
for child in model.named_children():
main_logger.info(child)
# for p in params:
# if len(list(p.size())) > 3:
# main_logger.info(f"Conv_{conv_count} : {list(p.size())}")
# conv_count += 1
# elif 1 < len(list(p.size())) < 3:
# main_logger.info(f"FC_{fc_count}: {list(p.size())}")
# fc_count += 1
def record_top_results(current_results, scores, epoch):
"""
Function to record the best validation F1-Score up to the current epoch.
More accurate than the alternate function record_top_results
Inputs:
current_results: list - current epoch results
scores: tuple - contains the best results for the experiment
epoch: int - The current epoch
Output
best_res: list - updated best result and epoch of discovery
"""
if current_results[8] > .86:
train_f = current_results[9] / 4
train_loss = current_results[10] / 10
dev_f = current_results[-6]
dev_loss = current_results[-5] / 10
total = train_f - train_loss + dev_f - dev_loss
if total > scores[0]:
best_res = [total, current_results[8], current_results[0],
current_results[1], current_results[9],
current_results[6], current_results[7],
current_results[10], current_results[23],
current_results[15], current_results[16], dev_f,
current_results[21], current_results[22],
current_results[25], epoch]
else:
best_res = scores
else:
best_res = scores
return best_res
def initialiser(test_value):
"""
Used to set a bool to True for the initialisation of some function or
variable
Input
test_value: int - If set to 1 then this is the initial condition
otherwise, already initialised
Output
bool - True if this is the initialisation case
"""
if test_value == 1:
return True
else:
return False
def compile_train_val_pred(train_res, val_res, comp_train, comp_val, epoch,
net_params):
"""
Used to group the latest results for both the training and the validation
set into their respective complete results array
Inputs
train_res: numpy.array - The current results for this epoch
val_res: numpy.array - The current results for this epoch
comp_train: numpy.array - The total recorded results
comp_val: numpy.array - The total recorded results
epoch: int - The current epoch used for initialisation
net_params: dictionary - Holds the model configurations
Outputs
comp_train: numpy.array - The updated complete results
comp_val - numpy.array - The updated complete results
"""
# 3D matrix ('Num_segments_batches', 'pred+label', 'epochs')
if epoch == 1:
comp_train = train_res
comp_val = val_res
else:
if train_res.shape[0] != comp_train.shape[0]:
difference = comp_train.shape[0] - train_res.shape[0]
if 'SOFTMAX_1' in net_params:
train_res = np.vstack((train_res, np.zeros((difference, 3))))
else:
train_res = np.vstack((train_res, np.zeros((difference, 2))))
comp_train = np.dstack((comp_train, train_res))
comp_val = np.dstack((comp_val, val_res))
return comp_train, comp_val
def update_complete_results(complete_results, avg_counter,
placeholder, best_scores):
"""
Finalises the complete results dataframe by calculating the mean of the 2
class scores for accuracy and F1-Score and in the case of the training
data, divides the results by the number of iterations in order to get the
average results from the current epoch (previously updated by accumulation)
Also obtains the best scores for the model.
Inputs
complete_results: dataframe - holds the complete results from the
experiment so far
label: str - set to train or dev
avg_counter: int - used in train mode to average the recorded results
for the current epoch
placeholder: Essentially the number of epochs (but can be used in
iteration mode)
best_scores: list - More accurate representation of the best score,
gives epoch for best validation F1-Score
Outputs
complete_results: dataframe - Updated version of the complete results
best_scores: list - Updated version of best_scores
"""
complete_results[0:11] = complete_results[0:11] / avg_counter
# Accuracy Mean
complete_results[8] = np.mean(complete_results[0:2])
complete_results[23] = np.mean(complete_results[15:17])
# FScore Mean
complete_results[9] = np.mean(complete_results[6:8])
complete_results[24] = np.mean(complete_results[21:23])
print_log_results(placeholder, complete_results[0:15], 'train')
print_log_results(placeholder, complete_results[15:], 'dev')
best_scores = record_top_results(complete_results, best_scores, placeholder)
return complete_results, best_scores
def prediction_and_accuracy(batch_output, batch_labels, initial_condition,
num_of_classes, complete_results, loss,
per_epoch_pred, config, f_score_average=None):
"""
Calculates the accuracy (including F1-Score) of the predictions from a
model. Also the True Negatives, False Negatives, True Positives, and False
Positives are calculated. These results are stored along with results
from previous epochs.
Input
batch_output: The output from the model
batch_labels: The respective labels for the batched output
initial_condition: Bool - True if this is the first instance to set
up the variables for logging accuracy
num_of_classes: The number of classes in this dataset
complete_results: Dataframe of the complete results obtained
loss: The value of the loss from the current epoch
per_epoch_pred: Combined batch outputs and labels for record keeping
config: The config file for the current experiment
f_score_average: The type of averaging to be used fro the F1-Score (
Macro, Micro, or None
Output
complete_results: Dataframe of the complete results to the current epoch
per_epoch_pred: Combined results of batch outputs and labels for
current epoch
"""
if type(batch_output) is not np.ndarray:
batch_output = batch_output.data.cpu().numpy()
batch_labels = batch_labels.data.cpu().numpy()
if len(batch_output.shape) == 1:
batch_output = batch_output.reshape(-1, 1)
if len(batch_labels.shape) == 1:
batch_labels = batch_labels.reshape(-1, 1)
if initial_condition:
per_epoch_pred = np.hstack((batch_output, batch_labels))
else:
temp_stack = np.hstack((batch_output, batch_labels))
per_epoch_pred = np.vstack((per_epoch_pred, temp_stack))
if 'SIGMOID_1' in config.NETWORK_PARAMS:
if config.NETWORK_PARAMS['SIGMOID_1'] == 'unnorm':
prediction = 1 - np.round(batch_output)
elif config.NETWORK_PARAMS['SIGMOID_1'] == 'round':
prediction = np.round(batch_output)
elif config.NETWORK_PARAMS['SIGMOID_1'] == 'threshold':
height, width = batch_output.shape
prediction = np.zeros((height, width))
for pointer, value in enumerate(batch_output):
if value >= 0.4:
prediction[pointer, :] = 1
else:
prediction[pointer, :] = 0
else:
print('Error - set "procedure_with_sig" to unnorm or round')
sys.exit()
prediction = prediction.reshape(-1)
else:
prediction = np.argmax(batch_output, axis=1)
if len(batch_labels.shape) > 1:
batch_labels = batch_labels.reshape(-1)
if batch_labels.dtype == 'float32':
batch_labels = batch_labels.astype(np.long)
acc, fscore, tn_fp_fn_tp = calculate_accuracy(batch_labels, prediction,
num_of_classes,
f_score_average)
complete_results[0:2] += acc
complete_results[2:8] += np.array(fscore[0:3]).reshape(1, -1)[0]
complete_results[10] += loss
complete_results[11:15] += tn_fp_fn_tp
return complete_results, per_epoch_pred
def print_log_results(epoch, results, data_type):
"""
Used to print/log results after every epoch
Inputs
epoch: int - The current epoch
results: numpy.array - The current results
data_type: str - Set to train, val, or test
"""
print('\n', config_dataset.SEPARATOR)
print(f"{data_type} accuracy at epoch: {epoch}\n{data_type} Accuracy: Mean:"
f" {np.round(results[8], 3)} - {np.round(results[0:2], 3)}, "
f"F1_Score: Mean: {np.round(results[9], 3)} -"
f" {np.round(results[6:8], 3)}, Loss: {np.round(results[10], 3)}")
print(config_dataset.SEPARATOR, '\n')
main_logger.info(f"\n{config_dataset.SEPARATOR}{config_dataset.SEPARATOR}")
main_logger.info(f"{data_type} accuracy at epoch: {epoch}\n{data_type} "
f"Accuracy: Mean: {np.round(results[8], 3)} -"
f" {np.round(results[0:2], 3)}, F1_Score: Mean:"
f" {np.round(results[9], 3)},"
f" {np.round(results[6:8], 3)}, Loss:"
f" {np.round(results[10], 3)}")
main_logger.info(f"{config_dataset.SEPARATOR}{config_dataset.SEPARATOR}\n")
def final_organisation(scores, train_pred, val_pred, df, patience,
epoch, workspace_files_dir):
"""
Records final information with the logger such as the best scores for
training and validation and saves/copies files from the current
experiment into the saved model directory for future analysis. The
complete results to the current epoch are saved for checkpoints or future
analysis
Inputs
scores: list - The best scores from the training and validation results
train_pred: numpy.array - Record of the complete outputs of the
network for every epoch
val_pred: numpy.array - Record of the complete outputs of the
network for every epoch
df: pandas.dataframe - The complete results for every epoch
patience: int - Used to record if early stopping was implemented
epoch: int - The current epoch
scores2: list - More accurate version of scores. Only holds the
best validation F1-Score location
workspace_files_dir: str - Location of the programme code
"""
main_logger.info(f"Best Train Acc: {scores[0]}\nBest Train Fscore:"
f" {scores[1]}\nBest Train Loss: {scores[2]}\nBest Val "
f"Acc: {scores[3]}\nBest Val Fscore: {scores[4]}\nBest "
f"Val Loss: {scores[5]}")
main_logger.info(f"\nscores: {scores[1:-1]}")
if epoch == final_iteration:
main_logger.info(f"System will exit as the total number of "
f"epochs has been reached {final_iteration}")
else:
main_logger.info(f"System will exit as the validation loss "
f"has not improved for {patience} epochs")
print(f"System will exit as the validation loss has not "
"improved for {patience} epochs")
util.save_model_outputs(model_dir, df, train_pred, val_pred, scores[1:])
copy_tree(workspace_files_dir, current_dir+'/daic')
def reduce_learning_rate(optimizer):
"""
Reduce the learning rate of the optimiser for training
Input
optimiser: obj - The optimiser setup at the start of the experiment
"""
learning_rate_reducer = 0.9
for param_group in optimizer.param_groups:
print('Reducing Learning rate from: ', param_group['lr'],
' to ', param_group['lr'] * learning_rate_reducer)
main_logger.info(f"Reducing Learning rate from: "
f"{param_group['lr']}, to "
f"{param_group['lr'] * learning_rate_reducer}")
param_group['lr'] *= learning_rate_reducer
def reproducibility(chosen_seed):
"""
The is required for reproducible experimentation. It sets the random
generators for the different libraries used to a specific, user chosen
seed.
Input
chosen_seed: int - The seed chosen for this experiment
"""
torch.manual_seed(chosen_seed)
torch.cuda.manual_seed_all(chosen_seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(chosen_seed)
random.seed(chosen_seed)
def collate_net_outputs(output, output_att=None, net_params='SOFTMAX_1',
learning_procedure='soft_mv',
avg_setting='arithmetic', current_batch_size=20,
iterator=0, num=1):
"""
Processes the output of the model depending on user specified options
such as whether hard or soft majority vote is used, and whether geometric
or arithmetic averaging are to be used. Also handles the situation where
an attention mechanism is used.
Input
output: Raw output from the model of this experiment
output_att: Raw output of the attention mechanism for this experiment
net_params: Dictionary containing the model configurations
learning_procedure: Soft or hard majority vote
avg_setting: Geometric or arithmetic averaging setting
current_batch_size: The current size of the batched output
Output
output: Processed output
"""
net_last_layer = [k for k in net_params.keys()][-1]
if 'ATTENTION_global' in net_params or 'ATTENTION_1' in net_params:
if iterator+1 == num:
if 'ATTENTION_global' in net_params:
soft = torch.nn.Softmax(dim=-1)
output_att = soft(output_att)
attended = output * output_att
output = torch.sum(attended,
dim=1).reshape(current_batch_size,
-1)
output = torch.clamp(output, min=0, max=1)
else:
if cuda:
output = output.cpu()
return output
elif learning_procedure == 'hard_mv':
if avg_setting == 'geometric':
if net_last_layer == 'SIGMOID_1':
output = torch.cat((output, 1 - output),
dim=1)
output = torch.clamp(torch.round(output)+EPS,
min=0, max=1)
output = torch.log(output)
else:
output = torch.round(output)
else:
if avg_setting == 'geometric':
if net_last_layer == 'SIGMOID_1':
output = torch.cat((output, 1 - output),
dim=1)
output = torch.log(output)
else:
output = output
if cuda:
output = output.cpu()
return output
def get_output_from_model(model, data, net_type, net_params, learning_procedure,
learning_procedure_decider, locator=[],
label='', recurrent_out='whole',
convert_to_image=False):
"""
Pushes the batched data to the user specified neural network and
depending on the settings the output will be processed regarding
how long the files are in the batch. The high level options are
random_sample, chunked_file, and whole_file. The output processing
depends also on whether soft majority vote or hard majority vote are
selected along with the type of averaging (arithmetic and geometric) and
the final layer of the network (softmax or sigmoid)
Inputs:
model: object - the NN used for experimentation
data: Data to be pushed to the model
net_type: What model is used?
net_params: The model configuration - includes layers and filter data
learning_procedure: How is the data processed: random_sample,
chunked_file, or whole_file
learning_procedure_decider: Soft majority vote or hard majority vote.
If single files are used instead of sequence
data, soft majority vote is used
locator: Array of the lengths of the files in the batch
label: Set to train or dev depending on the section of experiment
recurrent_out: If RNN is used, how is the output processed?
convert_to_im: Bool, are we converting the spectrograms to 3D via
delta and d-delta calculation?
Output
output: The output of the model from the input batch data
"""
net_last_layer = [k for k in net_params.keys()][-1]
if net_type == 'densenet':
output = model(data)
lsm = torch.nn.LogSoftmax(dim=-1)
output = lsm(output)
elif net_type == 'custom' or net_type == 'custom_att':
if learning_procedure == 'random_sample':
segments = 1
else:
segments = np.max(locator)
current_batch_size = data.shape[0] // segments
if 'SOFTMAX_1' in net_params:
output = torch.zeros(current_batch_size, 2)
else:
output = torch.zeros(current_batch_size, 1)
placeholder = 0
output_net = torch.zeros(current_batch_size, segments)
attout_net = torch.zeros(current_batch_size, segments)
for p in range(segments):
current_data = data[placeholder:placeholder+current_batch_size]
current_data = mu.create_tensor_data(current_data,
cuda,
select_net)
if 'ATTENTION_global' in net_params:
if p == 0:
temp_out, hc, temp_att = model(current_data, net_params,
convert_to_image,
recurrent_out=recurrent_out,
label=label)
else:
temp_out, hc, temp_att = model(current_data, net_params,
convert_to_image,
hidden=hc,
recurrent_out=recurrent_out,
label=label)
if label == 'dev':
zero_out_index = torch.ones((temp_out.shape[0], 1))
for pos, loc in enumerate(locator):
if p >= loc:
zero_out_index[pos] = 0
zero_out_index = mu.create_tensor_data(
zero_out_index, cuda, net_type)
temp_out = temp_out * zero_out_index
temp_att = temp_att * zero_out_index
output_net[:, p] = temp_out.reshape(current_batch_size)
attout_net[:, p] = temp_att.reshape(current_batch_size)
else:
if 'LSTM_1' in net_params or 'GRU_1' in net_params:
if p == 0:
output_net, hc, _ = model(current_data, net_params,
convert_to_image,
recurrent_out=recurrent_out,
label=label,
locator=locator)
else:
output_net, hc, _ = model(current_data, net_params,
convert_to_image,
hidden=hc,
recurrent_out=recurrent_out,
label=label,
locator=locator)
else:
output_net, _, _ = model(current_data, net_params,
convert_to_image,
recurrent_out=recurrent_out,
label=label,
locator=locator)
if label == 'dev':
zero_out_index = torch.ones((output_net.shape[0], 1))
for pos, loc in enumerate(locator):
if p >= loc:
zero_out_index[pos] = 0
zero_out_index = mu.create_tensor_data(
zero_out_index, cuda, net_type)
output_net = output_net * zero_out_index
attout_net = None
temp_out = collate_net_outputs(output_net, attout_net,
net_params,
learning_procedure_decider,
averaging,
current_batch_size, p, segments)
if 'ATTENTION_global' in net_params and p+1 != segments:
pass
else:
output = output + temp_out
# output = pre_output + output
placeholder += current_batch_size
if 'ATTENTION_global' in net_params:
pass
elif 'ATTENTION_1' in net_params:
if label == 'dev':
if output.dim() > 1:
locator = torch.Tensor(locator).view(-1, 1)
else:
locator = torch.Tensor(locator)
output = output / locator
else:
output = output / segments
# output = torch.clamp(output, min=0, max=1)
else:
if learning_procedure == 'whole_file' and label == 'dev':
if output.dim() > 1:
locator = torch.Tensor(locator).view(-1, 1)
else:
locator = torch.Tensor(locator)
output = output / locator
else:
output = output / segments
if learning_procedure_decider == 'hard_mv' and averaging == \