Skip to content

Commit 0d1cdae

Browse files
authored
Merge pull request #97 from deel-ai/feat/test_uniformization
Merge the unit tests using pytest. All tests are common with keras3 branch, and also torchlip library (https://github.com/deel-ai/deel-torchlip) The only difference is in the file: tests/utils_framework.py Modify Readme.md + docs/index.md to change the banners increment to minor version 1.5.1
2 parents 58bf1e4 + 7365bb0 commit 0d1cdae

28 files changed

+6346
-3363
lines changed

.github/workflows/python-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
matrix:
1818
include:
1919
- python-version: 3.8
20-
tf-version: 2.3
20+
tf-version: 2.4
2121
- python-version: 3.9
2222
tf-version: 2.7
2323
- python-version: "3.10"

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ $ make test
3232

3333
This command will:
3434
- check your code with black PEP-8 formatter and flake8 linter.
35-
- run `unittest` on the `tests/` folder with different Python and TensorFlow versions.
35+
- run `pytest` on the `tests/` folder with different Python and TensorFlow versions.
3636

3737

3838
## Submitting your changes

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div align="center">
22
<picture>
3-
<source media="(prefers-color-scheme: dark)" srcset="./docs/assets/logo_white.svg">
4-
<source media="(prefers-color-scheme: light)" srcset="./docs/assets/logo.svg">
5-
<img alt="Library Banner" src="./docs/assets/logo.svg">
3+
<source media="(prefers-color-scheme: dark)" srcset="./docs/assets/banner_dark_deellip.png">
4+
<source media="(prefers-color-scheme: light)" srcset="./docs/assets/banner_light_deellip.png">
5+
<img alt="DEEL-LIP Banner" src="./docs/assets/banner_light_deellip.png">
66
</picture>
77
</div>
88
<br>
@@ -44,9 +44,7 @@ layers for keras**.
4444
> **Incompatibility with TensorFlow >= 2.16 and Keras 3**
4545
>
4646
> Due to significant changes introduced in TensorFlow version 2.16 and Keras 3, this
47-
> package is currently incompatible with TensorFlow versions 2.16 and above. Users are
48-
> advised to use TensorFlow versions lower than 2.16 to ensure compatibility and proper
49-
> functionality of this package.
47+
> 'master' branch is currently incompatible with TensorFlow versions 2.16 and above.
5048
>
5149
> We're excited to announce that a nightly version of deel-lip with support for Keras 3
5250
> is now available for testing! If you'd like to experiment with the latest updates
@@ -136,7 +134,8 @@ More from the DEEL project:
136134
137135
- [Xplique](https://github.com/deel-ai/xplique) a Python library exclusively dedicated to explaining neural networks.
138136
- [Influenciae](https://github.com/deel-ai/influenciae) Python toolkit dedicated to computing influence values for the discovery of potentially problematic samples in a dataset.
139-
- [deel-torchlip](https://github.com/deel-ai/deel-torchlip) a Python library for training k-Lipschitz neural networks on PyTorch.
137+
- [deel-TorchLip](https://github.com/deel-ai/deel-torchlip) a Python library for training k-Lipschitz neural networks on PyTorch.
138+
- [Oodeel](https://github.com/deel-ai/oodeel) a Python library for post-hoc deep OOD (Out-of-Distribution) detection on already trained neural network image classifiers
140139
- [DEEL White paper](https://arxiv.org/abs/2103.10529) a summary of the DEEL team on the challenges of certifiable AI and the role of data quality, representativity and explainability for this purpose.
141140
142141
## 🙏 Acknowledgments

deel/lip/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.5.0
1+
1.5.1

deel/lip/layers/pooling.py

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""
2323

2424
import numpy as np
25+
from typing import Tuple
2526
import tensorflow as tf
2627
import tensorflow.keras.layers as keraslayers
2728
from tensorflow.keras.utils import register_keras_serializable
@@ -389,39 +390,51 @@ def __init__(
389390
The image shape must be divisible by the pool shape.
390391
391392
Args:
392-
pool_size: tuple describing the pool shape
393+
pool_size: int or tuple describing the pool shape (p_h, p_w)
393394
data_format: can either be `channels_last` or `channels_first`
394395
name: name of the layer
395396
dtype: dtype of the layer
396397
**kwargs: params passed to the Layers constructor
397398
"""
398399
super(InvertibleDownSampling, self).__init__(name=name, dtype=dtype, **kwargs)
399-
self.pool_size = pool_size
400400
self.data_format = data_format
401401

402-
def call(self, inputs):
403-
if self.data_format == "channels_last":
404-
return tf.concat(
405-
[
406-
inputs[
407-
:, i :: self.pool_size[0], j :: self.pool_size[1], :
408-
] # for now we handle only channels last
409-
for i in range(self.pool_size[0])
410-
for j in range(self.pool_size[1])
411-
],
412-
axis=-1,
413-
)
402+
ndims = 2
403+
ks: Tuple[int, ...]
404+
if isinstance(pool_size, int):
405+
ks = (pool_size,) * ndims
414406
else:
415-
return tf.concat(
416-
[
417-
inputs[
418-
:, :, i :: self.pool_size[0], j :: self.pool_size[1]
419-
] # for now we handle only channels last
420-
for i in range(self.pool_size[0])
421-
for j in range(self.pool_size[1])
422-
],
423-
axis=1,
407+
ks = tuple(pool_size)
408+
409+
if len(ks) != ndims:
410+
raise ValueError(
411+
f"Expected {ndims}-dimensional pool_size, but "
412+
f"got {len(ks)}-dimensional instead"
413+
)
414+
self.pool_size = ks
415+
416+
def call(self, inputs):
417+
if self.data_format == "channels_first":
418+
# convert to channels_first
419+
inputs = tf.transpose(inputs, [0, 2, 3, 1])
420+
# from shape (bs, w*pw, h*ph, c) to (bs, w, h, c, pw, ph)
421+
input_shape = tf.shape(inputs)
422+
w, h, c_in = input_shape[1], input_shape[2], input_shape[3]
423+
pw, ph = self.pool_size
424+
wo = w // pw
425+
ho = h // ph
426+
inputs = tf.reshape(inputs, (-1, wo, pw, h, c_in))
427+
inputs = tf.reshape(inputs, (-1, wo, pw, ho, ph, c_in))
428+
inputs = tf.transpose(
429+
inputs, [0, 1, 3, 5, 2, 4]
430+
) # (bs, wo, pw, ho, ph, c) -> (bs, wo, ho, c, pw, ph)
431+
inputs = tf.reshape(inputs, (-1, wo, ho, c_in * pw * ph))
432+
433+
if self.data_format == "channels_first":
434+
inputs = tf.transpose(
435+
inputs, [0, 3, 1, 2] # (bs, w, h, c*pw*ph) -> (bs, c*pw*ph, w, h) ->
424436
)
437+
return inputs
425438

426439
def get_config(self):
427440
config = {
@@ -453,16 +466,29 @@ def __init__(
453466
454467
455468
Args:
456-
pool_size: tuple describing the pool shape (p_h, p_w)
469+
pool_size: integer or tuple describing the pool shape (p_h, p_w)
457470
data_format: can either be `channels_last` or `channels_first`
458471
name: name of the layer
459472
dtype: dtype of the layer
460473
**kwargs: params passed to the Layers constructor
461474
"""
462475
super(InvertibleUpSampling, self).__init__(name=name, dtype=dtype, **kwargs)
463-
self.pool_size = pool_size
464476
self.data_format = data_format
465477

478+
ndims = 2
479+
ks: Tuple[int, ...]
480+
if isinstance(pool_size, int):
481+
ks = (pool_size,) * ndims
482+
else:
483+
ks = tuple(pool_size)
484+
485+
if len(ks) != ndims:
486+
raise ValueError(
487+
f"Expected {ndims}-dimensional pool_size, but "
488+
f"got {len(ks)}-dimensional instead"
489+
)
490+
self.pool_size = ks
491+
466492
def call(self, inputs):
467493
if self.data_format == "channels_first":
468494
# convert to channels_first
@@ -472,12 +498,12 @@ def call(self, inputs):
472498
w, h, c_in = input_shape[1], input_shape[2], input_shape[3]
473499
pw, ph = self.pool_size
474500
c = c_in // (pw * ph)
475-
inputs = tf.reshape(inputs, (-1, w, h, pw, ph, c))
501+
inputs = tf.reshape(inputs, (-1, w, h, c, pw, ph))
476502
inputs = tf.transpose(
477503
tf.reshape(
478504
tf.transpose(
479-
inputs, [0, 5, 2, 4, 1, 3]
480-
), # (bs, w, h, pw, ph, c) -> (bs, c, w, pw, h, ph)
505+
inputs, [0, 3, 2, 5, 1, 4]
506+
), # (bs, w, h, c, pw, ph) -> (bs, c, w, pw, h, ph)
481507
(-1, c, w, pw, h * ph),
482508
), # (bs, c, w, pw, h, ph) -> (bs, c, w, pw, h*ph) merge last axes
483509
[
121 KB
Loading
117 KB
Loading

docs/assets/logo.svg

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)