From 9f651409aa389f1adcc6f86be5ca9314a3b3d73d Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Thu, 27 Aug 2020 23:10:51 +0530 Subject: [PATCH 01/10] Delete test_custom_events.py --- .../contrib/handlers/test_custom_events.py | 133 ------------------ 1 file changed, 133 deletions(-) delete mode 100644 tests/ignite/contrib/handlers/test_custom_events.py diff --git a/tests/ignite/contrib/handlers/test_custom_events.py b/tests/ignite/contrib/handlers/test_custom_events.py deleted file mode 100644 index 9686c707141d..000000000000 --- a/tests/ignite/contrib/handlers/test_custom_events.py +++ /dev/null @@ -1,133 +0,0 @@ -import math - -import pytest - -from ignite.contrib.handlers.custom_events import CustomPeriodicEvent -from ignite.engine import Engine - - -def test_bad_input(): - - with pytest.warns(DeprecationWarning, match=r"CustomPeriodicEvent is deprecated"): - with pytest.raises(TypeError, match="Argument n_iterations should be an integer"): - CustomPeriodicEvent(n_iterations="a") - with pytest.raises(ValueError, match="Argument n_iterations should be positive"): - CustomPeriodicEvent(n_iterations=0) - with pytest.raises(TypeError, match="Argument n_iterations should be an integer"): - CustomPeriodicEvent(n_iterations=10.0) - with pytest.raises(TypeError, match="Argument n_epochs should be an integer"): - CustomPeriodicEvent(n_epochs="a") - with pytest.raises(ValueError, match="Argument n_epochs should be positive"): - CustomPeriodicEvent(n_epochs=0) - with pytest.raises(TypeError, match="Argument n_epochs should be an integer"): - CustomPeriodicEvent(n_epochs=10.0) - with pytest.raises(ValueError, match="Either n_iterations or n_epochs should be defined"): - CustomPeriodicEvent() - with pytest.raises(ValueError, match="Either n_iterations or n_epochs should be defined"): - CustomPeriodicEvent(n_iterations=1, n_epochs=2) - - -def test_new_events(): - def update(*args, **kwargs): - pass - - with pytest.warns(DeprecationWarning, match="CustomPeriodicEvent is deprecated"): - engine = Engine(update) - cpe = CustomPeriodicEvent(n_iterations=5) - cpe.attach(engine) - - assert hasattr(cpe, "Events") - assert hasattr(cpe.Events, "ITERATIONS_5_STARTED") - assert hasattr(cpe.Events, "ITERATIONS_5_COMPLETED") - - assert engine._allowed_events[-2] == getattr(cpe.Events, "ITERATIONS_5_STARTED") - assert engine._allowed_events[-1] == getattr(cpe.Events, "ITERATIONS_5_COMPLETED") - - with pytest.warns(DeprecationWarning, match="CustomPeriodicEvent is deprecated"): - cpe = CustomPeriodicEvent(n_epochs=5) - cpe.attach(engine) - - assert hasattr(cpe, "Events") - assert hasattr(cpe.Events, "EPOCHS_5_STARTED") - assert hasattr(cpe.Events, "EPOCHS_5_COMPLETED") - - assert engine._allowed_events[-2] == getattr(cpe.Events, "EPOCHS_5_STARTED") - assert engine._allowed_events[-1] == getattr(cpe.Events, "EPOCHS_5_COMPLETED") - - -def test_integration_iterations(): - def _test(n_iterations, max_epochs, n_iters_per_epoch): - def update(*args, **kwargs): - pass - - engine = Engine(update) - with pytest.warns(DeprecationWarning, match="CustomPeriodicEvent is deprecated"): - cpe = CustomPeriodicEvent(n_iterations=n_iterations) - cpe.attach(engine) - data = list(range(n_iters_per_epoch)) - - custom_period = [0] - n_calls_iter_started = [0] - n_calls_iter_completed = [0] - - event_started = getattr(cpe.Events, "ITERATIONS_{}_STARTED".format(n_iterations)) - - @engine.on(event_started) - def on_my_event_started(engine): - assert (engine.state.iteration - 1) % n_iterations == 0 - custom_period[0] += 1 - custom_iter = getattr(engine.state, "iterations_{}".format(n_iterations)) - assert custom_iter == custom_period[0] - n_calls_iter_started[0] += 1 - - event_completed = getattr(cpe.Events, "ITERATIONS_{}_COMPLETED".format(n_iterations)) - - @engine.on(event_completed) - def on_my_event_ended(engine): - assert engine.state.iteration % n_iterations == 0 - custom_iter = getattr(engine.state, "iterations_{}".format(n_iterations)) - assert custom_iter == custom_period[0] - n_calls_iter_completed[0] += 1 - - engine.run(data, max_epochs=max_epochs) - - n = len(data) * max_epochs / n_iterations - nf = math.floor(n) - assert custom_period[0] == n_calls_iter_started[0] - assert n_calls_iter_started[0] == nf + 1 if nf < n else nf - assert n_calls_iter_completed[0] == nf - - _test(3, 5, 16) - _test(4, 5, 16) - _test(5, 5, 16) - _test(300, 50, 1000) - - -def test_integration_epochs(): - def update(*args, **kwargs): - pass - - engine = Engine(update) - - n_epochs = 3 - with pytest.warns(DeprecationWarning, match="CustomPeriodicEvent is deprecated"): - cpe = CustomPeriodicEvent(n_epochs=n_epochs) - cpe.attach(engine) - data = list(range(16)) - - custom_period = [1] - - @engine.on(cpe.Events.EPOCHS_3_STARTED) - def on_my_epoch_started(engine): - assert (engine.state.epoch - 1) % n_epochs == 0 - assert engine.state.epochs_3 == custom_period[0] - - @engine.on(cpe.Events.EPOCHS_3_COMPLETED) - def on_my_epoch_ended(engine): - assert engine.state.epoch % n_epochs == 0 - assert engine.state.epochs_3 == custom_period[0] - custom_period[0] += 1 - - engine.run(data, max_epochs=10) - - assert custom_period[0] == 4 From c63edcfe681664b46732eedd8552fbfe444076ef Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Thu, 27 Aug 2020 23:12:34 +0530 Subject: [PATCH 02/10] Delete custom_events.py --- ignite/contrib/handlers/custom_events.py | 125 ----------------------- 1 file changed, 125 deletions(-) delete mode 100644 ignite/contrib/handlers/custom_events.py diff --git a/ignite/contrib/handlers/custom_events.py b/ignite/contrib/handlers/custom_events.py deleted file mode 100644 index 2b3e7313de57..000000000000 --- a/ignite/contrib/handlers/custom_events.py +++ /dev/null @@ -1,125 +0,0 @@ -import warnings - -from ignite.engine import EventEnum, Events, State - - -class CustomPeriodicEvent: - """DEPRECATED. Use filtered events instead. - Handler to define a custom periodic events as a number of elapsed iterations/epochs - for an engine. - - When custom periodic event is created and attached to an engine, the following events are fired: - 1) K iterations is specified: - - `Events.ITERATIONS__STARTED` - - `Events.ITERATIONS__COMPLETED` - - 1) K epochs is specified: - - `Events.EPOCHS__STARTED` - - `Events.EPOCHS__COMPLETED` - - - Examples: - - .. code-block:: python - - from ignite.engine import Engine, Events - from ignite.contrib.handlers import CustomPeriodicEvent - - # Let's define an event every 1000 iterations - cpe1 = CustomPeriodicEvent(n_iterations=1000) - cpe1.attach(trainer) - - # Let's define an event every 10 epochs - cpe2 = CustomPeriodicEvent(n_epochs=10) - cpe2.attach(trainer) - - @trainer.on(cpe1.Events.ITERATIONS_1000_COMPLETED) - def on_every_1000_iterations(engine): - # run a computation after 1000 iterations - # ... - print(engine.state.iterations_1000) - - @trainer.on(cpe2.Events.EPOCHS_10_STARTED) - def on_every_10_epochs(engine): - # run a computation every 10 epochs - # ... - print(engine.state.epochs_10) - - - Args: - n_iterations (int, optional): number iterations of the custom periodic event - n_epochs (int, optional): number iterations of the custom periodic event. Argument is optional, but only one, - either n_iterations or n_epochs should defined. - - """ - - def __init__(self, n_iterations=None, n_epochs=None): - - warnings.warn( - "CustomPeriodicEvent is deprecated since 0.4.0 and will be removed in 0.5.0. Use filtered events instead.", - DeprecationWarning, - ) - - if n_iterations is not None: - if not isinstance(n_iterations, int): - raise TypeError("Argument n_iterations should be an integer") - if n_iterations < 1: - raise ValueError("Argument n_iterations should be positive") - - if n_epochs is not None: - if not isinstance(n_epochs, int): - raise TypeError("Argument n_epochs should be an integer") - if n_epochs < 1: - raise ValueError("Argument n_epochs should be positive") - - if (n_iterations is None and n_epochs is None) or (n_iterations and n_epochs): - raise ValueError("Either n_iterations or n_epochs should be defined") - - if n_iterations: - prefix = "iterations" - self.state_attr = "iteration" - self.period = n_iterations - - if n_epochs: - prefix = "epochs" - self.state_attr = "epoch" - self.period = n_epochs - - self.custom_state_attr = "{}_{}".format(prefix, self.period) - event_name = "{}_{}".format(prefix.upper(), self.period) - setattr( - self, - "Events", - EventEnum("Events", " ".join(["{}_STARTED".format(event_name), "{}_COMPLETED".format(event_name)])), - ) - - # Update State.event_to_attr - for e in self.Events: - State.event_to_attr[e] = self.custom_state_attr - - # Create aliases - self._periodic_event_started = getattr(self.Events, "{}_STARTED".format(event_name)) - self._periodic_event_completed = getattr(self.Events, "{}_COMPLETED".format(event_name)) - - def _on_started(self, engine): - setattr(engine.state, self.custom_state_attr, 0) - - def _on_periodic_event_started(self, engine): - if getattr(engine.state, self.state_attr) % self.period == 1: - setattr(engine.state, self.custom_state_attr, getattr(engine.state, self.custom_state_attr) + 1) - engine.fire_event(self._periodic_event_started) - - def _on_periodic_event_completed(self, engine): - if getattr(engine.state, self.state_attr) % self.period == 0: - engine.fire_event(self._periodic_event_completed) - - def attach(self, engine): - engine.register_events(*self.Events) - - engine.add_event_handler(Events.STARTED, self._on_started) - engine.add_event_handler( - getattr(Events, "{}_STARTED".format(self.state_attr.upper())), self._on_periodic_event_started - ) - engine.add_event_handler( - getattr(Events, "{}_COMPLETED".format(self.state_attr.upper())), self._on_periodic_event_completed - ) From 185d228f26abba518928bb42f68cecf9850a2404 Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Thu, 27 Aug 2020 23:32:51 +0530 Subject: [PATCH 03/10] Removing depriciated CustomPeriodicEvent --- ignite/contrib/handlers/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ignite/contrib/handlers/__init__.py b/ignite/contrib/handlers/__init__.py index 0f8b6ecc3095..aabc1cf91907 100644 --- a/ignite/contrib/handlers/__init__.py +++ b/ignite/contrib/handlers/__init__.py @@ -1,4 +1,3 @@ -from ignite.contrib.handlers.custom_events import CustomPeriodicEvent from ignite.contrib.handlers.lr_finder import FastaiLRFinder from ignite.contrib.handlers.mlflow_logger import MLflowLogger from ignite.contrib.handlers.neptune_logger import NeptuneLogger From da0bfedb51d5375806bcf3ccc256dcd33ceb1135 Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Fri, 28 Aug 2020 00:09:09 +0530 Subject: [PATCH 04/10] Remove deprecated CustomPeriodicEvent --- tests/ignite/contrib/handlers/test_base_logger.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ignite/contrib/handlers/test_base_logger.py b/tests/ignite/contrib/handlers/test_base_logger.py index 4ca342dea6c3..c32a2eb768cd 100644 --- a/tests/ignite/contrib/handlers/test_base_logger.py +++ b/tests/ignite/contrib/handlers/test_base_logger.py @@ -4,7 +4,6 @@ import pytest import torch -from ignite.contrib.handlers import CustomPeriodicEvent from ignite.contrib.handlers.base_logger import BaseLogger, BaseOptimizerParamsHandler, BaseOutputHandler from ignite.engine import Engine, Events, State from tests.ignite.contrib.handlers import MockFP16DeepSpeedZeroOptimizer From f770b7ad12b20ccb766d278a13f5a87bdb710ed6 Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Fri, 28 Aug 2020 16:54:55 +0530 Subject: [PATCH 05/10] Update test_tqdm_logger.py --- tests/ignite/contrib/handlers/test_tqdm_logger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ignite/contrib/handlers/test_tqdm_logger.py b/tests/ignite/contrib/handlers/test_tqdm_logger.py index 8457673720e2..a351ab993308 100644 --- a/tests/ignite/contrib/handlers/test_tqdm_logger.py +++ b/tests/ignite/contrib/handlers/test_tqdm_logger.py @@ -6,7 +6,7 @@ import pytest import torch -from ignite.contrib.handlers import CustomPeriodicEvent, ProgressBar +from ignite.contrib.handlers import ProgressBar from ignite.engine import Engine, Events from ignite.handlers import TerminateOnNan from ignite.metrics import RunningAverage From d5afae2f6ce120ba6f6f832c7b2963cd63dc01b0 Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Fri, 28 Aug 2020 18:27:36 +0530 Subject: [PATCH 06/10] Remove deprecated CustomPeriodicEvent --- tests/ignite/contrib/handlers/test_tqdm_logger.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/ignite/contrib/handlers/test_tqdm_logger.py b/tests/ignite/contrib/handlers/test_tqdm_logger.py index a351ab993308..245c07f09b09 100644 --- a/tests/ignite/contrib/handlers/test_tqdm_logger.py +++ b/tests/ignite/contrib/handlers/test_tqdm_logger.py @@ -372,18 +372,6 @@ def test_pbar_wrong_events_order(): with pytest.raises(ValueError, match="should not be a filtered event"): pbar.attach(engine, event_name=Events.ITERATION_STARTED, closing_event_name=Events.EPOCH_COMPLETED(every=10)) - -def test_pbar_on_custom_events(capsys): - - engine = Engine(update_fn) - pbar = ProgressBar() - with pytest.warns(DeprecationWarning, match="CustomPeriodicEvent is deprecated"): - cpe = CustomPeriodicEvent(n_iterations=15) - - with pytest.raises(ValueError, match=r"not in allowed events for this engine"): - pbar.attach(engine, event_name=cpe.Events.ITERATIONS_15_COMPLETED, closing_event_name=Events.EPOCH_COMPLETED) - - def test_pbar_with_nan_input(): def update(engine, batch): x = batch From 4c24517a1f4f66805c32a5415fc4c60e5f81b0ed Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Fri, 28 Aug 2020 19:04:35 +0530 Subject: [PATCH 07/10] Update test_tqdm_logger.py Adding needed space. --- tests/ignite/contrib/handlers/test_tqdm_logger.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ignite/contrib/handlers/test_tqdm_logger.py b/tests/ignite/contrib/handlers/test_tqdm_logger.py index 245c07f09b09..3f4097c1a954 100644 --- a/tests/ignite/contrib/handlers/test_tqdm_logger.py +++ b/tests/ignite/contrib/handlers/test_tqdm_logger.py @@ -372,6 +372,7 @@ def test_pbar_wrong_events_order(): with pytest.raises(ValueError, match="should not be a filtered event"): pbar.attach(engine, event_name=Events.ITERATION_STARTED, closing_event_name=Events.EPOCH_COMPLETED(every=10)) + def test_pbar_with_nan_input(): def update(engine, batch): x = batch From 5dbd6ffc16931e9c65625f5870ed56d3c6e5d28f Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Fri, 28 Aug 2020 19:32:58 +0530 Subject: [PATCH 08/10] Removing CustomPeriodicEvent --- .../contrib/handlers/test_base_logger.py | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/tests/ignite/contrib/handlers/test_base_logger.py b/tests/ignite/contrib/handlers/test_base_logger.py index c32a2eb768cd..7eb572cb9bf9 100644 --- a/tests/ignite/contrib/handlers/test_base_logger.py +++ b/tests/ignite/contrib/handlers/test_base_logger.py @@ -171,33 +171,6 @@ def update_fn(engine, batch): mock_log_handler.assert_called_with(trainer, logger, event) assert mock_log_handler.call_count == n_calls - with pytest.warns(DeprecationWarning, match="CustomPeriodicEvent is deprecated"): - n_iterations = 10 - cpe1 = CustomPeriodicEvent(n_iterations=n_iterations) - n = len(data) * n_epochs / n_iterations - nf = math.floor(n) - ns = nf + 1 if nf < n else nf - _test(cpe1.Events.ITERATIONS_10_STARTED, ns, cpe1) - _test(cpe1.Events.ITERATIONS_10_COMPLETED, nf, cpe1) - - with pytest.warns(DeprecationWarning, match="CustomPeriodicEvent is deprecated"): - n_iterations = 15 - cpe2 = CustomPeriodicEvent(n_iterations=n_iterations) - n = len(data) * n_epochs / n_iterations - nf = math.floor(n) - ns = nf + 1 if nf < n else nf - _test(cpe2.Events.ITERATIONS_15_STARTED, ns, cpe2) - _test(cpe2.Events.ITERATIONS_15_COMPLETED, nf, cpe2) - - with pytest.warns(DeprecationWarning, match="CustomPeriodicEvent is deprecated"): - n_custom_epochs = 2 - cpe3 = CustomPeriodicEvent(n_epochs=n_custom_epochs) - n = n_epochs / n_custom_epochs - nf = math.floor(n) - ns = nf + 1 if nf < n else nf - _test(cpe3.Events.EPOCHS_2_STARTED, ns, cpe3) - _test(cpe3.Events.EPOCHS_2_COMPLETED, nf, cpe3) - def test_as_context_manager(): From 8efeecbf120b430e739c5a835cb01aca6fe6a59a Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Fri, 28 Aug 2020 19:49:34 +0530 Subject: [PATCH 09/10] Update handlers.rst --- docs/source/contrib/handlers.rst | 6 ------ 1 file changed, 6 deletions(-) diff --git a/docs/source/contrib/handlers.rst b/docs/source/contrib/handlers.rst index 4560aa9fcbbb..a36b51cb2507 100644 --- a/docs/source/contrib/handlers.rst +++ b/docs/source/contrib/handlers.rst @@ -4,12 +4,6 @@ ignite.contrib.handlers Contribution module of handlers -custom_events -------------- - -.. automodule:: ignite.contrib.handlers.custom_events - :members: - param_scheduler --------------- From 202a980932f4e230acd6108211ec843b8b1ae738 Mon Sep 17 00:00:00 2001 From: Tawishi <55306738+Tawishi@users.noreply.github.com> Date: Sat, 29 Aug 2020 13:26:54 +0530 Subject: [PATCH 10/10] Removing deprecated CustomPeriodicEvent Responding to the [comment](https://github.com/pytorch/ignite/issues/1247#issuecomment-683140646) --- .../EfficientNet_Cifar100_finetuning.ipynb | 42 ++----------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/examples/notebooks/EfficientNet_Cifar100_finetuning.ipynb b/examples/notebooks/EfficientNet_Cifar100_finetuning.ipynb index ae59e7712cc0..0d9363cd332f 100644 --- a/examples/notebooks/EfficientNet_Cifar100_finetuning.ipynb +++ b/examples/notebooks/EfficientNet_Cifar100_finetuning.ipynb @@ -1398,7 +1398,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let's create two evaluators to compute metrics on train/test images and log them to Tensorboard:" + "Let's create an evaluator to compute metrics on train/test images and log them to Tensorboard:" ] }, { @@ -1423,42 +1423,6 @@ " device=device, non_blocking=True)" ] }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": {}, - "outputs": [], - "source": [ - "from ignite.contrib.handlers import CustomPeriodicEvent\n", - "\n", - "cpe = CustomPeriodicEvent(n_epochs=3)\n", - "cpe.attach(trainer)\n", - "\n", - "\n", - "def run_evaluation(engine):\n", - " train_evaluator.run(eval_train_loader)\n", - " evaluator.run(test_loader)\n", - "\n", - "\n", - "trainer.add_event_handler(cpe.Events.EPOCHS_3_STARTED, run_evaluation)\n", - "trainer.add_event_handler(Events.COMPLETED, run_evaluation)\n", - "\n", - "\n", - "# Log train eval metrics:\n", - "tb_logger.attach(train_evaluator,\n", - " log_handler=OutputHandler(tag=\"training\",\n", - " metric_names=list(metrics.keys()),\n", - " another_engine=trainer),\n", - " event_name=Events.EPOCH_COMPLETED)\n", - "\n", - "# Log val metrics:\n", - "tb_logger.attach(evaluator,\n", - " log_handler=OutputHandler(tag=\"test\",\n", - " metric_names=list(metrics.keys()),\n", - " another_engine=trainer),\n", - " event_name=Events.EPOCH_COMPLETED)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -1835,9 +1799,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 2 -} \ No newline at end of file +}