|
| 1 | +# Copyright 2020 MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import os |
| 13 | +import tempfile |
| 14 | +import shutil |
| 15 | +import torch |
| 16 | +import unittest |
| 17 | +from ignite.engine import Engine |
| 18 | +import torch.optim as optim |
| 19 | +from monai.handlers import CheckpointSaver, CheckpointLoader |
| 20 | +import logging |
| 21 | +import sys |
| 22 | + |
| 23 | + |
| 24 | +class TestHandlerCheckpointLoader(unittest.TestCase): |
| 25 | + def test_one_save_one_load(self): |
| 26 | + logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
| 27 | + net1 = torch.nn.PReLU() |
| 28 | + data1 = net1.state_dict() |
| 29 | + data1["weight"] = torch.tensor([0.1]) |
| 30 | + net1.load_state_dict(data1) |
| 31 | + net2 = torch.nn.PReLU() |
| 32 | + data2 = net2.state_dict() |
| 33 | + data2["weight"] = torch.tensor([0.2]) |
| 34 | + net2.load_state_dict(data2) |
| 35 | + engine = Engine(lambda e, b: None) |
| 36 | + with tempfile.TemporaryDirectory() as tempdir: |
| 37 | + save_dir = os.path.join(tempdir, "checkpoint") |
| 38 | + CheckpointSaver(save_dir=save_dir, save_dict={"net": net1}, save_final=True).attach(engine) |
| 39 | + engine.run([0] * 8, max_epochs=5) |
| 40 | + path = save_dir + "/net_final_iteration=40.pth" |
| 41 | + CheckpointLoader(load_path=path, load_dict={"net": net2}).attach(engine) |
| 42 | + engine.run([0] * 8, max_epochs=1) |
| 43 | + torch.testing.assert_allclose(net2.state_dict()["weight"], 0.1) |
| 44 | + shutil.rmtree(save_dir) |
| 45 | + |
| 46 | + def test_two_save_one_load(self): |
| 47 | + logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
| 48 | + net1 = torch.nn.PReLU() |
| 49 | + optimizer = optim.SGD(net1.parameters(), lr=0.02) |
| 50 | + data1 = net1.state_dict() |
| 51 | + data1["weight"] = torch.tensor([0.1]) |
| 52 | + net1.load_state_dict(data1) |
| 53 | + net2 = torch.nn.PReLU() |
| 54 | + data2 = net2.state_dict() |
| 55 | + data2["weight"] = torch.tensor([0.2]) |
| 56 | + net2.load_state_dict(data2) |
| 57 | + engine = Engine(lambda e, b: None) |
| 58 | + with tempfile.TemporaryDirectory() as tempdir: |
| 59 | + save_dir = os.path.join(tempdir, "checkpoint") |
| 60 | + save_dict = {"net": net1, "opt": optimizer} |
| 61 | + CheckpointSaver(save_dir=save_dir, save_dict=save_dict, save_final=True).attach(engine) |
| 62 | + engine.run([0] * 8, max_epochs=5) |
| 63 | + path = save_dir + "/checkpoint_final_iteration=40.pth" |
| 64 | + CheckpointLoader(load_path=path, load_dict={"net": net2}).attach(engine) |
| 65 | + engine.run([0] * 8, max_epochs=1) |
| 66 | + torch.testing.assert_allclose(net2.state_dict()["weight"], 0.1) |
| 67 | + shutil.rmtree(save_dir) |
| 68 | + |
| 69 | + def test_save_single_device_load_multi_devices(self): |
| 70 | + logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
| 71 | + net1 = torch.nn.PReLU() |
| 72 | + data1 = net1.state_dict() |
| 73 | + data1["weight"] = torch.tensor([0.1]) |
| 74 | + net1.load_state_dict(data1) |
| 75 | + net2 = torch.nn.PReLU() |
| 76 | + data2 = net2.state_dict() |
| 77 | + data2["weight"] = torch.tensor([0.2]) |
| 78 | + net2.load_state_dict(data2) |
| 79 | + net2 = torch.nn.DataParallel(net2) |
| 80 | + engine = Engine(lambda e, b: None) |
| 81 | + with tempfile.TemporaryDirectory() as tempdir: |
| 82 | + save_dir = os.path.join(tempdir, "checkpoint") |
| 83 | + CheckpointSaver(save_dir=save_dir, save_dict={"net": net1}, save_final=True).attach(engine) |
| 84 | + engine.run([0] * 8, max_epochs=5) |
| 85 | + path = save_dir + "/net_final_iteration=40.pth" |
| 86 | + CheckpointLoader(load_path=path, load_dict={"net": net2}).attach(engine) |
| 87 | + engine.run([0] * 8, max_epochs=1) |
| 88 | + torch.testing.assert_allclose(net2.state_dict()["module.weight"], 0.1) |
| 89 | + shutil.rmtree(save_dir) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + unittest.main() |
0 commit comments