restructure project again
This commit is contained in:
1 parent
20c451bc3d
commit
ee7006a92a
24 files changed
+208
-633
No files matched your search
+55
-45
@@ -6,84 +6,94 @@ from lightning_modules.init_utils import *
|
||||
from lightning_modules.callbacks import *
|
||||
|
||||
|
||||
def train(config, model_config):
|
||||
def train(project_config, run_config):
|
||||
|
||||
# Init lightning model
|
||||
lit_model = init_lit_model(model_config)
|
||||
# Init PyTorch Lightning model ⚡
|
||||
lit_model = init_lit_model(model_config=run_config["model"])
|
||||
|
||||
# Init lightning datamodule
|
||||
datamodule = init_datamodule(model_config)
|
||||
# Init PyTorch Lightning datamodule ⚡
|
||||
datamodule = init_datamodule(dataset_config=run_config["dataset"])
|
||||
|
||||
# Init logger
|
||||
logger = init_wandb_logger(config, lit_model, datamodule)
|
||||
# Init Weights&Biases logger
|
||||
logger = init_wandb_logger(project_config, run_config, lit_model, datamodule)
|
||||
|
||||
# Init callbacks
|
||||
callbacks = init_main_callbacks(config)
|
||||
# Init checkpoint and early stoping callbacks
|
||||
callbacks = init_main_callbacks(project_config)
|
||||
|
||||
# Add your custom callbacks
|
||||
callbacks.extend([
|
||||
# MetricsHeatmapLoggerCallback(),
|
||||
# UnfreezeModelCallback(wait_epochs=5),
|
||||
# ImagePredictionLoggerCallback(datamodule=datamodule),
|
||||
SaveCodeToWandbCallback(wandb_save_dir=logger.save_dir, lit_model=lit_model),
|
||||
# SaveOnnxModelToWandbCallback(datamodule=datamodule, save_dir=logger.save_dir)
|
||||
])
|
||||
|
||||
# Init trainer
|
||||
# Init PyTorch Lightning trainer ⚡
|
||||
trainer = pl.Trainer(
|
||||
# whether to use gpu and how many
|
||||
gpus=project_config["num_of_gpus"],
|
||||
|
||||
# experiment logging
|
||||
logger=logger,
|
||||
|
||||
# useful callbacks
|
||||
callbacks=callbacks,
|
||||
gpus=config["num_of_gpus"],
|
||||
|
||||
resume_from_checkpoint=config["resume_training"]["lightning_ckpt"]["ckpt_path"]
|
||||
if config["resume_training"]["lightning_ckpt"]["resume_from_ckpt"] else None,
|
||||
# resuming training from checkpoint
|
||||
resume_from_checkpoint=project_config["resume_training"]["lightning_ckpt"]["ckpt_path"]
|
||||
if project_config["resume_training"]["lightning_ckpt"]["resume_from_ckpt"] else None,
|
||||
|
||||
# model related:
|
||||
max_epochs=model_config["max_epochs"],
|
||||
accumulate_grad_batches=model_config["accumulate_grad_batches"],
|
||||
gradient_clip_val=model_config["gradient_clip_val"],
|
||||
# print related
|
||||
progress_bar_refresh_rate=project_config["printing"]["progress_bar_refresh_rate"],
|
||||
profiler=SimpleProfiler() if project_config["printing"]["profiler"] else None,
|
||||
weights_summary=project_config["printing"]["weights_summary"],
|
||||
|
||||
# print related:
|
||||
progress_bar_refresh_rate=config["printing"]["progress_bar_refresh_rate"],
|
||||
profiler=SimpleProfiler() if config["printing"]["profiler"] else None,
|
||||
weights_summary=config["printing"]["weights_summary"],
|
||||
# run related
|
||||
max_epochs=run_config["trainer"]["max_epochs"],
|
||||
min_epochs=run_config["trainer"].get("min_epochs", 1),
|
||||
accumulate_grad_batches=run_config["trainer"].get("accumulate_grad_batches", 1),
|
||||
gradient_clip_val=run_config["trainer"].get("gradient_clip_val", 0.5),
|
||||
|
||||
# these are mostly for debugging (read TIPS.md for explanation):
|
||||
fast_dev_run=False,
|
||||
num_sanity_val_steps=3,
|
||||
limit_train_batches=1.0,
|
||||
limit_val_batches=1.0,
|
||||
limit_test_batches=1.0,
|
||||
val_check_interval=1.0,
|
||||
# these are mostly for debugging (read TIPS.md for explanation)
|
||||
fast_dev_run=run_config["trainer"].get("fast_dev_run", False),
|
||||
limit_train_batches=run_config["trainer"].get("limit_train_batches", 1.0),
|
||||
limit_val_batches=run_config["trainer"].get("limit_val_batches", 1.0),
|
||||
limit_test_batches=run_config["trainer"].get("limit_test_batches", 1.0),
|
||||
val_check_interval=run_config["trainer"].get("val_check_interval", 1.0),
|
||||
num_sanity_val_steps=run_config["trainer"].get("fast_dev_run", 3),
|
||||
|
||||
# default log dir if no logger is found
|
||||
default_root_dir="logs/lightning_logs",
|
||||
)
|
||||
|
||||
# Test before training
|
||||
# Evaluate model on test set before training
|
||||
# trainer.test(model=lit_model, datamodule=datamodule)
|
||||
|
||||
# Save checkpoint
|
||||
# trainer.save_checkpoint("random.ckpt")
|
||||
|
||||
# Train the model ⚡
|
||||
# Train the model
|
||||
trainer.fit(model=lit_model, datamodule=datamodule)
|
||||
|
||||
# Evaluate model on test set
|
||||
# Evaluate model on test set after training
|
||||
trainer.test()
|
||||
|
||||
|
||||
def load_config():
|
||||
with open("config.yaml", "r") as ymlfile:
|
||||
def load_config(path):
|
||||
with open(path, "r") as ymlfile:
|
||||
config = yaml.load(ymlfile, Loader=yaml.FullLoader)
|
||||
return config
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
conf = load_config()
|
||||
|
||||
# --------------------------------------- CHOOSE YOUR MODEL HEREEE ↓↓↓ --------------------------------------- #
|
||||
# --------------------------------------- CHOOSE YOUR RUN CONFIG HEREE --------------------------------------- #
|
||||
# --------------------------------------------------- ↓↓↓ ---------------------------------------------------- #
|
||||
# ------------------------------------------------------------------------------------------------------------ #
|
||||
# RUN_CONFIG = "MNIST_CLASSIFIER_V1"
|
||||
# RUN_CONFIG = "CIFAR10_CLASSIFIER_V1"
|
||||
RUN_CONFIG = "CIFAR10_CLASSIFIER_V2"
|
||||
# ------------------------------------------------------------------------------------------------------------ #
|
||||
# --------------------------------------------------- ↑↑↑ ---------------------------------------------------- #
|
||||
# --------------------------------------- CHOOSE YOUR RUN CONFIG HEREE --------------------------------------- #
|
||||
|
||||
model_conf = conf["model_configs"]["simple_mnist_classifier_v1"]
|
||||
# model_conf = conf["model_configs"]["transfer_learning_cifar10_classifier_v1"]
|
||||
project_conf = load_config("project_config.yaml")
|
||||
run_conf = load_config("run_configs.yaml")[RUN_CONFIG]
|
||||
|
||||
# --------------------------------------- CHOOSE YOUR MODEL HEREEE ↑↑↑ --------------------------------------- #
|
||||
|
||||
train(config=conf, model_config=model_conf)
|
||||
train(project_config=project_conf, run_config=run_conf)
|
||||
Reference in new issue
Block a user