Skip to content

Commit

Permalink
flake8 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Mamalet committed Oct 8, 2024
1 parent 554e499 commit 37a54f5
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 52 deletions.
2 changes: 1 addition & 1 deletion tests/test_activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def test_Householder_serialization():

# Instantiation error because of wrong data format
with pytest.raises(RuntimeError):
hh = uft.get_instance_framework(Householder, {"data_format": "channels_first"})
_ = uft.get_instance_framework(Householder, {"data_format": "channels_first"})


@pytest.mark.skipif(
Expand Down
10 changes: 1 addition & 9 deletions tests/test_compute_layer_sv.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
SpectralConv2d,
SpectralLinear,
)
from .utils_framework import Sequential
from .utils_framework import OrthLinearRegularizer

from . import utils_framework as uft
Expand Down Expand Up @@ -166,14 +165,7 @@ def train_compute_and_verifySV(
# define logging features
logdir = os.path.join("logs", uft.LIP_LAYERS, "%s" % layer_type.__name__)
os.makedirs(logdir, exist_ok=True)
hparams = dict(
layer_type=layer_type.__name__,
batch_size=batch_size,
steps_per_epoch=steps_per_epoch,
epochs=epochs,
k_lip_data=k_lip_data,
k_lip_model=k_lip_model,
)

callback_list = [] # [hp.KerasCallback(logdir, hparams)]
if "callbacks" in kwargs and (kwargs["callbacks"] is not None):
callback_list = callback_list + kwargs["callbacks"]
Expand Down
1 change: 0 additions & 1 deletion tests/test_condense.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
copy_model_parameters,
Sequential,
tModel,
Model,
)

from tests.utils_framework import (
Expand Down
3 changes: 1 addition & 2 deletions tests/test_initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
],
)
def test_initializer(layer_type, layer_params, input_shape, orthogonal_test):
batch_size = 1000
np.random.seed(42)
# clear session to avoid side effects from previous train
uft.init_session() # K.clear_session()
Expand All @@ -86,7 +85,7 @@ def test_initializer(layer_type, layer_params, input_shape, orthogonal_test):
loss=uft.MeanSquaredError(),
metrics=[uft.metric_mse()],
)
#######model.build((batch_size,) + input_shape)

sigmas = np.linalg.svd(
uft.to_numpy(uft.get_layer_weights_by_index(model, 0)),
full_matrices=False,
Expand Down
9 changes: 1 addition & 8 deletions tests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +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)
hparams = dict(
layer_type=layer_type.__name__,
batch_size=batch_size,
steps_per_epoch=steps_per_epoch,
epochs=epochs,
k_lip_data=k_lip_data,
k_lip_model=k_lip_model,
)

callback_list = (
[]
) # [callbacks.TensorBoard(logdir), hp.KerasCallback(logdir, hparams)]
Expand Down
19 changes: 11 additions & 8 deletions tests/test_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,18 @@ 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]


## global test values
# global test values
y_true1 = [1.0, 1.0, 1.0, 0.0, 0.0, 0.0]
y_pred1 = [0.5, 1.5, -0.5, -0.5, -1.5, 0.5]

Expand Down Expand Up @@ -157,7 +159,7 @@ def one_hot_data(x, n_class):
y_pred4 = np.random.normal(size=(num_items4,))


############Qui de la HKR????
# TODO add HKR tests
@pytest.mark.parametrize(
"loss_instance, loss_params, y_true_np, y_pred_np, expected_loss,rtol",
[
Expand Down Expand Up @@ -199,7 +201,7 @@ def one_hot_data(x, n_class):
{
"alpha": 5.0,
"min_margin": 0.2,
}, ##Warning alpha replaced by alpha/(1+alpha)
}, # Warning alpha replaced by alpha/(1+alpha)
y_true2,
y_pred2,
np.float32(1.0897621 * uft.scaleAlpha(5.0)),
Expand Down Expand Up @@ -356,7 +358,7 @@ def test_softhkrmulticlass_loss():
y_true_np, y_pred_np = y_true2, y_pred2
expected_loss = np.float32(1.0897621) * uft.scaleAlpha(
5.0
) ##warning alpha scaled to be in [0,1]
) # warning alpha scaled to be in [0,1]
rtol = 1e-5
loss = uft.get_instance_framework(
loss_instance, inst_params={"alpha": 5.0, "min_margin": 0.2}
Expand Down Expand Up @@ -838,8 +840,9 @@ 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 the corresponding multilabel problem.
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
# multilabel one.
Expand Down
9 changes: 5 additions & 4 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
# CRIAQ and ANITI - https://www.deel.ai/
# =====================================================================================
import pytest
from collections import defaultdict
import numpy as np
import os

Expand All @@ -41,7 +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]


