Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1052 Upgrade Ignite dependency to 0.4.2 #1053

Merged
merged 23 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
59ba32d
Merge pull request #86 from Project-MONAI/master
Nic-Ma Jul 17, 2020
10e95c4
Merge pull request #136 from Project-MONAI/master
Nic-Ma Sep 8, 2020
3b9280b
Merge pull request #137 from Project-MONAI/master
Nic-Ma Sep 8, 2020
8f679cc
Merge pull request #138 from Project-MONAI/master
Nic-Ma Sep 10, 2020
b430bac
Merge pull request #139 from Project-MONAI/master
Nic-Ma Sep 11, 2020
468a6bf
Merge pull request #140 from Project-MONAI/master
Nic-Ma Sep 14, 2020
9e476ca
Merge pull request #141 from Project-MONAI/master
Nic-Ma Sep 16, 2020
68592bb
Merge pull request #142 from Project-MONAI/master
Nic-Ma Sep 19, 2020
a358ed4
Merge pull request #143 from Project-MONAI/master
Nic-Ma Sep 21, 2020
4bec902
[DLMED] update ignite to 0.4.2
Nic-Ma Sep 21, 2020
f0a69e0
[DLMED] update according to new APIs
Nic-Ma Sep 21, 2020
9a2e129
[DLMED] fix conflicts
Nic-Ma Sep 21, 2020
3735fa1
Merge branch 'master' into 1052-upgrade-ignite
Nic-Ma Sep 21, 2020
78ae214
[DLMED] fix GPU test
Nic-Ma Sep 21, 2020
52dfba6
[DLMED] add missing torch.cuda.set_device
Nic-Ma Sep 21, 2020
8cf88c6
Merge branch 'master' into 1052-upgrade-ignite
Nic-Ma Sep 21, 2020
4d636f4
Merge branch 'master' into 1052-upgrade-ignite
Nic-Ma Sep 27, 2020
f7bb907
[DLMED] update integration testY
Nic-Ma Sep 27, 2020
af6751a
Merge branch 'master' into 1052-upgrade-ignite
Nic-Ma Sep 28, 2020
85980ea
[DLMED] update test results
Nic-Ma Sep 28, 2020
7e2dcd0
[DLMED] update test results
Nic-Ma Sep 28, 2020
7f21a43
[DLMED] fix typo
Nic-Ma Sep 28, 2020
eaa8a4c
Merge branch 'master' into 1052-upgrade-ignite
wyli Sep 28, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-f https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp37-cp37m-linux_x86_64.whl
torch>=1.4.0
pytorch-ignite==0.3.0
pytorch-ignite==0.4.2
numpy>=1.17
itk
nibabel
Expand Down
4 changes: 2 additions & 2 deletions monai/engines/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
from ignite.engine import Engine
from ignite.metrics import Metric
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Metric, _ = optional_import("ignite.metrics", "0.3.0", exact_version, "Metric")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")
Metric, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "Metric")


class Evaluator(Workflow):
Expand Down
10 changes: 5 additions & 5 deletions monai/engines/multi_gpu_supervised_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
from monai.engines.utils import get_devices_spec
from monai.utils import exact_version, optional_import

create_supervised_trainer, _ = optional_import("ignite.engine", "0.3.0", exact_version, "create_supervised_trainer")
create_supervised_evaluator, _ = optional_import("ignite.engine", "0.3.0", exact_version, "create_supervised_evaluator")
_prepare_batch, _ = optional_import("ignite.engine", "0.3.0", exact_version, "_prepare_batch")
create_supervised_trainer, _ = optional_import("ignite.engine", "0.4.2", exact_version, "create_supervised_trainer")
create_supervised_evaluator, _ = optional_import("ignite.engine", "0.4.2", exact_version, "create_supervised_evaluator")
_prepare_batch, _ = optional_import("ignite.engine", "0.4.2", exact_version, "_prepare_batch")
if TYPE_CHECKING:
from ignite.engine import Engine
from ignite.metrics import Metric
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Metric, _ = optional_import("ignite.metrics", "0.3.0", exact_version, "Metric")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")
Metric, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "Metric")


