From 7ad472500dcce284231fc5968effa755802ab4ea Mon Sep 17 00:00:00 2001 From: Alexandre Gramfort Date: Wed, 12 Jul 2017 23:09:20 +0200 Subject: more --- examples/plot_OTDA_mapping_color_images.py | 136 +++++++++++++++-------------- 1 file changed, 69 insertions(+), 67 deletions(-) (limited to 'examples/plot_OTDA_mapping_color_images.py') diff --git a/examples/plot_OTDA_mapping_color_images.py b/examples/plot_OTDA_mapping_color_images.py index f07dc6c..b42dcdc 100644 --- a/examples/plot_OTDA_mapping_color_images.py +++ b/examples/plot_OTDA_mapping_color_images.py @@ -12,147 +12,149 @@ OT for domain adaptation with image color adaptation [6] with mapping estimation """ import numpy as np -import scipy.ndimage as spi +from scipy import ndimage import matplotlib.pylab as pl import ot #%% Loading images -I1=spi.imread('../data/ocean_day.jpg').astype(np.float64)/256 -I2=spi.imread('../data/ocean_sunset.jpg').astype(np.float64)/256 +I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 +I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256 #%% Plot images -pl.figure(1) - -pl.subplot(1,2,1) +pl.figure(1, figsize=(6.4, 3)) +pl.subplot(1, 2, 1) pl.imshow(I1) +pl.axis('off') pl.title('Image 1') -pl.subplot(1,2,2) +pl.subplot(1, 2, 2) pl.imshow(I2) +pl.axis('off') pl.title('Image 2') - -pl.show() +pl.tight_layout() #%% Image conversion and dataset generation def im2mat(I): """Converts and image to matrix (one pixel per line)""" - return I.reshape((I.shape[0]*I.shape[1],I.shape[2])) + return I.reshape((I.shape[0] * I.shape[1], I.shape[2])) + -def mat2im(X,shape): +def mat2im(X, shape): """Converts back a matrix to an image""" return X.reshape(shape) -X1=im2mat(I1) -X2=im2mat(I2) +X1 = im2mat(I1) +X2 = im2mat(I2) # training samples -nb=1000 -idx1=np.random.randint(X1.shape[0],size=(nb,)) -idx2=np.random.randint(X2.shape[0],size=(nb,)) +nb = 1000 +idx1 = np.random.randint(X1.shape[0], size=(nb,)) +idx2 = np.random.randint(X2.shape[0], size=(nb,)) -xs=X1[idx1,:] -xt=X2[idx2,:] +xs = X1[idx1, :] +xt = X2[idx2, :] #%% Plot image distributions -pl.figure(2,(10,5)) +pl.figure(2, figsize=(6.4, 5)) -pl.subplot(1,2,1) -pl.scatter(xs[:,0],xs[:,2],c=xs) -pl.axis([0,1,0,1]) +pl.subplot(1, 2, 1) +pl.scatter(xs[:, 0], xs[:, 2], c=xs) +pl.axis([0, 1, 0, 1]) pl.xlabel('Red') pl.ylabel('Blue') pl.title('Image 1') -pl.subplot(1,2,2) -#pl.imshow(I2) -pl.scatter(xt[:,0],xt[:,2],c=xt) -pl.axis([0,1,0,1]) +pl.subplot(1, 2, 2) +pl.scatter(xt[:, 0], xt[:, 2], c=xt) +pl.axis([0, 1, 0, 1]) pl.xlabel('Red') pl.ylabel('Blue') pl.title('Image 2') - -pl.show() - - +pl.tight_layout() #%% domain adaptation between images def minmax(I): - return np.minimum(np.maximum(I,0),1) + return np.clip(I, 0, 1) + # LP problem -da_emd=ot.da.OTDA() # init class -da_emd.fit(xs,xt) # fit distributions +da_emd = ot.da.OTDA() # init class +da_emd.fit(xs, xt) # fit distributions -X1t=da_emd.predict(X1) # out of sample -I1t=minmax(mat2im(X1t,I1.shape)) +X1t = da_emd.predict(X1) # out of sample +I1t = minmax(mat2im(X1t, I1.shape)) # sinkhorn regularization -lambd=1e-1 -da_entrop=ot.da.OTDA_sinkhorn() -da_entrop.fit(xs,xt,reg=lambd) +lambd = 1e-1 +da_entrop = ot.da.OTDA_sinkhorn() +da_entrop.fit(xs, xt, reg=lambd) -X1te=da_entrop.predict(X1) -I1te=minmax(mat2im(X1te,I1.shape)) +X1te = da_entrop.predict(X1) +I1te = minmax(mat2im(X1te, I1.shape)) # linear mapping estimation -eta=1e-8 # quadratic regularization for regression -mu=1e0 # weight of the OT linear term -bias=True # estimate a bias +eta = 1e-8 # quadratic regularization for regression +mu = 1e0 # weight of the OT linear term +bias = True # estimate a bias -ot_mapping=ot.da.OTDA_mapping_linear() -ot_mapping.fit(xs,xt,mu=mu,eta=eta,bias=bias,numItermax = 20,verbose=True) +ot_mapping = ot.da.OTDA_mapping_linear() +ot_mapping.fit(xs, xt, mu=mu, eta=eta, bias=bias, numItermax=20, verbose=True) -X1tl=ot_mapping.predict(X1) # use the estimated mapping -I1tl=minmax(mat2im(X1tl,I1.shape)) +X1tl = ot_mapping.predict(X1) # use the estimated mapping +I1tl = minmax(mat2im(X1tl, I1.shape)) # nonlinear mapping estimation -eta=1e-2 # quadratic regularization for regression -mu=1e0 # weight of the OT linear term -bias=False # estimate a bias -sigma=1 # sigma bandwidth fot gaussian kernel - +eta = 1e-2 # quadratic regularization for regression +mu = 1e0 # weight of the OT linear term +bias = False # estimate a bias +sigma = 1 # sigma bandwidth fot gaussian kernel -ot_mapping_kernel=ot.da.OTDA_mapping_kernel() -ot_mapping_kernel.fit(xs,xt,mu=mu,eta=eta,sigma=sigma,bias=bias,numItermax = 10,verbose=True) -X1tn=ot_mapping_kernel.predict(X1) # use the estimated mapping -I1tn=minmax(mat2im(X1tn,I1.shape)) -#%% plot images +ot_mapping_kernel = ot.da.OTDA_mapping_kernel() +ot_mapping_kernel.fit( + xs, xt, mu=mu, eta=eta, sigma=sigma, bias=bias, numItermax=10, verbose=True) +X1tn = ot_mapping_kernel.predict(X1) # use the estimated mapping +I1tn = minmax(mat2im(X1tn, I1.shape)) -pl.figure(2,(10,8)) +#%% plot images -pl.subplot(2,3,1) +pl.figure(2, figsize=(8, 4)) +pl.subplot(2, 3, 1) pl.imshow(I1) +pl.axis('off') pl.title('Im. 1') -pl.subplot(2,3,2) - +pl.subplot(2, 3, 2) pl.imshow(I2) +pl.axis('off') pl.title('Im. 2') - -pl.subplot(2,3,3) +pl.subplot(2, 3, 3) pl.imshow(I1t) +pl.axis('off') pl.title('Im. 1 Interp LP') -pl.subplot(2,3,4) +pl.subplot(2, 3, 4) pl.imshow(I1te) +pl.axis('off') pl.title('Im. 1 Interp Entrop') - -pl.subplot(2,3,5) +pl.subplot(2, 3, 5) pl.imshow(I1tl) +pl.axis('off') pl.title('Im. 1 Linear mapping') -pl.subplot(2,3,6) +pl.subplot(2, 3, 6) pl.imshow(I1tn) +pl.axis('off') pl.title('Im. 1 nonlinear mapping') +pl.tight_layout() pl.show() -- cgit v1.2.3 From da21b9888e77f7512727a4f50c60bd475e2c9606 Mon Sep 17 00:00:00 2001 From: Alexandre Gramfort Date: Wed, 12 Jul 2017 23:19:09 +0200 Subject: pep8 --- examples/plot_OTDA_mapping_color_images.py | 3 +++ examples/plot_WDA.py | 3 +-- examples/plot_optim_OTreg.py | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) (limited to 'examples/plot_OTDA_mapping_color_images.py') diff --git a/examples/plot_OTDA_mapping_color_images.py b/examples/plot_OTDA_mapping_color_images.py index b42dcdc..85c4b6b 100644 --- a/examples/plot_OTDA_mapping_color_images.py +++ b/examples/plot_OTDA_mapping_color_images.py @@ -36,6 +36,7 @@ pl.axis('off') pl.title('Image 2') pl.tight_layout() + #%% Image conversion and dataset generation def im2mat(I): @@ -78,7 +79,9 @@ pl.ylabel('Blue') pl.title('Image 2') pl.tight_layout() + #%% domain adaptation between images + def minmax(I): return np.clip(I, 0, 1) diff --git a/examples/plot_WDA.py b/examples/plot_WDA.py index 8a44022..9eb8693 100644 --- a/examples/plot_WDA.py +++ b/examples/plot_WDA.py @@ -9,8 +9,7 @@ Wasserstein Discriminant Analysis import numpy as np import matplotlib.pylab as pl -import ot -from ot.datasets import get_1D_gauss as gauss + from ot.dr import wda, fda diff --git a/examples/plot_optim_OTreg.py b/examples/plot_optim_OTreg.py index 1c3a1c5..e38253c 100644 --- a/examples/plot_optim_OTreg.py +++ b/examples/plot_optim_OTreg.py @@ -36,6 +36,7 @@ ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') #%% Example with Frobenius norm regularization + def f(G): return 0.5 * np.sum(G**2) -- cgit v1.2.3 From 6ada23e5a672b08f28e21123c4135bc787e83b19 Mon Sep 17 00:00:00 2001 From: Alexandre Gramfort Date: Thu, 20 Jul 2017 15:39:50 +0200 Subject: pep8 --- examples/plot_OTDA_color_images.py | 2 ++ examples/plot_OTDA_mapping_color_images.py | 2 ++ examples/plot_optim_OTreg.py | 3 +++ ot/utils.py | 6 ++++-- test/test_gpu_sinkhorn_lpl1.py | 1 + 5 files changed, 12 insertions(+), 2 deletions(-) (limited to 'examples/plot_OTDA_mapping_color_images.py') diff --git a/examples/plot_OTDA_color_images.py b/examples/plot_OTDA_color_images.py index a8861c6..75ac5b6 100644 --- a/examples/plot_OTDA_color_images.py +++ b/examples/plot_OTDA_color_images.py @@ -48,6 +48,7 @@ def mat2im(X, shape): """Converts back a matrix to an image""" return X.reshape(shape) + X1 = im2mat(I1) X2 = im2mat(I2) @@ -102,6 +103,7 @@ X2te = da_entrop.predict(X2, -1) def minmax(I): return np.clip(I, 0, 1) + I1t = minmax(mat2im(X1t, I1.shape)) I2t = minmax(mat2im(X2t, I2.shape)) diff --git a/examples/plot_OTDA_mapping_color_images.py b/examples/plot_OTDA_mapping_color_images.py index 85c4b6b..9710461 100644 --- a/examples/plot_OTDA_mapping_color_images.py +++ b/examples/plot_OTDA_mapping_color_images.py @@ -48,6 +48,7 @@ def mat2im(X, shape): """Converts back a matrix to an image""" return X.reshape(shape) + X1 = im2mat(I1) X2 = im2mat(I2) @@ -85,6 +86,7 @@ pl.tight_layout() def minmax(I): return np.clip(I, 0, 1) + # LP problem da_emd = ot.da.OTDA() # init class da_emd.fit(xs, xt) # fit distributions diff --git a/examples/plot_optim_OTreg.py b/examples/plot_optim_OTreg.py index e38253c..276b250 100644 --- a/examples/plot_optim_OTreg.py +++ b/examples/plot_optim_OTreg.py @@ -44,6 +44,7 @@ def f(G): def df(G): return G + reg = 1e-1 Gl2 = ot.optim.cg(a, b, M, reg, f, df, verbose=True) @@ -61,6 +62,7 @@ def f(G): def df(G): return np.log(G) + 1. + reg = 1e-3 Ge = ot.optim.cg(a, b, M, reg, f, df, verbose=True) @@ -78,6 +80,7 @@ def f(G): def df(G): return G + reg1 = 1e-3 reg2 = 1e-1 diff --git a/ot/utils.py b/ot/utils.py index 6a43f61..1dee932 100644 --- a/ot/utils.py +++ b/ot/utils.py @@ -2,11 +2,13 @@ """ Various function that can be usefull """ +import multiprocessing +from functools import reduce +import time + import numpy as np from scipy.spatial.distance import cdist -import multiprocessing -import time __time_tic_toc = time.time() diff --git a/test/test_gpu_sinkhorn_lpl1.py b/test/test_gpu_sinkhorn_lpl1.py index e6cdd31..f0eb7e6 100644 --- a/test/test_gpu_sinkhorn_lpl1.py +++ b/test/test_gpu_sinkhorn_lpl1.py @@ -8,6 +8,7 @@ def describeRes(r): print("min:{:.3E}, max:{:.3E}, mean:{:.3E}, std:{:.3E}" .format(np.min(r), np.max(r), np.mean(r), np.std(r))) + for n in [5000, 10000, 15000, 20000]: print(n) a = np.random.rand(n // 4, 100) -- cgit v1.2.3 From 251af8eec2b39e74000242cbf5bff5e13910cfe8 Mon Sep 17 00:00:00 2001 From: RĂ©mi Flamary Date: Wed, 26 Jul 2017 12:18:33 +0200 Subject: add author to all examples --- examples/plot_OTDA_2D.py | 4 ++++ examples/plot_OTDA_classes.py | 4 ++++ examples/plot_OTDA_color_images.py | 4 ++++ examples/plot_OTDA_mapping.py | 4 ++++ examples/plot_OTDA_mapping_color_images.py | 4 ++++ examples/plot_OT_1D.py | 5 ++++- examples/plot_OT_2D_samples.py | 5 ++++- examples/plot_OT_L1_vs_L2.py | 5 ++++- examples/plot_WDA.py | 5 ++++- examples/plot_barycenter_1D.py | 6 ++++-- examples/plot_compute_emd.py | 5 ++++- 11 files changed, 44 insertions(+), 7 deletions(-) (limited to 'examples/plot_OTDA_mapping_color_images.py') diff --git a/examples/plot_OTDA_2D.py b/examples/plot_OTDA_2D.py index 1bda59c..f2108c6 100644 --- a/examples/plot_OTDA_2D.py +++ b/examples/plot_OTDA_2D.py @@ -6,6 +6,10 @@ OT for empirical distributions """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_OTDA_classes.py b/examples/plot_OTDA_classes.py index 4d3846a..53e4bae 100644 --- a/examples/plot_OTDA_classes.py +++ b/examples/plot_OTDA_classes.py @@ -6,6 +6,10 @@ OT for domain adaptation """ +# Author: Remi Flamary +# +# License: MIT License + import matplotlib.pylab as pl import ot diff --git a/examples/plot_OTDA_color_images.py b/examples/plot_OTDA_color_images.py index 75ac5b6..c5ff873 100644 --- a/examples/plot_OTDA_color_images.py +++ b/examples/plot_OTDA_color_images.py @@ -9,6 +9,10 @@ Regularized discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np from scipy import ndimage import matplotlib.pylab as pl diff --git a/examples/plot_OTDA_mapping.py b/examples/plot_OTDA_mapping.py index a5c2b21..a0d7f8b 100644 --- a/examples/plot_OTDA_mapping.py +++ b/examples/plot_OTDA_mapping.py @@ -9,6 +9,10 @@ OT mapping estimation for domain adaptation [8] Neural Information Processing Systems (NIPS), 2016. """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_OTDA_mapping_color_images.py b/examples/plot_OTDA_mapping_color_images.py index 9710461..8064b25 100644 --- a/examples/plot_OTDA_mapping_color_images.py +++ b/examples/plot_OTDA_mapping_color_images.py @@ -11,6 +11,10 @@ OT for domain adaptation with image color adaptation [6] with mapping estimation """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np from scipy import ndimage import matplotlib.pylab as pl diff --git a/examples/plot_OT_1D.py b/examples/plot_OT_1D.py index 2f3b924..0f3a26a 100644 --- a/examples/plot_OT_1D.py +++ b/examples/plot_OT_1D.py @@ -4,9 +4,12 @@ 1D optimal transport ==================== -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_OT_2D_samples.py b/examples/plot_OT_2D_samples.py index 75ed7db..023e645 100644 --- a/examples/plot_OT_2D_samples.py +++ b/examples/plot_OT_2D_samples.py @@ -4,9 +4,12 @@ 2D Optimal transport between empirical distributions ==================================================== -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_OT_L1_vs_L2.py b/examples/plot_OT_L1_vs_L2.py index 86d902b..dfc9462 100644 --- a/examples/plot_OT_L1_vs_L2.py +++ b/examples/plot_OT_L1_vs_L2.py @@ -8,9 +8,12 @@ Stole the figure idea from Fig. 1 and 2 in https://arxiv.org/pdf/1706.07650.pdf -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_WDA.py b/examples/plot_WDA.py index 9eb8693..42789f2 100644 --- a/examples/plot_WDA.py +++ b/examples/plot_WDA.py @@ -4,9 +4,12 @@ Wasserstein Discriminant Analysis ================================= -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl diff --git a/examples/plot_barycenter_1D.py b/examples/plot_barycenter_1D.py index ab236e1..875f44c 100644 --- a/examples/plot_barycenter_1D.py +++ b/examples/plot_barycenter_1D.py @@ -4,10 +4,12 @@ 1D Wasserstein barycenter demo ============================== - -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot diff --git a/examples/plot_compute_emd.py b/examples/plot_compute_emd.py index 558facb..893eecf 100644 --- a/examples/plot_compute_emd.py +++ b/examples/plot_compute_emd.py @@ -4,9 +4,12 @@ 1D optimal transport ==================== -@author: rflamary """ +# Author: Remi Flamary +# +# License: MIT License + import numpy as np import matplotlib.pylab as pl import ot -- cgit v1.2.3 From a8fa91bec26caa93329e61a104e0ad6afdf37363 Mon Sep 17 00:00:00 2001 From: Slasnista Date: Mon, 28 Aug 2017 11:03:28 +0200 Subject: handling input arguments in fit, transform... methods + remove old examples --- examples/plot_OTDA_2D.py | 126 ----------------- examples/plot_OTDA_classes.py | 117 ---------------- examples/plot_OTDA_color_images.py | 152 -------------------- examples/plot_OTDA_mapping.py | 124 ----------------- examples/plot_OTDA_mapping_color_images.py | 169 ---------------------- ot/da.py | 217 ++++++++++++++++------------- 6 files changed, 121 insertions(+), 784 deletions(-) delete mode 100644 examples/plot_OTDA_2D.py delete mode 100644 examples/plot_OTDA_classes.py delete mode 100644 examples/plot_OTDA_color_images.py delete mode 100644 examples/plot_OTDA_mapping.py delete mode 100644 examples/plot_OTDA_mapping_color_images.py (limited to 'examples/plot_OTDA_mapping_color_images.py') diff --git a/examples/plot_OTDA_2D.py b/examples/plot_OTDA_2D.py deleted file mode 100644 index f2108c6..0000000 --- a/examples/plot_OTDA_2D.py +++ /dev/null @@ -1,126 +0,0 @@ -# -*- coding: utf-8 -*- -""" -============================== -OT for empirical distributions -============================== - -""" - -# Author: Remi Flamary -# -# License: MIT License - -import numpy as np -import matplotlib.pylab as pl -import ot - - -#%% parameters - -n = 150 # nb bins - -xs, ys = ot.datasets.get_data_classif('3gauss', n) -xt, yt = ot.datasets.get_data_classif('3gauss2', n) - -a, b = ot.unif(n), ot.unif(n) -# loss matrix -M = ot.dist(xs, xt) -# M/=M.max() - -#%% plot samples - -pl.figure(1) -pl.subplot(2, 2, 1) -pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') -pl.legend(loc=0) -pl.title('Source distributions') - -pl.subplot(2, 2, 2) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') -pl.legend(loc=0) -pl.title('target distributions') - -pl.figure(2) -pl.imshow(M, interpolation='nearest') -pl.title('Cost matrix M') - - -#%% OT estimation - -# EMD -G0 = ot.emd(a, b, M) - -# sinkhorn -lambd = 1e-1 -Gs = ot.sinkhorn(a, b, M, lambd) - - -# Group lasso regularization -reg = 1e-1 -eta = 1e0 -Gg = ot.da.sinkhorn_lpl1_mm(a, ys.astype(np.int), b, M, reg, eta) - - -#%% visu matrices - -pl.figure(3) - -pl.subplot(2, 3, 1) -pl.imshow(G0, interpolation='nearest') -pl.title('OT matrix ') - -pl.subplot(2, 3, 2) -pl.imshow(Gs, interpolation='nearest') -pl.title('OT matrix Sinkhorn') - -pl.subplot(2, 3, 3) -pl.imshow(Gg, interpolation='nearest') -pl.title('OT matrix Group lasso') - -pl.subplot(2, 3, 4) -ot.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1]) -pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') - - -pl.subplot(2, 3, 5) -ot.plot.plot2D_samples_mat(xs, xt, Gs, c=[.5, .5, 1]) -pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') - -pl.subplot(2, 3, 6) -ot.plot.plot2D_samples_mat(xs, xt, Gg, c=[.5, .5, 1]) -pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') -pl.tight_layout() - -#%% sample interpolation - -xst0 = n * G0.dot(xt) -xsts = n * Gs.dot(xt) -xstg = n * Gg.dot(xt) - -pl.figure(4, figsize=(8, 3)) -pl.subplot(1, 3, 1) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=0.5) -pl.scatter(xst0[:, 0], xst0[:, 1], c=ys, - marker='+', label='Transp samples', s=30) -pl.title('Interp samples') -pl.legend(loc=0) - -pl.subplot(1, 3, 2) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=0.5) -pl.scatter(xsts[:, 0], xsts[:, 1], c=ys, - marker='+', label='Transp samples', s=30) -pl.title('Interp samples Sinkhorn') - -pl.subplot(1, 3, 3) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=0.5) -pl.scatter(xstg[:, 0], xstg[:, 1], c=ys, - marker='+', label='Transp samples', s=30) -pl.title('Interp samples Grouplasso') -pl.tight_layout() -pl.show() diff --git a/examples/plot_OTDA_classes.py b/examples/plot_OTDA_classes.py deleted file mode 100644 index 53e4bae..0000000 --- a/examples/plot_OTDA_classes.py +++ /dev/null @@ -1,117 +0,0 @@ -# -*- coding: utf-8 -*- -""" -======================== -OT for domain adaptation -======================== - -""" - -# Author: Remi Flamary -# -# License: MIT License - -import matplotlib.pylab as pl -import ot - - -#%% parameters - -n = 150 # nb samples in source and target datasets - -xs, ys = ot.datasets.get_data_classif('3gauss', n) -xt, yt = ot.datasets.get_data_classif('3gauss2', n) - - -#%% plot samples - -pl.figure(1, figsize=(6.4, 3)) - -pl.subplot(1, 2, 1) -pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') -pl.legend(loc=0) -pl.title('Source distributions') - -pl.subplot(1, 2, 2) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') -pl.legend(loc=0) -pl.title('target distributions') - - -#%% OT estimation - -# LP problem -da_emd = ot.da.OTDA() # init class -da_emd.fit(xs, xt) # fit distributions -xst0 = da_emd.interp() # interpolation of source samples - -# sinkhorn regularization -lambd = 1e-1 -da_entrop = ot.da.OTDA_sinkhorn() -da_entrop.fit(xs, xt, reg=lambd) -xsts = da_entrop.interp() - -# non-convex Group lasso regularization -reg = 1e-1 -eta = 1e0 -da_lpl1 = ot.da.OTDA_lpl1() -da_lpl1.fit(xs, ys, xt, reg=reg, eta=eta) -xstg = da_lpl1.interp() - -# True Group lasso regularization -reg = 1e-1 -eta = 2e0 -da_l1l2 = ot.da.OTDA_l1l2() -da_l1l2.fit(xs, ys, xt, reg=reg, eta=eta, numItermax=20, verbose=True) -xstgl = da_l1l2.interp() - -#%% plot interpolated source samples - -param_img = {'interpolation': 'nearest', 'cmap': 'spectral'} - -pl.figure(2, figsize=(8, 4.5)) -pl.subplot(2, 4, 1) -pl.imshow(da_emd.G, **param_img) -pl.title('OT matrix') - -pl.subplot(2, 4, 2) -pl.imshow(da_entrop.G, **param_img) -pl.title('OT matrix\nsinkhorn') - -pl.subplot(2, 4, 3) -pl.imshow(da_lpl1.G, **param_img) -pl.title('OT matrix\nnon-convex Group Lasso') - -pl.subplot(2, 4, 4) -pl.imshow(da_l1l2.G, **param_img) -pl.title('OT matrix\nGroup Lasso') - -pl.subplot(2, 4, 5) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=0.3) -pl.scatter(xst0[:, 0], xst0[:, 1], c=ys, - marker='+', label='Transp samples', s=30) -pl.title('Interp samples') -pl.legend(loc=0) - -pl.subplot(2, 4, 6) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=0.3) -pl.scatter(xsts[:, 0], xsts[:, 1], c=ys, - marker='+', label='Transp samples', s=30) -pl.title('Interp samples\nSinkhorn') - -pl.subplot(2, 4, 7) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=0.3) -pl.scatter(xstg[:, 0], xstg[:, 1], c=ys, - marker='+', label='Transp samples', s=30) -pl.title('Interp samples\nnon-convex Group Lasso') - -pl.subplot(2, 4, 8) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=0.3) -pl.scatter(xstgl[:, 0], xstgl[:, 1], c=ys, - marker='+', label='Transp samples', s=30) -pl.title('Interp samples\nGroup Lasso') -pl.tight_layout() -pl.show() diff --git a/examples/plot_OTDA_color_images.py b/examples/plot_OTDA_color_images.py deleted file mode 100644 index c5ff873..0000000 --- a/examples/plot_OTDA_color_images.py +++ /dev/null @@ -1,152 +0,0 @@ -# -*- coding: utf-8 -*- -""" -======================================================== -OT for domain adaptation with image color adaptation [6] -======================================================== - -[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). -Regularized discrete optimal transport. -SIAM Journal on Imaging Sciences, 7(3), 1853-1882. -""" - -# Author: Remi Flamary -# -# License: MIT License - -import numpy as np -from scipy import ndimage -import matplotlib.pylab as pl -import ot - - -#%% Loading images - -I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 -I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256 - -#%% Plot images - -pl.figure(1, figsize=(6.4, 3)) - -pl.subplot(1, 2, 1) -pl.imshow(I1) -pl.axis('off') -pl.title('Image 1') - -pl.subplot(1, 2, 2) -pl.imshow(I2) -pl.axis('off') -pl.title('Image 2') - -pl.show() - -#%% Image conversion and dataset generation - - -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) - - -X1 = im2mat(I1) -X2 = im2mat(I2) - -# training samples -nb = 1000 -idx1 = np.random.randint(X1.shape[0], size=(nb,)) -idx2 = np.random.randint(X2.shape[0], size=(nb,)) - -xs = X1[idx1, :] -xt = X2[idx2, :] - -#%% Plot image distributions - - -pl.figure(2, figsize=(6.4, 3)) - -pl.subplot(1, 2, 1) -pl.scatter(xs[:, 0], xs[:, 2], c=xs) -pl.axis([0, 1, 0, 1]) -pl.xlabel('Red') -pl.ylabel('Blue') -pl.title('Image 1') - -pl.subplot(1, 2, 2) -pl.scatter(xt[:, 0], xt[:, 2], c=xt) -pl.axis([0, 1, 0, 1]) -pl.xlabel('Red') -pl.ylabel('Blue') -pl.title('Image 2') -pl.tight_layout() - -#%% domain adaptation between images - -# LP problem -da_emd = ot.da.OTDA() # init class -da_emd.fit(xs, xt) # fit distributions - -# sinkhorn regularization -lambd = 1e-1 -da_entrop = ot.da.OTDA_sinkhorn() -da_entrop.fit(xs, xt, reg=lambd) - -#%% prediction between images (using out of sample prediction as in [6]) - -X1t = da_emd.predict(X1) -X2t = da_emd.predict(X2, -1) - -X1te = da_entrop.predict(X1) -X2te = da_entrop.predict(X2, -1) - - -def minmax(I): - return np.clip(I, 0, 1) - - -I1t = minmax(mat2im(X1t, I1.shape)) -I2t = minmax(mat2im(X2t, I2.shape)) - -I1te = minmax(mat2im(X1te, I1.shape)) -I2te = minmax(mat2im(X2te, I2.shape)) - -#%% plot all images - -pl.figure(2, figsize=(8, 4)) - -pl.subplot(2, 3, 1) -pl.imshow(I1) -pl.axis('off') -pl.title('Image 1') - -pl.subplot(2, 3, 2) -pl.imshow(I1t) -pl.axis('off') -pl.title('Image 1 Adapt') - -pl.subplot(2, 3, 3) -pl.imshow(I1te) -pl.axis('off') -pl.title('Image 1 Adapt (reg)') - -pl.subplot(2, 3, 4) -pl.imshow(I2) -pl.axis('off') -pl.title('Image 2') - -pl.subplot(2, 3, 5) -pl.imshow(I2t) -pl.axis('off') -pl.title('Image 2 Adapt') - -pl.subplot(2, 3, 6) -pl.imshow(I2te) -pl.axis('off') -pl.title('Image 2 Adapt (reg)') -pl.tight_layout() - -pl.show() diff --git a/examples/plot_OTDA_mapping.py b/examples/plot_OTDA_mapping.py deleted file mode 100644 index a0d7f8b..0000000 --- a/examples/plot_OTDA_mapping.py +++ /dev/null @@ -1,124 +0,0 @@ -# -*- coding: utf-8 -*- -""" -=============================================== -OT mapping estimation for domain adaptation [8] -=============================================== - -[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, - "Mapping estimation for discrete optimal transport", - Neural Information Processing Systems (NIPS), 2016. -""" - -# Author: Remi Flamary -# -# License: MIT License - -import numpy as np -import matplotlib.pylab as pl -import ot - - -#%% dataset generation - -np.random.seed(0) # makes example reproducible - -n = 100 # nb samples in source and target datasets -theta = 2 * np.pi / 20 -nz = 0.1 -xs, ys = ot.datasets.get_data_classif('gaussrot', n, nz=nz) -xt, yt = ot.datasets.get_data_classif('gaussrot', n, theta=theta, nz=nz) - -# one of the target mode changes its variance (no linear mapping) -xt[yt == 2] *= 3 -xt = xt + 4 - - -#%% plot samples - -pl.figure(1, (6.4, 3)) -pl.clf() -pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples') -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples') -pl.legend(loc=0) -pl.title('Source and target distributions') - - -#%% OT linear mapping estimation - -eta = 1e-8 # quadratic regularization for regression -mu = 1e0 # weight of the OT linear term -bias = True # estimate a bias - -ot_mapping = ot.da.OTDA_mapping_linear() -ot_mapping.fit(xs, xt, mu=mu, eta=eta, bias=bias, numItermax=20, verbose=True) - -xst = ot_mapping.predict(xs) # use the estimated mapping -xst0 = ot_mapping.interp() # use barycentric mapping - - -pl.figure(2) -pl.clf() -pl.subplot(2, 2, 1) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=.3) -pl.scatter(xst0[:, 0], xst0[:, 1], c=ys, - marker='+', label='barycentric mapping') -pl.title("barycentric mapping") - -pl.subplot(2, 2, 2) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=.3) -pl.scatter(xst[:, 0], xst[:, 1], c=ys, marker='+', label='Learned mapping') -pl.title("Learned mapping") -pl.tight_layout() - -#%% Kernel mapping estimation - -eta = 1e-5 # quadratic regularization for regression -mu = 1e-1 # weight of the OT linear term -bias = True # estimate a bias -sigma = 1 # sigma bandwidth fot gaussian kernel - - -ot_mapping_kernel = ot.da.OTDA_mapping_kernel() -ot_mapping_kernel.fit( - xs, xt, mu=mu, eta=eta, sigma=sigma, bias=bias, numItermax=10, verbose=True) - -xst_kernel = ot_mapping_kernel.predict(xs) # use the estimated mapping -xst0_kernel = ot_mapping_kernel.interp() # use barycentric mapping - - -#%% Plotting the mapped samples - -pl.figure(2) -pl.clf() -pl.subplot(2, 2, 1) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=.2) -pl.scatter(xst0[:, 0], xst0[:, 1], c=ys, marker='+', - label='Mapped source samples') -pl.title("Bary. mapping (linear)") -pl.legend(loc=0) - -pl.subplot(2, 2, 2) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=.2) -pl.scatter(xst[:, 0], xst[:, 1], c=ys, marker='+', label='Learned mapping') -pl.title("Estim. mapping (linear)") - -pl.subplot(2, 2, 3) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=.2) -pl.scatter(xst0_kernel[:, 0], xst0_kernel[:, 1], c=ys, - marker='+', label='barycentric mapping') -pl.title("Bary. mapping (kernel)") - -pl.subplot(2, 2, 4) -pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', - label='Target samples', alpha=.2) -pl.scatter(xst_kernel[:, 0], xst_kernel[:, 1], c=ys, - marker='+', label='Learned mapping') -pl.title("Estim. mapping (kernel)") -pl.tight_layout() - -pl.show() diff --git a/examples/plot_OTDA_mapping_color_images.py b/examples/plot_OTDA_mapping_color_images.py deleted file mode 100644 index 8064b25..0000000 --- a/examples/plot_OTDA_mapping_color_images.py +++ /dev/null @@ -1,169 +0,0 @@ -# -*- coding: utf-8 -*- -""" -==================================================================================== -OT for domain adaptation with image color adaptation [6] with mapping estimation [8] -==================================================================================== - -[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized - discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. -[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for - discrete optimal transport", Neural Information Processing Systems (NIPS), 2016. - -""" - -# Author: Remi Flamary -# -# License: MIT License - -import numpy as np -from scipy import ndimage -import matplotlib.pylab as pl -import ot - - -#%% Loading images - -I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 -I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256 - -#%% Plot images - -pl.figure(1, figsize=(6.4, 3)) -pl.subplot(1, 2, 1) -pl.imshow(I1) -pl.axis('off') -pl.title('Image 1') - -pl.subplot(1, 2, 2) -pl.imshow(I2) -pl.axis('off') -pl.title('Image 2') -pl.tight_layout() - - -#%% Image conversion and dataset generation - -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) - - -X1 = im2mat(I1) -X2 = im2mat(I2) - -# training samples -nb = 1000 -idx1 = np.random.randint(X1.shape[0], size=(nb,)) -idx2 = np.random.randint(X2.shape[0], size=(nb,)) - -xs = X1[idx1, :] -xt = X2[idx2, :] - -#%% Plot image distributions - - -pl.figure(2, figsize=(6.4, 5)) - -pl.subplot(1, 2, 1) -pl.scatter(xs[:, 0], xs[:, 2], c=xs) -pl.axis([0, 1, 0, 1]) -pl.xlabel('Red') -pl.ylabel('Blue') -pl.title('Image 1') - -pl.subplot(1, 2, 2) -pl.scatter(xt[:, 0], xt[:, 2], c=xt) -pl.axis([0, 1, 0, 1]) -pl.xlabel('Red') -pl.ylabel('Blue') -pl.title('Image 2') -pl.tight_layout() - - -#%% domain adaptation between images - -def minmax(I): - return np.clip(I, 0, 1) - - -# LP problem -da_emd = ot.da.OTDA() # init class -da_emd.fit(xs, xt) # fit distributions - -X1t = da_emd.predict(X1) # out of sample -I1t = minmax(mat2im(X1t, I1.shape)) - -# sinkhorn regularization -lambd = 1e-1 -da_entrop = ot.da.OTDA_sinkhorn() -da_entrop.fit(xs, xt, reg=lambd) - -X1te = da_entrop.predict(X1) -I1te = minmax(mat2im(X1te, I1.shape)) - -# linear mapping estimation -eta = 1e-8 # quadratic regularization for regression -mu = 1e0 # weight of the OT linear term -bias = True # estimate a bias - -ot_mapping = ot.da.OTDA_mapping_linear() -ot_mapping.fit(xs, xt, mu=mu, eta=eta, bias=bias, numItermax=20, verbose=True) - -X1tl = ot_mapping.predict(X1) # use the estimated mapping -I1tl = minmax(mat2im(X1tl, I1.shape)) - -# nonlinear mapping estimation -eta = 1e-2 # quadratic regularization for regression -mu = 1e0 # weight of the OT linear term -bias = False # estimate a bias -sigma = 1 # sigma bandwidth fot gaussian kernel - - -ot_mapping_kernel = ot.da.OTDA_mapping_kernel() -ot_mapping_kernel.fit( - xs, xt, mu=mu, eta=eta, sigma=sigma, bias=bias, numItermax=10, verbose=True) - -X1tn = ot_mapping_kernel.predict(X1) # use the estimated mapping -I1tn = minmax(mat2im(X1tn, I1.shape)) - -#%% plot images - -pl.figure(2, figsize=(8, 4)) - -pl.subplot(2, 3, 1) -pl.imshow(I1) -pl.axis('off') -pl.title('Im. 1') - -pl.subplot(2, 3, 2) -pl.imshow(I2) -pl.axis('off') -pl.title('Im. 2') - -pl.subplot(2, 3, 3) -pl.imshow(I1t) -pl.axis('off') -pl.title('Im. 1 Interp LP') - -pl.subplot(2, 3, 4) -pl.imshow(I1te) -pl.axis('off') -pl.title('Im. 1 Interp Entrop') - -pl.subplot(2, 3, 5) -pl.imshow(I1tl) -pl.axis('off') -pl.title('Im. 1 Linear mapping') - -pl.subplot(2, 3, 6) -pl.imshow(I1tn) -pl.axis('off') -pl.title('Im. 1 nonlinear mapping') -pl.tight_layout() - -pl.show() diff --git a/ot/da.py b/ot/da.py index 8c62669..369b6a2 100644 --- a/ot/da.py +++ b/ot/da.py @@ -976,36 +976,41 @@ class BaseTransport(BaseEstimator): Returns self. """ - # pairwise distance - self.cost_ = dist(Xs, Xt, metric=self.metric) + if Xs is not None and Xt is not None: + # pairwise distance + self.cost_ = dist(Xs, Xt, metric=self.metric) - if (ys is not None) and (yt is not None): + if (ys is not None) and (yt is not None): - if self.limit_max != np.infty: - self.limit_max = self.limit_max * np.max(self.cost_) + if self.limit_max != np.infty: + self.limit_max = self.limit_max * np.max(self.cost_) - # assumes labeled source samples occupy the first rows - # and labeled target samples occupy the first columns - classes = np.unique(ys) - for c in classes: - idx_s = np.where((ys != c) & (ys != -1)) - idx_t = np.where(yt == c) + # assumes labeled source samples occupy the first rows + # and labeled target samples occupy the first columns + classes = np.unique(ys) + for c in classes: + idx_s = np.where((ys != c) & (ys != -1)) + idx_t = np.where(yt == c) - # all the coefficients corresponding to a source sample - # and a target sample : - # with different labels get a infinite - for j in idx_t[0]: - self.cost_[idx_s[0], j] = self.limit_max + # all the coefficients corresponding to a source sample + # and a target sample : + # with different labels get a infinite + for j in idx_t[0]: + self.cost_[idx_s[0], j] = self.limit_max - # distribution estimation - self.mu_s = self.distribution_estimation(Xs) - self.mu_t = self.distribution_estimation(Xt) + # distribution estimation + self.mu_s = self.distribution_estimation(Xs) + self.mu_t = self.distribution_estimation(Xt) - # store arrays of samples - self.Xs = Xs - self.Xt = Xt + # store arrays of samples + self.Xs = Xs + self.Xt = Xt - return self + return self + else: + print("POT-Warning") + print("Please provide both Xs and Xt arguments when calling") + print("fit method") def fit_transform(self, Xs=None, ys=None, Xt=None, yt=None): """Build a coupling matrix from source and target sets of samples @@ -1053,42 +1058,47 @@ class BaseTransport(BaseEstimator): The transport source samples. """ - if np.array_equal(self.Xs, Xs): - # perform standard barycentric mapping - transp = self.coupling_ / np.sum(self.coupling_, 1)[:, None] + if Xs is not None: + if np.array_equal(self.Xs, Xs): + # perform standard barycentric mapping + transp = self.coupling_ / np.sum(self.coupling_, 1)[:, None] - # set nans to 0 - transp[~ np.isfinite(transp)] = 0 + # set nans to 0 + transp[~ np.isfinite(transp)] = 0 - # compute transported samples - transp_Xs = np.dot(transp, self.Xt) - else: - # perform out of sample mapping - indices = np.arange(Xs.shape[0]) - batch_ind = [ - indices[i:i + batch_size] - for i in range(0, len(indices), batch_size)] + # compute transported samples + transp_Xs = np.dot(transp, self.Xt) + else: + # perform out of sample mapping + indices = np.arange(Xs.shape[0]) + batch_ind = [ + indices[i:i + batch_size] + for i in range(0, len(indices), batch_size)] - transp_Xs = [] - for bi in batch_ind: + transp_Xs = [] + for bi in batch_ind: - # get the nearest neighbor in the source domain - D0 = dist(Xs[bi], self.Xs) - idx = np.argmin(D0, axis=1) + # get the nearest neighbor in the source domain + D0 = dist(Xs[bi], self.Xs) + idx = np.argmin(D0, axis=1) - # transport the source samples - transp = self.coupling_ / np.sum(self.coupling_, 1)[:, None] - transp[~ np.isfinite(transp)] = 0 - transp_Xs_ = np.dot(transp, self.Xt) + # transport the source samples + transp = self.coupling_ / np.sum( + self.coupling_, 1)[:, None] + transp[~ np.isfinite(transp)] = 0 + transp_Xs_ = np.dot(transp, self.Xt) - # define the transported points - transp_Xs_ = transp_Xs_[idx, :] + Xs[bi] - self.Xs[idx, :] + # define the transported points + transp_Xs_ = transp_Xs_[idx, :] + Xs[bi] - self.Xs[idx, :] - transp_Xs.append(transp_Xs_) + transp_Xs.append(transp_Xs_) - transp_Xs = np.concatenate(transp_Xs, axis=0) + transp_Xs = np.concatenate(transp_Xs, axis=0) - return transp_Xs + return transp_Xs + else: + print("POT-Warning") + print("Please provide Xs argument when calling transform method") def inverse_transform(self, Xs=None, ys=None, Xt=None, yt=None, batch_size=128): @@ -1113,41 +1123,46 @@ class BaseTransport(BaseEstimator): The transported target samples. """ - if np.array_equal(self.Xt, Xt): - # perform standard barycentric mapping - transp_ = self.coupling_.T / np.sum(self.coupling_, 0)[:, None] + if Xt is not None: + if np.array_equal(self.Xt, Xt): + # perform standard barycentric mapping + transp_ = self.coupling_.T / np.sum(self.coupling_, 0)[:, None] - # set nans to 0 - transp_[~ np.isfinite(transp_)] = 0 + # set nans to 0 + transp_[~ np.isfinite(transp_)] = 0 - # compute transported samples - transp_Xt = np.dot(transp_, self.Xs) - else: - # perform out of sample mapping - indices = np.arange(Xt.shape[0]) - batch_ind = [ - indices[i:i + batch_size] - for i in range(0, len(indices), batch_size)] + # compute transported samples + transp_Xt = np.dot(transp_, self.Xs) + else: + # perform out of sample mapping + indices = np.arange(Xt.shape[0]) + batch_ind = [ + indices[i:i + batch_size] + for i in range(0, len(indices), batch_size)] - transp_Xt = [] - for bi in batch_ind: + transp_Xt = [] + for bi in batch_ind: - D0 = dist(Xt[bi], self.Xt) - idx = np.argmin(D0, axis=1) + D0 = dist(Xt[bi], self.Xt) + idx = np.argmin(D0, axis=1) - # transport the target samples - transp_ = self.coupling_.T / np.sum(self.coupling_, 0)[:, None] - transp_[~ np.isfinite(transp_)] = 0 - transp_Xt_ = np.dot(transp_, self.Xs) + # transport the target samples + transp_ = self.coupling_.T / np.sum( + self.coupling_, 0)[:, None] + transp_[~ np.isfinite(transp_)] = 0 + transp_Xt_ = np.dot(transp_, self.Xs) - # define the transported points - transp_Xt_ = transp_Xt_[idx, :] + Xt[bi] - self.Xt[idx, :] + # define the transported points + transp_Xt_ = transp_Xt_[idx, :] + Xt[bi] - self.Xt[idx, :] - transp_Xt.append(transp_Xt_) + transp_Xt.append(transp_Xt_) - transp_Xt = np.concatenate(transp_Xt, axis=0) + transp_Xt = np.concatenate(transp_Xt, axis=0) - return transp_Xt + return transp_Xt + else: + print("POT-Warning") + print("Please provide Xt argument when calling inverse_transform") class SinkhornTransport(BaseTransport): @@ -1413,15 +1428,20 @@ class SinkhornLpl1Transport(BaseTransport): Returns self. """ - super(SinkhornLpl1Transport, self).fit(Xs, ys, Xt, yt) + if Xs is not None and Xt is not None and ys is not None: - self.coupling_ = sinkhorn_lpl1_mm( - a=self.mu_s, labels_a=ys, b=self.mu_t, M=self.cost_, - reg=self.reg_e, eta=self.reg_cl, numItermax=self.max_iter, - numInnerItermax=self.max_inner_iter, stopInnerThr=self.tol, - verbose=self.verbose) + super(SinkhornLpl1Transport, self).fit(Xs, ys, Xt, yt) - return self + self.coupling_ = sinkhorn_lpl1_mm( + a=self.mu_s, labels_a=ys, b=self.mu_t, M=self.cost_, + reg=self.reg_e, eta=self.reg_cl, numItermax=self.max_iter, + numInnerItermax=self.max_inner_iter, stopInnerThr=self.tol, + verbose=self.verbose) + + return self + else: + print("POT-Warning") + print("Please provide both Xs, Xt, ys arguments to fit method") class SinkhornL1l2Transport(BaseTransport): @@ -1517,22 +1537,27 @@ class SinkhornL1l2Transport(BaseTransport): Returns self. """ - super(SinkhornL1l2Transport, self).fit(Xs, ys, Xt, yt) + if Xs is not None and Xt is not None and ys is not None: - returned_ = sinkhorn_l1l2_gl( - a=self.mu_s, labels_a=ys, b=self.mu_t, M=self.cost_, - reg=self.reg_e, eta=self.reg_cl, numItermax=self.max_iter, - numInnerItermax=self.max_inner_iter, stopInnerThr=self.tol, - verbose=self.verbose, log=self.log) + super(SinkhornL1l2Transport, self).fit(Xs, ys, Xt, yt) - # deal with the value of log - if self.log: - self.coupling_, self.log_ = returned_ - else: - self.coupling_ = returned_ - self.log_ = dict() + returned_ = sinkhorn_l1l2_gl( + a=self.mu_s, labels_a=ys, b=self.mu_t, M=self.cost_, + reg=self.reg_e, eta=self.reg_cl, numItermax=self.max_iter, + numInnerItermax=self.max_inner_iter, stopInnerThr=self.tol, + verbose=self.verbose, log=self.log) - return self + # deal with the value of log + if self.log: + self.coupling_, self.log_ = returned_ + else: + self.coupling_ = returned_ + self.log_ = dict() + + return self + else: + print("POT-Warning") + print("Please, provide both Xs, Xt and ys argument to fit method") class MappingTransport(BaseEstimator): -- cgit v1.2.3