diff --git a/python/paddle/tests/test_datasets.py b/python/paddle/tests/test_datasets.py index 3aa21ae2db267d40d4483b27ed89b0da349ce0db..89fa01cbceb45c4812cbe6363c9ae0af9ed6ae7d 100644 --- a/python/paddle/tests/test_datasets.py +++ b/python/paddle/tests/test_datasets.py @@ -94,13 +94,13 @@ class TestMNISTTest(unittest.TestCase): mnist = MNIST(mode='test', transform=transform) self.assertTrue(len(mnist) == 10000) - for i in range(len(mnist)): - image, label = mnist[i] - self.assertTrue(image.shape[0] == 1) - self.assertTrue(image.shape[1] == 28) - self.assertTrue(image.shape[2] == 28) - self.assertTrue(label.shape[0] == 1) - self.assertTrue(0 <= int(label) <= 9) + i = np.random.randint(0, len(mnist) - 1) + image, label = mnist[i] + self.assertTrue(image.shape[0] == 1) + self.assertTrue(image.shape[1] == 28) + self.assertTrue(image.shape[2] == 28) + self.assertTrue(label.shape[0] == 1) + self.assertTrue(0 <= int(label) <= 9) class TestMNISTTrain(unittest.TestCase): @@ -109,13 +109,13 @@ class TestMNISTTrain(unittest.TestCase): mnist = MNIST(mode='train', transform=transform) self.assertTrue(len(mnist) == 60000) - for i in range(len(mnist)): - image, label = mnist[i] - self.assertTrue(image.shape[0] == 1) - self.assertTrue(image.shape[1] == 28) - self.assertTrue(image.shape[2] == 28) - self.assertTrue(label.shape[0] == 1) - self.assertTrue(0 <= int(label) <= 9) + i = np.random.randint(0, len(mnist) - 1) + image, label = mnist[i] + self.assertTrue(image.shape[0] == 1) + self.assertTrue(image.shape[1] == 28) + self.assertTrue(image.shape[2] == 28) + self.assertTrue(label.shape[0] == 1) + self.assertTrue(0 <= int(label) <= 9) # test cv2 backend mnist = MNIST(mode='train', transform=transform, backend='cv2') @@ -140,13 +140,13 @@ class TestFASHIONMNISTTest(unittest.TestCase): mnist = FashionMNIST(mode='test', transform=transform) self.assertTrue(len(mnist) == 10000) - for i in range(len(mnist)): - image, label = mnist[i] - self.assertTrue(image.shape[0] == 1) - self.assertTrue(image.shape[1] == 28) - self.assertTrue(image.shape[2] == 28) - self.assertTrue(label.shape[0] == 1) - self.assertTrue(0 <= int(label) <= 9) + i = np.random.randint(0, len(mnist) - 1) + image, label = mnist[i] + self.assertTrue(image.shape[0] == 1) + self.assertTrue(image.shape[1] == 28) + self.assertTrue(image.shape[2] == 28) + self.assertTrue(label.shape[0] == 1) + self.assertTrue(0 <= int(label) <= 9) class TestFASHIONMNISTTrain(unittest.TestCase): @@ -155,13 +155,13 @@ class TestFASHIONMNISTTrain(unittest.TestCase): mnist = FashionMNIST(mode='train', transform=transform) self.assertTrue(len(mnist) == 60000) - for i in range(len(mnist)): - image, label = mnist[i] - self.assertTrue(image.shape[0] == 1) - self.assertTrue(image.shape[1] == 28) - self.assertTrue(image.shape[2] == 28) - self.assertTrue(label.shape[0] == 1) - self.assertTrue(0 <= int(label) <= 9) + i = np.random.randint(0, len(mnist) - 1) + image, label = mnist[i] + self.assertTrue(image.shape[0] == 1) + self.assertTrue(image.shape[1] == 28) + self.assertTrue(image.shape[2] == 28) + self.assertTrue(label.shape[0] == 1) + self.assertTrue(0 <= int(label) <= 9) # test cv2 backend mnist = FashionMNIST(mode='train', transform=transform, backend='cv2')