def _default_transform(_x: torch.Tensor, _y: torch.Tensor, _y_pred: torch.Tensor, loss: torch.Tensor) -> float:
Expand Down
6 changes: 2 additions & 4 deletions monai/engines/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
from ignite.engine import Engine
from ignite.metrics import Metric
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Metric, _ = optional_import("ignite.metrics", "0.3.0", exact_version, "Metric")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")
Metric, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "Metric")


class Trainer(Workflow):
Expand All @@ -42,8 +42,6 @@ def run(self) -> None:
If call this function multiple times, it will continuously run from the previous state.

"""
if self._is_done(self.state):
self.state.iteration = 0 # to avoid creating new State instance in ignite Engine.run
self.scaler = torch.cuda.amp.GradScaler() if self.amp else None
super().run()

Expand Down
14 changes: 7 additions & 7 deletions monai/engines/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
from monai.transforms import apply_transform
from monai.utils import ensure_tuple, exact_version, optional_import

IgniteEngine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
State, _ = optional_import("ignite.engine", "0.3.0", exact_version, "State")
Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
IgniteEngine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")
State, _ = optional_import("ignite.engine", "0.4.2", exact_version, "State")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
if TYPE_CHECKING:
from ignite.engine import Engine
from ignite.metrics import Metric
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Metric, _ = optional_import("ignite.metrics", "0.3.0", exact_version, "Metric")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")
Metric, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "Metric")


class Workflow(IgniteEngine): # type: ignore[valid-type, misc] # due to optional_import
Expand Down Expand Up @@ -99,7 +99,7 @@ def set_sampler_epoch(engine: Engine):
iteration=0,
epoch=0,
max_epochs=max_epochs,
epoch_length=-1,
epoch_length=len(data_loader),
output=None,
batch=None,
metrics={},
Expand Down Expand Up @@ -154,7 +154,7 @@ def run(self) -> None:
Execute training, validation or evaluation based on Ignite Engine.

"""
super().run(data=self.data_loader, epoch_length=len(self.data_loader))
super().run(data=self.data_loader, max_epochs=self.state.max_epochs)

