Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this one, as you're already declaring another RandomState below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad yeah i missed that one


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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this one too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they come from a rather chaotic git revert :) should have checked before pushing though


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