From 8e73cfd34e1702dd717e4d93fd781eaaf954411e Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 7 Feb 2023 00:02:45 +0900 Subject: [PATCH 01/40] Update engine.py to resolve #1992 Update engine.py to resolve #1992 --- ignite/engine/engine.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index a013fda29b1..f60a41637c1 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -125,6 +125,10 @@ def compute_mean_std(engine, batch): _state_dict_all_req_keys = ("epoch_length", "max_epochs") _state_dict_one_of_opt_keys = ("iteration", "epoch") + DEBUG_EVENTS = 1 + DEBUG_OUTPUT = 2 + DEBUG_GRADS = 3 + # Flag to disable engine._internal_run as generator feature for BC interrupt_resume_enabled = True @@ -425,6 +429,16 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> first, others = ((args[0],), args[1:]) if (args and args[0] == self) else ((), args) func(*first, *(event_args + others), **kwargs) + + def debug(self, level: int = 0, **kwargs): + if level > 2 : + self.logger.debug(f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}, Gradients : {kwargs['loss'].grad}") + elif level > 1 : + self.logger.debug(f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}") + elif level > 0 : + self.logger.debug(f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}") + + def fire_event(self, event_name: Any) -> None: """Execute all the handlers associated with given event. From b840aa20a596887eb9ac69a870bf78564051611f Mon Sep 17 00:00:00 2001 From: puhuk Date: Mon, 6 Feb 2023 15:03:46 +0000 Subject: [PATCH 02/40] autopep8 fix --- ignite/engine/engine.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index f60a41637c1..08ac903a7a4 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -429,15 +429,19 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> first, others = ((args[0],), args[1:]) if (args and args[0] == self) else ((), args) func(*first, *(event_args + others), **kwargs) - def debug(self, level: int = 0, **kwargs): - if level > 2 : - self.logger.debug(f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}, Gradients : {kwargs['loss'].grad}") - elif level > 1 : - self.logger.debug(f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}") - elif level > 0 : - self.logger.debug(f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}") - + if level > 2: + self.logger.debug( + f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}, Gradients : {kwargs['loss'].grad}" + ) + elif level > 1: + self.logger.debug( + f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}" + ) + elif level > 0: + self.logger.debug( + f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}" + ) def fire_event(self, event_name: Any) -> None: """Execute all the handlers associated with given event. From a1ee3ad9d9e47c338d86a414fa72a9f39d08eb1a Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 7 Feb 2023 00:15:22 +0900 Subject: [PATCH 03/40] Update engine.py --- ignite/engine/engine.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index f60a41637c1..85d285d6bda 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -434,9 +434,13 @@ def debug(self, level: int = 0, **kwargs): if level > 2 : self.logger.debug(f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}, Gradients : {kwargs['loss'].grad}") elif level > 1 : - self.logger.debug(f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}") + self.logger.debug( + f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}" + ) elif level > 0 : - self.logger.debug(f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}") + self.logger.debug( + f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}" + ) def fire_event(self, event_name: Any) -> None: From 32850f364d11abd691e0a2d3774e1cfb3c0b793a Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 7 Feb 2023 16:19:35 +0900 Subject: [PATCH 04/40] Correct file and test code --- ignite/engine/engine.py | 26 ++++-- test_debug.py | 202 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+), 6 deletions(-) create mode 100644 test_debug.py diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 08ac903a7a4..097c2f96920 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -147,6 +147,8 @@ def __init__(self, process_function: Callable[["Engine", Any], Any]): self._dataloader_iter: Optional[Iterator[Any]] = None self._init_iter: Optional[int] = None + self.debug_level = 0 + self.register_events(*Events) if self._process_function is None: @@ -430,17 +432,29 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> func(*first, *(event_args + others), **kwargs) def debug(self, level: int = 0, **kwargs): - if level > 2: + self.level = level + if self.level > 2: + print( + f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, Loss : {self.state.output}, \ + LR : {self.state.output}, Gradients : {self.state.output}" + ) self.logger.debug( - f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}, Gradients : {kwargs['loss'].grad}" + f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, Loss : {self.state.output}, \ + LR : {self.state.output}, Gradients : {self.state.output}" + ) + elif self.level > 1: + print( + f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, Loss : {self.state.output}, \ + LR : {self.state.output}" ) - elif level > 1: self.logger.debug( - f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {kwargs['event_name']}, Loss : {self.state.output}, LR : {kwargs['optimizer'].param_groups[0]['lr']}" + f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, Loss : {self.state.output}, \ + LR : {self.state.output}" ) - elif level > 0: + elif self.level > 0: + print(f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}") self.logger.debug( - f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {kwargs['event_name']}" + f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}" ) def fire_event(self, event_name: Any) -> None: diff --git a/test_debug.py b/test_debug.py new file mode 100644 index 00000000000..5ddc4dbdbc5 --- /dev/null +++ b/test_debug.py @@ -0,0 +1,202 @@ +import torch +from torch import nn +from torch.utils.data import DataLoader +from torchvision.datasets import MNIST +from torchvision.models import resnet18 +from torchvision.transforms import Compose, Normalize, ToTensor + +from ignite.engine import Engine, Events, create_supervised_trainer, create_supervised_evaluator +from ignite.metrics import Accuracy, Loss +from ignite.handlers import ModelCheckpoint +from ignite.contrib.handlers import TensorboardLogger, global_step_from_engine + +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") + +class Net(nn.Module): + + def __init__(self): + super(Net, self).__init__() + + # Changed the output layer to output 10 classes instead of 1000 classes + self.model = resnet18(num_classes=10) + + # Changed the input layer to take grayscale images for MNIST instaed of RGB images + self.model.conv1 = nn.Conv2d( + 1, 64, kernel_size=3, padding=1, bias=False + ) + + def forward(self, x): + return self.model(x) + + +model = Net().to(device) + +data_transform = Compose([ToTensor(), Normalize((0.1307,), (0.3081,))]) + +train_loader = DataLoader( + MNIST(download=True, root=".", transform=data_transform, train=True), batch_size=128, shuffle=True +) + +val_loader = DataLoader( + MNIST(download=True, root=".", transform=data_transform, train=False), batch_size=256, shuffle=False +) + +optimizer = torch.optim.RMSprop(model.parameters(), lr=0.005) +criterion = nn.CrossEntropyLoss() + +trainer = create_supervised_trainer(model, optimizer, criterion, device) + +val_metrics = { + "accuracy": Accuracy(), + "loss": Loss(criterion) +} + +train_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) +val_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) + +trainer = create_supervised_trainer(model, optimizer, criterion, device) + +val_metrics = { + "accuracy": Accuracy(), + "loss": Loss(criterion) +} + +train_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) +val_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) + +def train_step(engine, batch): + model.train() + optimizer.zero_grad() + x, y = batch[0].to(device), batch[1].to(device) + y_pred = model(x) + loss = criterion(y_pred, y) + loss.backward() + optimizer.step() + return loss.item() + +trainer = Engine(train_step) + +def validation_step(engine, batch): + model.eval() + with torch.no_grad(): + x, y = batch[0].to(device), batch[1].to(device) + y_pred = model(x) + return y_pred, y + +train_evaluator = Engine(validation_step) +val_evaluator = Engine(validation_step) + +# Attach metrics to the evaluators +for name, metric in val_metrics.items(): + metric.attach(train_evaluator, name) + +for name, metric in val_metrics.items(): + metric.attach(val_evaluator, name) + +def train_step(engine, batch): + model.train() + optimizer.zero_grad() + x, y = batch[0].to(device), batch[1].to(device) + y_pred = model(x) + loss = criterion(y_pred, y) + loss.backward() + optimizer.step() + return loss.item() + +trainer = Engine(train_step) + +def validation_step(engine, batch): + model.eval() + with torch.no_grad(): + x, y = batch[0].to(device), batch[1].to(device) + y_pred = model(x) + return y_pred, y + +train_evaluator = Engine(validation_step) +val_evaluator = Engine(validation_step) + +# Attach metrics to the evaluators +for name, metric in val_metrics.items(): + metric.attach(train_evaluator, name) + +for name, metric in val_metrics.items(): + metric.attach(val_evaluator, name) + +# How many batches to wait before logging training status +log_interval = 100 + +@trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) +def trigger_custom_event(): + trainer.debug(level=Engine.DEBUG_OUTPUT) + +# Score function to return current value of any metric we defined above in val_metrics +def score_function(engine): + return engine.state.metrics["accuracy"] + +# Checkpoint to store n_saved best models wrt score function +model_checkpoint = ModelCheckpoint( + "checkpoint", + n_saved=2, + filename_prefix="best", + score_function=score_function, + score_name="accuracy", + global_step_transform=global_step_from_engine(trainer), # helps fetch the trainer's state, + require_empty=False +) + +# Save the model after every epoch of val_evaluator is completed +val_evaluator.add_event_handler(Events.COMPLETED, model_checkpoint, {"model": model}) + +# Define a Tensorboard logger +tb_logger = TensorboardLogger(log_dir="tb-logger") + +# Attach handler to plot trainer's loss every 100 iterations +tb_logger.attach_output_handler( + trainer, + event_name=Events.ITERATION_COMPLETED(every=100), + tag="training", + output_transform=lambda loss: {"batch_loss": loss}, +) + +# Attach handler for plotting both evaluators' metrics after every epoch completes +for tag, evaluator in [("training", train_evaluator), ("validation", val_evaluator)]: + tb_logger.attach_output_handler( + evaluator, + event_name=Events.EPOCH_COMPLETED, + tag=tag, + metric_names="all", + global_step_transform=global_step_from_engine(trainer), + ) + +trainer.run(train_loader, max_epochs=2) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 7cea85959e4f67cb550fbbed37ebb5d12326dbdd Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 7 Feb 2023 07:20:50 +0000 Subject: [PATCH 05/40] autopep8 fix --- test_debug.py | 73 ++++++++++++++++----------------------------------- 1 file changed, 23 insertions(+), 50 deletions(-) diff --git a/test_debug.py b/test_debug.py index 5ddc4dbdbc5..a3c08e0eca4 100644 --- a/test_debug.py +++ b/test_debug.py @@ -5,25 +5,24 @@ from torchvision.models import resnet18 from torchvision.transforms import Compose, Normalize, ToTensor -from ignite.engine import Engine, Events, create_supervised_trainer, create_supervised_evaluator -from ignite.metrics import Accuracy, Loss +from ignite.contrib.handlers import global_step_from_engine, TensorboardLogger + +from ignite.engine import create_supervised_evaluator, create_supervised_trainer, Engine, Events from ignite.handlers import ModelCheckpoint -from ignite.contrib.handlers import TensorboardLogger, global_step_from_engine +from ignite.metrics import Accuracy, Loss device = torch.device("cuda" if torch.cuda.is_available() else "cpu") -class Net(nn.Module): +class Net(nn.Module): def __init__(self): super(Net, self).__init__() - + # Changed the output layer to output 10 classes instead of 1000 classes self.model = resnet18(num_classes=10) # Changed the input layer to take grayscale images for MNIST instaed of RGB images - self.model.conv1 = nn.Conv2d( - 1, 64, kernel_size=3, padding=1, bias=False - ) + self.model.conv1 = nn.Conv2d(1, 64, kernel_size=3, padding=1, bias=False) def forward(self, x): return self.model(x) @@ -46,24 +45,19 @@ def forward(self, x): trainer = create_supervised_trainer(model, optimizer, criterion, device) -val_metrics = { - "accuracy": Accuracy(), - "loss": Loss(criterion) -} +val_metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)} train_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) val_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) trainer = create_supervised_trainer(model, optimizer, criterion, device) -val_metrics = { - "accuracy": Accuracy(), - "loss": Loss(criterion) -} +val_metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)} train_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) val_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) + def train_step(engine, batch): model.train() optimizer.zero_grad() @@ -74,8 +68,10 @@ def train_step(engine, batch): optimizer.step() return loss.item() + trainer = Engine(train_step) + def validation_step(engine, batch): model.eval() with torch.no_grad(): @@ -83,6 +79,7 @@ def validation_step(engine, batch): y_pred = model(x) return y_pred, y + train_evaluator = Engine(validation_step) val_evaluator = Engine(validation_step) @@ -93,6 +90,7 @@ def validation_step(engine, batch): for name, metric in val_metrics.items(): metric.attach(val_evaluator, name) + def train_step(engine, batch): model.train() optimizer.zero_grad() @@ -103,8 +101,10 @@ def train_step(engine, batch): optimizer.step() return loss.item() + trainer = Engine(train_step) + def validation_step(engine, batch): model.eval() with torch.no_grad(): @@ -112,6 +112,7 @@ def validation_step(engine, batch): y_pred = model(x) return y_pred, y + train_evaluator = Engine(validation_step) val_evaluator = Engine(validation_step) @@ -125,14 +126,17 @@ def validation_step(engine, batch): # How many batches to wait before logging training status log_interval = 100 + @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def trigger_custom_event(): trainer.debug(level=Engine.DEBUG_OUTPUT) + # Score function to return current value of any metric we defined above in val_metrics def score_function(engine): return engine.state.metrics["accuracy"] + # Checkpoint to store n_saved best models wrt score function model_checkpoint = ModelCheckpoint( "checkpoint", @@ -140,10 +144,10 @@ def score_function(engine): filename_prefix="best", score_function=score_function, score_name="accuracy", - global_step_transform=global_step_from_engine(trainer), # helps fetch the trainer's state, - require_empty=False + global_step_transform=global_step_from_engine(trainer), # helps fetch the trainer's state, + require_empty=False, ) - + # Save the model after every epoch of val_evaluator is completed val_evaluator.add_event_handler(Events.COMPLETED, model_checkpoint, {"model": model}) @@ -169,34 +173,3 @@ def score_function(engine): ) trainer.run(train_loader, max_epochs=2) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 36ecbb9911b8aacd0f41edbde1ef4ec0874173ad Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 14 Feb 2023 01:24:07 +0900 Subject: [PATCH 06/40] Update test_engine.py --- tests/ignite/engine/test_engine.py | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index 994eb49f72b..da31d8862d1 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1388,3 +1388,49 @@ def check_iter_epoch(): state = engine.run(data, max_epochs=max_epochs) assert state.iteration == max_epochs * len(data) and state.epoch == max_epochs assert num_calls_check_iter_epoch == 1 + + +def test_engine_debug(): + + from torch import nn + from torch.utils.data import DataLoader + from torchvision.models import resnet18 + + class Net(nn.Module): + def __init__(self): + super(Net, self).__init__() + + self.model = resnet18(num_classes=10) + + self.model.conv1 = nn.Conv2d(1, 64, kernel_size=3, padding=1, bias=False) + + def forward(self, x): + return self.model(x) + + def _test(sampler_type=None): + model = Net() + + from torchvision.datasets import MNIST + + train_loader = DataLoader(MNIST(download=True, root=".", train=True), batch_size=128, shuffle=True) + + optimizer = torch.optim.RMSprop(model.parameters(), lr=0.005) + criterion = nn.CrossEntropyLoss() + + def train_step(engine, batch): + model.train() + optimizer.zero_grad() + x, y = batch[0], batch[1] + y_pred = model(x) + loss = criterion(y_pred, y) + loss.backward() + optimizer.step() + return loss.item() + + trainer = Engine(train_step) + + @trainer.on(Events.ITERATION_COMPLETED(every=100)) + def trigger_custom_event(): + trainer.debug(level=Engine.DEBUG_OUTPUT) + + _test() From 4448d5284b77c89c3eb99a2ea0f67f0bc3332101 Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 14 Feb 2023 01:26:08 +0900 Subject: [PATCH 07/40] Delete test_debug.py --- test_debug.py | 175 -------------------------------------------------- 1 file changed, 175 deletions(-) delete mode 100644 test_debug.py diff --git a/test_debug.py b/test_debug.py deleted file mode 100644 index a3c08e0eca4..00000000000 --- a/test_debug.py +++ /dev/null @@ -1,175 +0,0 @@ -import torch -from torch import nn -from torch.utils.data import DataLoader -from torchvision.datasets import MNIST -from torchvision.models import resnet18 -from torchvision.transforms import Compose, Normalize, ToTensor - -from ignite.contrib.handlers import global_step_from_engine, TensorboardLogger - -from ignite.engine import create_supervised_evaluator, create_supervised_trainer, Engine, Events -from ignite.handlers import ModelCheckpoint -from ignite.metrics import Accuracy, Loss - -device = torch.device("cuda" if torch.cuda.is_available() else "cpu") - - -class Net(nn.Module): - def __init__(self): - super(Net, self).__init__() - - # Changed the output layer to output 10 classes instead of 1000 classes - self.model = resnet18(num_classes=10) - - # Changed the input layer to take grayscale images for MNIST instaed of RGB images - self.model.conv1 = nn.Conv2d(1, 64, kernel_size=3, padding=1, bias=False) - - def forward(self, x): - return self.model(x) - - -model = Net().to(device) - -data_transform = Compose([ToTensor(), Normalize((0.1307,), (0.3081,))]) - -train_loader = DataLoader( - MNIST(download=True, root=".", transform=data_transform, train=True), batch_size=128, shuffle=True -) - -val_loader = DataLoader( - MNIST(download=True, root=".", transform=data_transform, train=False), batch_size=256, shuffle=False -) - -optimizer = torch.optim.RMSprop(model.parameters(), lr=0.005) -criterion = nn.CrossEntropyLoss() - -trainer = create_supervised_trainer(model, optimizer, criterion, device) - -val_metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)} - -train_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) -val_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) - -trainer = create_supervised_trainer(model, optimizer, criterion, device) - -val_metrics = {"accuracy": Accuracy(), "loss": Loss(criterion)} - -train_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) -val_evaluator = create_supervised_evaluator(model, metrics=val_metrics, device=device) - - -def train_step(engine, batch): - model.train() - optimizer.zero_grad() - x, y = batch[0].to(device), batch[1].to(device) - y_pred = model(x) - loss = criterion(y_pred, y) - loss.backward() - optimizer.step() - return loss.item() - - -trainer = Engine(train_step) - - -def validation_step(engine, batch): - model.eval() - with torch.no_grad(): - x, y = batch[0].to(device), batch[1].to(device) - y_pred = model(x) - return y_pred, y - - -train_evaluator = Engine(validation_step) -val_evaluator = Engine(validation_step) - -# Attach metrics to the evaluators -for name, metric in val_metrics.items(): - metric.attach(train_evaluator, name) - -for name, metric in val_metrics.items(): - metric.attach(val_evaluator, name) - - -def train_step(engine, batch): - model.train() - optimizer.zero_grad() - x, y = batch[0].to(device), batch[1].to(device) - y_pred = model(x) - loss = criterion(y_pred, y) - loss.backward() - optimizer.step() - return loss.item() - - -trainer = Engine(train_step) - - -def validation_step(engine, batch): - model.eval() - with torch.no_grad(): - x, y = batch[0].to(device), batch[1].to(device) - y_pred = model(x) - return y_pred, y - - -train_evaluator = Engine(validation_step) -val_evaluator = Engine(validation_step) - -# Attach metrics to the evaluators -for name, metric in val_metrics.items(): - metric.attach(train_evaluator, name) - -for name, metric in val_metrics.items(): - metric.attach(val_evaluator, name) - -# How many batches to wait before logging training status -log_interval = 100 - - -@trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) -def trigger_custom_event(): - trainer.debug(level=Engine.DEBUG_OUTPUT) - - -# Score function to return current value of any metric we defined above in val_metrics -def score_function(engine): - return engine.state.metrics["accuracy"] - - -# Checkpoint to store n_saved best models wrt score function -model_checkpoint = ModelCheckpoint( - "checkpoint", - n_saved=2, - filename_prefix="best", - score_function=score_function, - score_name="accuracy", - global_step_transform=global_step_from_engine(trainer), # helps fetch the trainer's state, - require_empty=False, -) - -# Save the model after every epoch of val_evaluator is completed -val_evaluator.add_event_handler(Events.COMPLETED, model_checkpoint, {"model": model}) - -# Define a Tensorboard logger -tb_logger = TensorboardLogger(log_dir="tb-logger") - -# Attach handler to plot trainer's loss every 100 iterations -tb_logger.attach_output_handler( - trainer, - event_name=Events.ITERATION_COMPLETED(every=100), - tag="training", - output_transform=lambda loss: {"batch_loss": loss}, -) - -# Attach handler for plotting both evaluators' metrics after every epoch completes -for tag, evaluator in [("training", train_evaluator), ("validation", val_evaluator)]: - tb_logger.attach_output_handler( - evaluator, - event_name=Events.EPOCH_COMPLETED, - tag=tag, - metric_names="all", - global_step_transform=global_step_from_engine(trainer), - ) - -trainer.run(train_loader, max_epochs=2) From 2a630db19a361b7a854242337f6af213eb3d6bff Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 14 Feb 2023 08:05:44 +0900 Subject: [PATCH 08/40] Update --- ignite/engine/engine.py | 16 ++++++++-------- tests/ignite/engine/test_engine.py | 8 +++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 097c2f96920..523853ee944 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -435,21 +435,21 @@ def debug(self, level: int = 0, **kwargs): self.level = level if self.level > 2: print( - f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, Loss : {self.state.output}, \ - LR : {self.state.output}, Gradients : {self.state.output}" + f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ + Loss : {self.state.output}, LR : {self.state.output}, Gradients : {self.state.output}" ) self.logger.debug( - f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, Loss : {self.state.output}, \ - LR : {self.state.output}, Gradients : {self.state.output}" + f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ + Loss : {self.state.output}, LR : {self.state.output}, Gradients : {self.state.output}" ) elif self.level > 1: print( - f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, Loss : {self.state.output}, \ - LR : {self.state.output}" + f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, \ + Loss : {self.state.output}, LR : {self.state.output}" ) self.logger.debug( - f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, Loss : {self.state.output}, \ - LR : {self.state.output}" + f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, \ + Loss : {self.state.output}, LR : {self.state.output}" ) elif self.level > 0: print(f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}") diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index da31d8862d1..6b5f44dcd15 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1407,7 +1407,7 @@ def __init__(self): def forward(self, x): return self.model(x) - def _test(sampler_type=None): + def _test(level=Engine.DEBUG_EVENTS): model = Net() from torchvision.datasets import MNIST @@ -1431,6 +1431,8 @@ def train_step(engine, batch): @trainer.on(Events.ITERATION_COMPLETED(every=100)) def trigger_custom_event(): - trainer.debug(level=Engine.DEBUG_OUTPUT) + trainer.debug(level=level) - _test() + _test(Engine.DEBUG_EVENTS) + _test(Engine.DEBUG_OUTPUT) + _test(Engine.DEBUG_GRADS) From f99fd748ec10df75ef75f1fc7389634857564ddf Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 14 Feb 2023 08:16:45 +0900 Subject: [PATCH 09/40] Update engine.py --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 523853ee944..8ec4ef49c14 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -431,7 +431,7 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> first, others = ((args[0],), args[1:]) if (args and args[0] == self) else ((), args) func(*first, *(event_args + others), **kwargs) - def debug(self, level: int = 0, **kwargs): + def debug(self, level: int = 0, **kwargs) -> None: self.level = level if self.level > 2: print( From a36842fe358880892a04a9ce76335b9dc5dfcaca Mon Sep 17 00:00:00 2001 From: puhuk Date: Tue, 14 Feb 2023 08:24:54 +0900 Subject: [PATCH 10/40] Update engine.py --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 8ec4ef49c14..028befcdfba 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -431,7 +431,7 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> first, others = ((args[0],), args[1:]) if (args and args[0] == self) else ((), args) func(*first, *(event_args + others), **kwargs) - def debug(self, level: int = 0, **kwargs) -> None: + def debug(self, level: int = 0, **kwargs: Any) -> None: self.level = level if self.level > 2: print( From ab7d751fca3322fc2f397cee4e965feff46c5adc Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 17 Feb 2023 01:25:14 +0900 Subject: [PATCH 11/40] update --- ignite/engine/engine.py | 18 ++++---- tests/ignite/engine/test_engine.py | 66 +++++++++++++++++------------- 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 028befcdfba..cedf6b34930 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -433,30 +433,26 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> def debug(self, level: int = 0, **kwargs: Any) -> None: self.level = level + if self.level > 2: - print( - f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ - Loss : {self.state.output}, LR : {self.state.output}, Gradients : {self.state.output}" - ) + self.lr = kwargs["optimizer"].param_groups[0]["lr"] + self.layer = kwargs["layer"] self.logger.debug( f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ - Loss : {self.state.output}, LR : {self.state.output}, Gradients : {self.state.output}" + Loss : {self.state.output}, LR : {self.lr}, Gradients : {self.layer.weight.grad}" ) elif self.level > 1: - print( - f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, \ - Loss : {self.state.output}, LR : {self.state.output}" - ) + self.lr = kwargs["optimizer"].param_groups[0]["lr"] self.logger.debug( f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, \ - Loss : {self.state.output}, LR : {self.state.output}" + Loss : {self.state.output}, LR : {self.lr}" ) elif self.level > 0: - print(f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}") self.logger.debug( f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}" ) + def fire_event(self, event_name: Any) -> None: """Execute all the handlers associated with given event. diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index 6b5f44dcd15..880758fbb66 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1391,48 +1391,58 @@ def check_iter_epoch(): def test_engine_debug(): - from torch import nn from torch.utils.data import DataLoader - from torchvision.models import resnet18 class Net(nn.Module): def __init__(self): super(Net, self).__init__() - - self.model = resnet18(num_classes=10) - - self.model.conv1 = nn.Conv2d(1, 64, kernel_size=3, padding=1, bias=False) + self.conv1 = nn.Conv2d(1, 10, kernel_size=5) + self.conv2 = nn.Conv2d(10, 20, kernel_size=5) + self.conv2_drop = nn.Dropout2d() + self.fc1 = nn.Linear(320, 50) + self.fc2 = nn.Linear(50, 10) def forward(self, x): - return self.model(x) + x = F.relu(F.max_pool2d(self.conv1(x), 2)) + x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) + x = x.view(-1, 320) + x = F.relu(self.fc1(x)) + x = F.dropout(x, training=self.training) + x = self.fc2(x) + return F.log_softmax(x, dim=-1) + + def _test(): + train_loader = DataLoader( + MNIST(download=True, root=".", transform=Compose([ToTensor()]), train=True), + batch_size=train_batch_size, + shuffle=True, + ) - def _test(level=Engine.DEBUG_EVENTS): model = Net() + device = "cpu" - from torchvision.datasets import MNIST + if torch.cuda.is_available(): + device = "cuda" - train_loader = DataLoader(MNIST(download=True, root=".", train=True), batch_size=128, shuffle=True) + model.to(device) # Move model before creating optimizer + optimizer = SGD(model.parameters(), lr=lr, momentum=momentum) + criterion = nn.NLLLoss() + trainer = create_supervised_trainer(model, optimizer, criterion, device=device) + trainer.logger = setup_logger("trainer") - optimizer = torch.optim.RMSprop(model.parameters(), lr=0.005) - criterion = nn.CrossEntropyLoss() + @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) + def log_training_debug_events(engine): + trainer.debug(level=DEBUG_EVENTS) - def train_step(engine, batch): - model.train() - optimizer.zero_grad() - x, y = batch[0], batch[1] - y_pred = model(x) - loss = criterion(y_pred, y) - loss.backward() - optimizer.step() - return loss.item() + @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) + def log_training_debug_outputs(engine): + trainer.debug(level=DEBUG_OUTPUT, optimizer=optimizer) - trainer = Engine(train_step) + @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) + def log_training_debug_grads(engine): + trainer.debug(level=DEBUG_GRADS, optimizer=optimizer, layer=model.fc2) - @trainer.on(Events.ITERATION_COMPLETED(every=100)) - def trigger_custom_event(): - trainer.debug(level=level) + trainer.run(train_loader, max_epochs=epochs) - _test(Engine.DEBUG_EVENTS) - _test(Engine.DEBUG_OUTPUT) - _test(Engine.DEBUG_GRADS) + _test() From e7754e39506db58960a77764e29711e73490cd03 Mon Sep 17 00:00:00 2001 From: puhuk Date: Thu, 16 Feb 2023 16:26:24 +0000 Subject: [PATCH 12/40] autopep8 fix --- ignite/engine/engine.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index cedf6b34930..b45c44828f9 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -452,7 +452,6 @@ def debug(self, level: int = 0, **kwargs: Any) -> None: f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}" ) - def fire_event(self, event_name: Any) -> None: """Execute all the handlers associated with given event. From d685df52fe2b50db7f2559a30f095c284cf55c8d Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 17 Feb 2023 10:49:01 +0900 Subject: [PATCH 13/40] Update engine.py --- ignite/engine/engine.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index cedf6b34930..b45c44828f9 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -452,7 +452,6 @@ def debug(self, level: int = 0, **kwargs: Any) -> None: f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}" ) - def fire_event(self, event_name: Any) -> None: """Execute all the handlers associated with given event. From 44d39aa73c82a9883e294fc282d4ab2fa3024c52 Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 17 Feb 2023 10:56:47 +0900 Subject: [PATCH 14/40] Update test_engine.py --- tests/ignite/engine/test_engine.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index 880758fbb66..7c7b6d78c36 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1391,8 +1391,19 @@ def check_iter_epoch(): def test_engine_debug(): + import torch.nn.functional as F from torch import nn + from torch.optim import SGD from torch.utils.data import DataLoader + from torchvision.datasets import MNIST + from torchvision.transforms import Compose, ToTensor + + from ignite.engine import create_supervised_trainer + from ignite.utils import setup_logger + + DEBUG_EVENTS = 1 + DEBUG_OUTPUT = 2 + DEBUG_GRADS = 3 class Net(nn.Module): def __init__(self): @@ -1415,18 +1426,20 @@ def forward(self, x): def _test(): train_loader = DataLoader( MNIST(download=True, root=".", transform=Compose([ToTensor()]), train=True), - batch_size=train_batch_size, + batch_size=64, shuffle=True, ) model = Net() device = "cpu" + log_interval = 10 + epochs = 10 if torch.cuda.is_available(): device = "cuda" model.to(device) # Move model before creating optimizer - optimizer = SGD(model.parameters(), lr=lr, momentum=momentum) + optimizer = SGD(model.parameters(), lr=0.01, momentum=0.5) criterion = nn.NLLLoss() trainer = create_supervised_trainer(model, optimizer, criterion, device=device) trainer.logger = setup_logger("trainer") From 46017f08ef099780f21746a219fd9840a0e0eb8a Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 17 Feb 2023 11:11:11 +0900 Subject: [PATCH 15/40] Update test_engine.py --- tests/ignite/engine/test_engine.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index 7c7b6d78c36..74a88cd72f3 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1399,7 +1399,6 @@ def test_engine_debug(): from torchvision.transforms import Compose, ToTensor from ignite.engine import create_supervised_trainer - from ignite.utils import setup_logger DEBUG_EVENTS = 1 DEBUG_OUTPUT = 2 @@ -1442,7 +1441,6 @@ def _test(): optimizer = SGD(model.parameters(), lr=0.01, momentum=0.5) criterion = nn.NLLLoss() trainer = create_supervised_trainer(model, optimizer, criterion, device=device) - trainer.logger = setup_logger("trainer") @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_events(engine): From e942a3c1a8454845f5ddd4f7ca11a8f51bb8dcd3 Mon Sep 17 00:00:00 2001 From: puhuk Date: Wed, 22 Feb 2023 23:51:09 +0900 Subject: [PATCH 16/40] Update debug mode --- ignite/engine/engine.py | 28 ++++++++++++++++++++-------- tests/ignite/engine/test_engine.py | 20 ++++++++++++++++---- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index b45c44828f9..e7fd5c4d4d3 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -125,9 +125,10 @@ def compute_mean_std(engine, batch): _state_dict_all_req_keys = ("epoch_length", "max_epochs") _state_dict_one_of_opt_keys = ("iteration", "epoch") - DEBUG_EVENTS = 1 - DEBUG_OUTPUT = 2 - DEBUG_GRADS = 3 + DEBUG_NONE = "Engine.DEBUG_NONE" + DEBUG_EVENTS = "Engine.DEBUG_EVENTS" + DEBUG_OUTPUT = "Engine.DEBUG_OUTPUT" + DEBUG_GRADS = "Engine.DEBUG_GRADS" # Flag to disable engine._internal_run as generator feature for BC interrupt_resume_enabled = True @@ -151,6 +152,8 @@ def __init__(self, process_function: Callable[["Engine", Any], Any]): self.register_events(*Events) + self.debug_attr = {Engine.DEBUG_NONE: 0, Engine.DEBUG_EVENTS: 1, Engine.DEBUG_OUTPUT: 2, Engine.DEBUG_GRADS: 3} + if self._process_function is None: raise ValueError("Engine must be given a processing function in order to run.") @@ -431,18 +434,23 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> first, others = ((args[0],), args[1:]) if (args and args[0] == self) else ((), args) func(*first, *(event_args + others), **kwargs) - def debug(self, level: int = 0, **kwargs: Any) -> None: - self.level = level + def debug(self, level: str = "Engine.DEBUG_NONE") -> None: + if level not in self.debug_attr: + raise RuntimeError( + f"Unknown event name '{level}'. Level should be one of Engine.DEBUG_NONE, Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" + ) + + self.level = self.debug_attr[level] if self.level > 2: - self.lr = kwargs["optimizer"].param_groups[0]["lr"] - self.layer = kwargs["layer"] + self.lr = self.state.debug_config["optimizer"].param_groups[0]["lr"] + self.layer = self.state.debug_config["layer"] self.logger.debug( f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ Loss : {self.state.output}, LR : {self.lr}, Gradients : {self.layer.weight.grad}" ) elif self.level > 1: - self.lr = kwargs["optimizer"].param_groups[0]["lr"] + self.lr = self.state.debug_config["optimizer"].param_groups[0]["lr"] self.logger.debug( f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, \ Loss : {self.state.output}, LR : {self.lr}" @@ -803,6 +811,7 @@ def run( max_epochs: Optional[int] = None, max_iters: Optional[int] = None, epoch_length: Optional[int] = None, + debug_config: Optional[Any] = None, ) -> State: """Runs the ``process_function`` over the passed data. @@ -923,6 +932,9 @@ def switch_batch(engine): if self._dataloader_iter is None: self.state.dataloader = data + if debug_config is not None: + self.state.debug_config = debug_config + if self.interrupt_resume_enabled: return self._internal_run() else: diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index 74a88cd72f3..1952760265a 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1444,16 +1444,28 @@ def _test(): @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_events(engine): - trainer.debug(level=DEBUG_EVENTS) + trainer.debug(level=Engine.DEBUG_EVENTS) @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_outputs(engine): - trainer.debug(level=DEBUG_OUTPUT, optimizer=optimizer) + trainer.debug(level=Engine.DEBUG_OUTPUT) @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_grads(engine): - trainer.debug(level=DEBUG_GRADS, optimizer=optimizer, layer=model.fc2) + trainer.debug(level=Engine.DEBUG_GRADS) - trainer.run(train_loader, max_epochs=epochs) + with pytest.raises( + ValueError, + match=r"Unknown event name '2'. Level should be one of Engine.DEBUG_NONE, Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS", + ): + + @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) + def log_training_debug_grads(engine): + trainer.debug(level=2) + + debug_config = {} + debug_config["optimizer"] = optimizer + debug_config["layer"] = model.fc2 + trainer.run(train_loader, max_epochs=epochs, debug_config=debug_config) _test() From e77bb0f4d736671b6ae40fa2466c6f8ea2a6bd45 Mon Sep 17 00:00:00 2001 From: puhuk Date: Wed, 22 Feb 2023 23:54:27 +0900 Subject: [PATCH 17/40] Update test_engine.py --- tests/ignite/engine/test_engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index 1952760265a..e8fff39fc96 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1460,7 +1460,7 @@ def log_training_debug_grads(engine): ): @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) - def log_training_debug_grads(engine): + def log_training_debug_int(engine): trainer.debug(level=2) debug_config = {} From d1be8360cf6b114e7a30929a72d25164c9d0a122 Mon Sep 17 00:00:00 2001 From: puhuk Date: Wed, 22 Feb 2023 23:57:15 +0900 Subject: [PATCH 18/40] Update --- ignite/engine/engine.py | 3 ++- tests/ignite/engine/test_engine.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index e7fd5c4d4d3..94cc4b8b813 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -437,7 +437,8 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> def debug(self, level: str = "Engine.DEBUG_NONE") -> None: if level not in self.debug_attr: raise RuntimeError( - f"Unknown event name '{level}'. Level should be one of Engine.DEBUG_NONE, Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" + f"Unknown event name '{level}'. Level should be one of Engine.DEBUG_NONE, \ + Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" ) self.level = self.debug_attr[level] diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index e8fff39fc96..d9abca921f7 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1456,7 +1456,8 @@ def log_training_debug_grads(engine): with pytest.raises( ValueError, - match=r"Unknown event name '2'. Level should be one of Engine.DEBUG_NONE, Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS", + match=r"Unknown event name '2'. Level should be one of Engine.DEBUG_NONE, Engine.DEBUG_EVENTS, \ + Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS", ): @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) From b4e2b60e8305e1b6bb0aa48707509de558b8f631 Mon Sep 17 00:00:00 2001 From: puhuk Date: Thu, 23 Feb 2023 00:05:27 +0900 Subject: [PATCH 19/40] Update events.py --- ignite/engine/events.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ignite/engine/events.py b/ignite/engine/events.py index ef76babdc5b..05c8f300a63 100644 --- a/ignite/engine/events.py +++ b/ignite/engine/events.py @@ -445,6 +445,7 @@ def __init__(self, **kwargs: Any) -> None: self.batch: Optional[int] = None self.metrics: Dict[str, Any] = {} self.dataloader: Optional[Union[DataLoader, Iterable[Any]]] = None + self.debug_config : Optional[Dict[str, Any]] = None self.seed: Optional[int] = None self.times: Dict[str, Optional[float]] = { Events.EPOCH_COMPLETED.name: None, From eed1689a43e8a3340db60e86080db0c97afb66ef Mon Sep 17 00:00:00 2001 From: puhuk Date: Wed, 22 Feb 2023 15:06:19 +0000 Subject: [PATCH 20/40] autopep8 fix --- ignite/engine/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/events.py b/ignite/engine/events.py index 05c8f300a63..538562af093 100644 --- a/ignite/engine/events.py +++ b/ignite/engine/events.py @@ -445,7 +445,7 @@ def __init__(self, **kwargs: Any) -> None: self.batch: Optional[int] = None self.metrics: Dict[str, Any] = {} self.dataloader: Optional[Union[DataLoader, Iterable[Any]]] = None - self.debug_config : Optional[Dict[str, Any]] = None + self.debug_config: Optional[Dict[str, Any]] = None self.seed: Optional[int] = None self.times: Dict[str, Optional[float]] = { Events.EPOCH_COMPLETED.name: None, From e735d07ff7a354f30076caade0d25cb692b0b407 Mon Sep 17 00:00:00 2001 From: puhuk Date: Thu, 23 Feb 2023 00:08:40 +0900 Subject: [PATCH 21/40] Update events.py --- ignite/engine/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/events.py b/ignite/engine/events.py index 05c8f300a63..538562af093 100644 --- a/ignite/engine/events.py +++ b/ignite/engine/events.py @@ -445,7 +445,7 @@ def __init__(self, **kwargs: Any) -> None: self.batch: Optional[int] = None self.metrics: Dict[str, Any] = {} self.dataloader: Optional[Union[DataLoader, Iterable[Any]]] = None - self.debug_config : Optional[Dict[str, Any]] = None + self.debug_config: Optional[Dict[str, Any]] = None self.seed: Optional[int] = None self.times: Dict[str, Optional[float]] = { Events.EPOCH_COMPLETED.name: None, From 88a5482a6dc5d161cdb09e7587986aaa075dce1f Mon Sep 17 00:00:00 2001 From: puhuk Date: Thu, 23 Feb 2023 00:27:16 +0900 Subject: [PATCH 22/40] Update events.py --- ignite/engine/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/events.py b/ignite/engine/events.py index 538562af093..69dfc80f338 100644 --- a/ignite/engine/events.py +++ b/ignite/engine/events.py @@ -445,7 +445,7 @@ def __init__(self, **kwargs: Any) -> None: self.batch: Optional[int] = None self.metrics: Dict[str, Any] = {} self.dataloader: Optional[Union[DataLoader, Iterable[Any]]] = None - self.debug_config: Optional[Dict[str, Any]] = None + self.debug_config: Optional[Dict[Any]] = None self.seed: Optional[int] = None self.times: Dict[str, Optional[float]] = { Events.EPOCH_COMPLETED.name: None, From 7bbedb94f66124d9024677814f97ec151c5c14c8 Mon Sep 17 00:00:00 2001 From: puhuk Date: Thu, 23 Feb 2023 00:33:44 +0900 Subject: [PATCH 23/40] Update events.py --- ignite/engine/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/events.py b/ignite/engine/events.py index 69dfc80f338..4bf83ddbb69 100644 --- a/ignite/engine/events.py +++ b/ignite/engine/events.py @@ -445,7 +445,7 @@ def __init__(self, **kwargs: Any) -> None: self.batch: Optional[int] = None self.metrics: Dict[str, Any] = {} self.dataloader: Optional[Union[DataLoader, Iterable[Any]]] = None - self.debug_config: Optional[Dict[Any]] = None + self.debug_config: Dict[str, Any] = {} self.seed: Optional[int] = None self.times: Dict[str, Optional[float]] = { Events.EPOCH_COMPLETED.name: None, From fe40ba0669361c76318e47572f896b013d5140f0 Mon Sep 17 00:00:00 2001 From: puhuk Date: Thu, 23 Feb 2023 00:44:53 +0900 Subject: [PATCH 24/40] Update engine.py --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 94cc4b8b813..28475e9fbba 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -436,7 +436,7 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> def debug(self, level: str = "Engine.DEBUG_NONE") -> None: if level not in self.debug_attr: - raise RuntimeError( + raise ValueError( f"Unknown event name '{level}'. Level should be one of Engine.DEBUG_NONE, \ Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" ) From c5f8e07a14c5ed3a187f4943aefc6c9138fa626b Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 17:25:03 +0900 Subject: [PATCH 25/40] Update --- ignite/engine/engine.py | 75 +++++++++++++++++------------- tests/ignite/engine/test_engine.py | 20 ++++---- 2 files changed, 50 insertions(+), 45 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 28475e9fbba..7d6ae017dab 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -12,6 +12,7 @@ from ignite.base import Serializable from ignite.engine.events import CallableEventWithFilter, EventEnum, Events, EventsList, RemovableEventHandle, State +from enum import Flag from ignite.engine.utils import _check_signature, _to_hours_mins_secs __all__ = ["Engine"] @@ -125,10 +126,19 @@ def compute_mean_std(engine, batch): _state_dict_all_req_keys = ("epoch_length", "max_epochs") _state_dict_one_of_opt_keys = ("iteration", "epoch") - DEBUG_NONE = "Engine.DEBUG_NONE" - DEBUG_EVENTS = "Engine.DEBUG_EVENTS" - DEBUG_OUTPUT = "Engine.DEBUG_OUTPUT" - DEBUG_GRADS = "Engine.DEBUG_GRADS" + class debug_mode(Flag): + DEBUG_NONE = 0 + DEBUG_EVENTS = 1 + DEBUG_OUTPUT = 2 + DEBUG_GRADS = 4 + + def __int__(self): + return self.value + + DEBUG_NONE = debug_mode.DEBUG_NONE + DEBUG_EVENTS = debug_mode.DEBUG_EVENTS + DEBUG_OUTPUT = debug_mode.DEBUG_OUTPUT + DEBUG_GRADS = debug_mode.DEBUG_GRADS # Flag to disable engine._internal_run as generator feature for BC interrupt_resume_enabled = True @@ -152,8 +162,6 @@ def __init__(self, process_function: Callable[["Engine", Any], Any]): self.register_events(*Events) - self.debug_attr = {Engine.DEBUG_NONE: 0, Engine.DEBUG_EVENTS: 1, Engine.DEBUG_OUTPUT: 2, Engine.DEBUG_GRADS: 3} - if self._process_function is None: raise ValueError("Engine must be given a processing function in order to run.") @@ -434,32 +442,37 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> first, others = ((args[0],), args[1:]) if (args and args[0] == self) else ((), args) func(*first, *(event_args + others), **kwargs) - def debug(self, level: str = "Engine.DEBUG_NONE") -> None: - if level not in self.debug_attr: + def debug(self, level: debug_mode = DEBUG_NONE, config: dict = None) -> None: + if not isinstance(level, Engine.debug_mode): raise ValueError( - f"Unknown event name '{level}'. Level should be one of Engine.DEBUG_NONE, \ + f"Unknown event name '{level}'. Level should be combinations of Engine.DEBUG_NONE, \ Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" ) - - self.level = self.debug_attr[level] - - if self.level > 2: - self.lr = self.state.debug_config["optimizer"].param_groups[0]["lr"] - self.layer = self.state.debug_config["layer"] - self.logger.debug( - f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ - Loss : {self.state.output}, LR : {self.lr}, Gradients : {self.layer.weight.grad}" - ) - elif self.level > 1: - self.lr = self.state.debug_config["optimizer"].param_groups[0]["lr"] - self.logger.debug( - f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, \ - Loss : {self.state.output}, LR : {self.lr}" - ) - elif self.level > 0: - self.logger.debug( - f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}" - ) + self.lr = config["optimizer"].param_groups[0]["lr"] + self.layer = config["layer"] + level = level.value + if level == 0: + pass + elif level == 1: + log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}" + elif level == 2: + log = f"{self.state.epoch} | {self.state.iteration} Loss : {self.state.output}, LR : {self.lr}" + elif level == 3: + log = f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, \ + Loss : {self.state.output}, LR : {self.lr}" + elif level == 4: + log = f"{self.state.epoch} | {self.state.iteration}, Gradients : {self.layer.weight.grad}" + elif level == 5: + log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ + Gradients : {self.layer.weight.grad}" + elif level == 6: + log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ + Gradients : {self.layer.weight.grad}" + elif level == 7: + log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ + Loss : {self.state.output}, LR : {self.lr}, Gradients : {self.layer.weight.grad}" + + self.logger.debug(log) def fire_event(self, event_name: Any) -> None: """Execute all the handlers associated with given event. @@ -812,7 +825,6 @@ def run( max_epochs: Optional[int] = None, max_iters: Optional[int] = None, epoch_length: Optional[int] = None, - debug_config: Optional[Any] = None, ) -> State: """Runs the ``process_function`` over the passed data. @@ -933,9 +945,6 @@ def switch_batch(engine): if self._dataloader_iter is None: self.state.dataloader = data - if debug_config is not None: - self.state.debug_config = debug_config - if self.interrupt_resume_enabled: return self._internal_run() else: diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index d9abca921f7..8af13b8edd5 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1400,10 +1400,6 @@ def test_engine_debug(): from ignite.engine import create_supervised_trainer - DEBUG_EVENTS = 1 - DEBUG_OUTPUT = 2 - DEBUG_GRADS = 3 - class Net(nn.Module): def __init__(self): super(Net, self).__init__() @@ -1441,18 +1437,21 @@ def _test(): optimizer = SGD(model.parameters(), lr=0.01, momentum=0.5) criterion = nn.NLLLoss() trainer = create_supervised_trainer(model, optimizer, criterion, device=device) + debug_config = {} + debug_config["optimizer"] = optimizer + debug_config["layer"] = model.fc2 @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_events(engine): - trainer.debug(level=Engine.DEBUG_EVENTS) + trainer.debug(level=Engine.DEBUG_EVENTS, config=debug_config) @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_outputs(engine): - trainer.debug(level=Engine.DEBUG_OUTPUT) + trainer.debug(level=Engine.DEBUG_OUTPUT, config=debug_config) @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_grads(engine): - trainer.debug(level=Engine.DEBUG_GRADS) + trainer.debug(level=Engine.DEBUG_GRADS, config=debug_config) with pytest.raises( ValueError, @@ -1462,11 +1461,8 @@ def log_training_debug_grads(engine): @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_int(engine): - trainer.debug(level=2) + trainer.debug(level=2, config=debug_config) - debug_config = {} - debug_config["optimizer"] = optimizer - debug_config["layer"] = model.fc2 - trainer.run(train_loader, max_epochs=epochs, debug_config=debug_config) + trainer.run(train_loader, max_epochs=epochs) _test() From 0720841a2d27a7eea99f6c49f43a92cfaddceb4e Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 08:26:04 +0000 Subject: [PATCH 26/40] autopep8 fix --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 7d6ae017dab..f125c0cf55b 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -6,13 +6,13 @@ import weakref from collections import defaultdict, OrderedDict from collections.abc import Mapping +from enum import Flag from typing import Any, Callable, Dict, Generator, Iterable, Iterator, List, Optional, Tuple, Union from torch.utils.data import DataLoader from ignite.base import Serializable from ignite.engine.events import CallableEventWithFilter, EventEnum, Events, EventsList, RemovableEventHandle, State -from enum import Flag from ignite.engine.utils import _check_signature, _to_hours_mins_secs __all__ = ["Engine"] From fbaf3a48710236e8391a41b637bd6c71716740d0 Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 17:52:28 +0900 Subject: [PATCH 27/40] Update engine.py --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 7d6ae017dab..f125c0cf55b 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -6,13 +6,13 @@ import weakref from collections import defaultdict, OrderedDict from collections.abc import Mapping +from enum import Flag from typing import Any, Callable, Dict, Generator, Iterable, Iterator, List, Optional, Tuple, Union from torch.utils.data import DataLoader from ignite.base import Serializable from ignite.engine.events import CallableEventWithFilter, EventEnum, Events, EventsList, RemovableEventHandle, State -from enum import Flag from ignite.engine.utils import _check_signature, _to_hours_mins_secs __all__ = ["Engine"] From a9bc5009a51b155134b19aa5dec56c0e8d0fd25a Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 21:34:50 +0900 Subject: [PATCH 28/40] Update engine.py --- ignite/engine/engine.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index f125c0cf55b..d9ae964083c 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -442,7 +442,7 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> first, others = ((args[0],), args[1:]) if (args and args[0] == self) else ((), args) func(*first, *(event_args + others), **kwargs) - def debug(self, level: debug_mode = DEBUG_NONE, config: dict = None) -> None: + def debug(self, level: debug_mode = DEBUG_NONE, config: Union[Dict, None] = None) -> None: if not isinstance(level, Engine.debug_mode): raise ValueError( f"Unknown event name '{level}'. Level should be combinations of Engine.DEBUG_NONE, \ @@ -450,25 +450,25 @@ def debug(self, level: debug_mode = DEBUG_NONE, config: dict = None) -> None: ) self.lr = config["optimizer"].param_groups[0]["lr"] self.layer = config["layer"] - level = level.value - if level == 0: + self.level = level.value + if self.level == 0: pass - elif level == 1: + elif self.level == 1: log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}" - elif level == 2: + elif self.level == 2: log = f"{self.state.epoch} | {self.state.iteration} Loss : {self.state.output}, LR : {self.lr}" - elif level == 3: + elif self.level == 3: log = f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, \ Loss : {self.state.output}, LR : {self.lr}" - elif level == 4: + elif self.level == 4: log = f"{self.state.epoch} | {self.state.iteration}, Gradients : {self.layer.weight.grad}" - elif level == 5: + elif self.level == 5: log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ Gradients : {self.layer.weight.grad}" - elif level == 6: + elif self.level == 6: log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ Gradients : {self.layer.weight.grad}" - elif level == 7: + elif self.level == 7: log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ Loss : {self.state.output}, LR : {self.lr}, Gradients : {self.layer.weight.grad}" From 0cd306f7acfed88e7c973e046d42918922a188cb Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 21:47:37 +0900 Subject: [PATCH 29/40] Update engine.py --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index d9ae964083c..445f93dce49 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -442,7 +442,7 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> first, others = ((args[0],), args[1:]) if (args and args[0] == self) else ((), args) func(*first, *(event_args + others), **kwargs) - def debug(self, level: debug_mode = DEBUG_NONE, config: Union[Dict, None] = None) -> None: + def debug(self, level: debug_mode = DEBUG_NONE, config: Union[Dict, Any] = None) -> None: if not isinstance(level, Engine.debug_mode): raise ValueError( f"Unknown event name '{level}'. Level should be combinations of Engine.DEBUG_NONE, \ From 3e195f206e642b7cdc33755034d76d56f7a51ba1 Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 22:39:48 +0900 Subject: [PATCH 30/40] Update --- ignite/engine/engine.py | 6 +++--- tests/ignite/engine/test_engine.py | 15 +++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 445f93dce49..ccf9c00c159 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -132,7 +132,7 @@ class debug_mode(Flag): DEBUG_OUTPUT = 2 DEBUG_GRADS = 4 - def __int__(self): + def __int__(self) -> int: return self.value DEBUG_NONE = debug_mode.DEBUG_NONE @@ -445,8 +445,8 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> def debug(self, level: debug_mode = DEBUG_NONE, config: Union[Dict, Any] = None) -> None: if not isinstance(level, Engine.debug_mode): raise ValueError( - f"Unknown event name '{level}'. Level should be combinations of Engine.DEBUG_NONE, \ - Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" + f"Unknown event name '{level}'. Level should be combinations of Engine.DEBUG_NONE, " + f"Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" ) self.lr = config["optimizer"].param_groups[0]["lr"] self.layer = config["layer"] diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index 8af13b8edd5..116eb1b9cd0 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1453,14 +1453,13 @@ def log_training_debug_outputs(engine): def log_training_debug_grads(engine): trainer.debug(level=Engine.DEBUG_GRADS, config=debug_config) - with pytest.raises( - ValueError, - match=r"Unknown event name '2'. Level should be one of Engine.DEBUG_NONE, Engine.DEBUG_EVENTS, \ - Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS", - ): - - @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) - def log_training_debug_int(engine): + @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) + def log_training_debug_int(engine): + with pytest.raises( + ValueError, + match=r"Unknown event name '2'. Level should be combinations of Engine.DEBUG_NONE, " + r"Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" + ): trainer.debug(level=2, config=debug_config) trainer.run(train_loader, max_epochs=epochs) From c70498616a99600977b0b74205ee0fcfb4a3f38e Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 13:40:38 +0000 Subject: [PATCH 31/40] autopep8 fix --- ignite/engine/engine.py | 2 +- tests/ignite/engine/test_engine.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index ccf9c00c159..d4c2c4fe0ef 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -446,7 +446,7 @@ def debug(self, level: debug_mode = DEBUG_NONE, config: Union[Dict, Any] = None) if not isinstance(level, Engine.debug_mode): raise ValueError( f"Unknown event name '{level}'. Level should be combinations of Engine.DEBUG_NONE, " - f"Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" + f"Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" ) self.lr = config["optimizer"].param_groups[0]["lr"] self.layer = config["layer"] diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index 116eb1b9cd0..71298b78cbf 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1458,7 +1458,7 @@ def log_training_debug_int(engine): with pytest.raises( ValueError, match=r"Unknown event name '2'. Level should be combinations of Engine.DEBUG_NONE, " - r"Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" + r"Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS", ): trainer.debug(level=2, config=debug_config) From 6a5c5899ebc9b1fbed0c7cd505ef81a34b4b212f Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 22:46:32 +0900 Subject: [PATCH 32/40] Update engine.py --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index ccf9c00c159..d4c2c4fe0ef 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -446,7 +446,7 @@ def debug(self, level: debug_mode = DEBUG_NONE, config: Union[Dict, Any] = None) if not isinstance(level, Engine.debug_mode): raise ValueError( f"Unknown event name '{level}'. Level should be combinations of Engine.DEBUG_NONE, " - f"Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" + f"Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" ) self.lr = config["optimizer"].param_groups[0]["lr"] self.layer = config["layer"] From 49364d69a73fbf637c0ee9fae9d1e7699b0ce4c9 Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 22:50:25 +0900 Subject: [PATCH 33/40] Update --- ignite/engine/engine.py | 1 + tests/ignite/engine/test_engine.py | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index d4c2c4fe0ef..17cbb49eb30 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -473,6 +473,7 @@ def debug(self, level: debug_mode = DEBUG_NONE, config: Union[Dict, Any] = None) Loss : {self.state.output}, LR : {self.lr}, Gradients : {self.layer.weight.grad}" self.logger.debug(log) + print(log) def fire_event(self, event_name: Any) -> None: """Execute all the handlers associated with given event. diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py index 71298b78cbf..161a4ee5ab5 100644 --- a/tests/ignite/engine/test_engine.py +++ b/tests/ignite/engine/test_engine.py @@ -1441,19 +1441,15 @@ def _test(): debug_config["optimizer"] = optimizer debug_config["layer"] = model.fc2 - @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_events(engine): trainer.debug(level=Engine.DEBUG_EVENTS, config=debug_config) - @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_outputs(engine): trainer.debug(level=Engine.DEBUG_OUTPUT, config=debug_config) - @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_grads(engine): trainer.debug(level=Engine.DEBUG_GRADS, config=debug_config) - @trainer.on(Events.ITERATION_COMPLETED(every=log_interval)) def log_training_debug_int(engine): with pytest.raises( ValueError, From 05668c19208ca5ab30beb6bafa6df2c126748838 Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 25 Feb 2023 00:27:43 +0900 Subject: [PATCH 34/40] Update --- ignite/engine/engine.py | 43 ++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 17cbb49eb30..6818898efbf 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -126,7 +126,7 @@ def compute_mean_std(engine, batch): _state_dict_all_req_keys = ("epoch_length", "max_epochs") _state_dict_one_of_opt_keys = ("iteration", "epoch") - class debug_mode(Flag): + class debug_mode(EventEnum): DEBUG_NONE = 0 DEBUG_EVENTS = 1 DEBUG_OUTPUT = 2 @@ -140,6 +140,11 @@ def __int__(self) -> int: DEBUG_OUTPUT = debug_mode.DEBUG_OUTPUT DEBUG_GRADS = debug_mode.DEBUG_GRADS + # DEBUG_NONE = "DEBUG_NONE" + # DEBUG_EVENTS = "DEBUG_EVENTS" + # DEBUG_OUTPUT = "DEBUG_OUTPUT" + # DEBUG_GRADS = "DEBUG_GRADS" + # Flag to disable engine._internal_run as generator feature for BC interrupt_resume_enabled = True @@ -443,37 +448,27 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> func(*first, *(event_args + others), **kwargs) def debug(self, level: debug_mode = DEBUG_NONE, config: Union[Dict, Any] = None) -> None: - if not isinstance(level, Engine.debug_mode): + print(type(level), level) + if isinstance(level, int): raise ValueError( f"Unknown event name '{level}'. Level should be combinations of Engine.DEBUG_NONE, " f"Engine.DEBUG_EVENTS, Engine.DEBUG_OUTPUT, Engine.DEBUG_GRADS" ) self.lr = config["optimizer"].param_groups[0]["lr"] self.layer = config["layer"] - self.level = level.value - if self.level == 0: - pass - elif self.level == 1: - log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}" - elif self.level == 2: - log = f"{self.state.epoch} | {self.state.iteration} Loss : {self.state.output}, LR : {self.lr}" - elif self.level == 3: - log = f"{self.state.epoch} | {self.state.iteration} Firing handlers for event {self.last_event_name}, \ - Loss : {self.state.output}, LR : {self.lr}" - elif self.level == 4: - log = f"{self.state.epoch} | {self.state.iteration}, Gradients : {self.layer.weight.grad}" - elif self.level == 5: - log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ - Gradients : {self.layer.weight.grad}" - elif self.level == 6: - log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ - Gradients : {self.layer.weight.grad}" - elif self.level == 7: - log = f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name}, \ - Loss : {self.state.output}, LR : {self.lr}, Gradients : {self.layer.weight.grad}" + + log = "" + for item in level: + if item == Engine.DEBUG_NONE: + log += "" + elif item == Engine.DEBUG_EVENTS: + log += f"{self.state.epoch} | {self.state.iteration}, Firing handlers for event {self.last_event_name} " + elif item == Engine.DEBUG_OUTPUT: + log += f"Loss : {self.state.output}, LR : {self.lr} " + elif item == Engine.DEBUG_GRADS: + log += f"Gradients : {self.layer.weight.grad} " self.logger.debug(log) - print(log) def fire_event(self, event_name: Any) -> None: """Execute all the handlers associated with given event. From c93ba568f2c641918ac8cfbf55b9cea4675507a5 Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 25 Feb 2023 00:46:21 +0900 Subject: [PATCH 35/40] Update engine.py --- ignite/engine/engine.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 6818898efbf..90d3a453e2d 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -6,7 +6,6 @@ import weakref from collections import defaultdict, OrderedDict from collections.abc import Mapping -from enum import Flag from typing import Any, Callable, Dict, Generator, Iterable, Iterator, List, Optional, Tuple, Union from torch.utils.data import DataLoader From eca4f9fef7a87a838491f7da98cf93f6b03ebe34 Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 25 Feb 2023 01:01:39 +0900 Subject: [PATCH 36/40] Update engine.py --- ignite/engine/engine.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 90d3a453e2d..c3cb6b6c76c 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -131,6 +131,9 @@ class debug_mode(EventEnum): DEBUG_OUTPUT = 2 DEBUG_GRADS = 4 + def __iter__(self): + return self + def __int__(self) -> int: return self.value From c8bcaa025a36ef8e646062e33dc27559cac1f359 Mon Sep 17 00:00:00 2001 From: puhuk Date: Fri, 24 Feb 2023 16:02:30 +0000 Subject: [PATCH 37/40] autopep8 fix --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index c3cb6b6c76c..70b51cb341f 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -132,7 +132,7 @@ class debug_mode(EventEnum): DEBUG_GRADS = 4 def __iter__(self): - return self + return self def __int__(self) -> int: return self.value From 984b3fee34581f498a42613c1e86a513eeb54bdc Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 25 Feb 2023 01:04:36 +0900 Subject: [PATCH 38/40] Update engine.py --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index c3cb6b6c76c..70b51cb341f 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -132,7 +132,7 @@ class debug_mode(EventEnum): DEBUG_GRADS = 4 def __iter__(self): - return self + return self def __int__(self) -> int: return self.value From 0f9ee21dc60154cba9d6bacdec24ba8a55aed71c Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 25 Feb 2023 01:23:11 +0900 Subject: [PATCH 39/40] Update engine.py --- ignite/engine/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 70b51cb341f..7ea9a16e1ce 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -134,7 +134,7 @@ class debug_mode(EventEnum): def __iter__(self): return self - def __int__(self) -> int: + def __int__(self) -> str: return self.value DEBUG_NONE = debug_mode.DEBUG_NONE From 90f549f71fe4b1facfb61e769cee26b3c9307a41 Mon Sep 17 00:00:00 2001 From: puhuk Date: Sat, 25 Feb 2023 01:42:19 +0900 Subject: [PATCH 40/40] Update engine.py --- ignite/engine/engine.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py index 7ea9a16e1ce..f4333eee324 100644 --- a/ignite/engine/engine.py +++ b/ignite/engine/engine.py @@ -131,8 +131,8 @@ class debug_mode(EventEnum): DEBUG_OUTPUT = 2 DEBUG_GRADS = 4 - def __iter__(self): - return self + def __iter__(self) -> Iterator: + return iter(self.name) def __int__(self) -> str: return self.value @@ -142,11 +142,6 @@ def __int__(self) -> str: DEBUG_OUTPUT = debug_mode.DEBUG_OUTPUT DEBUG_GRADS = debug_mode.DEBUG_GRADS - # DEBUG_NONE = "DEBUG_NONE" - # DEBUG_EVENTS = "DEBUG_EVENTS" - # DEBUG_OUTPUT = "DEBUG_OUTPUT" - # DEBUG_GRADS = "DEBUG_GRADS" - # Flag to disable engine._internal_run as generator feature for BC interrupt_resume_enabled = True @@ -450,7 +445,6 @@ def _fire_event(self, event_name: Any, *event_args: Any, **event_kwargs: Any) -> func(*first, *(event_args + others), **kwargs) def debug(self, level: debug_mode = DEBUG_NONE, config: Union[Dict, Any] = None) -> None: - print(type(level), level) if isinstance(level, int): raise ValueError( f"Unknown event name '{level}'. Level should be combinations of Engine.DEBUG_NONE, "