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 ----------------------------- 5 files changed, 688 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') 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() -- cgit v1.2.3