From 016b9e18c0154895a77703107f659911358e3db1 Mon Sep 17 00:00:00 2001 From: Franck Mamalet Date: Wed, 9 Oct 2024 10:28:45 +0200 Subject: [PATCH] seed on test normalizers --- tests/test_layers.py | 2 +- tests/test_losses.py | 12 ++-- tests/test_metrics.py | 4 +- tests/test_normalizers.py | 13 ++++ tests/test_updownsampling_pytest.py | 95 ----------------------------- tests/utils_framework.py | 1 + 6 files changed, 23 insertions(+), 104 deletions(-) delete mode 100644 tests/test_updownsampling_pytest.py diff --git a/tests/test_layers.py b/tests/test_layers.py index 26337df..2a05e83 100644 --- a/tests/test_layers.py +++ b/tests/test_layers.py @@ -192,7 +192,7 @@ def train_k_lip_model( # define logging features logdir = os.path.join("logs", uft.LIP_LAYERS, "%s" % layer_type.__name__) os.makedirs(logdir, exist_ok=True) - + callback_list = ( [] ) # [callbacks.TensorBoard(logdir), hp.KerasCallback(logdir, hparams)] diff --git a/tests/test_losses.py b/tests/test_losses.py index ee78b76..ea2ed07 100644 --- a/tests/test_losses.py +++ b/tests/test_losses.py @@ -92,14 +92,14 @@ def check_serialization(nb_class, loss): def binary_data(x): - """Return a Framework float32 tensor of shape [N, 1] - from a list/np.array of shape [N]""" + """Return a Framework float32 tensor of shape [N, 1] + from a list/np.array of shape [N]""" return np.expand_dims(np.array(x, dtype=np.float32), axis=-1) def one_hot_data(x, n_class): - """Return a Framework float32 tensor of shape [N, n_class] - from a list/np.array of shape [N]""" + """Return a Framework float32 tensor of shape [N, n_class] + from a list/np.array of shape [N]""" return np.eye(n_class)[x] @@ -840,8 +840,8 @@ def test_minibatches_binary_loss_generic( def test_multilabel_loss_generic(loss_instance, loss_params, rtol): """ Assert binary losses with multilabels. - Three losses are tested (KRLoss, HingeMarginLoss and HKRLoss). - We compare losses with three separate binary classification and + Three losses are tested (KRLoss, HingeMarginLoss and HKRLoss). + We compare losses with three separate binary classification and the corresponding multilabel problem. """ # Create predictions and labels for 3 binary problems and the concatenated diff --git a/tests/test_metrics.py b/tests/test_metrics.py index d903c0a..0be592c 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -40,8 +40,8 @@ def one_hot_data(x, n_class): - """Return a Framework float32 tensor of shape [N, n_class] - from a list/np.array of shape [N]""" + """Return a Framework float32 tensor of shape [N, n_class] + from a list/np.array of shape [N]""" return np.eye(n_class)[x] diff --git a/tests/test_normalizers.py b/tests/test_normalizers.py index 0f551eb..630e56b 100644 --- a/tests/test_normalizers.py +++ b/tests/test_normalizers.py @@ -149,6 +149,8 @@ def test_kernel_conv_svd(kernel_shape, strides): if hasattr(_padding_circular, "unavailable_class"): pytest.skip("_padding_circular not implemented") + np.random.seed(42) + kernel = np.random.normal(size=kernel_shape).astype("float32") spectral_input_shape, RO_case, cPad = set_spectral_input_shape(kernel, strides) @@ -220,6 +222,8 @@ def test_kernel_conv_svd(kernel_shape, strides): ], ) def test_bjorck_normalization(kernel_shape): + np.random.seed(42) + kernel = np.random.normal(size=kernel_shape).astype("float32") """Compare max singular value using power iteration and tf.linalg.svd""" sigmas_svd = np.linalg.svd( @@ -265,6 +269,7 @@ def test_bjorck_normalization(kernel_shape): def test_reshaped_kernel_orthogonalization(kernel_shape): if hasattr(reshaped_kernel_orthogonalization, "unavailable_class"): pytest.skip("reshaped_kernel_orthogonalization not implemented") + np.random.seed(42) kernel = np.random.normal(size=kernel_shape).astype("float32") """Compare max singular value using power iteration and tf.linalg.svd""" @@ -312,6 +317,8 @@ def test_bjorck_norm(): """ test bjorck_norm parametrization implementation """ + np.random.seed(42) + m = uft.get_instance_framework( tLinear, {"in_features": 2, "out_features": 2} ) # torch.nn.Linear(2, 2) @@ -351,6 +358,8 @@ def test_frobenius_norm(): """ test frobenius_norm parametrization implementation """ + np.random.seed(42) + m = uft.get_instance_framework( tLinear, {"in_features": 2, "out_features": 2} ) # torch.nn.Linear(2, 2) @@ -386,6 +395,8 @@ def test_frobenius_norm_disjoint_neurons(): """ Test `disjoint_neurons=True` argument in frobenius_norm parametrization """ + np.random.seed(42) + params = {"in_features": 5, "out_features": 3} m = uft.get_instance_framework(tLinear, params) @@ -410,6 +421,8 @@ def test_lconv_norm(): """ test lconv_norm parametrization implementation """ + np.random.seed(42) + params = { "in_channels": 1, "out_channels": 2, diff --git a/tests/test_updownsampling_pytest.py b/tests/test_updownsampling_pytest.py deleted file mode 100644 index d99dd6f..0000000 --- a/tests/test_updownsampling_pytest.py +++ /dev/null @@ -1,95 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright IRT Antoine de Saint Exupéry et Université Paul Sabatier Toulouse III - All -# rights reserved. DEEL is a research program operated by IVADO, IRT Saint Exupéry, -# CRIAQ and ANITI - https://www.deel.ai/ -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# Copyright IRT Antoine de Saint Exupéry et Université Paul Sabatier Toulouse III - All -# rights reserved. DEEL is a research program operated by IVADO, IRT Saint Exupéry, -# CRIAQ and ANITI - https://www.deel.ai/ -# ===================================================================================== -import torch -import pytest -import numpy as np - -from tests.utils_framework import invertible_downsample, invertible_upsample - -from tests.utils_framework import ( - to_tensor, - to_numpy, - to_framework_channel, - get_instance_framework, -) - - -@pytest.mark.skipif( - hasattr(invertible_downsample, "unavailable_class"), - reason="invertible_downsample not available", -) -def test_invertible_downsample(): - # 1D input - x = to_tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]) - x = get_instance_framework(invertible_downsample, {"input": x, "kernel_size": (2,)}) - assert x.shape == (1, 4, 2) - - # TODO: Check this. - np.testing.assert_equal(to_numpy(x), [[[1, 2], [3, 4], [5, 6], [7, 8]]]) - - # 2D input - x = np.random.rand(10, 1, 128, 128) # torch.rand(10, 1, 128, 128) - x = to_tensor(x) - assert invertible_downsample(x, (4, 4)).shape == (10, 16, 32, 32) - - x = np.random.rand(10, 4, 64, 64) - x = to_tensor(x) - assert invertible_downsample(x, (2, 2)).shape == (10, 16, 32, 32) - - # 3D input - x = np.random.rand(10, 2, 128, 64, 64) - x = to_tensor(x) - assert invertible_downsample(x, 2).shape == (10, 16, 64, 32, 32) - - -@pytest.mark.skipif( - hasattr(invertible_upsample, "unavailable_class"), - reason="invertible_upsample not available", -) -def test_invertible_upsample(): - # 1D input - x = to_tensor([[[1, 2], [3, 4], [5, 6], [7, 8]]]) - x = get_instance_framework(invertible_upsample, {"input": x, "kernel_size": (2,)}) - - assert x.shape == (1, 2, 4) - - # Check output. - np.testing.assert_equal(to_numpy(x), [[[1, 2, 3, 4], [5, 6, 7, 8]]]) - - # 2D input - x = np.random.rand(10, 16, 32, 32) - x = to_tensor(x) - y = get_instance_framework(invertible_upsample, {"input": x, "kernel_size": (4, 4)}) - assert y.shape == (10, 1, 128, 128) - y = get_instance_framework(invertible_upsample, {"input": x, "kernel_size": (2, 2)}) - assert y.shape == (10, 4, 64, 64) - - # 3D input - x = np.random.rand(10, 16, 64, 32, 32) - x = to_tensor(x) - y = get_instance_framework(invertible_upsample, {"input": x, "kernel_size": 2}) - assert y.shape == (10, 2, 128, 64, 64) diff --git a/tests/utils_framework.py b/tests/utils_framework.py index 2b46ec2..27fa2c6 100644 --- a/tests/utils_framework.py +++ b/tests/utils_framework.py @@ -224,6 +224,7 @@ def get_instance_withcheck( ), } + def get_instance_framework(instance_type, inst_params): if instance_type not in getters_dict: instance = get_instance_generic(instance_type, inst_params)