Skip to content

Commit

Permalink
[fbsync] using np.random.RandomState(seed) instead of `np.random.se…
Browse files Browse the repository at this point in the history
…ed(seed)` (#4250)

Summary: Co-authored-by: Vincent Moens <vmoens@fb.com>

Reviewed By: NicolasHug

Differential Revision: D30417196

fbshipit-source-id: f53bc950aea4935c164939cab0e14b266e3dd1cb
  • Loading branch information
yiwen-song authored and facebook-github-bot committed Aug 20, 2021
1 parent d36e8c5 commit a42159c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
1 change: 0 additions & 1 deletion test/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion test/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions test/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
30 changes: 18 additions & 12 deletions test/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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():
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/test_transforms_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a42159c

Please sign in to comment.