(self)
| 22 | |
| 23 | class TestMixup(unittest.TestCase): |
| 24 | def test_mixup(self): |
| 25 | for dims in [2, 3]: |
| 26 | shape = (6, 3) + (32,) * dims |
| 27 | sample = torch.rand(*shape, dtype=torch.float32) |
| 28 | mixup = MixUp(6, 1.0) |
| 29 | mixup.set_random_state(seed=0) |
| 30 | output = mixup(sample) |
| 31 | np.random.seed(0) |
| 32 | # simulate the randomize() of transform |
| 33 | np.random.random() |
| 34 | weight = torch.from_numpy(np.random.beta(1.0, 1.0, 6)).type(torch.float32) |
| 35 | perm = np.random.permutation(6) |
| 36 | self.assertEqual(output.shape, sample.shape) |
| 37 | mixweight = weight[(Ellipsis,) + (None,) * (dims + 1)] |
| 38 | expected = mixweight * sample + (1 - mixweight) * sample[perm, ...] |
| 39 | assert_allclose(output, expected, type_test=False, atol=1e-7) |
| 40 | |
| 41 | with self.assertRaises(ValueError): |
| 42 | MixUp(6, -0.5) |
| 43 | |
| 44 | mixup = MixUp(6, 0.5) |
| 45 | for dims in [2, 3]: |
| 46 | with self.assertRaises(ValueError): |
| 47 | shape = (5, 3) + (32,) * dims |
| 48 | sample = torch.rand(*shape, dtype=torch.float32) |
| 49 | mixup(sample) |
| 50 | |
| 51 | def test_mixupd(self): |
| 52 | for dims in [2, 3]: |
nothing calls this directly
no test coverage detected