diff options
author | Rémi Flamary <remi.flamary@gmail.com> | 2018-03-20 16:39:24 +0100 |
---|---|---|
committer | Rémi Flamary <remi.flamary@gmail.com> | 2018-03-20 16:39:24 +0100 |
commit | 4fc9ccc7c6c96a43c48be54e89133c8f481d8bf4 (patch) | |
tree | 048d7b1ca7a46f1b10de6bce6a5653babb62aa61 /examples/plot_otda_linear_mapping.py | |
parent | c1046238d826fe9cf1294f8ea60b8d44743fac78 (diff) |
better example+test
Diffstat (limited to 'examples/plot_otda_linear_mapping.py')
-rw-r--r-- | examples/plot_otda_linear_mapping.py | 98 |
1 files changed, 76 insertions, 22 deletions
diff --git a/examples/plot_otda_linear_mapping.py b/examples/plot_otda_linear_mapping.py index 143f129..163f8f1 100644 --- a/examples/plot_otda_linear_mapping.py +++ b/examples/plot_otda_linear_mapping.py @@ -9,11 +9,11 @@ Created on Tue Mar 20 14:31:15 2018 import numpy as np import pylab as pl import ot -import scipy.linalg as linalg - - -#%% +from scipy import ndimage +############################################################################## +# Generate data +# ------------- n = 1000 d = 2 @@ -37,49 +37,103 @@ A = np.array([[1.5, .7], [.7, 1.5]]) b = np.array([[4, 2]]) xt = xt.dot(A) + b -#%% +############################################################################## +# Plot data +# --------- pl.figure(1, (5, 5)) pl.plot(xs[:, 0], xs[:, 1], '+') pl.plot(xt[:, 0], xt[:, 1], 'o') -#%% -Ae, be = ot.da.OT_mapping_linear(xs, xt) +############################################################################## +# Estimate linear mapping and transport +# ------------------------------------- -Ae1 = linalg.inv(Ae) -be1 = -be.dot(Ae1) +Ae, be = ot.da.OT_mapping_linear(xs, xt) xst = xs.dot(Ae) + be -xts = xt.dot(Ae1) + be1 -# %% + +############################################################################## +# Plot transported samples +# ------------------------ pl.figure(1, (5, 5)) pl.clf() pl.plot(xs[:, 0], xs[:, 1], '+') pl.plot(xt[:, 0], xt[:, 1], 'o') pl.plot(xst[:, 0], xst[:, 1], '+') -pl.plot(xts[:, 0], xts[:, 1], 'o') pl.show() +############################################################################## +# Mapping Class between images +# ---------------------------- + + +def im2mat(I): + """Converts and image to matrix (one pixel per line)""" + return I.reshape((I.shape[0] * I.shape[1], I.shape[2])) + + +def mat2im(X, shape): + """Converts back a matrix to an image""" + return X.reshape(shape) + + +def minmax(I): + return np.clip(I, 0, 1) + + +# Loading images +I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 +I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256 -#%% Example class with on images + +X1 = im2mat(I1) +X2 = im2mat(I2) + +############################################################################## +# Estimate mapping and adapt +# ---------------------------- mapping = ot.da.LinearTransport() -mapping.fit(Xs=xs, Xt=xt) +mapping.fit(Xs=X1, Xt=X2) -xst = mapping.transform(Xs=xs) -xts = mapping.inverse_transform(Xt=xt) +xst = mapping.transform(Xs=X1) +xts = mapping.inverse_transform(Xt=X2) + +I1t = minmax(mat2im(xst, I1.shape)) +I2t = minmax(mat2im(xts, I2.shape)) # %% -pl.figure(1, (5, 5)) -pl.clf() -pl.plot(xs[:, 0], xs[:, 1], '+') -pl.plot(xt[:, 0], xt[:, 1], 'o') -pl.plot(xst[:, 0], xst[:, 1], '+') -pl.plot(xts[:, 0], xts[:, 1], 'o') + +############################################################################## +# Plot transformed images +# ----------------------- + +pl.figure(2, figsize=(10, 7)) + +pl.subplot(2, 2, 1) +pl.imshow(I1) +pl.axis('off') +pl.title('Im. 1') + +pl.subplot(2, 2, 2) +pl.imshow(I2) +pl.axis('off') +pl.title('Im. 2') + +pl.subplot(2, 2, 3) +pl.imshow(I1t) +pl.axis('off') +pl.title('Mapping Im. 1') + +pl.subplot(2, 2, 4) +pl.imshow(I2t) +pl.axis('off') +pl.title('Inverse mapping Im. 2') |