change folder structure

This commit is contained in:
hobogalaxy committed 2021-01-12 18:33:04 +01:00
1 parent fbe2464426
commit 9a5a4eb192
23 files changed
+148 -120

No files matched your search

+34 -23
View File
@@ -1,31 +1,42 @@
# THIS FILE IS FOR HYPERPARAMETER SEARCH WITH Weights&Biases
# IT SHOULDN't BE EXECUTED DIRECTLY, IT SHOULD BE RUN BY WANDB SWEEP AGENT!
# hydra imports
from omegaconf import DictConfig
import hydra
# template utils imports
import template_utils.initializers as utils
import wandb
import train
from train import train
# Choose which run config to optimize
RUN_CONFIG_NAME = "SIMPLE_CONFIG_EXAMPLE_MNIST"
# Load configs
project_config = train.load_config("config.yaml")
run_config = train.load_config("run_configs.yaml")[RUN_CONFIG_NAME]
# Initialize wandb
wandb.init()
# Replace run config hyperparameters with the ones loaded from wandb sweep server
sweep_hparams = wandb.Config._as_dict(wandb.config)
for key, value in sweep_hparams.items():
if key == "_wandb":
continue
elif key in run_config["trainer"]["args"]:
run_config["trainer"]["args"][key] = value
elif key in run_config["model"]["hparams"]:
run_config["model"]["hparams"][key] = value
elif key in run_config["datamodule"]["hparams"]:
run_config["datamodule"]["hparams"][key] = value
def replace_hparams(config, sweep_hparams):
for key, value in sweep_hparams.items():
if key == "_wandb":
continue
elif key in config["trainer"]["args"]:
config["trainer"]["args"][key] = value
elif key in config["model"]["args"]:
config["model"]["args"][key] = value
elif key in config["optimizer"]["args"]:
config["optimizer"]["args"][key] = value
elif key in config["datamodule"]["args"]:
config["datamodule"]["args"][key] = value
# Execute run
train.train(project_config=project_config, run_config=run_config, use_wandb=True)
@hydra.main(config_path="configs/", config_name="config.yaml")
def main(config: DictConfig) -> None:
wandb.init()
sweep_hparams = wandb.Config._as_dict(wandb.config)
utils.show_config(config) # print content of config
train(config)
wandb.finish()
if __name__ == "__main__":
main()
+2 -2
View File
@@ -1,8 +1,8 @@
from pytorch_modules.transforms import mnist_transforms
from src.transforms import mnist_transforms
from PIL import Image
# the LitModel you import should be the same as the one you used for training!
from pytorch_modules.lightning_models.simple_mnist_classifier import LitModel
from src.models.simple_mnist_classifier import LitModel
# ckpt can be a url!
+19 -5
View File
@@ -138,11 +138,11 @@ def log_extra_hparams(loggers: List[pl.loggers.LightningLoggerBase],
log_hparams(loggers, config["datamodule"]["args"])
if "log_model_class" in extra_logs and extra_logs["log_model_class"]:
log_hparams(loggers, {"model_class": config["model"]["class"]})
log_hparams(loggers, {"_class_model": config["model"]["class"]})
if "log_optimizer_class" in extra_logs and extra_logs["log_optimizer_class"]:
log_hparams(loggers, {"optimizer_class": config["optimizer"]["class"]})
log_hparams(loggers, {"_class_optimizer": config["optimizer"]["class"]})
if "log_datamodule_class" in extra_logs and extra_logs["log_datamodule_class"]:
log_hparams(loggers, {"datamodule_class": config["datamodule"]["class"]})
log_hparams(loggers, {"_class_datamodule": config["datamodule"]["class"]})
if "log_train_val_test_sizes" in extra_logs and extra_logs["log_train_val_test_sizes"]:
hparams = {}
@@ -154,9 +154,10 @@ def log_extra_hparams(loggers: List[pl.loggers.LightningLoggerBase],
hparams["test_size"] = len(datamodule.data_test)
log_hparams(loggers, hparams)
if "log_model_architecture_class_name" in extra_logs and extra_logs["log_model_architecture_class_name"]:
if "log_model_architecture_class" in extra_logs and extra_logs["log_model_architecture_class"]:
if hasattr(model, "model"):
log_hparams(loggers, {"model_architecture_class_name": model.model.__class__.__name__})
obj = model.model
log_hparams(loggers, {"_class_model_architecture": obj.__module__ + "." + obj.__class__.__name__})
def show_init_info(model, datamodule, callbacks, loggers):
@@ -177,6 +178,19 @@ def show_init_info(model, datamodule, callbacks, loggers):
log.info(message)
def auto_find_lr(trainer, model, datamodule, loggers):
lr_finder = trainer.tuner.lr_find(model=model, datamodule=datamodule)
new_lr = lr_finder.suggestion()
# Save lr plot
fig = lr_finder.plot(suggest=True)
fig.savefig("lr_loss_curve.jpg")
# Set new lr
model.hparams.lr = new_lr
log_hparams(loggers, {"lr": new_lr})
def show_config(config: DictConfig):
log.info(f"\n{OmegaConf.to_yaml(config, resolve=True)}")