def run_for_epoch(self, epoch, x, y, type): self.network.eval() predictions_dict = {"tp": [], "fp": [], "tn": [], "fn": []} predictions = [] self.test_batch_loss, self.test_batch_accuracy, self.test_batch_uar, self.test_batch_ua, audio_for_tensorboard_test = [], [], [], [], None with torch.no_grad(): for i, (audio_data, label) in enumerate(zip(x, y)): label = tensor(label).float() test_predictions = self.network(audio_data).squeeze(1) test_loss = self.loss_function(test_predictions, label) test_predictions = nn.Sigmoid()(test_predictions) predictions.append(test_predictions.numpy()) test_accuracy, test_uar = accuracy_fn(test_predictions, label, self.threshold) self.test_batch_loss.append(test_loss.numpy()) self.test_batch_accuracy.append(test_accuracy.numpy()) self.test_batch_uar.append(test_uar) tp, fp, tn, fn = custom_confusion_matrix( test_predictions, label, threshold=self.threshold) predictions_dict['tp'].extend(tp) predictions_dict['fp'].extend(fp) predictions_dict['tn'].extend(tn) predictions_dict['fn'].extend(fn) print(f'***** {type} Metrics ***** ') print(f'***** {type} Metrics ***** ', file=self.log_file) print( f"Loss: {np.mean(self.test_batch_loss)} | Accuracy: {np.mean(self.test_batch_accuracy)} | UAR: {np.mean(self.test_batch_uar)}" ) print( f"Loss: {np.mean(self.test_batch_loss)} | Accuracy: {np.mean(self.test_batch_accuracy)} | UAR: {np.mean(self.test_batch_uar)}", file=self.log_file) log_summary(self.writer, epoch, accuracy=np.mean(self.test_batch_accuracy), loss=np.mean(self.test_batch_loss), uar=np.mean(self.test_batch_uar), lr=self.optimiser.state_dict()['param_groups'][0]['lr'], type=type) log_conf_matrix(self.writer, epoch, predictions_dict=predictions_dict, type=type) y = [element for sublist in y for element in sublist] predictions = [ element for sublist in predictions for element in sublist ] write_to_npy(filename=self.debug_filename, predictions=predictions, labels=y, epoch=epoch, accuracy=np.mean(self.test_batch_accuracy), loss=np.mean(self.test_batch_loss), uar=np.mean(self.test_batch_uar), lr=self.optimiser.state_dict()['param_groups'][0]['lr'], predictions_dict=predictions_dict, type=type)
def run_for_epoch(self, epoch, x, y, type): self.network.eval() for m in self.network.modules(): if isinstance(m, nn.BatchNorm2d): m.track_running_stats = False predictions_dict = {"tp": [], "fp": [], "tn": [], "fn": []} overall_predictions = [] self.test_batch_loss, self.test_batch_accuracy, self.test_batch_uar, self.test_batch_ua, self.test_batch_f1, self.test_batch_precision, self.test_batch_recall, audio_for_tensorboard_test = [], [], [], [], [], [], [], None with torch.no_grad(): for i, (audio_data, label) in enumerate(zip(x, y)): label = to_tensor(label, device=self.device).float() audio_data = to_tensor(audio_data, device=self.device) test_predictions = self.network(audio_data).squeeze(1) test_loss = self.loss_function(test_predictions, label) test_predictions = nn.Sigmoid()(test_predictions) overall_predictions.extend(to_numpy(test_predictions)) test_accuracy, test_uar, test_precision, test_recall, test_f1 = accuracy_fn( test_predictions, label, self.threshold) self.test_batch_loss.append(to_numpy(test_loss)) self.test_batch_accuracy.append(to_numpy(test_accuracy)) self.test_batch_uar.append(test_uar) self.test_batch_f1.append(test_f1) self.test_batch_precision.append(test_precision) self.test_batch_recall.append(test_recall) tp, fp, tn, fn = custom_confusion_matrix( test_predictions, label, threshold=self.threshold) predictions_dict['tp'].extend(tp) predictions_dict['fp'].extend(fp) predictions_dict['tn'].extend(tn) predictions_dict['fn'].extend(fn) print(f'***** {type} Metrics ***** ') print(f'***** {type} Metrics ***** ', file=self.log_file) print( f"Loss: {'%.3f' % np.mean(self.test_batch_loss)} | Accuracy: {'%.3f' % np.mean(self.test_batch_accuracy)} | UAR: {'%.3f' % np.mean(self.test_batch_uar)}| F1:{'%.3f' % np.mean(self.test_batch_f1)} | Precision:{'%.3f' % np.mean(self.test_batch_precision)} | Recall:{'%.3f' % np.mean(self.test_batch_recall)}" ) print( f"Loss: {'%.3f' % np.mean(self.test_batch_loss)} | Accuracy: {'%.3f' % np.mean(self.test_batch_accuracy)} | UAR: {'%.3f' % np.mean(self.test_batch_uar)}| F1:{'%.3f' % np.mean(self.test_batch_f1)} | Precision:{'%.3f' % np.mean(self.test_batch_precision)} | Recall:{'%.3f' % np.mean(self.test_batch_recall)}", file=self.log_file) print( "****************************************************************") print(np.array(overall_predictions).shape) print(f"{type} predictions mean", np.mean(overall_predictions)) print(f"{type} predictions mean", np.mean(overall_predictions), file=self.log_file) print(f"{type} predictions sum", np.sum(overall_predictions)) print(f"{type} predictions sum", np.sum(overall_predictions), file=self.log_file) print(f"{type} predictions range", np.min(overall_predictions), np.max(overall_predictions)) print(f"{type} predictions range", np.min(overall_predictions), np.max(overall_predictions), file=self.log_file) print(f"{type} predictions hist", np.histogram(overall_predictions)) print(f"{type} predictions hist", np.histogram(overall_predictions), file=self.log_file) print(f"{type} predictions variance", np.var(overall_predictions)) print(f"{type} predictions variance", np.var(overall_predictions), file=self.log_file) print( "****************************************************************") log_summary(self.writer, epoch, accuracy=np.mean(self.test_batch_accuracy), loss=np.mean(self.test_batch_loss), uar=np.mean(self.test_batch_uar), lr=self.optimiser.state_dict()['param_groups'][0]['lr'], type=type) log_conf_matrix(self.writer, epoch, predictions_dict=predictions_dict, type=type) y = [element for sublist in y for element in sublist] write_to_npy(filename=self.debug_filename, predictions=overall_predictions, labels=y, epoch=epoch, accuracy=np.mean(self.test_batch_accuracy), loss=np.mean(self.test_batch_loss), uar=np.mean(self.test_batch_uar), lr=self.optimiser.state_dict()['param_groups'][0]['lr'], predictions_dict=predictions_dict, type=type)
def run_for_epoch(self, epoch, x, y, jitterx, type): # self.network.eval() # for m in self.network.modules(): # if isinstance(m, nn.BatchNorm2d): # m.track_running_stats = False predictions_dict = {"tp": [], "fp": [], "tn": [], "fn": []} logits, predictions = [], [] self.test_batch_loss, self.test_batch_accuracy, self.test_batch_uar, self.test_batch_ua, self.test_batch_f1, self.test_batch_precision, self.test_batch_recall, audio_for_tensorboard_test = [], [], [], [], [], [], [], None with torch.no_grad(): for i, (audio_data, label, jitter_shimmer_data) in enumerate(zip(x, y, jitterx)): label = to_tensor(label, device=self.device).float() audio_data = to_tensor(audio_data, device=self.device) jitter_shimmer_data = to_tensor(jitter_shimmer_data, device=self.device) test_predictions = self.network(audio_data, jitter_shimmer_data).squeeze(1) logits.extend(to_numpy(test_predictions)) test_loss = self.loss_function(test_predictions, label) test_predictions = nn.Sigmoid()(test_predictions) predictions.append(to_numpy(test_predictions)) test_accuracy, test_uar, test_precision, test_recall, test_f1 = accuracy_fn( test_predictions, label, self.threshold) self.test_batch_loss.append(to_numpy(test_loss)) self.test_batch_accuracy.append(to_numpy(test_accuracy)) self.test_batch_uar.append(test_uar) self.test_batch_f1.append(test_f1) self.test_batch_precision.append(test_precision) self.test_batch_recall.append(test_recall) tp, fp, tn, fn = custom_confusion_matrix( test_predictions, label, threshold=self.threshold) predictions_dict['tp'].extend(tp) predictions_dict['fp'].extend(fp) predictions_dict['tn'].extend(tn) predictions_dict['fn'].extend(fn) predictions = [ element for sublist in predictions for element in sublist ] self.logger.info(f'***** {type} Metrics ***** ') self.logger.info( f"Loss: {'%.3f' % np.mean(self.test_batch_loss)} | Accuracy: {'%.3f' % np.mean(self.test_batch_accuracy)} | UAR: {'%.3f' % np.mean(self.test_batch_uar)}| F1:{'%.3f' % np.mean(self.test_batch_f1)} | Precision:{'%.3f' % np.mean(self.test_batch_precision)} | Recall:{'%.3f' % np.mean(self.test_batch_recall)}" ) log_summary(self.writer, epoch, accuracy=np.mean(self.test_batch_accuracy), loss=np.mean(self.test_batch_loss), uar=np.mean(self.test_batch_uar), lr=self.optimiser.state_dict()['param_groups'][0]['lr'], type=type) log_conf_matrix(self.writer, epoch, predictions_dict=predictions_dict, type=type) log_learnable_parameter(self.writer, epoch, to_tensor(logits, device=self.device), name=f'{type}_logits') log_learnable_parameter(self.writer, epoch, to_tensor(predictions, device=self.device), name=f'{type}_predictions') write_to_npy(filename=self.debug_filename, predictions=predictions, labels=y, epoch=epoch, accuracy=np.mean(self.test_batch_accuracy), loss=np.mean(self.test_batch_loss), uar=np.mean(self.test_batch_uar), lr=self.optimiser.state_dict()['param_groups'][0]['lr'], predictions_dict=predictions_dict, type=type)