Expand Down Expand Up @@ -94,7 +94,7 @@ def test_serialization(nb_class, loss, loss_params, nb_classes):
loss_fn, optimizer, metrics = uft.compile_model(
m,
optimizer=uft.get_instance_framework(
SGD, inst_params={"lr": 0.001, "model": m}
uft.SGD, inst_params={"lr": 0.001, "model": m}
),
loss=uft.get_instance_framework(loss, inst_params=loss_params),
)
Expand Down Expand Up @@ -184,7 +184,8 @@ def test_provable_vs_adjusted(loss, loss_params, nb_class):
diff = np.min(np.abs(l1 - l2))
assert (
diff > 1e-4
), f"{loss} provable and adjusted robustness must give different values when y_true!=y_pred"
), f"{loss} provable and adjusted robustness must give
different values when y_true!=y_pred"


@pytest.mark.parametrize(
Expand Down
3 changes: 1 addition & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"""These tests assert that deel.lip Sequential and Model objects behave as expected."""

import warnings
from packaging import version
import pytest
import numpy as np
from collections import OrderedDict
Expand Down Expand Up @@ -333,6 +332,6 @@ def test_vanilla_export_with_named_layers():
)
x = np.random.normal(size=(1,) + input_shape)
x = uft.to_tensor(x)
y = model(x)
_ = model(x)
names = [name for name, _ in uft.get_named_children(model.vanilla_export())]
assert names == ["features", "classifier"]
8 changes: 4 additions & 4 deletions tests/test_normalizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ def test_kernel_conv_svd(kernel_shape, strides):
"""Compare power iteration conv against SVD."""

if hasattr(spectral_normalization_conv, "unavailable_class"):
pytest.skip(f"spectral_normalization_conv not implemented")
pytest.skip("spectral_normalization_conv not implemented")
if hasattr(_padding_circular, "unavailable_class"):
pytest.skip(f"_padding_circular not implemented")
pytest.skip("_padding_circular not implemented")

kernel = np.random.normal(size=kernel_shape).astype("float32")
spectral_input_shape, RO_case, cPad = set_spectral_input_shape(kernel, strides)
Expand Down Expand Up @@ -264,7 +264,7 @@ def test_bjorck_normalization(kernel_shape):
)
def test_reshaped_kernel_orthogonalization(kernel_shape):
if hasattr(reshaped_kernel_orthogonalization, "unavailable_class"):
pytest.skip(f"reshaped_kernel_orthogonalization not implemented")
pytest.skip("reshaped_kernel_orthogonalization not implemented")

kernel = np.random.normal(size=kernel_shape).astype("float32")
"""Compare max singular value using power iteration and tf.linalg.svd"""
Expand Down Expand Up @@ -429,7 +429,7 @@ def test_lconv_norm():
shape = uft.to_framework_channel(shape)
x = np.random.rand(*shape)
x = uft.to_tensor(x)
y = m(x)
_ = m(x)

# ensure that the original weight is the only torch parameter
uft.check_parametrization(m, is_parametrized=True)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_updownsampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# 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

Expand Down
95 changes: 95 additions & 0 deletions tests/test_updownsampling_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# -*- 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)
13 changes: 1 addition & 12 deletions tests/utils_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def get_instance_withcheck(


getters_dict = {
# GroupSort: partial(get_instance_withreplacement, dict_keys_replace={'n':'group_size'}),
ScaledAdaptiveAvgPool2d: partial(
get_instance_withreplacement, dict_keys_replace={"data_format": None}
),
Expand Down Expand Up @@ -223,17 +222,7 @@ def get_instance_withcheck(
),
},
),
# InvertibleDownSampling: partial(get_instance_withreplacement, dict_keys_replace={'pool_size':'kernel_size'}),
# InvertibleUpSampling: partial(get_instance_withreplacement, dict_keys_replace={'pool_size':'kernel_size'}),
}
"""def get_optim_generic(instance_type, inst_params):
layp = copy.deepcopy(inst_params)
layp.pop('model',None)
if 'lr' in layp:
layp['learning_rate'] = layp.pop('lr')
return instance_type(**layp)
"""


def get_instance_framework(instance_type, inst_params):
if instance_type not in getters_dict:
Expand Down Expand Up @@ -378,7 +367,7 @@ def Adam(lr=0.01, model=None):


def CategoricalCrossentropy(from_logits=True):
assert from_logits == True, "from_logits has to be True"
assert from_logits, "from_logits has to be True"
return CrossEntropyLoss()


Expand Down

0 comments on commit 37a54f5

Please sign in to comment.