From f80de1938f0c5e2424b753e9a190789f31b0c812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Z=2E=20Vitorelli?= Date: Thu, 16 Dec 2021 11:23:48 +0100 Subject: [PATCH 1/3] adds interpolation gradient testing --- tests/test_interpolation_gradients.py | 58 +++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/test_interpolation_gradients.py diff --git a/tests/test_interpolation_gradients.py b/tests/test_interpolation_gradients.py new file mode 100644 index 0000000..9ff2ace --- /dev/null +++ b/tests/test_interpolation_gradients.py @@ -0,0 +1,58 @@ +# This module tests tfa gradients in respect to interpolation methods. +import numpy as np +from tensorflow_addons.image import resampler +from scipy.misc import face +import numdifftools +import tensorflow as tf + +from numpy.testing import assert_allclose + + +def facer(interpolant, warp_tf): + image = face(gray=True)[-512:-512+128,-512:-512+128].astype('float32') + image_tf = tf.convert_to_tensor(image.reshape([1,128,128, 1])) + #define a shift + shift = tf.zeros([1,2]) + + #calculate derivatives via tf.GradientTape + with tf.GradientTape() as tape: + tape.watch(shift) + ws = tf.reshape(shift,[1,1,1,2]) + warp_tf + o = resampler(image_tf, ws, interpolant) + autodiff_jacobian = tape.batch_jacobian(o, shift) + + #calculate derivatives via numdifftools + def fn(shift): + shift = tf.convert_to_tensor(shift.astype('float32')) + ws = tf.reshape(shift,[1,1,1,2]) + warp_tf + o = resampler(image_tf, ws, interpolant) + return o.numpy().flatten() + + numdiff_jacobian = numdifftools.Jacobian(fn, order=4, step=0.04) + numdiff_jacobian = numdiff_jacobian(np.zeros([2])).reshape([128,128,2]) + + return autodiff_jacobian[0,...,0,:], numdiff_jacobian + + +def test_interpolation_gradients(): + atol = 0.003 #taken from the bilinear case with half step warp. + + interpolant = "bilinear" + #on pixel interpolation + int_warp = np.stack(np.meshgrid(np.arange(128), np.arange(128)), axis=-1).astype('float32') + int_warp_tf = tf.convert_to_tensor(int_warp.reshape([1,128,128,2])) + + #half step interpolation + half_warp = np.stack(np.meshgrid(np.arange(128), np.arange(128)), axis=-1).astype('float32') + half_warp_tf = tf.convert_to_tensor(half_warp.reshape([1,128,128,2])+.5) #add a half-step + + autodiff_jacobian_int, numdiff_jacobian_int = facer(interpolant,int_warp_tf) + autodiff_jacobian_half, numdiff_jacobian_half = facer(interpolant,half_warp_tf) + + + assert_allclose(autodiff_jacobian_half,numdiff_jacobian_half, rtol=0.1, atol=atol) + assert_allclose(autodiff_jacobian_int, numdiff_jacobian_int, rtol=0.1, atol=atol) + + +if __name__=='__main__': + test_interpolation_gradients() \ No newline at end of file From f9196e981adc3e027796a29005124ed0d1c7e74a Mon Sep 17 00:00:00 2001 From: Andre Zamorano Vitorelli Date: Fri, 7 Jan 2022 11:20:12 +0100 Subject: [PATCH 2/3] Update main.yml added numdifftools --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8a0fb9a..0956746 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -52,7 +52,7 @@ jobs: galsim \ ngmix - python -m pip install tensorflow==2.5.0 tensorflow_addons==0.13.0 tensorflow_probability==0.13.0 + python -m pip install tensorflow==2.5.0 tensorflow_addons==0.13.0 tensorflow_probability==0.13.0 numdifftools python -m pip install git+https://github.com/DifferentiableUniverseInitiative/GalFlow.git python -m pip install -e . - name: test From 851e48c1471ca00bbcc04b09cc4ee5c403024a7d Mon Sep 17 00:00:00 2001 From: Andre Zamorano Vitorelli Date: Wed, 23 Feb 2022 23:38:29 +0100 Subject: [PATCH 3/3] apply xfail to functions needing tfa modification --- tests/test_interpolation_gradients.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_interpolation_gradients.py b/tests/test_interpolation_gradients.py index 9ff2ace..47670fd 100644 --- a/tests/test_interpolation_gradients.py +++ b/tests/test_interpolation_gradients.py @@ -33,7 +33,10 @@ def fn(shift): return autodiff_jacobian[0,...,0,:], numdiff_jacobian +import pytest +xfail = pytest.mark.xfail +@xfail(reason="Fails because it needs the modified tensorflow_addons to work") def test_interpolation_gradients(): atol = 0.003 #taken from the bilinear case with half step warp. @@ -55,4 +58,4 @@ def test_interpolation_gradients(): if __name__=='__main__': - test_interpolation_gradients() \ No newline at end of file + test_interpolation_gradients()