def _iteration(self, engine: Engine, batchdata: Dict[str, torch.Tensor]):
"""
Expand Down
6 changes: 3 additions & 3 deletions monai/handlers/checkpoint_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

from monai.utils import exact_version, optional_import

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
Checkpoint, _ = optional_import("ignite.handlers", "0.3.0", exact_version, "Checkpoint")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
Checkpoint, _ = optional_import("ignite.handlers", "0.4.2", exact_version, "Checkpoint")
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")


class CheckpointLoader:
Expand Down
6 changes: 3 additions & 3 deletions monai/handlers/checkpoint_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

from monai.utils import exact_version, optional_import

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
ModelCheckpoint, _ = optional_import("ignite.handlers", "0.3.0", exact_version, "ModelCheckpoint")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
ModelCheckpoint, _ = optional_import("ignite.handlers", "0.4.2", exact_version, "ModelCheckpoint")
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")


class CheckpointSaver:
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/classification_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
from monai.data import CSVSaver
from monai.utils import exact_version, optional_import

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")


class ClassificationSaver:
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/lr_schedule_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

from monai.utils import ensure_tuple, exact_version, optional_import

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")


class LrScheduleHandler:
Expand Down
8 changes: 4 additions & 4 deletions monai/handlers/mean_dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
from monai.metrics import DiceMetric
from monai.utils import MetricReduction, exact_version, optional_import

NotComputableError, _ = optional_import("ignite.exceptions", "0.3.0", exact_version, "NotComputableError")
Metric, _ = optional_import("ignite.metrics", "0.3.0", exact_version, "Metric")
reinit__is_reduced, _ = optional_import("ignite.metrics.metric", "0.3.0", exact_version, "reinit__is_reduced")
sync_all_reduce, _ = optional_import("ignite.metrics.metric", "0.3.0", exact_version, "sync_all_reduce")
NotComputableError, _ = optional_import("ignite.exceptions", "0.4.2", exact_version, "NotComputableError")
Metric, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "Metric")
reinit__is_reduced, _ = optional_import("ignite.metrics.metric", "0.4.2", exact_version, "reinit__is_reduced")
sync_all_reduce, _ = optional_import("ignite.metrics.metric", "0.4.2", exact_version, "sync_all_reduce")


class MeanDice(Metric): # type: ignore[valid-type, misc] # due to optional_import
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/metric_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

from monai.utils import exact_version, optional_import

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")


class MetricLogger:
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/roc_auc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from monai.metrics import compute_roc_auc
from monai.utils import Average, exact_version, optional_import

Metric, _ = optional_import("ignite.metrics", "0.3.0", exact_version, "Metric")
reinit__is_reduced, _ = optional_import("ignite.metrics.metric", "0.3.0", exact_version, "reinit__is_reduced")
Metric, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "Metric")
reinit__is_reduced, _ = optional_import("ignite.metrics.metric", "0.4.2", exact_version, "reinit__is_reduced")


class ROCAUC(Metric): # type: ignore[valid-type, misc] # due to optional_import
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/segmentation_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
from monai.data import NiftiSaver, PNGSaver
from monai.utils import GridSampleMode, GridSamplePadMode, InterpolateMode, exact_version, optional_import

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")


class SegmentationSaver:
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/smartcache_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
from monai.data import SmartCacheDataset
from monai.utils import exact_version, optional_import

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")


class SmartCacheHandler:
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/stats_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

from monai.utils import exact_version, is_scalar, optional_import

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")

DEFAULT_KEY_VAL_FORMAT = "{}: {:.4f} "
DEFAULT_TAG = "Loss"
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/tensorboard_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
from monai.utils import exact_version, is_scalar, optional_import
from monai.visualize import plot_2d_or_3d_image

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
if TYPE_CHECKING:
from ignite.engine import Engine
from torch.utils.tensorboard import SummaryWriter
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")
SummaryWriter, _ = optional_import("torch.utils.tensorboard", name="SummaryWriter")

DEFAULT_TAG = "Loss"
Expand Down
2 changes: 1 addition & 1 deletion monai/handlers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")


def stopping_fn_from_metric(metric_name: str) -> Callable[[Engine], Any]:
Expand Down
4 changes: 2 additions & 2 deletions monai/handlers/validation_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
from monai.engines.evaluator import Evaluator
from monai.utils import exact_version, optional_import

Events, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Events")
Events, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Events")
if TYPE_CHECKING:
from ignite.engine import Engine
else:
Engine, _ = optional_import("ignite.engine", "0.3.0", exact_version, "Engine")
Engine, _ = optional_import("ignite.engine", "0.4.2", exact_version, "Engine")


class ValidationHandler:
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Full requirements for developments
-r requirements-min.txt
pytorch-ignite==0.3.0
pytorch-ignite==0.4.2
gdown>=3.6.4
scipy
itk
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ all =
scikit-image>=0.14.2
pillow
tensorboard
pytorch-ignite==0.3.0
pytorch-ignite==0.4.2
gdown>=3.6.4
torchvision
itk
Expand All @@ -39,7 +39,7 @@ tensorboard =
gdown =
gdown>=3.6.4
ignite =
pytorch-ignite==0.3.0
pytorch-ignite==0.4.2
torchvision =
torchvision
itk =
Expand Down
15 changes: 9 additions & 6 deletions tests/test_handler_checkpoint_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ def test_one_save_one_load(self):
data2 = net2.state_dict()
data2["weight"] = torch.tensor([0.2])
net2.load_state_dict(data2)
engine = Engine(lambda e, b: None)
with tempfile.TemporaryDirectory() as tempdir:
engine = Engine(lambda e, b: None)
CheckpointSaver(save_dir=tempdir, save_dict={"net": net1}, save_final=True).attach(engine)
engine.run([0] * 8, max_epochs=5)
path = tempdir + "/net_final_iteration=40.pth"
path = tempdir + "/net_final_iteration=40.pt"
engine = Engine(lambda e, b: None)
CheckpointLoader(load_path=path, load_dict={"net": net2}).attach(engine)
engine.run([0] * 8, max_epochs=1)
torch.testing.assert_allclose(net2.state_dict()["weight"], 0.1)
Expand All @@ -52,12 +53,13 @@ def test_two_save_one_load(self):
data2 = net2.state_dict()
data2["weight"] = torch.tensor([0.2])
net2.load_state_dict(data2)
engine = Engine(lambda e, b: None)
with tempfile.TemporaryDirectory() as tempdir:
engine = Engine(lambda e, b: None)
save_dict = {"net": net1, "opt": optimizer}
CheckpointSaver(save_dir=tempdir, save_dict=save_dict, save_final=True).attach(engine)
engine.run([0] * 8, max_epochs=5)
path = tempdir + "/checkpoint_final_iteration=40.pth"
path = tempdir + "/checkpoint_final_iteration=40.pt"
engine = Engine(lambda e, b: None)
CheckpointLoader(load_path=path, load_dict={"net": net2}).attach(engine)
engine.run([0] * 8, max_epochs=1)
torch.testing.assert_allclose(net2.state_dict()["weight"], 0.1)
Expand All @@ -73,11 +75,12 @@ def test_save_single_device_load_multi_devices(self):
data2["weight"] = torch.tensor([0.2])
net2.load_state_dict(data2)
net2 = torch.nn.DataParallel(net2)
engine = Engine(lambda e, b: None)
with tempfile.TemporaryDirectory() as tempdir:
engine = Engine(lambda e, b: None)
CheckpointSaver(save_dir=tempdir, save_dict={"net": net1}, save_final=True).attach(engine)
engine.run([0] * 8, max_epochs=5)
path = tempdir + "/net_final_iteration=40.pth"
path = tempdir + "/net_final_iteration=40.pt"
engine = Engine(lambda e, b: None)
CheckpointLoader(load_path=path, load_dict={"net": net2}).attach(engine)
engine.run([0] * 8, max_epochs=1)
torch.testing.assert_allclose(net2.state_dict()["module.weight"], 0.1)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_handler_checkpoint_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from monai.handlers import CheckpointSaver

TEST_CASE_1 = [True, False, None, 1, True, 0, None, ["test_checkpoint_final_iteration=40.pth"]]
TEST_CASE_1 = [True, False, None, 1, True, 0, None, ["test_checkpoint_final_iteration=40.pt"]]

TEST_CASE_2 = [
False,
Expand All @@ -32,10 +32,10 @@
True,
0,
None,
["test_checkpoint_key_metric=32.pth", "test_checkpoint_key_metric=40.pth"],
["test_checkpoint_key_metric=32.pt", "test_checkpoint_key_metric=40.pt"],
]

TEST_CASE_3 = [False, False, None, 1, True, 2, 2, ["test_checkpoint_epoch=2.pth", "test_checkpoint_epoch=4.pth"]]
TEST_CASE_3 = [False, False, None, 1, True, 2, 2, ["test_checkpoint_epoch=2.pt", "test_checkpoint_epoch=4.pt"]]

TEST_CASE_4 = [
False,
Expand All @@ -45,10 +45,10 @@
False,
10,
2,
["test_checkpoint_iteration=30.pth", "test_checkpoint_iteration=40.pth"],
["test_checkpoint_iteration=30.pt", "test_checkpoint_iteration=40.pt"],
]

TEST_CASE_5 = [True, False, None, 1, True, 0, None, ["test_checkpoint_final_iteration=40.pth"], True]
TEST_CASE_5 = [True, False, None, 1, True, 0, None, ["test_checkpoint_final_iteration=40.pt"], True]


class TestHandlerCheckpointSaver(unittest.TestCase):
Expand Down Expand Up @@ -115,7 +115,7 @@ def _train_func(engine, batch):

with self.assertRaises(RuntimeError):
engine.run(range(3), max_epochs=2)
self.assertTrue(os.path.exists(os.path.join(tempdir, "net_final_iteration=1.pth")))
self.assertTrue(os.path.exists(os.path.join(tempdir, "net_final_iteration=1.pt")))


if __name__ == "__main__":
Expand Down