Пример #1
0
class TestStd(unittest.TestCase):
    def setUp(self):
        self._metric = Metric('test')
        self._metric.process = Mock()
        self._metric.process.side_effect = [torch.zeros(torch.Size([])),
                                            torch.FloatTensor([0.1, 0.2, 0.3]),
                                            torch.FloatTensor([0.4, 0.5, 0.6]),
                                            torch.FloatTensor([0.7, 0.8, 0.9]),
                                            torch.ones(torch.Size([]))]

        self._std = Std('test')
        self._std.reset({})
        self._target = 0.31622776601684

    def test_train(self):
        self._std.train()
        for i in range(5):
            self._std.process(self._metric.process())
        result = self._std.process_final({})
        self.assertAlmostEqual(self._target, result)

    def test_validate(self):
        self._std.eval()
        for i in range(5):
            self._std.process(self._metric.process())
        result = self._std.process_final({})
        self.assertAlmostEqual(self._target, result)
Пример #2
0
class TestStd(unittest.TestCase):
    def setUp(self):
        self._metric = Metric('test')
        self._metric.process = Mock()
        self._metric.process.side_effect = [torch.zeros(torch.Size([])),
                                            torch.FloatTensor([0.1, 0.2, 0.3]),
                                            torch.FloatTensor([0.4, 0.5, 0.6]),
                                            torch.FloatTensor([0.7, 0.8, 0.9]),
                                            torch.ones(torch.Size([]))]

        self._std = Std('test')
        self._std.reset({})
        self._target = 0.31622776601684

    def test_train(self):
        self.setUp()
        self._std.train()
        for i in range(5):
            self._std.process(self._metric.process())
        result = self._std.process_final({})
        self.assertAlmostEqual(self._target, result, places=5)

    def test_validate(self):
        self.setUp()
        self._std.eval()
        for i in range(5):
            self._std.process(self._metric.process())
        result = self._std.process_final({})
        self.assertAlmostEqual(self._target, result, places=5)

    def test_precision_error(self):
        self.setUp()
        self._std.train()
        val = torch.tensor([0.55])
        for i in range(2):
            self._std.process(val)

        result = self._std.process_final({})
        self.assertEqual(0, result)

    def setUpMoreDims(self):
        self._metric = Metric('test')
        self._metric.process = Mock()
        self._metric.process.side_effect = [torch.zeros(torch.Size([])),
                                            torch.FloatTensor([[0.1, 0.2, 0.3], [1.1, 1.2, 1.3]]),
                                            torch.FloatTensor([[0.4, 0.5, 0.6], [1.4, 1.5, 1.6]]),
                                            torch.FloatTensor([[0.7, 0.8, 0.9], [1.7, 1.8, 1.9]]),
                                            torch.ones(torch.Size([]))]
        self._std = Std('test')
        self._std.reset({})
        self._target = 0.57662804083742

    def test_more_dims(self):
        self.setUpMoreDims()
        for i in range(5):
            self._std.process(self._metric.process())
        result = self._std.process_final({})
        self.assertAlmostEqual(self._target, result, places=5)
Пример #3
0
class TestMean(unittest.TestCase):
    def setUp(self):
        self._metric = Metric('test')
        self._metric.process = Mock()
        self._metric.process.side_effect = [torch.zeros(torch.Size([])),
                                            torch.FloatTensor([0.1, 0.2, 0.3]),
                                            torch.FloatTensor([0.4, 0.5, 0.6]),
                                            torch.FloatTensor([0.7, 0.8, 0.9]),
                                            torch.ones(torch.Size([]))]

        self._mean = Mean('test')
        self._mean.reset({})
        self._target = 0.5

    def test_train_dict(self):
        self.setUp()
        self._mean.train()
        for i in range(5):
            self._mean.process(self._metric.process())
        result = self._mean.process_final({})
        self.assertAlmostEqual(self._target, result, places=5)

    def test_validate_dict(self):
        self.setUp()
        self._mean.eval()
        for i in range(5):
            self._mean.process(self._metric.process())
        result = self._mean.process_final({})
        self.assertAlmostEqual(self._target, result, places=5)

    def setUpMoreDims(self):
        self._metric = Metric('test')
        self._metric.process = Mock()
        self._metric.process.side_effect = [torch.zeros(torch.Size([])),
                                            torch.FloatTensor([[0.1, 0.2, 0.3], [1.1, 1.2, 1.3]]),
                                            torch.FloatTensor([[0.4, 0.5, 0.6], [1.4, 1.5, 1.6]]),
                                            torch.FloatTensor([[0.7, 0.8, 0.9], [1.7, 1.8, 1.9]]),
                                            torch.ones(torch.Size([]))]
        self._mean = Mean('test')
        self._mean.reset({})
        self._target = 0.95

    def test_more_dims(self):
        self.setUpMoreDims()
        for i in range(5):
            self._mean.process(self._metric.process())
        result = self._mean.process_final({})
        self.assertAlmostEqual(self._target, result, places=5)
