Andrey Lukyanenko
DS TechLead, MTS AI
def configure_optimizers(self):
optimizer = load_obj(self.cfg.optimizer.class_name)(self.model.parameters(),
**self.cfg.optimizer.params)
scheduler = load_obj(self.cfg.scheduler.class_name)(optimizer,
**self.cfg.scheduler.params)
return (
[optimizer],
[{'scheduler': scheduler,
'interval': self.cfg.scheduler.step,
'monitor': self.cfg.scheduler.monitor}],
)
>>> python train.py
>>> python train.py optimizer=sgd
>>> python train.py model=efficientnet_model
>>> python train.py model.encoder.params.arch=resnet34
>>> python train.py datamodule.fold_n=0,1,2 -m
@hydra.main(config_path='conf', config_name='config')
def run_model(cfg: DictConfig) -> None:
os.makedirs('logs', exist_ok=True)
print(cfg.pretty())
if cfg.general.log_code:
save_useful_info()
run(cfg)
if __name__ == '__main__':
run_model()
def training_step(
self, batch: torch.Tensor, batch_idx: int
) -> Union[int, Dict[str, Union[torch.Tensor, Dict[str, torch.Tensor]]]]:
image = batch['image']
target = batch['target']
logits, loss = self(image, target)
score = self.metric(logits.argmax(1), target)
logs = {'train_loss': loss, f'train_{self.cfg.training.metric}': score}
return {
'loss': loss,
'log': logs,
'progress_bar': logs,
'logits': logits,
'target': target,
f'train_{self.cfg.training.metric}': score,
}
def training_epoch_end(self, outputs):
avg_loss = torch.stack([x['loss'] for x in outputs]).mean()
y_true = torch.cat([x['target'] for x in outputs])
y_pred = torch.cat([x['logits'] for x in outputs])
score = self.metric(y_pred.argmax(1), y_true)
logs = {'train_loss': avg_loss, f'train_{self.cfg.training.metric}': score}
return {'log': logs, 'progress_bar': logs}
def set_seed(seed: int = 42) -> None:
np.random.seed(seed)
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
>>> python train.py optimizer=sgd
>>> python train.py trainer.gpus=2