From 193b3b6a266c9934418b72b79f7ccf8ba2d848ad Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Wed, 4 Aug 2021 16:03:01 +0100 Subject: [PATCH 1/9] using np.random.RandomState(seed) instead of np.random.seed(seed) --- test/common_utils.py | 1 - test/test_datasets.py | 4 +++- test/test_image.py | 6 ++++-- test/test_transforms.py | 25 +++++++++++++------------ test/test_transforms_video.py | 4 +++- 5 files changed, 23 insertions(+), 17 deletions(-) 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..3013eb019a3 100644 --- a/test/test_datasets.py +++ b/test/test_datasets.py @@ -21,6 +21,8 @@ import torch.nn.functional as F from torchvision import datasets +random_state_numpy = np.random.RandomState(0) + class STL10TestCase(datasets_utils.ImageDatasetTestCase): DATASET_CLASS = datasets.STL10 @@ -445,7 +447,7 @@ def inject_fake_data(self, tmpdir, config): def _create_batch_file(self, root, name, num_images): 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 = random_state_numpy.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..dd71e8ee822 100644 --- a/test/test_image.py +++ b/test/test_image.py @@ -23,6 +23,8 @@ IS_WINDOWS = sys.platform in ('win32', 'cygwin') PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION.split('.')) +random_state_numpy = np.random.RandomState(0) + def _get_safe_image_name(name): # Used when we need to change the pytest "id" for an "image path" parameter. @@ -274,7 +276,7 @@ def test_write_file_non_ascii(): def test_read_1_bit_png(shape): with get_tmp_dir() as root: image_path = os.path.join(root, f'test_{shape}.png') - pixels = np.random.rand(*shape) > 0.5 + pixels = random_state_numpy.rand(*shape) > 0.5 img = Image.fromarray(pixels) img.save(image_path) img1 = read_image(image_path) @@ -294,7 +296,7 @@ def test_read_1_bit_png(shape): def test_read_1_bit_png_consistency(shape, mode): with get_tmp_dir() as root: image_path = os.path.join(root, f'test_{shape}.png') - pixels = np.random.rand(*shape) > 0.5 + pixels = random_state_numpy.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..eed78fd5e80 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -21,6 +21,7 @@ from common_utils import cycle_over, int_dtypes, float_dtypes, assert_equal +random_state_numpy = np.random.RandomState(0) GRACE_HOPPER = get_file_path_2( os.path.dirname(os.path.abspath(__file__)), 'assets', 'encode_jpeg', 'grace_hopper_517x606.jpg') @@ -206,12 +207,12 @@ def test_to_tensor(self, channels): 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 = random_state_numpy.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 = random_state_numpy.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) @@ -227,20 +228,20 @@ def test_to_tensor_errors(self): trans = transforms.ToTensor() with pytest.raises(TypeError): - trans(np.random.rand(1, height, width).tolist()) + trans(random_state_numpy.rand(1, height, width).tolist()) with pytest.raises(ValueError): - trans(np.random.rand(height)) + trans(random_state_numpy.rand(height)) with pytest.raises(ValueError): - trans(np.random.rand(1, 1, height, width)) + trans(random_state_numpy.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): 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 = random_state_numpy.randint(0, 255, (32, 32, 3), dtype=np.uint8) img = Image.fromarray(np_arr) torch.set_default_dtype(dtype) @@ -259,13 +260,13 @@ def test_pil_to_tensor(self, channels): 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 = random_state_numpy.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(random_state_numpy.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() @@ -282,10 +283,10 @@ def test_pil_to_tensor_errors(self): trans = transforms.PILToTensor() with pytest.raises(TypeError): - trans(np.random.rand(1, height, width).tolist()) + trans(random_state_numpy.rand(1, height, width).tolist()) with pytest.raises(TypeError): - trans(np.random.rand(1, height, width)) + trans(random_state_numpy.rand(1, height, width)) def test_randomresized_params(): @@ -1183,7 +1184,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 = random_state_numpy.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 +1207,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 = random_state_numpy.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..5e5a74c7190 100644 --- a/test/test_transforms_video.py +++ b/test/test_transforms_video.py @@ -16,6 +16,8 @@ warnings.simplefilter("always") import torchvision.transforms._transforms_video as transforms +random_state_numpy = np.random.RandomState(0) + class TestVideoTransforms(): @@ -131,7 +133,7 @@ def test_to_tensor_video(self): trans = transforms.ToTensorVideo() with pytest.raises(TypeError): - trans(np.random.rand(numFrames, height, width, 1).tolist()) + trans(random_state_numpy.rand(numFrames, height, width, 1).tolist()) trans(torch.rand((numFrames, height, width, 1), dtype=torch.float)) with pytest.raises(ValueError): From d5dda533be660191fd3693e066bc0226b41b8cfc Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Wed, 4 Aug 2021 16:03:01 +0100 Subject: [PATCH 2/9] using np.random.RandomState(seed) instead of np.random.seed(seed) --- test/common_utils.py | 1 - test/test_datasets.py | 4 +++- test/test_image.py | 6 ++++-- test/test_transforms.py | 25 +++++++++++++------------ test/test_transforms_video.py | 4 +++- 5 files changed, 23 insertions(+), 17 deletions(-) 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..3013eb019a3 100644 --- a/test/test_datasets.py +++ b/test/test_datasets.py @@ -21,6 +21,8 @@ import torch.nn.functional as F from torchvision import datasets +random_state_numpy = np.random.RandomState(0) + class STL10TestCase(datasets_utils.ImageDatasetTestCase): DATASET_CLASS = datasets.STL10 @@ -445,7 +447,7 @@ def inject_fake_data(self, tmpdir, config): def _create_batch_file(self, root, name, num_images): 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 = random_state_numpy.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..dd71e8ee822 100644 --- a/test/test_image.py +++ b/test/test_image.py @@ -23,6 +23,8 @@ IS_WINDOWS = sys.platform in ('win32', 'cygwin') PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION.split('.')) +random_state_numpy = np.random.RandomState(0) + def _get_safe_image_name(name): # Used when we need to change the pytest "id" for an "image path" parameter. @@ -274,7 +276,7 @@ def test_write_file_non_ascii(): def test_read_1_bit_png(shape): with get_tmp_dir() as root: image_path = os.path.join(root, f'test_{shape}.png') - pixels = np.random.rand(*shape) > 0.5 + pixels = random_state_numpy.rand(*shape) > 0.5 img = Image.fromarray(pixels) img.save(image_path) img1 = read_image(image_path) @@ -294,7 +296,7 @@ def test_read_1_bit_png(shape): def test_read_1_bit_png_consistency(shape, mode): with get_tmp_dir() as root: image_path = os.path.join(root, f'test_{shape}.png') - pixels = np.random.rand(*shape) > 0.5 + pixels = random_state_numpy.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..eed78fd5e80 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -21,6 +21,7 @@ from common_utils import cycle_over, int_dtypes, float_dtypes, assert_equal +random_state_numpy = np.random.RandomState(0) GRACE_HOPPER = get_file_path_2( os.path.dirname(os.path.abspath(__file__)), 'assets', 'encode_jpeg', 'grace_hopper_517x606.jpg') @@ -206,12 +207,12 @@ def test_to_tensor(self, channels): 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 = random_state_numpy.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 = random_state_numpy.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) @@ -227,20 +228,20 @@ def test_to_tensor_errors(self): trans = transforms.ToTensor() with pytest.raises(TypeError): - trans(np.random.rand(1, height, width).tolist()) + trans(random_state_numpy.rand(1, height, width).tolist()) with pytest.raises(ValueError): - trans(np.random.rand(height)) + trans(random_state_numpy.rand(height)) with pytest.raises(ValueError): - trans(np.random.rand(1, 1, height, width)) + trans(random_state_numpy.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): 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 = random_state_numpy.randint(0, 255, (32, 32, 3), dtype=np.uint8) img = Image.fromarray(np_arr) torch.set_default_dtype(dtype) @@ -259,13 +260,13 @@ def test_pil_to_tensor(self, channels): 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 = random_state_numpy.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(random_state_numpy.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() @@ -282,10 +283,10 @@ def test_pil_to_tensor_errors(self): trans = transforms.PILToTensor() with pytest.raises(TypeError): - trans(np.random.rand(1, height, width).tolist()) + trans(random_state_numpy.rand(1, height, width).tolist()) with pytest.raises(TypeError): - trans(np.random.rand(1, height, width)) + trans(random_state_numpy.rand(1, height, width)) def test_randomresized_params(): @@ -1183,7 +1184,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 = random_state_numpy.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 +1207,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 = random_state_numpy.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..5e5a74c7190 100644 --- a/test/test_transforms_video.py +++ b/test/test_transforms_video.py @@ -16,6 +16,8 @@ warnings.simplefilter("always") import torchvision.transforms._transforms_video as transforms +random_state_numpy = np.random.RandomState(0) + class TestVideoTransforms(): @@ -131,7 +133,7 @@ def test_to_tensor_video(self): trans = transforms.ToTensorVideo() with pytest.raises(TypeError): - trans(np.random.rand(numFrames, height, width, 1).tolist()) + trans(random_state_numpy.rand(numFrames, height, width, 1).tolist()) trans(torch.rand((numFrames, height, width, 1), dtype=torch.float)) with pytest.raises(ValueError): From 6659e829604aed6468b88b889e6b62898e501507 Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Thu, 5 Aug 2021 09:52:26 +0100 Subject: [PATCH 3/9] passing random_state_numpy to inception_v3 amend --- test/test_models.py | 8 ++++++-- torchvision/models/inception.py | 8 ++++++-- torchvision/models/quantization/inception.py | 5 +++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/test/test_models.py b/test/test_models.py index 72ae68f5615..fa24d984c88 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -14,10 +14,13 @@ import pytest import warnings import traceback +import numpy as np ACCEPT = os.getenv('EXPECTTEST_ACCEPT', '0') == '1' +random_state_numpy = np.random.RandomState(0) + def get_available_classification_models(): # TODO add a registration mechanism to torchvision.models @@ -84,7 +87,7 @@ def _assert_expected(output, name, prec): else: expected = torch.load(expected_file) rtol = atol = prec - torch.testing.assert_close(output, expected, rtol=rtol, atol=atol, check_dtype=False) + torch.testing.assert_close(output, expected, rtol=rtol, atol=atol, check_dtype=False, msg=f'for query {name}') def _check_jit_scriptable(nn_module, args, unwrapper=None, skip=False): @@ -193,7 +196,8 @@ def _check_fx_compatible(model, inputs): # the _test_*_model methods. _model_params = { 'inception_v3': { - 'input_shape': (1, 3, 299, 299) + 'input_shape': (1, 3, 299, 299), + 'random_state_numpy': random_state_numpy, }, 'retinanet_resnet50_fpn': { 'num_classes': 20, diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index b9c6ab74534..78ed30c4f10 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -1,6 +1,7 @@ from collections import namedtuple import warnings import torch +import numpy as np from torch import nn, Tensor import torch.nn.functional as F from .._internally_replaced_utils import load_state_dict_from_url @@ -69,7 +70,8 @@ def __init__( aux_logits: bool = True, transform_input: bool = False, inception_blocks: Optional[List[Callable[..., nn.Module]]] = None, - init_weights: Optional[bool] = None + init_weights: Optional[bool] = None, + random_state_numpy: np.random.RandomState = np.random.RandomState(), ) -> None: super(Inception3, self).__init__() if inception_blocks is None: @@ -123,7 +125,9 @@ def __init__( import scipy.stats as stats stddev = m.stddev if hasattr(m, 'stddev') else 0.1 X = stats.truncnorm(-2, 2, scale=stddev) - values = torch.as_tensor(X.rvs(m.weight.numel()), dtype=m.weight.dtype) + values = torch.as_tensor( + X.rvs(m.weight.numel(), random_state=random_state_numpy), + dtype=m.weight.dtype) values = values.view(m.weight.size()) with torch.no_grad(): m.weight.copy_(values) diff --git a/torchvision/models/quantization/inception.py b/torchvision/models/quantization/inception.py index 833d8fb8b75..6d5a8ac1c6d 100644 --- a/torchvision/models/quantization/inception.py +++ b/torchvision/models/quantization/inception.py @@ -177,7 +177,7 @@ def __init__(self, *args, **kwargs): class QuantizableInception3(inception_module.Inception3): - def __init__(self, num_classes=1000, aux_logits=True, transform_input=False): + def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, **kwargs): super(QuantizableInception3, self).__init__( num_classes=num_classes, aux_logits=aux_logits, @@ -190,7 +190,8 @@ def __init__(self, num_classes=1000, aux_logits=True, transform_input=False): QuantizableInceptionD, QuantizableInceptionE, QuantizableInceptionAux - ] + ], + **kwargs ) self.quant = torch.quantization.QuantStub() self.dequant = torch.quantization.DeQuantStub() From 746d612b97b65c85dfc70728114dc530c2aa3131 Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Thu, 5 Aug 2021 11:18:37 +0100 Subject: [PATCH 4/9] reverting inception random_state kwarg --- test/test_models.py | 5 ----- torchvision/models/inception.py | 8 ++------ torchvision/models/quantization/inception.py | 5 ++--- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/test/test_models.py b/test/test_models.py index fa24d984c88..3f9604621c7 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -1,6 +1,5 @@ import os import io -import sys from common_utils import map_nested_tensor_object, freeze_rng_state, set_rng_seed, cpu_and_gpu, needs_cuda from _utils_internal import get_relative_path from collections import OrderedDict @@ -14,13 +13,10 @@ import pytest import warnings import traceback -import numpy as np ACCEPT = os.getenv('EXPECTTEST_ACCEPT', '0') == '1' -random_state_numpy = np.random.RandomState(0) - def get_available_classification_models(): # TODO add a registration mechanism to torchvision.models @@ -197,7 +193,6 @@ def _check_fx_compatible(model, inputs): _model_params = { 'inception_v3': { 'input_shape': (1, 3, 299, 299), - 'random_state_numpy': random_state_numpy, }, 'retinanet_resnet50_fpn': { 'num_classes': 20, diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index 78ed30c4f10..b9c6ab74534 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -1,7 +1,6 @@ from collections import namedtuple import warnings import torch -import numpy as np from torch import nn, Tensor import torch.nn.functional as F from .._internally_replaced_utils import load_state_dict_from_url @@ -70,8 +69,7 @@ def __init__( aux_logits: bool = True, transform_input: bool = False, inception_blocks: Optional[List[Callable[..., nn.Module]]] = None, - init_weights: Optional[bool] = None, - random_state_numpy: np.random.RandomState = np.random.RandomState(), + init_weights: Optional[bool] = None ) -> None: super(Inception3, self).__init__() if inception_blocks is None: @@ -125,9 +123,7 @@ def __init__( import scipy.stats as stats stddev = m.stddev if hasattr(m, 'stddev') else 0.1 X = stats.truncnorm(-2, 2, scale=stddev) - values = torch.as_tensor( - X.rvs(m.weight.numel(), random_state=random_state_numpy), - dtype=m.weight.dtype) + values = torch.as_tensor(X.rvs(m.weight.numel()), dtype=m.weight.dtype) values = values.view(m.weight.size()) with torch.no_grad(): m.weight.copy_(values) diff --git a/torchvision/models/quantization/inception.py b/torchvision/models/quantization/inception.py index 6d5a8ac1c6d..833d8fb8b75 100644 --- a/torchvision/models/quantization/inception.py +++ b/torchvision/models/quantization/inception.py @@ -177,7 +177,7 @@ def __init__(self, *args, **kwargs): class QuantizableInception3(inception_module.Inception3): - def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, **kwargs): + def __init__(self, num_classes=1000, aux_logits=True, transform_input=False): super(QuantizableInception3, self).__init__( num_classes=num_classes, aux_logits=aux_logits, @@ -190,8 +190,7 @@ def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, **k QuantizableInceptionD, QuantizableInceptionE, QuantizableInceptionAux - ], - **kwargs + ] ) self.quant = torch.quantization.QuantStub() self.dequant = torch.quantization.DeQuantStub() From 65dcd02373c855f13c227cf8a2b2483875aaa5aa Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Thu, 5 Aug 2021 11:18:37 +0100 Subject: [PATCH 5/9] reverting inception random_state kwarg amend --- test/common_utils.py | 1 + test/test_models.py | 7 +------ torchvision/models/inception.py | 8 ++------ torchvision/models/quantization/inception.py | 5 ++--- 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/test/common_utils.py b/test/common_utils.py index 5936ae1f713..a8f5a91ef6b 100644 --- a/test/common_utils.py +++ b/test/common_utils.py @@ -44,6 +44,7 @@ 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_models.py b/test/test_models.py index fa24d984c88..c54e458e16a 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -1,6 +1,5 @@ import os import io -import sys from common_utils import map_nested_tensor_object, freeze_rng_state, set_rng_seed, cpu_and_gpu, needs_cuda from _utils_internal import get_relative_path from collections import OrderedDict @@ -14,13 +13,10 @@ import pytest import warnings import traceback -import numpy as np ACCEPT = os.getenv('EXPECTTEST_ACCEPT', '0') == '1' -random_state_numpy = np.random.RandomState(0) - def get_available_classification_models(): # TODO add a registration mechanism to torchvision.models @@ -87,7 +83,7 @@ def _assert_expected(output, name, prec): else: expected = torch.load(expected_file) rtol = atol = prec - torch.testing.assert_close(output, expected, rtol=rtol, atol=atol, check_dtype=False, msg=f'for query {name}') + torch.testing.assert_close(output, expected, rtol=rtol, atol=atol, check_dtype=False) def _check_jit_scriptable(nn_module, args, unwrapper=None, skip=False): @@ -197,7 +193,6 @@ def _check_fx_compatible(model, inputs): _model_params = { 'inception_v3': { 'input_shape': (1, 3, 299, 299), - 'random_state_numpy': random_state_numpy, }, 'retinanet_resnet50_fpn': { 'num_classes': 20, diff --git a/torchvision/models/inception.py b/torchvision/models/inception.py index 78ed30c4f10..b9c6ab74534 100644 --- a/torchvision/models/inception.py +++ b/torchvision/models/inception.py @@ -1,7 +1,6 @@ from collections import namedtuple import warnings import torch -import numpy as np from torch import nn, Tensor import torch.nn.functional as F from .._internally_replaced_utils import load_state_dict_from_url @@ -70,8 +69,7 @@ def __init__( aux_logits: bool = True, transform_input: bool = False, inception_blocks: Optional[List[Callable[..., nn.Module]]] = None, - init_weights: Optional[bool] = None, - random_state_numpy: np.random.RandomState = np.random.RandomState(), + init_weights: Optional[bool] = None ) -> None: super(Inception3, self).__init__() if inception_blocks is None: @@ -125,9 +123,7 @@ def __init__( import scipy.stats as stats stddev = m.stddev if hasattr(m, 'stddev') else 0.1 X = stats.truncnorm(-2, 2, scale=stddev) - values = torch.as_tensor( - X.rvs(m.weight.numel(), random_state=random_state_numpy), - dtype=m.weight.dtype) + values = torch.as_tensor(X.rvs(m.weight.numel()), dtype=m.weight.dtype) values = values.view(m.weight.size()) with torch.no_grad(): m.weight.copy_(values) diff --git a/torchvision/models/quantization/inception.py b/torchvision/models/quantization/inception.py index 6d5a8ac1c6d..833d8fb8b75 100644 --- a/torchvision/models/quantization/inception.py +++ b/torchvision/models/quantization/inception.py @@ -177,7 +177,7 @@ def __init__(self, *args, **kwargs): class QuantizableInception3(inception_module.Inception3): - def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, **kwargs): + def __init__(self, num_classes=1000, aux_logits=True, transform_input=False): super(QuantizableInception3, self).__init__( num_classes=num_classes, aux_logits=aux_logits, @@ -190,8 +190,7 @@ def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, **k QuantizableInceptionD, QuantizableInceptionE, QuantizableInceptionAux - ], - **kwargs + ] ) self.quant = torch.quantization.QuantStub() self.dequant = torch.quantization.DeQuantStub() From 6e22a1d485233b7c40dfe46b82849e7af75df691 Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Thu, 5 Aug 2021 11:31:58 +0100 Subject: [PATCH 6/9] move np_rng locally --- test/test_datasets.py | 4 ++-- test/test_image.py | 7 ++++--- test/test_transforms.py | 33 ++++++++++++++++++++------------- test/test_transforms_video.py | 4 ++-- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/test/test_datasets.py b/test/test_datasets.py index 3013eb019a3..41446928068 100644 --- a/test/test_datasets.py +++ b/test/test_datasets.py @@ -21,7 +21,6 @@ import torch.nn.functional as F from torchvision import datasets -random_state_numpy = np.random.RandomState(0) class STL10TestCase(datasets_utils.ImageDatasetTestCase): @@ -446,8 +445,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 = random_state_numpy.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 dd71e8ee822..17aed0dbd72 100644 --- a/test/test_image.py +++ b/test/test_image.py @@ -23,7 +23,6 @@ IS_WINDOWS = sys.platform in ('win32', 'cygwin') PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION.split('.')) -random_state_numpy = np.random.RandomState(0) def _get_safe_image_name(name): @@ -275,8 +274,9 @@ def test_write_file_non_ascii(): ]) def test_read_1_bit_png(shape): with get_tmp_dir() as root: + np_rng = np.random.RandomState(0) image_path = os.path.join(root, f'test_{shape}.png') - pixels = random_state_numpy.rand(*shape) > 0.5 + pixels = np_rng.rand(*shape) > 0.5 img = Image.fromarray(pixels) img.save(image_path) img1 = read_image(image_path) @@ -295,8 +295,9 @@ def test_read_1_bit_png(shape): ]) def test_read_1_bit_png_consistency(shape, mode): with get_tmp_dir() as root: + np_rng = np.random.RandomState(0) image_path = os.path.join(root, f'test_{shape}.png') - pixels = random_state_numpy.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 eed78fd5e80..ab311e0c548 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -21,7 +21,6 @@ from common_utils import cycle_over, int_dtypes, float_dtypes, assert_equal -random_state_numpy = np.random.RandomState(0) GRACE_HOPPER = get_file_path_2( os.path.dirname(os.path.abspath(__file__)), 'assets', 'encode_jpeg', 'grace_hopper_517x606.jpg') @@ -201,18 +200,20 @@ 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 = random_state_numpy.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) + np_rng = np.random.RandomState(0) + 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 = random_state_numpy.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) @@ -226,22 +227,25 @@ 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) + np_rng = np.random.RandomState(0) with pytest.raises(TypeError): - trans(random_state_numpy.rand(1, height, width).tolist()) + trans(np_rng.rand(1, height, width).tolist()) with pytest.raises(ValueError): - trans(random_state_numpy.rand(height)) + trans(np_rng.rand(height)) with pytest.raises(ValueError): - trans(random_state_numpy.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 = random_state_numpy.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) @@ -254,19 +258,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 = random_state_numpy.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(random_state_numpy.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() @@ -281,12 +286,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(random_state_numpy.rand(1, height, width).tolist()) + trans(np_rng.rand(1, height, width).tolist()) with pytest.raises(TypeError): - trans(random_state_numpy.rand(1, height, width)) + trans(np_rng.rand(1, height, width)) def test_randomresized_params(): @@ -1181,10 +1187,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 = random_state_numpy.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) @@ -1207,7 +1214,7 @@ def test_random_grayscale(): random_state = random.getstate() random.seed(42) x_shape = [2, 2, 3] - x_np = random_state_numpy.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 5e5a74c7190..b3675d676fe 100644 --- a/test/test_transforms_video.py +++ b/test/test_transforms_video.py @@ -16,7 +16,6 @@ warnings.simplefilter("always") import torchvision.transforms._transforms_video as transforms -random_state_numpy = np.random.RandomState(0) class TestVideoTransforms(): @@ -133,7 +132,8 @@ def test_to_tensor_video(self): trans = transforms.ToTensorVideo() with pytest.raises(TypeError): - trans(random_state_numpy.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): From 47cbd1c722724903ffef7a109d5d0156af6a3108 Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Thu, 5 Aug 2021 11:31:58 +0100 Subject: [PATCH 7/9] move np_rng locally linter --- test/test_datasets.py | 5 ++--- test/test_image.py | 8 ++++---- test/test_transforms.py | 33 ++++++++++++++++++++------------- test/test_transforms_video.py | 5 ++--- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/test/test_datasets.py b/test/test_datasets.py index 3013eb019a3..5b7eabc4cb1 100644 --- a/test/test_datasets.py +++ b/test/test_datasets.py @@ -21,8 +21,6 @@ import torch.nn.functional as F from torchvision import datasets -random_state_numpy = np.random.RandomState(0) - class STL10TestCase(datasets_utils.ImageDatasetTestCase): DATASET_CLASS = datasets.STL10 @@ -446,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 = random_state_numpy.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 dd71e8ee822..11690bf84bd 100644 --- a/test/test_image.py +++ b/test/test_image.py @@ -23,8 +23,6 @@ IS_WINDOWS = sys.platform in ('win32', 'cygwin') PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION.split('.')) -random_state_numpy = np.random.RandomState(0) - def _get_safe_image_name(name): # Used when we need to change the pytest "id" for an "image path" parameter. @@ -275,8 +273,9 @@ def test_write_file_non_ascii(): ]) def test_read_1_bit_png(shape): with get_tmp_dir() as root: + np_rng = np.random.RandomState(0) image_path = os.path.join(root, f'test_{shape}.png') - pixels = random_state_numpy.rand(*shape) > 0.5 + pixels = np_rng.rand(*shape) > 0.5 img = Image.fromarray(pixels) img.save(image_path) img1 = read_image(image_path) @@ -295,8 +294,9 @@ def test_read_1_bit_png(shape): ]) def test_read_1_bit_png_consistency(shape, mode): with get_tmp_dir() as root: + np_rng = np.random.RandomState(0) image_path = os.path.join(root, f'test_{shape}.png') - pixels = random_state_numpy.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 eed78fd5e80..ab311e0c548 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -21,7 +21,6 @@ from common_utils import cycle_over, int_dtypes, float_dtypes, assert_equal -random_state_numpy = np.random.RandomState(0) GRACE_HOPPER = get_file_path_2( os.path.dirname(os.path.abspath(__file__)), 'assets', 'encode_jpeg', 'grace_hopper_517x606.jpg') @@ -201,18 +200,20 @@ 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 = random_state_numpy.randint(low=0, high=255, size=(height, width, channels)).astype(np.uint8) + np_rng = np.random.RandomState(0) + 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 = random_state_numpy.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) @@ -226,22 +227,25 @@ 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) + np_rng = np.random.RandomState(0) with pytest.raises(TypeError): - trans(random_state_numpy.rand(1, height, width).tolist()) + trans(np_rng.rand(1, height, width).tolist()) with pytest.raises(ValueError): - trans(random_state_numpy.rand(height)) + trans(np_rng.rand(height)) with pytest.raises(ValueError): - trans(random_state_numpy.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 = random_state_numpy.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) @@ -254,19 +258,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 = random_state_numpy.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(random_state_numpy.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() @@ -281,12 +286,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(random_state_numpy.rand(1, height, width).tolist()) + trans(np_rng.rand(1, height, width).tolist()) with pytest.raises(TypeError): - trans(random_state_numpy.rand(1, height, width)) + trans(np_rng.rand(1, height, width)) def test_randomresized_params(): @@ -1181,10 +1187,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 = random_state_numpy.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) @@ -1207,7 +1214,7 @@ def test_random_grayscale(): random_state = random.getstate() random.seed(42) x_shape = [2, 2, 3] - x_np = random_state_numpy.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 5e5a74c7190..5ae9192c4b6 100644 --- a/test/test_transforms_video.py +++ b/test/test_transforms_video.py @@ -16,8 +16,6 @@ warnings.simplefilter("always") import torchvision.transforms._transforms_video as transforms -random_state_numpy = np.random.RandomState(0) - class TestVideoTransforms(): @@ -133,7 +131,8 @@ def test_to_tensor_video(self): trans = transforms.ToTensorVideo() with pytest.raises(TypeError): - trans(random_state_numpy.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): From 674b4b1419ee1d88aefbb755e5382159b4c3d78b Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Fri, 6 Aug 2021 08:59:37 +0100 Subject: [PATCH 8/9] removing np random seed from test/common_utils.py --- test/common_utils.py | 1 - 1 file changed, 1 deletion(-) 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): From 5b0a71be8716c5f562ce5dbfcc6696cb3b706558 Mon Sep 17 00:00:00 2001 From: Vincent Moens Date: Fri, 6 Aug 2021 10:28:57 +0100 Subject: [PATCH 9/9] minor --- test/test_image.py | 4 ++-- test/test_models.py | 3 ++- test/test_transforms.py | 2 -- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/test/test_image.py b/test/test_image.py index 11690bf84bd..47023a45be2 100644 --- a/test/test_image.py +++ b/test/test_image.py @@ -272,8 +272,8 @@ 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: - np_rng = np.random.RandomState(0) image_path = os.path.join(root, f'test_{shape}.png') pixels = np_rng.rand(*shape) > 0.5 img = Image.fromarray(pixels) @@ -293,8 +293,8 @@ 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: - np_rng = np.random.RandomState(0) image_path = os.path.join(root, f'test_{shape}.png') pixels = np_rng.rand(*shape) > 0.5 img = Image.fromarray(pixels) diff --git a/test/test_models.py b/test/test_models.py index c54e458e16a..72ae68f5615 100644 --- a/test/test_models.py +++ b/test/test_models.py @@ -1,5 +1,6 @@ import os import io +import sys from common_utils import map_nested_tensor_object, freeze_rng_state, set_rng_seed, cpu_and_gpu, needs_cuda from _utils_internal import get_relative_path from collections import OrderedDict @@ -192,7 +193,7 @@ def _check_fx_compatible(model, inputs): # the _test_*_model methods. _model_params = { 'inception_v3': { - 'input_shape': (1, 3, 299, 299), + 'input_shape': (1, 3, 299, 299) }, 'retinanet_resnet50_fpn': { 'num_classes': 20, diff --git a/test/test_transforms.py b/test/test_transforms.py index ab311e0c548..c5cc80ef87e 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -207,7 +207,6 @@ def test_to_tensor(self, channels): output = trans(img) torch.testing.assert_close(output, input_data) - np_rng = np.random.RandomState(0) 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 @@ -229,7 +228,6 @@ def test_to_tensor_errors(self): trans = transforms.ToTensor() np_rng = np.random.RandomState(0) - np_rng = np.random.RandomState(0) with pytest.raises(TypeError): trans(np_rng.rand(1, height, width).tolist())