Unverified Commit 3fa2055b authored by Vincent Moens's avatar Vincent Moens Committed by GitHub
Browse files

using `np.random.RandomState(seed)` instead of `np.random.seed(seed)` (#4250)


Co-authored-by: default avatarVincent Moens <vmoens@fb.com>
parent f88985b5
......@@ -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):
......
......@@ -444,8 +444,9 @@ class CIFAR10TestCase(datasets_utils.ImageDatasetTestCase):
)
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):
......
......@@ -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)
......
......@@ -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 @@ class TestToTensor:
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 @@ class TestToTensor:
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 @@ class TestToTensor:
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)
......
......@@ -131,7 +131,8 @@ class TestVideoTransforms():
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):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment