Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: merge output_path and filepath into output_dir
  • Loading branch information
ydcjeff committed Apr 7, 2021
commit edb5a20b834c7db3fe785e7e3e3c7d79f53433e6
8 changes: 4 additions & 4 deletions templates/_base/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_handlers(
Config object for setting up handlers

`config` has to contain
- `output_path`: output path to indicate where to_save objects are stored
- `output_dir`: output path to indicate where to_save objects are stored
- `save_every_iters`: saving iteration interval
- `n_saved`: number of best models to store
- `log_every_iters`: logging interval for iteration progress bar and `GpuInfo` if true
Expand Down Expand Up @@ -83,7 +83,7 @@ def get_handlers(
to_save=to_save,
lr_scheduler=lr_scheduler,
output_names=output_names,
output_path=config.output_path,
output_path=config.output_dir / 'checkpoints',
save_every_iters=config.save_every_iters,
n_saved=config.n_saved,
log_every_iters=config.log_every_iters,
Expand All @@ -98,7 +98,7 @@ def get_handlers(

# https://pytorch.org/ignite/contrib/engines.html#ignite.contrib.engines.common.save_best_model_by_val_score
best_model_handler = common.save_best_model_by_val_score(
output_path=config.output_path,
output_path=config.output_dir / 'checkpoints',
evaluator=eval_engine,
model=model,
metric_name=metric_name,
Expand Down Expand Up @@ -209,7 +209,7 @@ def get_logger(
)
{% elif logger_deps == 'tensorboard' %}
logger_handler = common.setup_tb_logging(
output_path=config.filepath,
output_path=config.output_dir,
trainer=train_engine,
optimizers=optimizers,
evaluators=eval_engine,
Expand Down
12 changes: 4 additions & 8 deletions templates/_base/_sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ def ignite_handlers_options(config):

def ignite_loggers_options(config):
st.markdown("## Ignite Loggers Options")
config["filepath"] = st.text_input(
"Logging file path (filepath)",
config["output_dir"] = st.text_input(
"Directory to save all outputs (output_dir)",
"./logs",
help="This option will be used by both python logging and ignite loggers if possible",
help="This option will be used by python logging, saving checkpoints, and ignite loggers if possible",
)
if st.checkbox("Use experiment tracking system ?", value=True):
config["logger_deps"] = st.selectbox(
"Select experiment eracking system",
"Select experiment tracking system",
["ClearML", "MLflow", "Neptune", "Polyaxon", "TensorBoard", "Visdom", "WandB"],
index=4,
).lower()
Expand All @@ -104,10 +104,6 @@ def ignite_loggers_options(config):


def _setup_common_training_handlers_options(config):
config["output_path"] = st.text_input(
"Output path to indicate where to_save objects are stored (output_path)",
value="./logs",
)
config["save_every_iters"] = st.number_input(
"Saving iteration interval (save_every_iters)", min_value=1, value=1000
)
Expand Down
21 changes: 9 additions & 12 deletions templates/gan/gan/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import warnings
from argparse import ArgumentParser
from datetime import datetime
from pathlib import Path
from typing import Any
from ignite.contrib.handlers.wandb_logger import WandBLogger
Expand Down Expand Up @@ -109,7 +110,7 @@ def run(local_rank: int, config: Any, *args: Any, **kwargs: Any):
@train_engine.on(Events.EPOCH_COMPLETED)
def save_fake_example(engine):
fake = netG(fixed_noise)
path = config.filepath / (FAKE_IMG_FNAME.format(engine.state.epoch))
path = config.output_dir / (FAKE_IMG_FNAME.format(engine.state.epoch))
vutils.save_image(fake.detach(), path, normalize=True)

# --------------------------------------------------
Expand All @@ -118,7 +119,7 @@ def save_fake_example(engine):
@train_engine.on(Events.EPOCH_COMPLETED)
def save_real_example(engine):
img, y = engine.state.batch
path = config.filepath / (REAL_IMG_FNAME.format(engine.state.epoch))
path = config.output_dir / (REAL_IMG_FNAME.format(engine.state.epoch))
vutils.save_image(img, path, normalize=True)

# -------------------------------------------------------------
Expand Down Expand Up @@ -147,11 +148,11 @@ def create_plots(engine):
warnings.warn("Loss plots will not be generated -- pandas or matplotlib not found")

else:
df = pd.read_csv(config.filepath / LOGS_FNAME, delimiter="\t", index_col="iteration")
df = pd.read_csv(config.output_dir / LOGS_FNAME, delimiter="\t", index_col="iteration")
_ = df.plot(subplots=True, figsize=(20, 20))
_ = plt.xlabel("Iteration number")
fig = plt.gcf()
path = config.filepath / PLOT_FNAME
path = config.output_dir / PLOT_FNAME

fig.savefig(path)

Expand Down Expand Up @@ -192,15 +193,11 @@ def main():
config = parser.parse_args()
manual_seed(config.seed)

if config.filepath:
path = Path(config.filepath)
if config.output_dir:
now = datetime.now().strftime("%Y%m%d-%H%M%S")
path = Path(config.output_dir, now)
path.mkdir(parents=True, exist_ok=True)
config.filepath = path

if config.output_path:
path = Path(config.output_path)
path.mkdir(parents=True, exist_ok=True)
config.output_path = path
config.output_dir = path

with idist.Parallel(
backend=config.backend,
Expand Down
9 changes: 3 additions & 6 deletions templates/gan/gan/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import hashlib
import logging
import shutil
from datetime import datetime
from logging import Logger
from pathlib import Path
from pprint import pformat
Expand All @@ -30,7 +29,7 @@
# TODO : PLEASE provide your custom model, optimizer, and loss function


def initialize(config: Optional[Any]) -> Tuple[Module, Optimizer, Module, Union[_LRScheduler, ParamScheduler]]:
def initialize(config: Optional[Any], num_channels: int) -> Tuple[Module, Optimizer, Module, Union[_LRScheduler, ParamScheduler]]:
"""Initializing model, optimizer, loss function, and lr scheduler
with correct settings.

Expand All @@ -46,7 +45,6 @@ def initialize(config: Optional[Any]) -> Tuple[Module, Optimizer, Module, Union[
netG = idist.auto_model(Generator(config.z_dim, config.g_filters, num_channels))
netD = idist.auto_model(Discriminator(num_channels, config.d_filters))
loss_fn = nn.BCELoss()
model = idist.auto_model(model)
optimizerG = optim.Adam(netG.parameters(), lr=config.lr, betas=(config.beta_1, 0.999))
optimizerD = optim.Adam(netD.parameters(), lr=config.lr, betas=(config.beta_1, 0.999))
loss_fn = loss_fn.to(idist.device())
Expand Down Expand Up @@ -111,18 +109,17 @@ def setup_logging(config: Any) -> Logger:
----------
config
config object. config has to contain
`verbose` and `filepath` attributes.
`verbose` and `output_dir` attributes.

Returns
-------
logger
an instance of `Logger`
"""
now = datetime.now().strftime("%Y%m%d-%X")
logger = setup_logger(
level=logging.INFO if config.verbose else logging.WARNING,
format="%(message)s",
filepath=config.filepath / f"{now}.log",
filepath=config.output_dir / "training-info.log",
)
return logger

Expand Down
6 changes: 4 additions & 2 deletions templates/gan/tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from argparse import Namespace
from pathlib import Path
from tempfile import TemporaryDirectory

from ignite.contrib.handlers import (
Expand All @@ -26,8 +27,9 @@ class TestHandlers(unittest.TestCase):
def test_get_handlers(self):
train_engine = Engine(lambda e, b: b)
with TemporaryDirectory() as tmp:
tmp = Path(tmp)
config = Namespace(
output_path=tmp,
output_dir=tmp,
save_every_iters=1,
n_saved=2,
log_every_iters=1,
Expand All @@ -53,7 +55,7 @@ def test_get_handlers(self):

def test_get_logger(self):
with TemporaryDirectory() as tmp:
config = Namespace(filepath=tmp, logger_log_every_iters=1)
config = Namespace(output_dir=tmp, logger_log_every_iters=1)
train_engine = Engine(lambda e, b: b)
optimizer = optim.Adam(nn.Linear(1, 1).parameters())
logger_handler = get_logger(
Expand Down
2 changes: 1 addition & 1 deletion templates/gan/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_log_metrics(self):
def test_setup_logging(self):
with TemporaryDirectory() as tmp:
tmp = Path(tmp)
config = Namespace(verbose=True, filepath=tmp)
config = Namespace(verbose=True, output_dir=tmp)
logger = setup_logging(config)
self.assertEqual(logger.level, logging.INFO)
self.assertIsInstance(logger, logging.Logger)
Expand Down
5 changes: 2 additions & 3 deletions templates/image_classification/image_classification/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,17 @@ def setup_logging(config: Any) -> Logger:
----------
config
config object. config has to contain
`verbose` and `filepath` attributes.
`verbose` and `output_dir` attributes.

Returns
-------
logger
an instance of `Logger`
"""
now = datetime.now().strftime("%Y%m%d-%X")
logger = setup_logger(
level=logging.INFO if config.verbose else logging.WARNING,
format="%(message)s",
filepath=config.filepath / f"{now}.log",
filepath=config.output_dir / "training-info.log",
)
return logger

Expand Down
6 changes: 4 additions & 2 deletions templates/image_classification/tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from argparse import Namespace
from pathlib import Path
from tempfile import TemporaryDirectory

from ignite.contrib.handlers import (
Expand All @@ -26,8 +27,9 @@ class TestHandlers(unittest.TestCase):
def test_get_handlers(self):
train_engine = Engine(lambda e, b: b)
with TemporaryDirectory() as tmp:
tmp = Path(tmp)
config = Namespace(
output_path=tmp,
output_dir=tmp,
save_every_iters=1,
n_saved=2,
log_every_iters=1,
Expand All @@ -53,7 +55,7 @@ def test_get_handlers(self):

def test_get_logger(self):
with TemporaryDirectory() as tmp:
config = Namespace(filepath=tmp, logger_log_every_iters=1)
config = Namespace(output_dir=tmp, logger_log_every_iters=1)
train_engine = Engine(lambda e, b: b)
optimizer = optim.Adam(nn.Linear(1, 1).parameters())
logger_handler = get_logger(
Expand Down
2 changes: 1 addition & 1 deletion templates/image_classification/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_log_metrics(self):
def test_setup_logging(self):
with TemporaryDirectory() as tmp:
tmp = Path(tmp)
config = Namespace(verbose=True, filepath=tmp)
config = Namespace(verbose=True, output_dir=tmp)
logger = setup_logging(config)
self.assertEqual(logger.level, logging.INFO)
self.assertIsInstance(logger, logging.Logger)
Expand Down
5 changes: 2 additions & 3 deletions templates/single/single/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,17 @@ def setup_logging(config: Any) -> Logger:
----------
config
config object. config has to contain
`verbose` and `filepath` attributes.
`verbose` and `output_dir` attributes.

Returns
-------
logger
an instance of `Logger`
"""
now = datetime.now().strftime("%Y%m%d-%X")
logger = setup_logger(
level=logging.INFO if config.verbose else logging.WARNING,
format="%(message)s",
filepath=config.filepath / f"{now}.log",
filepath=config.output_dir / "training-info.log",
)
return logger

Expand Down
6 changes: 4 additions & 2 deletions templates/single/tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from argparse import Namespace
from pathlib import Path
from tempfile import TemporaryDirectory

from ignite.contrib.handlers import (
Expand All @@ -26,8 +27,9 @@ class TestHandlers(unittest.TestCase):
def test_get_handlers(self):
train_engine = Engine(lambda e, b: b)
with TemporaryDirectory() as tmp:
tmp = Path(tmp)
config = Namespace(
output_path=tmp,
output_dir=tmp,
save_every_iters=1,
n_saved=2,
log_every_iters=1,
Expand All @@ -53,7 +55,7 @@ def test_get_handlers(self):

def test_get_logger(self):
with TemporaryDirectory() as tmp:
config = Namespace(filepath=tmp, logger_log_every_iters=1)
config = Namespace(output_dir=tmp, logger_log_every_iters=1)
train_engine = Engine(lambda e, b: b)
optimizer = optim.Adam(nn.Linear(1, 1).parameters())
logger_handler = get_logger(
Expand Down
2 changes: 1 addition & 1 deletion templates/single/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_log_metrics(self):
def test_setup_logging(self):
with TemporaryDirectory() as tmp:
tmp = Path(tmp)
config = Namespace(verbose=True, filepath=tmp)
config = Namespace(verbose=True, output_dir=tmp)
logger = setup_logging(config)
self.assertEqual(logger.level, logging.INFO)
self.assertIsInstance(logger, logging.Logger)
Expand Down