def test_swap_image_channel(self): x = np.random.rand(1, 10, 10) out = swap_image_channel(x) self.assertTrue(out.shape == (10, 10, 1)) x = np.random.rand(1, 10, 10, 3) out = swap_image_channel(x) self.assertTrue(out.shape == (1, 3, 10, 10))
def test_DataContainer_CIFAR10(self): dataname = 'CIFAR10' p = { 'range': (0., 1.), 'num_classes': 10, 'type': 'image', 'dim': (3, 32, 32), 'mean': [0.49139947, 0.48215836, 0.44653094], 'std': [0.20230092, 0.1994128, 0.20096162], 'train_shape': (50000, 32, 32, 3), 'test_shape': (10000, 32, 32, 3), } dc = self.init_datacontainer(dataname) # basic props mu = dc.train_mean np.testing.assert_array_almost_equal(mu, p['mean']) std = dc.train_std np.testing.assert_array_almost_equal(std, p['std']) # train set train_np = dc.x_train self.assertEqual(train_np.shape, p['train_shape']) y_np = dc.y_train self.assertEqual(y_np.shape, (p['train_shape'][0], )) train_loader = dc.get_dataloader(batch_size=8, is_train=True, shuffle=False) self.assertEqual(len(train_loader.dataset), p['train_shape'][0]) x_pt, y_pt = next(iter(train_loader)) self.assertEqual(x_pt.size(), tuple([8] + list(p['dim']))) self.assertEqual(y_pt.size(), (8, )) x1 = train_np[:8] x2 = swap_image_channel(x_pt.cpu().detach().numpy()) np.testing.assert_equal(x1, x2) y1 = y_np[:8] y2 = y_pt[:8].cpu().detach().numpy() np.testing.assert_equal(y1, y2) # test set test_np = dc.x_test self.assertEqual(test_np.shape, p['test_shape']) y_np = dc.y_test self.assertEqual(y_np.shape, (p['test_shape'][0], )) test_loader = dc.get_dataloader(batch_size=8, is_train=False, shuffle=False) self.assertEqual(len(test_loader.dataset), p['test_shape'][0]) x_pt, y_pt = next(iter(test_loader)) self.assertEqual(x_pt.size(), tuple([8] + list(p['dim']))) self.assertEqual(y_pt.size(), (8, )) x1 = test_np[:8] x2 = swap_image_channel(x_pt.cpu().detach().numpy()) np.testing.assert_equal(x1, x2) y1 = y_np[:8] y2 = y_pt[:8].cpu().detach().numpy() np.testing.assert_equal(y1, y2)
def test_DataContainer_SVHN(self): dataname = 'SVHN' p = { 'range': (0., 1.), 'num_classes': 10, 'type': 'image', 'dim': (3, 32, 32), 'mean': [0.43768215, 0.4437698, 0.47280422], 'std': [0.12008651, 0.12313706, 0.10520393], 'train_shape': (73257, 32, 32, 3), 'test_shape': (26032, 32, 32, 3), } dc = self.init_datacontainer(dataname) # train set train_np = dc.x_train self.assertEqual(train_np.shape, p['train_shape']) y_np = dc.y_train self.assertEqual(y_np.shape, (p['train_shape'][0], )) train_loader = dc.get_dataloader(batch_size=8, is_train=True, shuffle=False) self.assertEqual(len(train_loader.dataset), p['train_shape'][0]) x_pt, y_pt = next(iter(train_loader)) self.assertEqual(x_pt.size(), tuple([8] + list(p['dim']))) self.assertEqual(y_pt.size(), (8, )) x1 = train_np[:8] x2 = swap_image_channel(x_pt.cpu().detach().numpy()) np.testing.assert_equal(x1, x2) y1 = y_np[:8] y2 = y_pt[:8].cpu().detach().numpy() np.testing.assert_equal(y1, y2)
def test_load(self): self.mc.save(os.path.join('test', 'test_mnist'), overwrite=True) full_path = os.path.join('save', 'test', 'test_mnist.pt') self.mc2.load(full_path) x = torch.tensor(swap_image_channel(self.x)).to(self.mc.device) s1 = self.mc.model(x).cpu().detach().numpy() s2 = self.mc2.model(x).cpu().detach().numpy() np.testing.assert_almost_equal(s1, s2)
def test_predict_one(self): x = torch.tensor(swap_image_channel(self.x[0])).to(self.mc.device) p1 = self.mc.predict_one(x) p2 = self.mc.predict_one(self.x[0]) self.assertTrue((p1 == p2).all())
def test_DataContainer_MNIST(self): dataname = 'MNIST' p = { 'range': (0., 1.), 'num_classes': 10, 'type': 'image', 'dim': (1, 28, 28), 'mean': [0.13066046], 'std': [0.30150425], 'train_shape': (60000, 28, 28, 1), 'test_shape': (10000, 28, 28, 1) } dc = self.init_datacontainer(dataname) # basic props r = dc.data_range self.assertEqual(r, p['range']) name = dc.name self.assertEqual(name, dataname) m = dc.num_classes self.assertEqual(m, p['num_classes']) dtype = dc.data_type self.assertEqual(dtype, p['type']) dim = dc.dim_data self.assertEqual(dim, p['dim']) mu = dc.train_mean np.testing.assert_array_almost_equal(mu, p['mean']) std = dc.train_std np.testing.assert_array_almost_equal(std, p['std']) # train set train_np = dc.x_train self.assertEqual(train_np.shape, p['train_shape']) y_np = dc.y_train self.assertEqual(y_np.shape, (p['train_shape'][0], )) train_loader = dc.get_dataloader(batch_size=8, is_train=True, shuffle=False) self.assertEqual(len(train_loader.dataset), p['train_shape'][0]) x_pt, y_pt = next(iter(train_loader)) self.assertEqual(x_pt.size(), tuple([8] + list(p['dim']))) self.assertEqual(y_pt.size(), (8, )) x1 = train_np[:8] x2 = swap_image_channel(x_pt.cpu().detach().numpy()) np.testing.assert_equal(x1, x2) y1 = y_np[:8] y2 = y_pt[:8].cpu().detach().numpy() np.testing.assert_equal(y1, y2) # test set test_np = dc.x_test self.assertEqual(test_np.shape, p['test_shape']) y_np = dc.y_test self.assertEqual(y_np.shape, (p['test_shape'][0], )) test_loader = dc.get_dataloader(batch_size=8, is_train=False, shuffle=False) self.assertEqual(len(test_loader.dataset), p['test_shape'][0]) x_pt, y_pt = next(iter(test_loader)) self.assertEqual(x_pt.size(), tuple([8] + list(p['dim']))) self.assertEqual(y_pt.size(), (8, )) x1 = test_np[:8] x2 = swap_image_channel(x_pt.cpu().detach().numpy()) np.testing.assert_equal(x1, x2) y1 = y_np[:8] y2 = y_pt[:8].cpu().detach().numpy() np.testing.assert_equal(y1, y2)