From b6f28b307435c55448d8c834c11eb475265c6f04 Mon Sep 17 00:00:00 2001 From: Kavya Rambhia <161142013+kavya-r30@users.noreply.github.com> Date: Thu, 20 Feb 2025 17:01:35 +0530 Subject: [PATCH 1/4] Resnet pytest --- aeon/networks/tests/test_resnet.py | 166 +++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 aeon/networks/tests/test_resnet.py diff --git a/aeon/networks/tests/test_resnet.py b/aeon/networks/tests/test_resnet.py new file mode 100644 index 0000000000..63909a750b --- /dev/null +++ b/aeon/networks/tests/test_resnet.py @@ -0,0 +1,166 @@ +"""Tests for the ResNet Model.""" + +import pytest +import tensorflow as tf + +from aeon.networks import ResNetNetwork +from aeon.utils.validation._dependencies import _check_soft_dependencies + + +@pytest.mark.skipif( + not _check_soft_dependencies(["tensorflow"], severity="none"), + reason="skip test if required soft dependency not available", +) +def test_default_initialization(): + """Test if the network initializes with proper attributes.""" + model = ResNetNetwork() + assert model.n_residual_blocks == 3 + assert model.n_conv_per_residual_block == 3 + assert model.n_filters is None + assert model.kernel_size is None + assert model.strides == 1 + assert model.dilation_rate == 1 + assert model.activation == "relu" + assert model.use_bias is True + assert model.padding == "same" + + +@pytest.mark.skipif( + not _check_soft_dependencies(["tensorflow"], severity="none"), + reason="skip test if required soft dependency not available", +) +def test_custom_initialization(): + """Test whether custom kwargs are correctly set.""" + model = ResNetNetwork( + n_residual_blocks=3, + n_conv_per_residual_block=3, + n_filters=[64, 128, 128], + kernel_size=[8, 5, 3], + activation="relu", + strides=1, + padding="same", + ) + model.build_network((128, 1)) + assert model.n_residual_blocks == 3 + assert model.n_conv_per_residual_block == 3 + assert model._n_filters == [64, 128, 128] + assert model._kernel_size == [8, 5, 3] + assert model._activation == ["relu", "relu", "relu"] + assert model._strides == [1, 1, 1] + assert model._padding == ["same", "same", "same"] + + +@pytest.mark.skipif( + not _check_soft_dependencies(["tensorflow"], severity="none"), + reason="skip test if required soft dependency not available", +) +def test_edge_case_initialization(): + """Tests edge cases for minimal configuration.""" + model = ResNetNetwork( + n_residual_blocks=1, + n_conv_per_residual_block=1, + n_filters=[64], + kernel_size=[8], + ) + model.build_network((128, 1)) + assert model.n_residual_blocks == 1 + assert model.n_conv_per_residual_block == 1 + assert model._n_filters == [64] + assert model._kernel_size == [8] + + +@pytest.mark.skipif( + not _check_soft_dependencies(["tensorflow"], severity="none"), + reason="skip test if required soft dependency not available", +) +def test_invalid_initialization(): + """Test if the network raises valid exceptions for invalid configurations.""" + with pytest.raises(ValueError): + ResNetNetwork(n_filters=[64, 128], n_residual_blocks=3).build_network((128, 1)) + + with pytest.raises(ValueError): + ResNetNetwork(kernel_size=[8, 5], n_conv_per_residual_block=3).build_network( + (128, 1) + ) + + with pytest.raises(ValueError): + ResNetNetwork(strides=[1, 2], n_conv_per_residual_block=3).build_network( + (128, 1) + ) + + with pytest.raises(ValueError): + ResNetNetwork(dilation_rate=[1, 2], n_conv_per_residual_block=3).build_network( + (128, 1) + ) + + with pytest.raises(ValueError): + ResNetNetwork( + padding=["same", "valid"], n_conv_per_residual_block=3 + ).build_network((128, 1)) + + with pytest.raises(ValueError): + ResNetNetwork( + activation=["relu", "tanh"], n_conv_per_residual_block=3 + ).build_network((128, 1)) + + with pytest.raises(ValueError): + ResNetNetwork( + use_bias=[True, False], n_conv_per_residual_block=3 + ).build_network((128, 1)) + + +@pytest.mark.skipif( + not _check_soft_dependencies(["tensorflow"], severity="none"), + reason="skip test if required soft dependency not available", +) +def test_list_parameters(): + """Test correct handling of list parameters.""" + model = ResNetNetwork( + n_residual_blocks=3, + n_conv_per_residual_block=3, + n_filters=[64, 128, 128], + kernel_size=[8, 5, 3], + strides=[1, 1, 1], + dilation_rate=[1, 1, 1], + padding=["same", "same", "same"], + activation=["relu", "relu", "relu"], + use_bias=[True, True, True], + ) + model.build_network((128, 1)) + + assert model._n_filters == [64, 128, 128] + assert model._kernel_size == [8, 5, 3] + assert model._strides == [1, 1, 1] + assert model._dilation_rate == [1, 1, 1] + assert model._padding == ["same", "same", "same"] + assert model._activation == ["relu", "relu", "relu"] + assert model._use_bias == [True, True, True] + + +@pytest.mark.skipif( + not _check_soft_dependencies(["tensorflow"], severity="none"), + reason="skip test if required soft dependency not available", +) +def test_build_network(): + """Test network building with various input shapes.""" + model = ResNetNetwork() + + input_shapes = [(128, 1), (256, 3), (512, 1)] + for shape in input_shapes: + input_layer, output_layer = model.build_network(shape) + assert input_layer.shape[1:] == shape + assert output_layer.shape[-1] == 128 + + +@pytest.mark.skipif( + not _check_soft_dependencies(["tensorflow"], severity="none"), + reason="skip test if required soft dependency not available", +) +def test_shortcut_layer(): + """Test the shortcut layer functionality.""" + model = ResNetNetwork() + input_tensor = tf.keras.layers.Input((128, 64)) + output_tensor = tf.keras.layers.Conv1D(128, 8, padding="same")(input_tensor) + + shortcut = model._shortcut_layer(input_tensor, output_tensor) + assert shortcut.shape[-1] == 128 From cd3e8e61b9315d1b94cfe102f3c2247593bbd04d Mon Sep 17 00:00:00 2001 From: Kavya Rambhia <161142013+kavya-r30@users.noreply.github.com> Date: Thu, 20 Feb 2025 17:58:39 +0530 Subject: [PATCH 2/4] Resnet pytest --- aeon/networks/tests/test_resnet.py | 122 ++++++++--------------------- 1 file changed, 32 insertions(+), 90 deletions(-) diff --git a/aeon/networks/tests/test_resnet.py b/aeon/networks/tests/test_resnet.py index 63909a750b..5b9e781f59 100644 --- a/aeon/networks/tests/test_resnet.py +++ b/aeon/networks/tests/test_resnet.py @@ -14,15 +14,20 @@ def test_default_initialization(): """Test if the network initializes with proper attributes.""" model = ResNetNetwork() - assert model.n_residual_blocks == 3 - assert model.n_conv_per_residual_block == 3 - assert model.n_filters is None - assert model.kernel_size is None - assert model.strides == 1 - assert model.dilation_rate == 1 - assert model.activation == "relu" - assert model.use_bias is True - assert model.padding == "same" + assert isinstance( + model, ResNetNetwork + ), "Model initialization failed: Incorrect type" + assert model.n_residual_blocks == 3, "Default residual blocks count mismatch" + assert ( + model.n_conv_per_residual_block == 3 + ), "Default convolution blocks count mismatch" + assert model.n_filters is None, "Default n_filters should be None" + assert model.kernel_size is None, "Default kernel_size should be None" + assert model.strides == 1, "Default strides value mismatch" + assert model.dilation_rate == 1, "Default dilation rate mismatch" + assert model.activation == "relu", "Default activation mismatch" + assert model.use_bias is True, "Default use_bias mismatch" + assert model.padding == "same", "Default padding mismatch" @pytest.mark.skipif( @@ -41,32 +46,14 @@ def test_custom_initialization(): padding="same", ) model.build_network((128, 1)) - assert model.n_residual_blocks == 3 - assert model.n_conv_per_residual_block == 3 - assert model._n_filters == [64, 128, 128] - assert model._kernel_size == [8, 5, 3] - assert model._activation == ["relu", "relu", "relu"] - assert model._strides == [1, 1, 1] - assert model._padding == ["same", "same", "same"] - - -@pytest.mark.skipif( - not _check_soft_dependencies(["tensorflow"], severity="none"), - reason="skip test if required soft dependency not available", -) -def test_edge_case_initialization(): - """Tests edge cases for minimal configuration.""" - model = ResNetNetwork( - n_residual_blocks=1, - n_conv_per_residual_block=1, - n_filters=[64], - kernel_size=[8], - ) - model.build_network((128, 1)) - assert model.n_residual_blocks == 1 - assert model.n_conv_per_residual_block == 1 - assert model._n_filters == [64] - assert model._kernel_size == [8] + assert isinstance( + model, ResNetNetwork + ), "Custom initialization failed: Incorrect type" + assert model._n_filters == [64, 128, 128], "n_filters list mismatch" + assert model._kernel_size == [8, 5, 3], "kernel_size list mismatch" + assert model._activation == ["relu", "relu", "relu"], "activation list mismatch" + assert model._strides == [1, 1, 1], "strides list mismatch" + assert model._padding == ["same", "same", "same"], "padding list mismatch" @pytest.mark.skipif( @@ -75,67 +62,19 @@ def test_edge_case_initialization(): ) def test_invalid_initialization(): """Test if the network raises valid exceptions for invalid configurations.""" - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=".*same as number of residual blocks.*"): ResNetNetwork(n_filters=[64, 128], n_residual_blocks=3).build_network((128, 1)) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=".*same as number of convolution layers.*"): ResNetNetwork(kernel_size=[8, 5], n_conv_per_residual_block=3).build_network( (128, 1) ) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=".*same as number of convolution layers.*"): ResNetNetwork(strides=[1, 2], n_conv_per_residual_block=3).build_network( (128, 1) ) - with pytest.raises(ValueError): - ResNetNetwork(dilation_rate=[1, 2], n_conv_per_residual_block=3).build_network( - (128, 1) - ) - - with pytest.raises(ValueError): - ResNetNetwork( - padding=["same", "valid"], n_conv_per_residual_block=3 - ).build_network((128, 1)) - - with pytest.raises(ValueError): - ResNetNetwork( - activation=["relu", "tanh"], n_conv_per_residual_block=3 - ).build_network((128, 1)) - - with pytest.raises(ValueError): - ResNetNetwork( - use_bias=[True, False], n_conv_per_residual_block=3 - ).build_network((128, 1)) - - -@pytest.mark.skipif( - not _check_soft_dependencies(["tensorflow"], severity="none"), - reason="skip test if required soft dependency not available", -) -def test_list_parameters(): - """Test correct handling of list parameters.""" - model = ResNetNetwork( - n_residual_blocks=3, - n_conv_per_residual_block=3, - n_filters=[64, 128, 128], - kernel_size=[8, 5, 3], - strides=[1, 1, 1], - dilation_rate=[1, 1, 1], - padding=["same", "same", "same"], - activation=["relu", "relu", "relu"], - use_bias=[True, True, True], - ) - model.build_network((128, 1)) - - assert model._n_filters == [64, 128, 128] - assert model._kernel_size == [8, 5, 3] - assert model._strides == [1, 1, 1] - assert model._dilation_rate == [1, 1, 1] - assert model._padding == ["same", "same", "same"] - assert model._activation == ["relu", "relu", "relu"] - assert model._use_bias == [True, True, True] - @pytest.mark.skipif( not _check_soft_dependencies(["tensorflow"], severity="none"), @@ -148,8 +87,10 @@ def test_build_network(): input_shapes = [(128, 1), (256, 3), (512, 1)] for shape in input_shapes: input_layer, output_layer = model.build_network(shape) - assert input_layer.shape[1:] == shape - assert output_layer.shape[-1] == 128 + assert hasattr(input_layer, "shape"), "Input layer type mismatch" + assert hasattr(output_layer, "shape"), "Output layer type mismatch" + assert input_layer.shape[1:] == shape, "Input shape mismatch" + assert output_layer.shape[-1] == 128, "Output layer mismatch" @pytest.mark.skipif( @@ -159,8 +100,9 @@ def test_build_network(): def test_shortcut_layer(): """Test the shortcut layer functionality.""" model = ResNetNetwork() + input_tensor = tf.keras.layers.Input((128, 64)) output_tensor = tf.keras.layers.Conv1D(128, 8, padding="same")(input_tensor) - shortcut = model._shortcut_layer(input_tensor, output_tensor) - assert shortcut.shape[-1] == 128 + assert hasattr(shortcut, "shape"), "Shortcut layer output type mismatch" + assert shortcut.shape[-1] == 128, "Shortcut output shape mismatch" From da1256bbe74e023d8215528455ad268c8a6d04d0 Mon Sep 17 00:00:00 2001 From: Kavya Rambhia <161142013+kavya-r30@users.noreply.github.com> Date: Thu, 20 Feb 2025 18:49:45 +0530 Subject: [PATCH 3/4] Fixed tensorflow failing --- aeon/networks/tests/test_resnet.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/aeon/networks/tests/test_resnet.py b/aeon/networks/tests/test_resnet.py index 5b9e781f59..cb71a180ae 100644 --- a/aeon/networks/tests/test_resnet.py +++ b/aeon/networks/tests/test_resnet.py @@ -1,7 +1,6 @@ """Tests for the ResNet Model.""" import pytest -import tensorflow as tf from aeon.networks import ResNetNetwork from aeon.utils.validation._dependencies import _check_soft_dependencies @@ -101,8 +100,10 @@ def test_shortcut_layer(): """Test the shortcut layer functionality.""" model = ResNetNetwork() - input_tensor = tf.keras.layers.Input((128, 64)) - output_tensor = tf.keras.layers.Conv1D(128, 8, padding="same")(input_tensor) - shortcut = model._shortcut_layer(input_tensor, output_tensor) + input_shape = (128, 64) + input_layer, output_layer = model.build_network(input_shape) + + shortcut = model._shortcut_layer(input_layer, output_layer) + assert hasattr(shortcut, "shape"), "Shortcut layer output type mismatch" assert shortcut.shape[-1] == 128, "Shortcut output shape mismatch" From 11edb6343d92fda0edad917a26600fe9b9b62783 Mon Sep 17 00:00:00 2001 From: Kavya Rambhia <161142013+kavya-r30@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:46:26 +0530 Subject: [PATCH 4/4] Added Resnet in function name --- aeon/networks/tests/test_resnet.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/aeon/networks/tests/test_resnet.py b/aeon/networks/tests/test_resnet.py index cb71a180ae..4d5c58c5b9 100644 --- a/aeon/networks/tests/test_resnet.py +++ b/aeon/networks/tests/test_resnet.py @@ -10,7 +10,7 @@ not _check_soft_dependencies(["tensorflow"], severity="none"), reason="skip test if required soft dependency not available", ) -def test_default_initialization(): +def test_resnet_default_initialization(): """Test if the network initializes with proper attributes.""" model = ResNetNetwork() assert isinstance( @@ -33,7 +33,7 @@ def test_default_initialization(): not _check_soft_dependencies(["tensorflow"], severity="none"), reason="skip test if required soft dependency not available", ) -def test_custom_initialization(): +def test_resnet_custom_initialization(): """Test whether custom kwargs are correctly set.""" model = ResNetNetwork( n_residual_blocks=3, @@ -59,7 +59,7 @@ def test_custom_initialization(): not _check_soft_dependencies(["tensorflow"], severity="none"), reason="skip test if required soft dependency not available", ) -def test_invalid_initialization(): +def test_resnet_invalid_initialization(): """Test if the network raises valid exceptions for invalid configurations.""" with pytest.raises(ValueError, match=".*same as number of residual blocks.*"): ResNetNetwork(n_filters=[64, 128], n_residual_blocks=3).build_network((128, 1)) @@ -79,7 +79,7 @@ def test_invalid_initialization(): not _check_soft_dependencies(["tensorflow"], severity="none"), reason="skip test if required soft dependency not available", ) -def test_build_network(): +def test_resnet_build_network(): """Test network building with various input shapes.""" model = ResNetNetwork() @@ -96,7 +96,7 @@ def test_build_network(): not _check_soft_dependencies(["tensorflow"], severity="none"), reason="skip test if required soft dependency not available", ) -def test_shortcut_layer(): +def test_resnet_shortcut_layer(): """Test the shortcut layer functionality.""" model = ResNetNetwork()