add disabling warnings and logs to main config
This commit is contained in:
1 parent
3b46e402c7
commit
c26c73cc2d
4 files changed
+88
-73
No files matched your search
@@ -13,12 +13,12 @@ repos:
|
||||
- id: detect-private-key
|
||||
- id: check-yaml
|
||||
- id: check-added-large-files
|
||||
- id: check-merge-conflict
|
||||
# - id: check-merge-conflict
|
||||
|
||||
- repo: https://github.com/psf/black
|
||||
rev: 20.8b1
|
||||
hooks:
|
||||
- id: black
|
||||
# - repo: https://github.com/psf/black
|
||||
# rev: 20.8b1
|
||||
# hooks:
|
||||
# - id: black
|
||||
|
||||
# - repo: https://github.com/PyCQA/isort
|
||||
# rev: 5.7.0
|
||||
|
||||
+10
-5
@@ -15,7 +15,7 @@ defaults:
|
||||
|
||||
# path to original working directory (that `train.py` was executed from in command line)
|
||||
# hydra hijacks working directory by changing it to the current log directory,
|
||||
# so it's useful to have path to original working directory as a special variable
|
||||
# so it's useful to have path to original work dir as a special variable
|
||||
# read more here: https://hydra.cc/docs/next/tutorials/basic/running_your_app/working_directory
|
||||
work_dir: ${hydra:runtime.cwd}
|
||||
|
||||
@@ -28,6 +28,14 @@ data_dir: ${work_dir}/data/
|
||||
print_config: True
|
||||
|
||||
|
||||
# disable python warnings if they annoy you
|
||||
disable_warnings: True
|
||||
|
||||
|
||||
# disable lightning logs if they annoy you
|
||||
disable_lightning_logs: False
|
||||
|
||||
|
||||
# output paths for hydra logs
|
||||
hydra:
|
||||
run:
|
||||
@@ -38,10 +46,7 @@ hydra:
|
||||
|
||||
job:
|
||||
env_set:
|
||||
# currently there are some issues with running sweeps alongside wandb
|
||||
# currently there are some issues with running sweeps alongside wandb
|
||||
# https://github.com/wandb/client/issues/1314
|
||||
# this env variable fixes that
|
||||
WANDB_START_METHOD: thread
|
||||
|
||||
# ignore python warnings
|
||||
# PYTHONWARNINGS: ignore
|
||||
+43
-43
@@ -1,11 +1,11 @@
|
||||
# pytorch lightning imports
|
||||
# lightning imports
|
||||
import pytorch_lightning as pl
|
||||
|
||||
# hydra imports
|
||||
from omegaconf import DictConfig, OmegaConf
|
||||
from hydra.utils import get_original_cwd, to_absolute_path
|
||||
|
||||
# loggers
|
||||
# logger imports
|
||||
import wandb
|
||||
from pytorch_lightning.loggers.wandb import WandbLogger
|
||||
|
||||
@@ -15,9 +15,9 @@ from pytorch_lightning.loggers.wandb import WandbLogger
|
||||
# from pytorch_lightning.loggers.tensorboard import TensorBoardLogger
|
||||
|
||||
# rich imports
|
||||
from rich import print
|
||||
from rich.syntax import Syntax
|
||||
from rich.tree import Tree
|
||||
from rich import print
|
||||
|
||||
# normal imports
|
||||
from typing import List
|
||||
@@ -34,41 +34,41 @@ def print_config(config: DictConfig):
|
||||
# directory = to_absolute_path("configs/config.yaml")
|
||||
# print(f"Main config path: [link file://{directory}]{directory}")
|
||||
|
||||
style = "dim"
|
||||
style = 'dim'
|
||||
|
||||
tree = Tree(f":gear: FULL HYDRA CONFIG", style=style, guide_style=style)
|
||||
tree = Tree(f':gear: HYDRA CONFIG', style=style, guide_style=style)
|
||||
|
||||
trainer = OmegaConf.to_yaml(config["trainer"], resolve=True)
|
||||
trainer_branch = tree.add("Trainer", style=style, guide_style=style)
|
||||
trainer_branch.add(Syntax(trainer, "yaml"))
|
||||
trainer = OmegaConf.to_yaml(config['trainer'], resolve=True)
|
||||
trainer_branch = tree.add('Trainer', style=style, guide_style=style)
|
||||
trainer_branch.add(Syntax(trainer, 'yaml'))
|
||||
|
||||
model = OmegaConf.to_yaml(config["model"], resolve=True)
|
||||
model_branch = tree.add("Model", style=style, guide_style=style)
|
||||
model_branch.add(Syntax(model, "yaml"))
|
||||
model = OmegaConf.to_yaml(config['model'], resolve=True)
|
||||
model_branch = tree.add('Model', style=style, guide_style=style)
|
||||
model_branch.add(Syntax(model, 'yaml'))
|
||||
|
||||
datamodule = OmegaConf.to_yaml(config["datamodule"], resolve=True)
|
||||
datamodule_branch = tree.add("Datamodule", style=style, guide_style=style)
|
||||
datamodule_branch.add(Syntax(datamodule, "yaml"))
|
||||
datamodule = OmegaConf.to_yaml(config['datamodule'], resolve=True)
|
||||
datamodule_branch = tree.add('Datamodule', style=style, guide_style=style)
|
||||
datamodule_branch.add(Syntax(datamodule, 'yaml'))
|
||||
|
||||
callbacks_branch = tree.add("Callbacks", style=style, guide_style=style)
|
||||
if "callbacks" in config:
|
||||
for cb_name, cb_conf in config["callbacks"].items():
|
||||
callbacks_branch = tree.add('Callbacks', style=style, guide_style=style)
|
||||
if 'callbacks' in config:
|
||||
for cb_name, cb_conf in config['callbacks'].items():
|
||||
cb = callbacks_branch.add(cb_name, style=style, guide_style=style)
|
||||
cb.add(Syntax(OmegaConf.to_yaml(cb_conf, resolve=True), "yaml"))
|
||||
cb.add(Syntax(OmegaConf.to_yaml(cb_conf, resolve=True), 'yaml'))
|
||||
else:
|
||||
callbacks_branch.add("None")
|
||||
callbacks_branch.add('None')
|
||||
|
||||
logger_branch = tree.add("Logger", style=style, guide_style=style)
|
||||
if "logger" in config:
|
||||
for lg_name, lg_conf in config["logger"].items():
|
||||
logger_branch = tree.add('Logger', style=style, guide_style=style)
|
||||
if 'logger' in config:
|
||||
for lg_name, lg_conf in config['logger'].items():
|
||||
lg = logger_branch.add(lg_name, style=style, guide_style=style)
|
||||
lg.add(Syntax(OmegaConf.to_yaml(lg_conf, resolve=True), "yaml"))
|
||||
lg.add(Syntax(OmegaConf.to_yaml(lg_conf, resolve=True), 'yaml'))
|
||||
else:
|
||||
logger_branch.add("None")
|
||||
logger_branch.add('None')
|
||||
|
||||
seed = config.get("seed", "None")
|
||||
seed_branch = tree.add(f"Seed", style=style, guide_style=style)
|
||||
seed_branch.add(str(seed) + "\n")
|
||||
seed = config.get('seed', 'None')
|
||||
seed_branch = tree.add(f'Seed', style=style, guide_style=style)
|
||||
seed_branch.add(str(seed) + '\n')
|
||||
|
||||
print(tree)
|
||||
|
||||
@@ -95,32 +95,32 @@ def log_hparams_to_all_loggers(
|
||||
hparams = {}
|
||||
|
||||
# save all params of model, datamodule and trainer
|
||||
hparams.update(config["model"])
|
||||
hparams.update(config["datamodule"])
|
||||
hparams.update(config["trainer"])
|
||||
hparams.pop("_target_")
|
||||
hparams.update(config['model'])
|
||||
hparams.update(config['datamodule'])
|
||||
hparams.update(config['trainer'])
|
||||
hparams.pop('_target_')
|
||||
|
||||
# save seed
|
||||
hparams["seed"] = config.get("seed", "None")
|
||||
hparams['seed'] = config.get('seed', 'None')
|
||||
|
||||
# save targets
|
||||
hparams["_class_model"] = config["model"]["_target_"]
|
||||
hparams["_class_datamodule"] = config["datamodule"]["_target_"]
|
||||
hparams['_class_model'] = config['model']['_target_']
|
||||
hparams['_class_datamodule'] = config['datamodule']['_target_']
|
||||
|
||||
# save sizes of each dataset
|
||||
if hasattr(datamodule, "data_train") and datamodule.data_train:
|
||||
hparams["train_size"] = len(datamodule.data_train)
|
||||
if hasattr(datamodule, "data_val") and datamodule.data_val:
|
||||
hparams["val_size"] = len(datamodule.data_val)
|
||||
if hasattr(datamodule, "data_test") and datamodule.data_test:
|
||||
hparams["test_size"] = len(datamodule.data_test)
|
||||
if hasattr(datamodule, 'data_train') and datamodule.data_train:
|
||||
hparams['train_size'] = len(datamodule.data_train)
|
||||
if hasattr(datamodule, 'data_val') and datamodule.data_val:
|
||||
hparams['val_size'] = len(datamodule.data_val)
|
||||
if hasattr(datamodule, 'data_test') and datamodule.data_test:
|
||||
hparams['test_size'] = len(datamodule.data_test)
|
||||
|
||||
# save number of model parameters
|
||||
hparams["#params_total"] = sum(p.numel() for p in model.parameters())
|
||||
hparams["#params_trainable"] = sum(
|
||||
hparams['#params_total'] = sum(p.numel() for p in model.parameters())
|
||||
hparams['#params_trainable'] = sum(
|
||||
p.numel() for p in model.parameters() if p.requires_grad
|
||||
)
|
||||
hparams["#params_not_trainable"] = sum(
|
||||
hparams['#params_not_trainable'] = sum(
|
||||
p.numel() for p in model.parameters() if not p.requires_grad
|
||||
)
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# pytorch lightning imports
|
||||
# lightning imports
|
||||
from pytorch_lightning import LightningModule, LightningDataModule, Callback, Trainer
|
||||
from pytorch_lightning.loggers import LightningLoggerBase
|
||||
from pytorch_lightning import seed_everything
|
||||
@@ -9,6 +9,8 @@ import hydra
|
||||
|
||||
# normal imports
|
||||
from typing import List
|
||||
import warnings
|
||||
import logging
|
||||
|
||||
# src imports
|
||||
from src.utils import template_utils as utils
|
||||
@@ -16,39 +18,47 @@ from src.utils import template_utils as utils
|
||||
|
||||
def train(config: DictConfig):
|
||||
|
||||
# Disable python warnings
|
||||
if config['disable_warnings']:
|
||||
warnings.filterwarnings('ignore')
|
||||
|
||||
# Disable PyTorch Lightning logs
|
||||
if config['disable_lightning_logs']:
|
||||
logging.getLogger('lightning').setLevel(logging.ERROR)
|
||||
|
||||
# Pretty print config using Rich library
|
||||
if config["print_config"]:
|
||||
if config['print_config']:
|
||||
utils.print_config(config)
|
||||
|
||||
# Set seed for random number generators in pytorch, numpy and python.random
|
||||
if "seed" in config:
|
||||
seed_everything(config["seed"])
|
||||
if 'seed' in config:
|
||||
seed_everything(config['seed'])
|
||||
|
||||
# Init PyTorch Lightning model ⚡
|
||||
model: LightningModule = hydra.utils.instantiate(config["model"])
|
||||
model: LightningModule = hydra.utils.instantiate(config['model'])
|
||||
|
||||
# Init PyTorch Lightning datamodule ⚡
|
||||
datamodule: LightningDataModule = hydra.utils.instantiate(config["datamodule"])
|
||||
datamodule: LightningDataModule = hydra.utils.instantiate(config['datamodule'])
|
||||
datamodule.prepare_data()
|
||||
datamodule.setup()
|
||||
|
||||
# Init PyTorch Lightning callbacks ⚡
|
||||
# Init PyTorch Lightning callbacks
|
||||
callbacks: List[Callback] = []
|
||||
if "callbacks" in config:
|
||||
for _, cb_conf in config["callbacks"].items():
|
||||
if "_target_" in cb_conf:
|
||||
if 'callbacks' in config:
|
||||
for _, cb_conf in config['callbacks'].items():
|
||||
if '_target_' in cb_conf:
|
||||
callbacks.append(hydra.utils.instantiate(cb_conf))
|
||||
|
||||
# Init PyTorch Lightning loggers ⚡
|
||||
# Init PyTorch Lightning loggers
|
||||
logger: List[LightningLoggerBase] = []
|
||||
if "logger" in config:
|
||||
for _, lg_conf in config["logger"].items():
|
||||
if "_target_" in lg_conf:
|
||||
if 'logger' in config:
|
||||
for _, lg_conf in config['logger'].items():
|
||||
if '_target_' in lg_conf:
|
||||
logger.append(hydra.utils.instantiate(lg_conf))
|
||||
|
||||
# Init PyTorch Lightning trainer ⚡
|
||||
trainer: Trainer = hydra.utils.instantiate(
|
||||
config["trainer"], callbacks=callbacks, logger=logger
|
||||
config['trainer'], callbacks=callbacks, logger=logger
|
||||
)
|
||||
|
||||
# Send some parameters from config to all lightning loggers
|
||||
@@ -61,7 +71,7 @@ def train(config: DictConfig):
|
||||
logger=logger,
|
||||
)
|
||||
|
||||
# Train the model
|
||||
# Train the model ⚡
|
||||
trainer.fit(model=model, datamodule=datamodule)
|
||||
|
||||
# Evaluate model on test set after training
|
||||
@@ -77,16 +87,16 @@ def train(config: DictConfig):
|
||||
logger=logger,
|
||||
)
|
||||
|
||||
# Return best achieved metric score for optuna
|
||||
optimized_metric = config.get("optimized_metric", None)
|
||||
# Return metric score for optuna optimization
|
||||
optimized_metric = config.get('optimized_metric', None)
|
||||
if optimized_metric:
|
||||
return trainer.callback_metrics[optimized_metric]
|
||||
|
||||
|
||||
@hydra.main(config_path="configs/", config_name="config.yaml")
|
||||
@hydra.main(config_path='configs/', config_name='config.yaml')
|
||||
def main(config: DictConfig):
|
||||
return train(config)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user