diff --git a/test/common_utils.py b/test/common_utils.py index a8f5a91ef6b..5936ae1f713 100644 --- a/test/common_utils.py +++ b/test/common_utils.py @@ -44,7 +44,6 @@ def get_tmp_dir(src=None, **kwargs): def set_rng_seed(seed): torch.manual_seed(seed) random.seed(seed) - np.random.seed(seed) class MapNestedTensorObjectImpl(object): diff --git a/test/test_datasets.py b/test/test_datasets.py index a8a7f2b0e5c..5b7eabc4cb1 100644 --- a/test/test_datasets.py +++ b/test/test_datasets.py @@ -444,8 +444,9 @@ def inject_fake_data(self, tmpdir, config): ) def _create_batch_file(self, root, name, num_images): + np_rng = np.random.RandomState(0) data = datasets_utils.create_image_or_video_tensor((num_images, 32 * 32 * 3)) - labels = np.random.randint(0, self._VERSION_CONFIG["num_categories"], size=num_images).tolist() + labels = np_rng.randint(0, self._VERSION_CONFIG["num_categories"], size=num_images).tolist() self._create_binary_file(root, name, {"data": data, self._VERSION_CONFIG["labels_key"]: labels}) def _create_meta_file(self, root): diff --git a/test/test_image.py b/test/test_image.py index 2e427af26af..47023a45be2 100644 --- a/test/test_image.py +++ b/test/test_image.py @@ -272,9 +272,10 @@ def test_write_file_non_ascii(): (105, 105), ]) def test_read_1_bit_png(shape): + np_rng = np.random.RandomState(0) with get_tmp_dir() as root: image_path = os.path.join(root, f'test_{shape}.png') - pixels = np.random.rand(*shape) > 0.5 + pixels = np_rng.rand(*shape) > 0.5 img = Image.fromarray(pixels) img.save(image_path) img1 = read_image(image_path) @@ -292,9 +293,10 @@ def test_read_1_bit_png(shape): ImageReadMode.GRAY, ]) def test_read_1_bit_png_consistency(shape, mode): + np_rng = np.random.RandomState(0) with get_tmp_dir() as root: image_path = os.path.join(root, f'test_{shape}.png') - pixels = np.random.rand(*shape) > 0.5 + pixels = np_rng.rand(*shape) > 0.5 img = Image.fromarray(pixels) img.save(image_path) img1 = read_image(image_path, mode) diff --git a/test/test_transforms.py b/test/test_transforms.py index 74757bcb4e6..c5cc80ef87e 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -200,18 +200,19 @@ class TestToTensor: def test_to_tensor(self, channels): height, width = 4, 4 trans = transforms.ToTensor() + np_rng = np.random.RandomState(0) input_data = torch.ByteTensor(channels, height, width).random_(0, 255).float().div_(255) img = transforms.ToPILImage()(input_data) output = trans(img) torch.testing.assert_close(output, input_data) - ndarray = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) + ndarray = np_rng.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) output = trans(ndarray) expected_output = ndarray.transpose((2, 0, 1)) / 255.0 torch.testing.assert_close(output.numpy(), expected_output, check_dtype=False) - ndarray = np.random.rand(height, width, channels).astype(np.float32) + ndarray = np_rng.rand(height, width, channels).astype(np.float32) output = trans(ndarray) expected_output = ndarray.transpose((2, 0, 1)) torch.testing.assert_close(output.numpy(), expected_output, check_dtype=False) @@ -225,22 +226,24 @@ def test_to_tensor(self, channels): def test_to_tensor_errors(self): height, width = 4, 4 trans = transforms.ToTensor() + np_rng = np.random.RandomState(0) with pytest.raises(TypeError): - trans(np.random.rand(1, height, width).tolist()) + trans(np_rng.rand(1, height, width).tolist()) with pytest.raises(ValueError): - trans(np.random.rand(height)) + trans(np_rng.rand(height)) with pytest.raises(ValueError): - trans(np.random.rand(1, 1, height, width)) + trans(np_rng.rand(1, 1, height, width)) @pytest.mark.parametrize('dtype', [torch.float16, torch.float, torch.double]) def test_to_tensor_with_other_default_dtypes(self, dtype): + np_rng = np.random.RandomState(0) current_def_dtype = torch.get_default_dtype() t = transforms.ToTensor() - np_arr = np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8) + np_arr = np_rng.randint(0, 255, (32, 32, 3), dtype=np.uint8) img = Image.fromarray(np_arr) torch.set_default_dtype(dtype) @@ -253,19 +256,20 @@ def test_to_tensor_with_other_default_dtypes(self, dtype): def test_pil_to_tensor(self, channels): height, width = 4, 4 trans = transforms.PILToTensor() + np_rng = np.random.RandomState(0) input_data = torch.ByteTensor(channels, height, width).random_(0, 255) img = transforms.ToPILImage()(input_data) output = trans(img) torch.testing.assert_close(input_data, output) - input_data = np.random.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) + input_data = np_rng.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) img = transforms.ToPILImage()(input_data) output = trans(img) expected_output = input_data.transpose((2, 0, 1)) torch.testing.assert_close(output.numpy(), expected_output) - input_data = torch.as_tensor(np.random.rand(channels, height, width).astype(np.float32)) + input_data = torch.as_tensor(np_rng.rand(channels, height, width).astype(np.float32)) img = transforms.ToPILImage()(input_data) # CHW -> HWC and (* 255).byte() output = trans(img) # HWC -> CHW expected_output = (input_data * 255).byte() @@ -280,12 +284,13 @@ def test_pil_to_tensor(self, channels): def test_pil_to_tensor_errors(self): height, width = 4, 4 trans = transforms.PILToTensor() + np_rng = np.random.RandomState(0) with pytest.raises(TypeError): - trans(np.random.rand(1, height, width).tolist()) + trans(np_rng.rand(1, height, width).tolist()) with pytest.raises(TypeError): - trans(np.random.rand(1, height, width)) + trans(np_rng.rand(1, height, width)) def test_randomresized_params(): @@ -1180,10 +1185,11 @@ def test_random_grayscale(): """Unit tests for random grayscale transform""" # Test Set 1: RGB -> 3 channel grayscale + np_rng = np.random.RandomState(0) random_state = random.getstate() random.seed(42) x_shape = [2, 2, 3] - x_np = np.random.randint(0, 256, x_shape, np.uint8) + x_np = np_rng.randint(0, 256, x_shape, np.uint8) x_pil = Image.fromarray(x_np, mode='RGB') x_pil_2 = x_pil.convert('L') gray_np = np.array(x_pil_2) @@ -1206,7 +1212,7 @@ def test_random_grayscale(): random_state = random.getstate() random.seed(42) x_shape = [2, 2, 3] - x_np = np.random.randint(0, 256, x_shape, np.uint8) + x_np = np_rng.randint(0, 256, x_shape, np.uint8) x_pil = Image.fromarray(x_np, mode='RGB') x_pil_2 = x_pil.convert('L') gray_np = np.array(x_pil_2) diff --git a/test/test_transforms_video.py b/test/test_transforms_video.py index 1b6b85a29ba..5ae9192c4b6 100644 --- a/test/test_transforms_video.py +++ b/test/test_transforms_video.py @@ -131,7 +131,8 @@ def test_to_tensor_video(self): trans = transforms.ToTensorVideo() with pytest.raises(TypeError): - trans(np.random.rand(numFrames, height, width, 1).tolist()) + np_rng = np.random.RandomState(0) + trans(np_rng.rand(numFrames, height, width, 1).tolist()) trans(torch.rand((numFrames, height, width, 1), dtype=torch.float)) with pytest.raises(ValueError):