diff --git a/test/test_model_modules/test_advanced_paddings.py b/test/test_model_modules/test_advanced_paddings.py index 03782c3fa6a5de7d571fcf1ec76d6448a044f552..1a565bc5992eb272d7406675cceac10d509ade69 100644 --- a/test/test_model_modules/test_advanced_paddings.py +++ b/test/test_model_modules/test_advanced_paddings.py @@ -1,4 +1,5 @@ import keras +import tensorflow as tf import numpy as np import pytest import mock @@ -11,20 +12,20 @@ class TestPadUtils: def test_get_padding_for_same(self): with pytest.raises(ValueError): - pad_utils.get_padding_for_same((-1, 2)) - pad_utils.get_padding_for_same((1, 0)) + PadUtils.get_padding_for_same((-1, 2)) + PadUtils.get_padding_for_same((1, 0)) kernel = (1, 1) - pad = pad_utils.get_padding_for_same(kernel) + pad = PadUtils.get_padding_for_same(kernel) assert pad == (0, 0) assert isinstance(pad, tuple) assert isinstance(pad[0], int) and isinstance(pad[1], int) assert not isinstance(pad[0], np.int64) with pytest.raises(NotImplementedError): - pad_utils.get_padding_for_same(kernel, strides=2) + PadUtils.get_padding_for_same(kernel, strides=2) kernel = (3, 1) - pad = pad_utils.get_padding_for_same(kernel) + pad = PadUtils.get_padding_for_same(kernel) assert pad == (1, 0) assert isinstance(pad, tuple) assert isinstance(pad[0], int) and isinstance(pad[1], int) @@ -32,15 +33,132 @@ class TestPadUtils: kernel = (2, 2) with pytest.raises(NotImplementedError): - pad_utils.get_padding_for_same(kernel) + PadUtils.get_padding_for_same(kernel) 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 isinstance(pad, tuple) assert isinstance(pad[0], int) and isinstance(pad[1], int) 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)