Skip to content
Snippets Groups Projects
Commit f38b1cfd authored by Felix Kleinert's avatar Felix Kleinert
Browse files

update some tests for advanced paddings (#56)

parent abef8764
No related branches found
No related tags found
2 merge requests!59Develop,!46Felix #56 advanced paddings
Pipeline #30913 passed
import keras import keras
import tensorflow as tf
import numpy as np import numpy as np
import pytest import pytest
import mock import mock
...@@ -11,20 +12,20 @@ class TestPadUtils: ...@@ -11,20 +12,20 @@ class TestPadUtils:
def test_get_padding_for_same(self): def test_get_padding_for_same(self):
with pytest.raises(ValueError): with pytest.raises(ValueError):
pad_utils.get_padding_for_same((-1, 2)) PadUtils.get_padding_for_same((-1, 2))
pad_utils.get_padding_for_same((1, 0)) PadUtils.get_padding_for_same((1, 0))
kernel = (1, 1) kernel = (1, 1)
pad = pad_utils.get_padding_for_same(kernel) pad = PadUtils.get_padding_for_same(kernel)
assert pad == (0, 0) assert pad == (0, 0)
assert isinstance(pad, tuple) assert isinstance(pad, tuple)
assert isinstance(pad[0], int) and isinstance(pad[1], int) assert isinstance(pad[0], int) and isinstance(pad[1], int)
assert not isinstance(pad[0], np.int64) assert not isinstance(pad[0], np.int64)
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
pad_utils.get_padding_for_same(kernel, strides=2) PadUtils.get_padding_for_same(kernel, strides=2)
kernel = (3, 1) kernel = (3, 1)
pad = pad_utils.get_padding_for_same(kernel) pad = PadUtils.get_padding_for_same(kernel)
assert pad == (1, 0) assert pad == (1, 0)
assert isinstance(pad, tuple) assert isinstance(pad, tuple)
assert isinstance(pad[0], int) and isinstance(pad[1], int) assert isinstance(pad[0], int) and isinstance(pad[1], int)
...@@ -32,15 +33,132 @@ class TestPadUtils: ...@@ -32,15 +33,132 @@ class TestPadUtils:
kernel = (2, 2) kernel = (2, 2)
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
pad_utils.get_padding_for_same(kernel) PadUtils.get_padding_for_same(kernel)
kernel = (3, 3, 3) kernel = (3, 3, 3)
pad = pad_utils.get_padding_for_same(kernel) pad = PadUtils.get_padding_for_same(kernel)
assert pad == (1, 1, 1) assert pad == (1, 1, 1)
assert isinstance(pad, tuple) assert isinstance(pad, tuple)
assert isinstance(pad[0], int) and isinstance(pad[1], int) assert isinstance(pad[0], int) and isinstance(pad[1], int)
assert not (isinstance(pad[0], np.int64) and isinstance(pad[0], np.int64)) assert not (isinstance(pad[0], np.int64) and isinstance(pad[0], np.int64))
def test_check_padding_format(self):
with pytest.raises(ValueError) as einfo:
PadUtils.check_padding_format((-2, 1))
assert "The `1st entry of padding` argument must be >= 0. Received: -2 of type <class 'int'>" in str(einfo.value)
with pytest.raises(ValueError) as einfo:
PadUtils.check_padding_format((1, -1))
assert "The `2nd entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str(einfo.value)
with pytest.raises(ValueError) as einfo:
PadUtils.check_padding_format((1, 1, 2))
assert "`padding` should have two elements. Found: (1, 1, 2)" in str(einfo.value)
with pytest.raises(ValueError) as einfo:
PadUtils.check_padding_format((1.2, 1))
assert "The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.2" in str(einfo.value)
with pytest.raises(ValueError) as einfo:
PadUtils.check_padding_format((1, 1.))
assert "The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 1.0" in str(einfo.value)
with pytest.raises(ValueError) as einfo:
PadUtils.check_padding_format(1.2)
assert "`padding` should be either an int, a tuple of 2 ints (symmetric_height_pad, symmetric_width_pad), " \
"or a tuple of 2 tuples of 2 ints ((top_pad, bottom_pad), (left_pad, right_pad)). Found: 1.2 of type " \
"<class 'float'>" in str(einfo.value)
#
# def test_spatial_2d_padding(self):
# pass
class TestReflectionPadding2D:
@pytest.fixture
def input_x(self):
return keras.Input(shape=(10, 10, 3))
def test_init(self):
pad = (1, 1)
layer_name = "RefPAD"
ref_pad = ReflectionPadding2D(padding=pad, name=layer_name)
assert ref_pad.padding == ((1, 1), (1, 1))
assert ref_pad.name == 'RefPAD'
assert ref_pad.data_format == 'channels_last'
assert ref_pad.rank == 2
pad = 1
ref_pad = ReflectionPadding2D(padding=pad)
assert ref_pad.padding == ((1, 1), (1, 1))
pad = (5, 3)
layer_name = "RefPAD_5x3"
ref_pad = ReflectionPadding2D(padding=pad, name=layer_name)
assert ref_pad.padding == ((5, 5), (3, 3))
pad = ((1, 2), (3, 4))
ref_pad = ReflectionPadding2D(padding=pad)
assert ref_pad.padding == ((1, 2), (3, 4))
with pytest.raises(ValueError) as einfo:
ReflectionPadding2D(padding=(1., 2))
assert "The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.0" in str(einfo.value)
with pytest.raises(ValueError) as einfo:
ReflectionPadding2D(padding=(1, 2.2))
assert "The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 2.2" in str(einfo.value)
def test_call(self, input_x):
pad = (1, 0)
layer_name = "RefPad_3x1"
ref_pad = ReflectionPadding2D(padding=pad, name=layer_name)(input_x)
assert ref_pad.get_shape().as_list() == [None, 12, 10, 3]
assert ref_pad.name == 'RefPad_3x1/MirrorPad:0'
class TestSymmerticPadding2D:
@pytest.fixture
def input_x(self):
return keras.Input(shape=(10, 10, 3))
def test_init(self):
pad = (1, 1)
layer_name = "SymPAD"
sym_pad = SymmetricPadding2D(padding=pad, name=layer_name)
assert sym_pad.padding == ((1, 1), (1, 1))
assert sym_pad.name == 'SymPAD'
assert sym_pad.data_format == 'channels_last'
assert sym_pad.rank == 2
pad = 1
sym_pad = SymmetricPadding2D(padding=pad)
assert sym_pad.padding == ((1, 1), (1, 1))
pad = (5, 3)
layer_name = "SymPAD_5x3"
sym_pad = SymmetricPadding2D(padding=pad, name=layer_name)
assert sym_pad.padding == ((5, 5), (3, 3))
pad = ((1, 2), (3, 4))
sym_pad = SymmetricPadding2D(padding=pad)
assert sym_pad.padding == ((1, 2), (3, 4))
with pytest.raises(ValueError) as einfo:
SymmetricPadding2D(padding=(1., 2))
assert "The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.0" in str(einfo.value)
with pytest.raises(ValueError) as einfo:
SymmetricPadding2D(padding=(1, 2.2))
assert "The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 2.2" in str(einfo.value)
def test_call(self, input_x):
pad = (1, 0)
layer_name = "SymPad_3x1"
sym_pad = SymmetricPadding2D(padding=pad, name=layer_name)(input_x)
assert sym_pad.get_shape().as_list() == [None, 12, 10, 3]
assert sym_pad.name == 'SymPad_3x1/MirrorPad:0'
print(123)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment