Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
3fa2055b
Unverified
Commit
3fa2055b
authored
Aug 06, 2021
by
Vincent Moens
Committed by
GitHub
Aug 06, 2021
Browse files
using `np.random.RandomState(seed)` instead of `np.random.seed(seed)` (#4250)
Co-authored-by:
Vincent Moens
<
vmoens@fb.com
>
parent
f88985b5
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
26 additions
and
17 deletions
+26
-17
test/common_utils.py
test/common_utils.py
+0
-1
test/test_datasets.py
test/test_datasets.py
+2
-1
test/test_image.py
test/test_image.py
+4
-2
test/test_transforms.py
test/test_transforms.py
+18
-12
test/test_transforms_video.py
test/test_transforms_video.py
+2
-1
No files found.
test/common_utils.py
View file @
3fa2055b
...
...
@@ -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
):
...
...
test/test_datasets.py
View file @
3fa2055b
...
...
@@ -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
):
...
...
test/test_image.py
View file @
3fa2055b
...
...
@@ -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
)
...
...
test/test_transforms.py
View file @
3fa2055b
...
...
@@ -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
)
...
...
test/test_transforms_video.py
View file @
3fa2055b
...
...
@@ -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
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment