Пример #1
0
    def test_save_checkpoint_save_filename(self, mock_save):
        torchmodel = Mock()
        optim = Mock()
        state = {
            torchbearer.SELF: Model(torchmodel, optim, None, []),
            torchbearer.METRICS: {}
        }

        file_format = 'test_file.pt'
        check = _Checkpointer(file_format)
        check.save_checkpoint(state)
        mock_save.assert_called_once()

        self.assertTrue(mock_save.call_args[0][1] == 'test_file.pt')
Пример #2
0
    def test_save_checkpoint_subformatting(self, mock_save):
        torchmodel = Mock()
        optim = Mock()
        state = {
            torchbearer.SELF: Model(torchmodel, optim, None, []),
            torchbearer.METRICS: {'test_metric': 0.001},
            torchbearer.EPOCH: 2
        }

        file_format = 'test_file_{test_metric:.01f}.pt'
        check = _Checkpointer(file_format)
        check.save_checkpoint(state)
        mock_save.assert_called_once()

        self.assertTrue(mock_save.call_args[0][1] == 'test_file_0.0.pt')
Пример #3
0
    def test_save_checkpoint_wrong_format(self, _):
        torchmodel = Mock()
        optim = Mock()
        state = {
            torchbearer.SELF: Model(torchmodel, optim, None, []),
            torchbearer.METRICS: {'test_metric': 0.001},
            torchbearer.EPOCH: 2
        }

        file_format = 'test_file_{test_metric:d}.pt'
        check = _Checkpointer(file_format)
        try:
            check.save_checkpoint(state)
        except:
            return

        self.fail('No error was thrown when wrong format chosen for save file format')
Пример #4
0
    def test_save_checkpoint_overwrite_recent(self, _, __):
        torchmodel = Mock()
        optim = Mock()
        state = {
            torchbearer.SELF: Model(torchmodel, optim, None, []),
            torchbearer.EPOCH: 0,
            torchbearer.METRICS: {}
        }

        file_format = 'test_file_{epoch}.pt'
        check = _Checkpointer(file_format)
        check.save_checkpoint(state, True)
        self.assertTrue(check.most_recent == 'test_file_0.pt')

        state[torchbearer.EPOCH] = 1
        check.save_checkpoint(state, True)
        self.assertTrue(check.most_recent == 'test_file_1.pt')
Пример #5
0
    def test_save_checkpoint_subformatting(self, mock_save):
        torchmodel = Mock()
        optim = Mock()
        state = {
            torchbearer.SELF: Model(torchmodel, optim, None, []),
            torchbearer.METRICS: {
                'test_metric': 0.001
            },
            torchbearer.EPOCH: 2
        }

        file_format = 'test_file_{test_metric:.01f}.pt'
        check = _Checkpointer(file_format)
        check.save_checkpoint(state)
        self.assertEqual(mock_save.call_count, 1)

        self.assertTrue(mock_save.call_args[0][1] == 'test_file_0.0.pt')
Пример #6
0
    def test_save_checkpoint_overwrite_recent(self, _, __):
        torchmodel = Mock()
        optim = Mock()
        state = {
            torchbearer.SELF: Model(torchmodel, optim, None, []),
            torchbearer.EPOCH: 0,
            torchbearer.METRICS: {}
        }

        file_format = 'test_file_{epoch}.pt'
        check = _Checkpointer(file_format)
        check.save_checkpoint(state, True)
        self.assertTrue(check.most_recent == 'test_file_0.pt')

        state[torchbearer.EPOCH] = 1
        check.save_checkpoint(state, True)
        self.assertTrue(check.most_recent == 'test_file_1.pt')
    def test_save_checkpoint_model_only(self, mock_save):
        torchmodel = Mock()
        optim = Mock()
        state = {
            torchbearer.SELF: Trial(torchmodel, optim, None, []),
            torchbearer.METRICS: {
                'test_metric': 0.001
            },
            torchbearer.EPOCH: 2,
            torchbearer.MODEL: torchmodel,
        }

        file_format = 'test_file_{test_metric:.01f}.pt'
        check = _Checkpointer(file_format, save_model_params_only=True)
        check.save_checkpoint(state)
        self.assertEqual(mock_save.call_count, 1)
        self.assertTrue(mock_save.call_args[0][0] == torchmodel.state_dict())
        self.assertTrue(mock_save.call_args[0][1] == 'test_file_0.0.pt')
    def test_save_checkpoint_wrong_format(self, _):
        torchmodel = Mock()
        optim = Mock()
        state = {
            torchbearer.SELF: Trial(torchmodel, optim, None, []),
            torchbearer.METRICS: {
                'test_metric': 0.001
            },
            torchbearer.EPOCH: 2
        }

        file_format = 'test_file_{test_metric:d}.pt'
        check = _Checkpointer(file_format)
        try:
            check.save_checkpoint(state)
        except:
            return

        self.fail(
            'No error was thrown when wrong format chosen for save file format'
        )
 def test_make_dirs(self, mock_dirs):
     _Checkpointer('thisdirectoryshouldntexist/norshouldthis/model.pt')
     mock_dirs.assert_called_once_with(
         'thisdirectoryshouldntexist/norshouldthis')
Пример #10
0
 def test_make_dirs(self, mock_dirs):
     _Checkpointer('thisdirectoryshouldntexist/norshouldthis/model.pt')
     mock_dirs.assert_called_once_with('thisdirectoryshouldntexist/norshouldthis')