bug in metric calculation
This commit is contained in:
@ -56,18 +56,17 @@ class ValMixin:
|
||||
for file_name in sorted_y:
|
||||
sorted_y.update({file_name: torch.stack(sorted_y[file_name])})
|
||||
|
||||
y_max = torch.stack(
|
||||
[torch.argmax(x.mean(dim=0)) if x.shape[0] > 1 else torch.argmax(x) for x in sorted_y.values()]
|
||||
).squeeze()
|
||||
y_one_hot = torch.nn.functional.one_hot(y_max, num_classes=self.params.n_classes).float()
|
||||
|
||||
target_y = torch.stack(tuple(sorted_batch_y.values())).long()
|
||||
if self.params.n_classes <= 2:
|
||||
if y_one_hot.ndim == 1:
|
||||
y_one_hot = y_one_hot.unsqueeze(0)
|
||||
if target_y.ndim == 1:
|
||||
target_y = target_y.unsqueeze(-1)
|
||||
|
||||
self.metrics.update(y_one_hot, target_y)
|
||||
mean_sorted_y = torch.stack([x.mean(dim=0) if x.shape[0] > 1 else x for x in sorted_y.values()])
|
||||
self.metrics.update(mean_sorted_y, target_y)
|
||||
else:
|
||||
y_max = torch.stack(
|
||||
[torch.argmax(x.mean(dim=0)) if x.shape[0] > 1 else torch.argmax(x) for x in sorted_y.values()]
|
||||
).squeeze()
|
||||
y_one_hot = torch.nn.functional.one_hot(y_max, num_classes=self.params.n_classes).float()
|
||||
self.metrics.update(y_one_hot, target_y)
|
||||
if self.params.n_classes <= 2:
|
||||
val_loss = self.bce_loss(y.squeeze().float(), batch_y.float())
|
||||
else:
|
||||
@ -109,14 +108,15 @@ class ValMixin:
|
||||
#mean_vote_loss = self.ce_loss(y_mean, sorted_batch_y)
|
||||
#summary_dict.update(val_mean_vote_loss=mean_vote_loss)
|
||||
|
||||
y_max = torch.stack(
|
||||
[torch.argmax(x.mean(dim=0)) if x.shape[0] > 1 else torch.argmax(x) for x in sorted_y.values()]
|
||||
).squeeze()
|
||||
y_one_hot = torch.nn.functional.one_hot(y_max, num_classes=self.params.n_classes).float()
|
||||
if self.params.n_classes >= 2:
|
||||
max_vote_loss = self.ce_loss(y_one_hot, sorted_batch_y)
|
||||
if self.params.n_classes <= 2:
|
||||
mean_sorted_y = torch.stack([x.mean(dim=0) if x.shape[0] > 1 else x for x in sorted_y.values()])
|
||||
max_vote_loss = self.bce_loss(mean_sorted_y.float(), sorted_batch_y.float())
|
||||
else:
|
||||
max_vote_loss = self.bce_loss(y_one_hot, sorted_batch_y)
|
||||
y_max = torch.stack(
|
||||
[torch.argmax(x.mean(dim=0)) if x.shape[0] > 1 else torch.argmax(x) for x in sorted_y.values()]
|
||||
).squeeze()
|
||||
y_one_hot = torch.nn.functional.one_hot(y_max, num_classes=self.params.n_classes).float()
|
||||
max_vote_loss = self.ce_loss(y_one_hot, sorted_batch_y)
|
||||
summary_dict.update(val_max_vote_loss=max_vote_loss)
|
||||
|
||||
summary_dict.update({f'mean_{key}': torch.mean(torch.stack([output[key]
|
||||
@ -124,7 +124,10 @@ class ValMixin:
|
||||
for key in keys if 'loss' in key}
|
||||
)
|
||||
# Sklearn Scores
|
||||
additional_scores = self.additional_scores(dict(y=y_one_hot, batch_y=sorted_batch_y))
|
||||
if self.params.n_classes <= 2:
|
||||
additional_scores = self.additional_scores(dict(y=y_max, batch_y=sorted_batch_y))
|
||||
else:
|
||||
additional_scores = self.additional_scores(dict(y=y_one_hot, batch_y=sorted_batch_y))
|
||||
summary_dict.update(**additional_scores)
|
||||
|
||||
pl_metrics, pl_images = self.metrics.compute_and_prepare()
|
||||
@ -132,7 +135,9 @@ class ValMixin:
|
||||
summary_dict.update(**pl_metrics)
|
||||
summary_dict.update(epoch=self.current_epoch)
|
||||
|
||||
self.log_dict(summary_dict, on_epoch=True)
|
||||
self.log_dict(summary_dict)
|
||||
# For Debugging:
|
||||
# print(f'Summary Metrics are: {summary_dict}')
|
||||
|
||||
for name, image in pl_images.items():
|
||||
self.logger.log_image(name, image, step=self.global_step)
|
||||
|
Reference in New Issue
Block a user