Пример #4
0
 def test_process(self):
     my_mock = Metric('test')
     my_mock.process = Mock(return_value={'test': -1})
     metric = MetricList([my_mock])
     result = metric.process({'state': -1})
     self.assertEqual({'test': -1}, result)
     my_mock.process.assert_called_once_with({'state': -1})
Пример #5
0
    def test_main_loop_metrics(self):
        metric = Metric('test')
        metric.process = Mock(return_value={'test': 0})
        metric.process_final = Mock(return_value={'test': 0})
        metric.reset = Mock(return_value=None)

        data = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2])), (torch.Tensor([3]), torch.Tensor([3]))]
        generator = DataLoader(data)
        train_steps = len(data)

        epochs = 1

        callback = MagicMock()

        torchmodel = MagicMock()
        torchmodel.forward = Mock(return_value=1)
        optimizer = MagicMock()

        loss = torch.tensor([2], requires_grad=True)
        criterion = Mock(return_value=loss)

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])
        torchbearerstate = torchbearermodel.fit_generator(generator, train_steps, epochs, 0, [callback], initial_epoch=0, pass_state=False)

        torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].reset.assert_called_once()
        self.assertTrue(torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].process.call_count == len(data))
        torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].process_final.assert_called_once()
        self.assertTrue(torchbearerstate[torchbearer.METRICS]['test'] == 0)
Пример #6
0
    def test_test_loop_metrics(self):
        metric = Metric('test')
        metric.process = Mock(return_value={'test': 0})
        metric.process_final = Mock(return_value={'test': 0})
        metric.reset = Mock(return_value=None)
        metric_list = MetricList([metric])

        data = [(torch.Tensor([1]), torch.Tensor([1])), (torch.Tensor([2]), torch.Tensor([2])), (torch.Tensor([3]), torch.Tensor([3]))]
        validation_generator = DataLoader(data)
        validation_steps = len(data)

        callback = MagicMock()
        callback_List = torchbearer.CallbackList([callback])

        torchmodel = MagicMock()
        torchmodel.forward = Mock(return_value=1)
        optimizer = MagicMock()

        criterion = Mock(return_value=2)

        torchbearermodel = Model(torchmodel, optimizer, criterion, [metric])

        state = torchbearermodel.main_state.copy()
        state.update({torchbearer.METRIC_LIST: metric_list, torchbearer.VALIDATION_GENERATOR: validation_generator,
                 torchbearer.CallbackList: callback_List, torchbearer.MODEL: torchmodel, torchbearer.VALIDATION_STEPS: validation_steps,
                 torchbearer.CRITERION: criterion, torchbearer.STOP_TRAINING: False, torchbearer.METRICS: {}})

        torchbearerstate = torchbearermodel._test_loop(state, callback_List, False, Model._load_batch_standard, num_steps=None)

        torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].reset.assert_called_once()
        self.assertTrue(torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].process.call_count == len(data))
        torchbearerstate[torchbearer.METRIC_LIST].metric_list[0].process_final.assert_called_once()
        self.assertTrue(torchbearerstate[torchbearer.METRICS]['test'] == 0)
Пример #7
0
    def test_process(self):
        root = Metric('test')
        root.process = Mock(return_value='test')
        leaf1 = Metric('test')
        leaf1.process = Mock(return_value={'test': 10})
        leaf2 = Metric('test')
        leaf2.process = Mock(return_value=None)

        tree = MetricTree(root)
        tree.add_child(leaf1)
        tree.add_child(leaf2)

        self.assertTrue(tree.process('args') == {'test': 10})

        root.process.assert_called_once_with('args')
        leaf1.process.assert_called_once_with('test')
        leaf2.process.assert_called_once_with('test')