Final Train Runs

This commit is contained in:
Steffen Illium
2021-03-18 07:45:07 +01:00
parent ad254dae92
commit fecf4923c2
14 changed files with 672 additions and 362 deletions

View File

@@ -1,3 +1,6 @@
from collections import defaultdict
from pathlib import Path
from abc import ABC
import torch
@@ -42,24 +45,67 @@ class ValMixin:
model_out = self(batch_x)
y = model_out.main_out
sorted_y = defaultdict(list)
sorted_batch_y = dict()
for idx, file_name in enumerate(batch_files):
sorted_y[file_name].append(y[idx])
sorted_batch_y.update({file_name: batch_y[idx]})
sorted_y = dict(sorted_y)
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=5).float()
self.metrics.update(y_one_hot, torch.stack(tuple(sorted_batch_y.values())).long())
val_loss = self.ce_loss(y, batch_y.long())
self.metrics.update(y, batch_y) # torch.argmax(y, -1), batch_y)
return dict(val_loss=val_loss, batch_files=batch_files,
return dict(batch_files=batch_files, val_loss=val_loss,
batch_idx=batch_idx, y=y, batch_y=batch_y)
def validation_epoch_end(self, outputs, *_, **__):
assert isinstance(self, LightningBaseModule)
summary_dict = dict()
keys = list(outputs[0].keys())
summary_dict.update({f'mean_{key}': torch.mean(torch.stack([output[key]
for output in outputs]))
for key in keys if 'loss' in key}
)
sorted_y = defaultdict(list)
sorted_batch_y = dict()
for output in outputs:
for idx, file_name in enumerate(output['batch_files']):
sorted_y[file_name].append(output['y'][idx])
sorted_batch_y.update({file_name: output['batch_y'][idx]})
sorted_y = dict(sorted_y)
sorted_batch_y = torch.stack(tuple(sorted_batch_y.values())).long()
for file_name in sorted_y:
sorted_y.update({file_name: torch.stack(sorted_y[file_name])})
y_mean = torch.stack(
[torch.mean(x, dim=0, keepdim=True) if x.shape[0] > 1 else x for x in sorted_y.values()]
).squeeze()
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=5).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]
for output in outputs]))
for key in keys if 'loss' in key}
)
# Sklearn Scores
additional_scores = self.additional_scores(outputs)
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()
@@ -85,13 +131,40 @@ class TestMixin:
def test_epoch_end(self, outputs, *_, **__):
assert isinstance(self, LightningBaseModule)
y_arg_max = torch.argmax(outputs[0]['y'])
pd.DataFrame(data=dict(filenames=outputs[0]['batch_files'], predtiction=y_arg_max))
# No logging, just inference.
# self.log_dict(summary_dict, on_epoch=True)
sorted_y = defaultdict(list)
for output in outputs:
for idx, file_name in enumerate(output['batch_files']):
sorted_y[file_name].append(output['y'][idx].cpu())
sorted_y = dict(sorted_y)
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().cpu()
class_names = {val: key for val, key in enumerate(['background', 'chimpanze', 'geunon', 'mandrille', 'redcap'])}
df = pd.DataFrame(data=dict(filenames=[Path(x).stem for x in sorted_y.keys()],
prediction=y_max.cpu().numpy(),
prediction_named=[class_names[x.item()] for x in y_max.cpu().numpy()]))
result_file = Path(self.logger.log_dir / 'predictions.csv')
if result_file.exists():
try:
result_file.unlink()
except:
print('File allready existed')
pass
with result_file.open(mode='wb') as csv_file:
df.to_csv(index=False, path_or_buf=csv_file)
with result_file.open(mode='rb') as csv_file:
try:
self.logger.neptunelogger.log_artifact(csv_file)
except:
print('No possible to send to neptune')
pass
class CombinedModelMixins(LossMixin,

View File

@@ -24,11 +24,16 @@ class OptimizerMixin:
if self.params.sto_weight_avg:
optimizer = SWA(optimizer, swa_start=10, swa_freq=5, swa_lr=0.05)
optimizer_dict.update(optimizer=optimizer)
if self.params.lr_warm_restart_epochs:
scheduler = CosineAnnealingWarmRestarts(optimizer, self.params.lr_warm_restart_epochs)
if self.params.scheduler == CosineAnnealingWarmRestarts.__name__:
scheduler = CosineAnnealingWarmRestarts(optimizer, self.params.lr_scheduler_parameter)
elif self.params.scheduler == LambdaLR.__name__:
lr_reduce_ratio = self.params.lr_scheduler_parameter
scheduler = LambdaLR(optimizer, lr_lambda=lambda epoch: lr_reduce_ratio ** epoch)
else:
scheduler = LambdaLR(optimizer, lr_lambda=lambda epoch: 0.95 ** epoch)
optimizer_dict.update(lr_scheduler=scheduler)
scheduler = None
if scheduler:
optimizer_dict.update(lr_scheduler=scheduler)
return optimizer_dict
def on_train_end(self):