From 74ca2d77bca479785c581d2f48d87628d5c1444d Mon Sep 17 00:00:00 2001 From: Slasnista Date: Fri, 25 Aug 2017 15:12:20 +0200 Subject: refactoring examples according to new DA classes --- examples/da/plot_otda_mapping.py | 119 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 examples/da/plot_otda_mapping.py (limited to 'examples/da/plot_otda_mapping.py') diff --git a/examples/da/plot_otda_mapping.py b/examples/da/plot_otda_mapping.py new file mode 100644 index 0000000..ed234f5 --- /dev/null +++ b/examples/da/plot_otda_mapping.py @@ -0,0 +1,119 @@ +# -*- coding: utf-8 -*- +""" +=============================================== +OT mapping estimation for domain adaptation [8] +=============================================== + +This example presents how to use MappingTransport to estimate at the same +time both the coupling transport and approximate the transport map with either +a linear or a kernelized mapping as introduced in [8] + +[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, + "Mapping estimation for discrete optimal transport", + Neural Information Processing Systems (NIPS), 2016. +""" + +# Authors: Remi Flamary +# Stanilslas Chambon +# +# License: MIT License + +import numpy as np +import matplotlib.pylab as pl +import ot + + +np.random.seed(0) + +############################################################################## +# generate +############################################################################## + +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) +Xs_new, _ = 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 + + +# MappingTransport with linear kernel +ot_mapping_linear = ot.da.MappingTransport( + kernel="linear", mu=1e0, eta=1e-8, bias=True, + max_iter=20, verbose=True) + +ot_mapping_linear.fit( + Xs=Xs, Xt=Xt) + +# for original source samples, transform applies barycentric mapping +transp_Xs_linear = ot_mapping_linear.transform(Xs=Xs) + +# for out of source samples, transform applies the linear mapping +transp_Xs_linear_new = ot_mapping_linear.transform(Xs=Xs_new) + + +# MappingTransport with gaussian kernel +ot_mapping_gaussian = ot.da.MappingTransport( + kernel="gaussian", eta=1e-5, mu=1e-1, bias=True, sigma=1, + max_iter=10, verbose=True) +ot_mapping_gaussian.fit(Xs=Xs, Xt=Xt) + +# for original source samples, transform applies barycentric mapping +transp_Xs_gaussian = ot_mapping_gaussian.transform(Xs=Xs) + +# for out of source samples, transform applies the gaussian mapping +transp_Xs_gaussian_new = ot_mapping_gaussian.transform(Xs=Xs_new) + + +############################################################################## +# plot data +############################################################################## + +pl.figure(1, (10, 5)) +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') + +############################################################################## +# plot transported 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(transp_Xs_linear[:, 0], transp_Xs_linear[:, 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(transp_Xs_linear_new[:, 0], transp_Xs_linear_new[:, 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(transp_Xs_gaussian[:, 0], transp_Xs_gaussian[:, 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(transp_Xs_gaussian_new[:, 0], transp_Xs_gaussian_new[:, 1], c=ys, + marker='+', label='Learned mapping') +pl.title("Estim. mapping (kernel)") +pl.tight_layout() + +pl.show() -- cgit v1.2.3 From f80693b545ac40817cb95eb31260fe7598514b96 Mon Sep 17 00:00:00 2001 From: Slasnista Date: Fri, 25 Aug 2017 15:15:52 +0200 Subject: small corrections for examples --- examples/da/plot_otda_classes.py | 2 +- examples/da/plot_otda_color_images.py | 2 +- examples/da/plot_otda_d2.py | 2 +- examples/da/plot_otda_mapping.py | 2 +- examples/da/plot_otda_mapping_colors_images.py | 6 +++--- 5 files changed, 7 insertions(+), 7 deletions(-) (limited to 'examples/da/plot_otda_mapping.py') diff --git a/examples/da/plot_otda_classes.py b/examples/da/plot_otda_classes.py index 1bfe2bb..e5c82fb 100644 --- a/examples/da/plot_otda_classes.py +++ b/examples/da/plot_otda_classes.py @@ -10,7 +10,7 @@ approaches currently supported in POT. """ # Authors: Remi Flamary -# Stanilslas Chambon +# Stanislas Chambon # # License: MIT License diff --git a/examples/da/plot_otda_color_images.py b/examples/da/plot_otda_color_images.py index a46ac29..bca7350 100644 --- a/examples/da/plot_otda_color_images.py +++ b/examples/da/plot_otda_color_images.py @@ -13,7 +13,7 @@ SIAM Journal on Imaging Sciences, 7(3), 1853-1882. """ # Authors: Remi Flamary -# Stanilslas Chambon +# Stanislas Chambon # # License: MIT License diff --git a/examples/da/plot_otda_d2.py b/examples/da/plot_otda_d2.py index 78c0372..1d2192f 100644 --- a/examples/da/plot_otda_d2.py +++ b/examples/da/plot_otda_d2.py @@ -14,7 +14,7 @@ of what the transport methods are doing. """ # Authors: Remi Flamary -# Stanilslas Chambon +# Stanislas Chambon # # License: MIT License diff --git a/examples/da/plot_otda_mapping.py b/examples/da/plot_otda_mapping.py index ed234f5..6d83507 100644 --- a/examples/da/plot_otda_mapping.py +++ b/examples/da/plot_otda_mapping.py @@ -14,7 +14,7 @@ a linear or a kernelized mapping as introduced in [8] """ # Authors: Remi Flamary -# Stanilslas Chambon +# Stanislas Chambon # # License: MIT License diff --git a/examples/da/plot_otda_mapping_colors_images.py b/examples/da/plot_otda_mapping_colors_images.py index 56b5a6f..05d9046 100644 --- a/examples/da/plot_otda_mapping_colors_images.py +++ b/examples/da/plot_otda_mapping_colors_images.py @@ -14,7 +14,7 @@ OT for domain adaptation with image color adaptation [6] with mapping estimation """ # Authors: Remi Flamary -# Stanilslas Chambon +# Stanislas Chambon # # License: MIT License @@ -82,14 +82,14 @@ ot_mapping_linear = ot.da.MappingTransport( mu=1e0, eta=1e-8, bias=True, max_iter=20, verbose=True) ot_mapping_linear.fit(Xs=Xs, Xt=Xt) -X1tl = ot_mapping_linear.transform(X1) +X1tl = ot_mapping_linear.transform(Xs=X1) Image_mapping_linear = minmax(mat2im(X1tl, I1.shape)) ot_mapping_gaussian = ot.da.MappingTransport( mu=1e0, eta=1e-2, sigma=1, bias=False, max_iter=10, verbose=True) ot_mapping_gaussian.fit(Xs=Xs, Xt=Xt) -X1tn = ot_mapping_gaussian.transform(X1) # use the estimated mapping +X1tn = ot_mapping_gaussian.transform(Xs=X1) # use the estimated mapping Image_mapping_gaussian = minmax(mat2im(X1tn, I1.shape)) ############################################################################## -- cgit v1.2.3 From a29e22db4772ebc4a8266c917e2e662f624c6baa Mon Sep 17 00:00:00 2001 From: Slasnista Date: Tue, 29 Aug 2017 09:05:01 +0200 Subject: addressed AG comments + adding random seed --- examples/da/plot_otda_classes.py | 2 ++ examples/da/plot_otda_color_images.py | 3 ++- examples/da/plot_otda_d2.py | 14 ++++++++------ examples/da/plot_otda_mapping.py | 14 +++++++------- examples/da/plot_otda_mapping_colors_images.py | 2 ++ 5 files changed, 21 insertions(+), 14 deletions(-) (limited to 'examples/da/plot_otda_mapping.py') diff --git a/examples/da/plot_otda_classes.py b/examples/da/plot_otda_classes.py index e5c82fb..6870fa4 100644 --- a/examples/da/plot_otda_classes.py +++ b/examples/da/plot_otda_classes.py @@ -15,8 +15,10 @@ approaches currently supported in POT. # License: MIT License import matplotlib.pylab as pl +import numpy as np import ot +np.random.seed(42) # number of source and target points to generate ns = 150 diff --git a/examples/da/plot_otda_color_images.py b/examples/da/plot_otda_color_images.py index bca7350..805d0b0 100644 --- a/examples/da/plot_otda_color_images.py +++ b/examples/da/plot_otda_color_images.py @@ -20,9 +20,10 @@ SIAM Journal on Imaging Sciences, 7(3), 1853-1882. import numpy as np from scipy import ndimage import matplotlib.pylab as pl - import ot +np.random.seed(42) + def im2mat(I): """Converts and image to matrix (one pixel per line)""" diff --git a/examples/da/plot_otda_d2.py b/examples/da/plot_otda_d2.py index 1d2192f..8833eb2 100644 --- a/examples/da/plot_otda_d2.py +++ b/examples/da/plot_otda_d2.py @@ -19,17 +19,19 @@ of what the transport methods are doing. # License: MIT License import matplotlib.pylab as pl +import numpy as np import ot -# number of source and target points to generate -ns = 150 -nt = 150 +np.random.seed(42) -Xs, ys = ot.datasets.get_data_classif('3gauss', ns) -Xt, yt = ot.datasets.get_data_classif('3gauss2', nt) +n_samples_source = 150 +n_samples_target = 150 + +Xs, ys = ot.datasets.get_data_classif('3gauss', n_samples_source) +Xt, yt = ot.datasets.get_data_classif('3gauss2', n_samples_target) # Cost matrix -M = ot.dist(Xs, Xt) +M = ot.dist(Xs, Xt, metric='sqeuclidean') # Instantiate the different transport algorithms and fit them diff --git a/examples/da/plot_otda_mapping.py b/examples/da/plot_otda_mapping.py index 6d83507..aea7f09 100644 --- a/examples/da/plot_otda_mapping.py +++ b/examples/da/plot_otda_mapping.py @@ -23,7 +23,7 @@ import matplotlib.pylab as pl import ot -np.random.seed(0) +np.random.seed(42) ############################################################################## # generate @@ -31,10 +31,11 @@ np.random.seed(0) 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) -Xs_new, _ = ot.datasets.get_data_classif('gaussrot', n, nz=nz) -Xt, yt = ot.datasets.get_data_classif('gaussrot', n, theta=theta, nz=nz) +noise_level = 0.1 +Xs, ys = ot.datasets.get_data_classif('gaussrot', n, nz=noise_level) +Xs_new, _ = ot.datasets.get_data_classif('gaussrot', n, nz=noise_level) +Xt, yt = ot.datasets.get_data_classif( + 'gaussrot', n, theta=theta, nz=noise_level) # one of the target mode changes its variance (no linear mapping) Xt[yt == 2] *= 3 @@ -46,8 +47,7 @@ ot_mapping_linear = ot.da.MappingTransport( kernel="linear", mu=1e0, eta=1e-8, bias=True, max_iter=20, verbose=True) -ot_mapping_linear.fit( - Xs=Xs, Xt=Xt) +ot_mapping_linear.fit(Xs=Xs, Xt=Xt) # for original source samples, transform applies barycentric mapping transp_Xs_linear = ot_mapping_linear.transform(Xs=Xs) diff --git a/examples/da/plot_otda_mapping_colors_images.py b/examples/da/plot_otda_mapping_colors_images.py index 4209020..6c024ea 100644 --- a/examples/da/plot_otda_mapping_colors_images.py +++ b/examples/da/plot_otda_mapping_colors_images.py @@ -23,6 +23,8 @@ from scipy import ndimage import matplotlib.pylab as pl import ot +np.random.seed(42) + def im2mat(I): """Converts and image to matrix (one pixel per line)""" -- cgit v1.2.3 From 65de6fc9add57b95b8968e1e75fe1af342f81d01 Mon Sep 17 00:00:00 2001 From: Slasnista Date: Tue, 29 Aug 2017 13:34:34 +0200 Subject: pass on examples | introduced RandomState --- examples/da/plot_otda_classes.py | 20 +++++++++++++------- examples/da/plot_otda_color_images.py | 19 ++++++++++++++++--- examples/da/plot_otda_d2.py | 12 ++++++++++-- examples/da/plot_otda_mapping.py | 21 ++++++++++++++------- examples/da/plot_otda_mapping_colors_images.py | 9 ++++++--- 5 files changed, 59 insertions(+), 22 deletions(-) (limited to 'examples/da/plot_otda_mapping.py') diff --git a/examples/da/plot_otda_classes.py b/examples/da/plot_otda_classes.py index 6870fa4..ec57a37 100644 --- a/examples/da/plot_otda_classes.py +++ b/examples/da/plot_otda_classes.py @@ -15,19 +15,23 @@ approaches currently supported in POT. # License: MIT License import matplotlib.pylab as pl -import numpy as np import ot -np.random.seed(42) -# number of source and target points to generate -ns = 150 -nt = 150 +############################################################################## +# generate data +############################################################################## + +n_source_samples = 150 +n_target_samples = 150 + +Xs, ys = ot.datasets.get_data_classif('3gauss', n_source_samples) +Xt, yt = ot.datasets.get_data_classif('3gauss2', n_target_samples) -Xs, ys = ot.datasets.get_data_classif('3gauss', ns) -Xt, yt = ot.datasets.get_data_classif('3gauss2', nt) +############################################################################## # Instantiate the different transport algorithms and fit them +############################################################################## # EMD Transport ot_emd = ot.da.EMDTransport() @@ -52,6 +56,7 @@ transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs) transp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs) transp_Xs_l1l2 = ot_l1l2.transform(Xs=Xs) + ############################################################################## # Fig 1 : plots source and target samples ############################################################################## @@ -72,6 +77,7 @@ pl.legend(loc=0) pl.title('Target samples') pl.tight_layout() + ############################################################################## # Fig 2 : plot optimal couplings and transported samples ############################################################################## diff --git a/examples/da/plot_otda_color_images.py b/examples/da/plot_otda_color_images.py index 805d0b0..3984afb 100644 --- a/examples/da/plot_otda_color_images.py +++ b/examples/da/plot_otda_color_images.py @@ -22,7 +22,8 @@ from scipy import ndimage import matplotlib.pylab as pl import ot -np.random.seed(42) + +r = np.random.RandomState(42) def im2mat(I): @@ -39,6 +40,10 @@ def minmax(I): return np.clip(I, 0, 1) +############################################################################## +# generate data +############################################################################## + # Loading images I1 = ndimage.imread('../../data/ocean_day.jpg').astype(np.float64) / 256 I2 = ndimage.imread('../../data/ocean_sunset.jpg').astype(np.float64) / 256 @@ -48,12 +53,17 @@ 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,)) +idx1 = r.randint(X1.shape[0], size=(nb,)) +idx2 = r.randint(X2.shape[0], size=(nb,)) Xs = X1[idx1, :] Xt = X2[idx2, :] + +############################################################################## +# Instantiate the different transport algorithms and fit them +############################################################################## + # EMDTransport ot_emd = ot.da.EMDTransport() ot_emd.fit(Xs=Xs, Xt=Xt) @@ -75,6 +85,7 @@ I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) I1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) I2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape)) + ############################################################################## # plot original image ############################################################################## @@ -91,6 +102,7 @@ pl.imshow(I2) pl.axis('off') pl.title('Image 2') + ############################################################################## # scatter plot of colors ############################################################################## @@ -112,6 +124,7 @@ pl.ylabel('Blue') pl.title('Image 2') pl.tight_layout() + ############################################################################## # plot new images ############################################################################## diff --git a/examples/da/plot_otda_d2.py b/examples/da/plot_otda_d2.py index 8833eb2..3daa0a6 100644 --- a/examples/da/plot_otda_d2.py +++ b/examples/da/plot_otda_d2.py @@ -19,10 +19,12 @@ of what the transport methods are doing. # License: MIT License import matplotlib.pylab as pl -import numpy as np import ot -np.random.seed(42) + +############################################################################## +# generate data +############################################################################## n_samples_source = 150 n_samples_target = 150 @@ -33,7 +35,10 @@ Xt, yt = ot.datasets.get_data_classif('3gauss2', n_samples_target) # Cost matrix M = ot.dist(Xs, Xt, metric='sqeuclidean') + +############################################################################## # Instantiate the different transport algorithms and fit them +############################################################################## # EMD Transport ot_emd = ot.da.EMDTransport() @@ -52,6 +57,7 @@ transp_Xs_emd = ot_emd.transform(Xs=Xs) transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs) transp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs) + ############################################################################## # Fig 1 : plots source and target samples + matrix of pairwise distance ############################################################################## @@ -78,6 +84,7 @@ pl.yticks([]) pl.title('Matrix of pairwise distances') pl.tight_layout() + ############################################################################## # Fig 2 : plots optimal couplings for the different methods ############################################################################## @@ -127,6 +134,7 @@ pl.yticks([]) pl.title('Main coupling coefficients\nSinkhornLpl1Transport') pl.tight_layout() + ############################################################################## # Fig 3 : plot transported samples ############################################################################## diff --git a/examples/da/plot_otda_mapping.py b/examples/da/plot_otda_mapping.py index aea7f09..09d2cb4 100644 --- a/examples/da/plot_otda_mapping.py +++ b/examples/da/plot_otda_mapping.py @@ -23,25 +23,31 @@ import matplotlib.pylab as pl import ot -np.random.seed(42) - ############################################################################## -# generate +# generate data ############################################################################## -n = 100 # nb samples in source and target datasets +n_source_samples = 100 +n_target_samples = 100 theta = 2 * np.pi / 20 noise_level = 0.1 -Xs, ys = ot.datasets.get_data_classif('gaussrot', n, nz=noise_level) -Xs_new, _ = ot.datasets.get_data_classif('gaussrot', n, nz=noise_level) + +Xs, ys = ot.datasets.get_data_classif( + 'gaussrot', n_source_samples, nz=noise_level) +Xs_new, _ = ot.datasets.get_data_classif( + 'gaussrot', n_source_samples, nz=noise_level) Xt, yt = ot.datasets.get_data_classif( - 'gaussrot', n, theta=theta, nz=noise_level) + 'gaussrot', n_target_samples, theta=theta, nz=noise_level) # one of the target mode changes its variance (no linear mapping) Xt[yt == 2] *= 3 Xt = Xt + 4 +############################################################################## +# Instantiate the different transport algorithms and fit them +############################################################################## + # MappingTransport with linear kernel ot_mapping_linear = ot.da.MappingTransport( kernel="linear", mu=1e0, eta=1e-8, bias=True, @@ -80,6 +86,7 @@ pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') pl.legend(loc=0) pl.title('Source and target distributions') + ############################################################################## # plot transported samples ############################################################################## diff --git a/examples/da/plot_otda_mapping_colors_images.py b/examples/da/plot_otda_mapping_colors_images.py index 6c024ea..a628b05 100644 --- a/examples/da/plot_otda_mapping_colors_images.py +++ b/examples/da/plot_otda_mapping_colors_images.py @@ -23,7 +23,7 @@ from scipy import ndimage import matplotlib.pylab as pl import ot -np.random.seed(42) +r = np.random.RandomState(42) def im2mat(I): @@ -54,8 +54,8 @@ 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,)) +idx1 = r.randint(X1.shape[0], size=(nb,)) +idx2 = r.randint(X2.shape[0], size=(nb,)) Xs = X1[idx1, :] Xt = X2[idx2, :] @@ -91,6 +91,7 @@ ot_mapping_gaussian.fit(Xs=Xs, Xt=Xt) X1tn = ot_mapping_gaussian.transform(Xs=X1) # use the estimated mapping Image_mapping_gaussian = minmax(mat2im(X1tn, I1.shape)) + ############################################################################## # plot original images ############################################################################## @@ -107,6 +108,7 @@ pl.axis('off') pl.title('Image 2') pl.tight_layout() + ############################################################################## # plot pixel values distribution ############################################################################## @@ -128,6 +130,7 @@ pl.ylabel('Blue') pl.title('Image 2') pl.tight_layout() + ############################################################################## # plot transformed images ############################################################################## -- cgit v1.2.3