diff --git a/test/test_model_modules/test_advanced_paddings.py b/test/test_model_modules/test_advanced_paddings.py index 7fec96dae6e23e46c7b313f841fb468b8b2af98f..be268d401b25681cb9a36498bfc3022a9a55ef1e 100644 --- a/test/test_model_modules/test_advanced_paddings.py +++ b/test/test_model_modules/test_advanced_paddings.py @@ -6,39 +6,51 @@ from src.model_modules.advanced_paddings import * class TestPadUtils: - def test_get_padding_for_same(self): - with pytest.raises(ValueError): + def test_get_padding_for_same_negative_kernel_size(self): + print('In test_get_padding_for_same_negative_kernel_size') + with pytest.raises(ValueError) as einfo: PadUtils.get_padding_for_same((-1, 2)) - PadUtils.get_padding_for_same((1, 0)) + assert 'All values of kernel_size must be > 0. Got: (-1, 2) ' in str(einfo.value) - kernel = (1, 1) - 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): - PadUtils.get_padding_for_same(kernel, strides=2) + with pytest.raises(ValueError) as einfo: + PadUtils.get_padding_for_same((1, -2)) + assert 'All values of kernel_size must be > 0. Got: (1, -2) ' in str(einfo.value) - kernel = (3, 1) - 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) - assert not isinstance(pad[0], np.int64) + with pytest.raises(ValueError) as einfo: + PadUtils.get_padding_for_same((-1, -2)) + assert 'All values of kernel_size must be > 0. Got: (-1, -2) ' in str(einfo.value) + + def test_get_padding_for_same_strides_greater_one(self): + with pytest.raises(NotImplementedError) as einfo: + PadUtils.get_padding_for_same((1, 1), strides=2) + assert 'Strides other than 1 not implemented!' in str(einfo.value) - kernel = (2, 2) - with pytest.raises(NotImplementedError): - PadUtils.get_padding_for_same(kernel) + with pytest.raises(NotImplementedError) as einfo: + PadUtils.get_padding_for_same((1, 1), strides=-1) + assert 'Strides other than 1 not implemented!' in str(einfo.value) + def test_get_padding_for_same_stride_3d(self): kernel = (3, 3, 3) 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)) + assert not (isinstance(pad[0], np.int64) and isinstance(pad[1], np.int64) and isinstance(pad[2], np.int64)) + + def test_get_padding_for_same_even_pad(self): + with pytest.raises(NotImplementedError) as einfo: + PadUtils.get_padding_for_same((2, 1)) + assert 'even kernel size not implemented. Got (2, 1)' in str(einfo.value) + + with pytest.raises(NotImplementedError) as einfo: + PadUtils.get_padding_for_same((1, 4)) + assert 'even kernel size not implemented. Got (1, 4)' in str(einfo.value) - def test_check_padding_format(self): + with pytest.raises(NotImplementedError) as einfo: + PadUtils.get_padding_for_same((2, 4)) + assert 'even kernel size not implemented. Got (2, 4)' in str(einfo.value) + + def test_check_padding_format_negative_pads(self): with pytest.raises(ValueError) as einfo: PadUtils.check_padding_format((-2, 1)) @@ -46,12 +58,32 @@ class TestPadUtils: 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) + 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((-2, -1)) + assert "The `1st entry of padding` argument must be >= 0. Received: -2 of type <class 'int'>" in str( + einfo.value) + def test_check_padding_format_len_of_pad_tuple(self): 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, 1, 2, 2)) + assert "`padding` should have two elements. Found: (1, 1, 2, 2)" in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + PadUtils.check_padding_format(((1, 1, 3), (2, 2, 4))) + assert "`padding[0]` should have one or two elements. Found: (1, 1, 3)" in str(einfo.value) + + assert PadUtils.check_padding_format(((1, 1), (2, 2))) == ((1, 1), (2, 2)) + assert PadUtils.check_padding_format((1, 2)) == ((1, 1), (2, 2)) + assert PadUtils.check_padding_format(1) == ((1, 1), (1, 1)) + + def test_check_padding_format_tupe_of_none_integer(self): 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) @@ -66,9 +98,56 @@ class TestPadUtils: "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 + def test_check_padding_format_tupe_of_tuple_none_integer_first(self): + with pytest.raises(ValueError) as einfo: + PadUtils.check_padding_format(((1., 2), (3, 4))) + assert "`padding[0]` should have one or two elements of type int. Found:(1.0, 2) " \ + "of type [<class 'float'>, <class 'int'>]" in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + PadUtils.check_padding_format(((1, 2.), (3, 4))) + assert "`padding[0]` should have one or two elements of type int. Found:(1, 2.0) " \ + "of type [<class 'int'>, <class 'float'>]" in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + PadUtils.check_padding_format(((1, '2'), (3, 4))) + assert "`padding[0]` should have one or two elements of type int. Found:(1, '2') " \ + "of type [<class 'int'>, <class 'str'>]" in str(einfo.value) + + def test_check_padding_format_tupe_of_tuple_none_integer_second(self): + with pytest.raises(ValueError) as einfo: + PadUtils.check_padding_format(((1, 2), (3., 4))) + assert "`padding[1]` should have one or two elements of type int. Found:(3.0, 4) " \ + "of type [<class 'float'>, <class 'int'>]" in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + PadUtils.check_padding_format(((1, 2), (3, 4.))) + assert "`padding[1]` should have one or two elements of type int. Found:(3, 4.0) " \ + "of type [<class 'int'>, <class 'float'>]" in str(einfo.value) + + def test_check_padding_format_valid_mix_of_int_and_tuple(self): + assert PadUtils.check_padding_format(((1, 2), 3)) == ((1, 2), (3, 3)) + assert PadUtils.check_padding_format((1, (2, 3))) == ((1, 1), (2, 3)) + + def test_check_padding_format_invalid_mixed_tuple_and_int(self): + with pytest.raises(ValueError) as einfo: + PadUtils.check_padding_format(((1., 2), 3)) + assert "`padding[0]` should have one or two elements of type int. Found:(1.0, 2) " \ + "of type [<class 'float'>, <class 'int'>]" in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + PadUtils.check_padding_format(((1, 2), 3.)) + assert "The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 3.0" in str(einfo.value) + + def test_check_padding_format_invalid_mixed_int_and_tuple(self): + with pytest.raises(ValueError) as einfo: + PadUtils.check_padding_format((1., (2, 3))) + 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: + PadUtils.check_padding_format((1, (2., 3))) + assert "`padding[1]` should have one or two elements of type int. Found:(2.0, 3) " \ + "of type [<class 'float'>, <class 'int'>]" in str(einfo.value) class TestReflectionPadding2D: @@ -77,36 +156,117 @@ class TestReflectionPadding2D: def input_x(self): return keras.Input(shape=(10, 10, 3)) - def test_init(self): - pad = (1, 1) + def test_init_tuple_of_valid_int(self): + pad = (1, 3) layer_name = "RefPAD" ref_pad = ReflectionPadding2D(padding=pad, name=layer_name) - assert ref_pad.padding == ((1, 1), (1, 1)) + assert ref_pad.padding == ((1, 1), (3, 3)) + assert ref_pad.name == 'RefPAD' + assert ref_pad.data_format == 'channels_last' + assert ref_pad.rank == 2 + + pad = (0, 1) + ref_pad = ReflectionPadding2D(padding=pad, name=layer_name) + assert ref_pad.padding == ((0, 0), (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)) + def test_init_tuple_of_negative_int(self): + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=(-1, 1)) + assert "The `1st entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str(einfo.value) 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) + ReflectionPadding2D(padding=(1, -2)) + assert "The `2nd entry of padding` argument must be >= 0. Received: -2 of type <class 'int'>" in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=(-1, -2)) + assert "The `1st entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str(einfo.value) + + def test_init_tuple_of_invalid_format_float(self): + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=(1., 1)) + 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, 1.2)) + assert 'The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 1.2' in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=(1., 1.2)) + assert 'The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.0' in str(einfo.value) + + def test_init_tuple_of_invalid_format_string(self): + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=('1', 2)) + # This error message is not the best as it is missing the type information. + # But it is raised by keras.utils.conv_utils which I will not touch. + assert "`padding[0]` is str but must be int" in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=(1, '2')) + assert '`padding[1]` is str but must be int' in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=('1', '2')) + assert '`padding[0]` is str but must be int' in str(einfo.value) + + def test_init_int(self): + layer_name = "RefPAD" + ref_pad = ReflectionPadding2D(padding=1, name=layer_name) + assert ref_pad.padding == ((1, 1), (1, 1)) + assert ref_pad.name == "RefPAD" + + def test_init_tuple_of_tuple_of_valid_int(self): + ref_pad = ReflectionPadding2D(padding=((0, 1), (2, 3)), name="RefPAD") + assert ref_pad.padding == ((0, 1), (2, 3)) + assert ref_pad.name == "RefPAD" + + def test_init_tuple_of_tuple_of_invalid_int(self): + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=((-4, 1), (2, 3)), name="RefPAD") + assert "The `1st entry of padding` argument must be >= 0. Received: (-4, 1) of type <class 'tuple'>" in str( + einfo.value) + + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=((4, -1), (2, 3)), name="RefPAD") + assert "The `1st entry of padding` argument must be >= 0. Received: (4, -1) of type <class 'tuple'>" in str( + einfo.value) + + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=((4, 1), (-2, 3)), name="RefPAD") + assert "The `2nd entry of padding` argument must be >= 0. Received: (-2, 3) of type <class 'tuple'>" in str( + einfo.value) + + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=((4, 1), (2, -3)), name="RefPAD") + assert "The `2nd entry of padding` argument must be >= 0. Received: (2, -3) of type <class 'tuple'>" in str( + einfo.value) + + def test_init_tuple_of_tuple_of_invalid_format(self): + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=((0.1, 1), (2, 3)), name="RefPAD") + assert "`padding[0]` should have one or two elements of type int. Found:(0.1, 1) " \ + "of type [<class 'float'>, <class 'int'>]" 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) + with pytest.raises(ValueError) as einfo: + ReflectionPadding2D(padding=((0, 1), ('2', 3)), name="RefPAD") + assert "`padding[1]` should have one or two elements of type int. Found:('2', 3) " \ + "of type [<class 'str'>, <class 'int'>]" in str(einfo.value) + def test_call(self, input_x): + # here it behaves like a "normal" keras layer, I don't know how to test those pad = (1, 0) layer_name = "RefPad_3x1" ref_pad = ReflectionPadding2D(padding=pad, name=layer_name)(input_x) @@ -120,42 +280,122 @@ class TestSymmerticPadding2D: def input_x(self): return keras.Input(shape=(10, 10, 3)) - def test_init(self): - pad = (1, 1) - layer_name = "SymPAD" + def test_init_tuple_of_valid_int(self): + pad = (1, 3) + 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.padding == ((1, 1), (3, 3)) + assert sym_pad.name == 'SymPad' + assert sym_pad.data_format == 'channels_last' + assert sym_pad.rank == 2 + + pad = (0, 1) + sym_pad = SymmetricPadding2D(padding=pad, name=layer_name) + assert sym_pad.padding == ((0, 0), (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" + 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)) + def test_init_tuple_of_negative_int(self): + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=(-1, 1)) + assert "The `1st entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str( + einfo.value) 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) + SymmetricPadding2D(padding=(1, -2)) + assert "The `2nd entry of padding` argument must be >= 0. Received: -2 of type <class 'int'>" in str( + einfo.value) + + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=(-1, -2)) + assert "The `1st entry of padding` argument must be >= 0. Received: -1 of type <class 'int'>" in str( + einfo.value) + + def test_init_tuple_of_invalid_format_float(self): + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=(1., 1)) + 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, 1.2)) + assert 'The `2nd entry of padding` argument must be a tuple of 2 integers. Received: 1.2' in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=(1., 1.2)) + assert 'The `1st entry of padding` argument must be a tuple of 2 integers. Received: 1.0' in str(einfo.value) + + def test_init_tuple_of_invalid_format_string(self): + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=('1', 2)) + # This error message is not the best as it is missing the type information. + # But it is raised by keras.utils.conv_utils which I will not touch. + assert "`padding[0]` is str but must be int" in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=(1, '2')) + assert '`padding[1]` is str but must be int' in str(einfo.value) + + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=('1', '2')) + assert '`padding[0]` is str but must be int' in str(einfo.value) + + def test_init_int(self): + layer_name = "SymPad" + sym_pad = SymmetricPadding2D(padding=1, name=layer_name) + assert sym_pad.padding == ((1, 1), (1, 1)) + assert sym_pad.name == "SymPad" + + def test_init_tuple_of_tuple_of_valid_int(self): + sym_pad = SymmetricPadding2D(padding=((0, 1), (2, 3)), name="SymPad") + assert sym_pad.padding == ((0, 1), (2, 3)) + assert sym_pad.name == "SymPad" + + def test_init_tuple_of_tuple_of_invalid_int(self): + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=((-4, 1), (2, 3)), name="SymPad") + assert "The `1st entry of padding` argument must be >= 0. Received: (-4, 1) of type <class 'tuple'>" in str( + einfo.value) + + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=((4, -1), (2, 3)), name="SymPad") + assert "The `1st entry of padding` argument must be >= 0. Received: (4, -1) of type <class 'tuple'>" in str( + einfo.value) + + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=((4, 1), (-2, 3)), name="SymPad") + assert "The `2nd entry of padding` argument must be >= 0. Received: (-2, 3) of type <class 'tuple'>" in str( + einfo.value) + + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=((4, 1), (2, -3)), name="SymPad") + assert "The `2nd entry of padding` argument must be >= 0. Received: (2, -3) of type <class 'tuple'>" in str( + einfo.value) + + def test_init_tuple_of_tuple_of_invalid_format(self): + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=((0.1, 1), (2, 3)), name="SymPad") + assert "`padding[0]` should have one or two elements of type int. Found:(0.1, 1) " \ + "of type [<class 'float'>, <class 'int'>]" 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) + with pytest.raises(ValueError) as einfo: + SymmetricPadding2D(padding=((0, 1), ('2', 3)), name="SymPad") + assert "`padding[1]` should have one or two elements of type int. Found:('2', 3) " \ + "of type [<class 'str'>, <class 'int'>]" in str(einfo.value) + def test_call(self, input_x): + # here it behaves like a "normal" keras layer, I don't know how to test those 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' - - - -