From ab5918b2e2dc88a3520c059e6a79a6f81959381e Mon Sep 17 00:00:00 2001 From: RĂ©mi Flamary Date: Wed, 30 Aug 2017 17:02:59 +0200 Subject: add files and notebooks --- .../auto_examples/images/sphx_glr_plot_WDA_002.png | Bin 0 -> 90982 bytes .../images/sphx_glr_plot_otda_classes_001.png | Bin 0 -> 50114 bytes .../images/sphx_glr_plot_otda_classes_003.png | Bin 0 -> 194170 bytes .../images/sphx_glr_plot_otda_color_images_001.png | Bin 0 -> 144957 bytes .../images/sphx_glr_plot_otda_color_images_003.png | Bin 0 -> 50401 bytes .../images/sphx_glr_plot_otda_color_images_005.png | Bin 0 -> 234337 bytes .../images/sphx_glr_plot_otda_d2_001.png | Bin 0 -> 130439 bytes .../images/sphx_glr_plot_otda_d2_003.png | Bin 0 -> 224757 bytes .../images/sphx_glr_plot_otda_d2_006.png | Bin 0 -> 99742 bytes .../images/sphx_glr_plot_otda_mapping_001.png | Bin 0 -> 35810 bytes .../images/sphx_glr_plot_otda_mapping_003.png | Bin 0 -> 71391 bytes ...phx_glr_plot_otda_mapping_colors_images_001.png | Bin 0 -> 165592 bytes ...phx_glr_plot_otda_mapping_colors_images_003.png | Bin 0 -> 80722 bytes ...phx_glr_plot_otda_mapping_colors_images_004.png | Bin 0 -> 541483 bytes .../thumb/sphx_glr_plot_otda_classes_thumb.png | Bin 0 -> 30152 bytes .../sphx_glr_plot_otda_color_images_thumb.png | Bin 0 -> 51085 bytes .../images/thumb/sphx_glr_plot_otda_d2_thumb.png | Bin 0 -> 52925 bytes ...x_glr_plot_otda_mapping_colors_images_thumb.png | Bin 0 -> 58315 bytes .../thumb/sphx_glr_plot_otda_mapping_thumb.png | Bin 0 -> 18620 bytes docs/source/auto_examples/plot_otda_classes.ipynb | 126 +++++++++ docs/source/auto_examples/plot_otda_classes.py | 150 ++++++++++ docs/source/auto_examples/plot_otda_classes.rst | 258 ++++++++++++++++++ .../auto_examples/plot_otda_color_images.ipynb | 144 ++++++++++ .../source/auto_examples/plot_otda_color_images.py | 165 +++++++++++ .../auto_examples/plot_otda_color_images.rst | 257 ++++++++++++++++++ docs/source/auto_examples/plot_otda_d2.ipynb | 144 ++++++++++ docs/source/auto_examples/plot_otda_d2.py | 173 ++++++++++++ docs/source/auto_examples/plot_otda_d2.rst | 265 ++++++++++++++++++ docs/source/auto_examples/plot_otda_mapping.ipynb | 126 +++++++++ docs/source/auto_examples/plot_otda_mapping.py | 126 +++++++++ docs/source/auto_examples/plot_otda_mapping.rst | 231 ++++++++++++++++ .../plot_otda_mapping_colors_images.ipynb | 144 ++++++++++ .../plot_otda_mapping_colors_images.py | 171 ++++++++++++ .../plot_otda_mapping_colors_images.rst | 302 +++++++++++++++++++++ 34 files changed, 2782 insertions(+) create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_WDA_002.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png create mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png create mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png create mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png create mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png create mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png create mode 100644 docs/source/auto_examples/plot_otda_classes.ipynb create mode 100644 docs/source/auto_examples/plot_otda_classes.py create mode 100644 docs/source/auto_examples/plot_otda_classes.rst create mode 100644 docs/source/auto_examples/plot_otda_color_images.ipynb create mode 100644 docs/source/auto_examples/plot_otda_color_images.py create mode 100644 docs/source/auto_examples/plot_otda_color_images.rst create mode 100644 docs/source/auto_examples/plot_otda_d2.ipynb create mode 100644 docs/source/auto_examples/plot_otda_d2.py create mode 100644 docs/source/auto_examples/plot_otda_d2.rst create mode 100644 docs/source/auto_examples/plot_otda_mapping.ipynb create mode 100644 docs/source/auto_examples/plot_otda_mapping.py create mode 100644 docs/source/auto_examples/plot_otda_mapping.rst create mode 100644 docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb create mode 100644 docs/source/auto_examples/plot_otda_mapping_colors_images.py create mode 100644 docs/source/auto_examples/plot_otda_mapping_colors_images.rst (limited to 'docs') diff --git a/docs/source/auto_examples/images/sphx_glr_plot_WDA_002.png b/docs/source/auto_examples/images/sphx_glr_plot_WDA_002.png new file mode 100644 index 0000000..95ee7ca Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_WDA_002.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png new file mode 100644 index 0000000..45a823d Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png new file mode 100644 index 0000000..2798f3b Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_classes_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png new file mode 100644 index 0000000..95f882a Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png new file mode 100644 index 0000000..aa1a5d3 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png new file mode 100644 index 0000000..b43c0cb Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_color_images_005.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png new file mode 100644 index 0000000..bc583a8 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png new file mode 100644 index 0000000..7d85e76 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png new file mode 100644 index 0000000..fba820a Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_d2_006.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png new file mode 100644 index 0000000..03a3130 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png new file mode 100644 index 0000000..9c9be23 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png new file mode 100644 index 0000000..33134fc Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png new file mode 100644 index 0000000..42197e3 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png differ diff --git a/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png new file mode 100644 index 0000000..ebf268b Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png new file mode 100644 index 0000000..3dfc6ca Binary files /dev/null and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_classes_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png new file mode 100644 index 0000000..a919055 Binary files /dev/null and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png new file mode 100644 index 0000000..d9d673c Binary files /dev/null and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_d2_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png new file mode 100644 index 0000000..f7fd217 Binary files /dev/null and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png differ diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png new file mode 100644 index 0000000..4ab5023 Binary files /dev/null and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_otda_mapping_thumb.png differ diff --git a/docs/source/auto_examples/plot_otda_classes.ipynb b/docs/source/auto_examples/plot_otda_classes.ipynb new file mode 100644 index 0000000..16634b1 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_classes.ipynb @@ -0,0 +1,126 @@ +{ + "nbformat_minor": 0, + "nbformat": 4, + "cells": [ + { + "execution_count": null, + "cell_type": "code", + "source": [ + "%matplotlib inline" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "\n# OT for domain adaptation\n\n\nThis example introduces a domain adaptation in a 2D setting and the 4 OTDA\napproaches currently supported in POT.\n\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# Authors: Remi Flamary \n# Stanislas Chambon \n#\n# License: MIT License\n\nimport matplotlib.pylab as pl\nimport ot" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "generate data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "n_source_samples = 150\nn_target_samples = 150\n\nXs, ys = ot.datasets.get_data_classif('3gauss', n_source_samples)\nXt, yt = ot.datasets.get_data_classif('3gauss2', n_target_samples)" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# EMD Transport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\n\n# Sinkhorn Transport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\n\n# Sinkhorn Transport with Group lasso regularization\not_lpl1 = ot.da.SinkhornLpl1Transport(reg_e=1e-1, reg_cl=1e0)\not_lpl1.fit(Xs=Xs, ys=ys, Xt=Xt)\n\n# Sinkhorn Transport with Group lasso regularization l1l2\not_l1l2 = ot.da.SinkhornL1l2Transport(reg_e=1e-1, reg_cl=2e0, max_iter=20,\n verbose=True)\not_l1l2.fit(Xs=Xs, ys=ys, Xt=Xt)\n\n# transport source samples onto target samples\ntransp_Xs_emd = ot_emd.transform(Xs=Xs)\ntransp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs)\ntransp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs)\ntransp_Xs_l1l2 = ot_l1l2.transform(Xs=Xs)" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Fig 1 : plots source and target samples\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(1, figsize=(10, 5))\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples')\npl.xticks([])\npl.yticks([])\npl.legend(loc=0)\npl.title('Source samples')\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples')\npl.xticks([])\npl.yticks([])\npl.legend(loc=0)\npl.title('Target samples')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Fig 2 : plot optimal couplings and transported samples\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "param_img = {'interpolation': 'nearest', 'cmap': 'spectral'}\n\npl.figure(2, figsize=(15, 8))\npl.subplot(2, 4, 1)\npl.imshow(ot_emd.coupling_, **param_img)\npl.xticks([])\npl.yticks([])\npl.title('Optimal coupling\\nEMDTransport')\n\npl.subplot(2, 4, 2)\npl.imshow(ot_sinkhorn.coupling_, **param_img)\npl.xticks([])\npl.yticks([])\npl.title('Optimal coupling\\nSinkhornTransport')\n\npl.subplot(2, 4, 3)\npl.imshow(ot_lpl1.coupling_, **param_img)\npl.xticks([])\npl.yticks([])\npl.title('Optimal coupling\\nSinkhornLpl1Transport')\n\npl.subplot(2, 4, 4)\npl.imshow(ot_l1l2.coupling_, **param_img)\npl.xticks([])\npl.yticks([])\npl.title('Optimal coupling\\nSinkhornL1l2Transport')\n\npl.subplot(2, 4, 5)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=0.3)\npl.scatter(transp_Xs_emd[:, 0], transp_Xs_emd[:, 1], c=ys,\n marker='+', label='Transp samples', s=30)\npl.xticks([])\npl.yticks([])\npl.title('Transported samples\\nEmdTransport')\npl.legend(loc=\"lower left\")\n\npl.subplot(2, 4, 6)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=0.3)\npl.scatter(transp_Xs_sinkhorn[:, 0], transp_Xs_sinkhorn[:, 1], c=ys,\n marker='+', label='Transp samples', s=30)\npl.xticks([])\npl.yticks([])\npl.title('Transported samples\\nSinkhornTransport')\n\npl.subplot(2, 4, 7)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=0.3)\npl.scatter(transp_Xs_lpl1[:, 0], transp_Xs_lpl1[:, 1], c=ys,\n marker='+', label='Transp samples', s=30)\npl.xticks([])\npl.yticks([])\npl.title('Transported samples\\nSinkhornLpl1Transport')\n\npl.subplot(2, 4, 8)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=0.3)\npl.scatter(transp_Xs_l1l2[:, 0], transp_Xs_l1l2[:, 1], c=ys,\n marker='+', label='Transp samples', s=30)\npl.xticks([])\npl.yticks([])\npl.title('Transported samples\\nSinkhornL1l2Transport')\npl.tight_layout()\n\npl.show()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "name": "python2", + "language": "python" + }, + "language_info": { + "mimetype": "text/x-python", + "nbconvert_exporter": "python", + "name": "python", + "file_extension": ".py", + "version": "2.7.12", + "pygments_lexer": "ipython2", + "codemirror_mode": { + "version": 2, + "name": "ipython" + } + } + } +} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_otda_classes.py b/docs/source/auto_examples/plot_otda_classes.py new file mode 100644 index 0000000..ec57a37 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_classes.py @@ -0,0 +1,150 @@ +# -*- coding: utf-8 -*- +""" +======================== +OT for domain adaptation +======================== + +This example introduces a domain adaptation in a 2D setting and the 4 OTDA +approaches currently supported in POT. + +""" + +# Authors: Remi Flamary +# Stanislas Chambon +# +# License: MIT License + +import matplotlib.pylab as pl +import ot + + +############################################################################## +# 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) + + +############################################################################## +# Instantiate the different transport algorithms and fit them +############################################################################## + +# EMD Transport +ot_emd = ot.da.EMDTransport() +ot_emd.fit(Xs=Xs, Xt=Xt) + +# Sinkhorn Transport +ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) +ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + +# Sinkhorn Transport with Group lasso regularization +ot_lpl1 = ot.da.SinkhornLpl1Transport(reg_e=1e-1, reg_cl=1e0) +ot_lpl1.fit(Xs=Xs, ys=ys, Xt=Xt) + +# Sinkhorn Transport with Group lasso regularization l1l2 +ot_l1l2 = ot.da.SinkhornL1l2Transport(reg_e=1e-1, reg_cl=2e0, max_iter=20, + verbose=True) +ot_l1l2.fit(Xs=Xs, ys=ys, Xt=Xt) + +# transport source samples onto target samples +transp_Xs_emd = ot_emd.transform(Xs=Xs) +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 +############################################################################## + +pl.figure(1, figsize=(10, 5)) +pl.subplot(1, 2, 1) +pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples') +pl.xticks([]) +pl.yticks([]) +pl.legend(loc=0) +pl.title('Source samples') + +pl.subplot(1, 2, 2) +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') +pl.xticks([]) +pl.yticks([]) +pl.legend(loc=0) +pl.title('Target samples') +pl.tight_layout() + + +############################################################################## +# Fig 2 : plot optimal couplings and transported samples +############################################################################## + +param_img = {'interpolation': 'nearest', 'cmap': 'spectral'} + +pl.figure(2, figsize=(15, 8)) +pl.subplot(2, 4, 1) +pl.imshow(ot_emd.coupling_, **param_img) +pl.xticks([]) +pl.yticks([]) +pl.title('Optimal coupling\nEMDTransport') + +pl.subplot(2, 4, 2) +pl.imshow(ot_sinkhorn.coupling_, **param_img) +pl.xticks([]) +pl.yticks([]) +pl.title('Optimal coupling\nSinkhornTransport') + +pl.subplot(2, 4, 3) +pl.imshow(ot_lpl1.coupling_, **param_img) +pl.xticks([]) +pl.yticks([]) +pl.title('Optimal coupling\nSinkhornLpl1Transport') + +pl.subplot(2, 4, 4) +pl.imshow(ot_l1l2.coupling_, **param_img) +pl.xticks([]) +pl.yticks([]) +pl.title('Optimal coupling\nSinkhornL1l2Transport') + +pl.subplot(2, 4, 5) +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.3) +pl.scatter(transp_Xs_emd[:, 0], transp_Xs_emd[:, 1], c=ys, + marker='+', label='Transp samples', s=30) +pl.xticks([]) +pl.yticks([]) +pl.title('Transported samples\nEmdTransport') +pl.legend(loc="lower left") + +pl.subplot(2, 4, 6) +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.3) +pl.scatter(transp_Xs_sinkhorn[:, 0], transp_Xs_sinkhorn[:, 1], c=ys, + marker='+', label='Transp samples', s=30) +pl.xticks([]) +pl.yticks([]) +pl.title('Transported samples\nSinkhornTransport') + +pl.subplot(2, 4, 7) +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.3) +pl.scatter(transp_Xs_lpl1[:, 0], transp_Xs_lpl1[:, 1], c=ys, + marker='+', label='Transp samples', s=30) +pl.xticks([]) +pl.yticks([]) +pl.title('Transported samples\nSinkhornLpl1Transport') + +pl.subplot(2, 4, 8) +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.3) +pl.scatter(transp_Xs_l1l2[:, 0], transp_Xs_l1l2[:, 1], c=ys, + marker='+', label='Transp samples', s=30) +pl.xticks([]) +pl.yticks([]) +pl.title('Transported samples\nSinkhornL1l2Transport') +pl.tight_layout() + +pl.show() diff --git a/docs/source/auto_examples/plot_otda_classes.rst b/docs/source/auto_examples/plot_otda_classes.rst new file mode 100644 index 0000000..227a819 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_classes.rst @@ -0,0 +1,258 @@ + + +.. _sphx_glr_auto_examples_plot_otda_classes.py: + + +======================== +OT for domain adaptation +======================== + +This example introduces a domain adaptation in a 2D setting and the 4 OTDA +approaches currently supported in POT. + + + + +.. code-block:: python + + + # Authors: Remi Flamary + # Stanislas Chambon + # + # License: MIT License + + import matplotlib.pylab as pl + import ot + + + + + + + + +generate data +############################################################################# + + + +.. code-block:: python + + + 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) + + + + + + + + +Instantiate the different transport algorithms and fit them +############################################################################# + + + +.. code-block:: python + + + # EMD Transport + ot_emd = ot.da.EMDTransport() + ot_emd.fit(Xs=Xs, Xt=Xt) + + # Sinkhorn Transport + ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) + ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + + # Sinkhorn Transport with Group lasso regularization + ot_lpl1 = ot.da.SinkhornLpl1Transport(reg_e=1e-1, reg_cl=1e0) + ot_lpl1.fit(Xs=Xs, ys=ys, Xt=Xt) + + # Sinkhorn Transport with Group lasso regularization l1l2 + ot_l1l2 = ot.da.SinkhornL1l2Transport(reg_e=1e-1, reg_cl=2e0, max_iter=20, + verbose=True) + ot_l1l2.fit(Xs=Xs, ys=ys, Xt=Xt) + + # transport source samples onto target samples + transp_Xs_emd = ot_emd.transform(Xs=Xs) + transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs) + transp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs) + transp_Xs_l1l2 = ot_l1l2.transform(Xs=Xs) + + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + It. |Loss |Delta loss + -------------------------------- + 0|9.456043e+00|0.000000e+00 + 1|2.059035e+00|-3.592463e+00 + 2|1.839814e+00|-1.191540e-01 + 3|1.787860e+00|-2.905942e-02 + 4|1.766582e+00|-1.204485e-02 + 5|1.760573e+00|-3.413038e-03 + 6|1.755288e+00|-3.010556e-03 + 7|1.749124e+00|-3.523968e-03 + 8|1.744159e+00|-2.846760e-03 + 9|1.741007e+00|-1.810862e-03 + 10|1.739839e+00|-6.710130e-04 + 11|1.737221e+00|-1.507260e-03 + 12|1.736011e+00|-6.970742e-04 + 13|1.734948e+00|-6.126425e-04 + 14|1.733901e+00|-6.038775e-04 + 15|1.733768e+00|-7.618542e-05 + 16|1.732821e+00|-5.467723e-04 + 17|1.732678e+00|-8.226843e-05 + 18|1.731934e+00|-4.300066e-04 + 19|1.731850e+00|-4.848002e-05 + It. |Loss |Delta loss + -------------------------------- + 20|1.731699e+00|-8.729590e-05 + + +Fig 1 : plots source and target samples +############################################################################# + + + +.. code-block:: python + + + pl.figure(1, figsize=(10, 5)) + pl.subplot(1, 2, 1) + pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples') + pl.xticks([]) + pl.yticks([]) + pl.legend(loc=0) + pl.title('Source samples') + + pl.subplot(1, 2, 2) + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') + pl.xticks([]) + pl.yticks([]) + pl.legend(loc=0) + pl.title('Target samples') + pl.tight_layout() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_classes_001.png + :align: center + + + + +Fig 2 : plot optimal couplings and transported samples +############################################################################# + + + +.. code-block:: python + + + param_img = {'interpolation': 'nearest', 'cmap': 'spectral'} + + pl.figure(2, figsize=(15, 8)) + pl.subplot(2, 4, 1) + pl.imshow(ot_emd.coupling_, **param_img) + pl.xticks([]) + pl.yticks([]) + pl.title('Optimal coupling\nEMDTransport') + + pl.subplot(2, 4, 2) + pl.imshow(ot_sinkhorn.coupling_, **param_img) + pl.xticks([]) + pl.yticks([]) + pl.title('Optimal coupling\nSinkhornTransport') + + pl.subplot(2, 4, 3) + pl.imshow(ot_lpl1.coupling_, **param_img) + pl.xticks([]) + pl.yticks([]) + pl.title('Optimal coupling\nSinkhornLpl1Transport') + + pl.subplot(2, 4, 4) + pl.imshow(ot_l1l2.coupling_, **param_img) + pl.xticks([]) + pl.yticks([]) + pl.title('Optimal coupling\nSinkhornL1l2Transport') + + pl.subplot(2, 4, 5) + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.3) + pl.scatter(transp_Xs_emd[:, 0], transp_Xs_emd[:, 1], c=ys, + marker='+', label='Transp samples', s=30) + pl.xticks([]) + pl.yticks([]) + pl.title('Transported samples\nEmdTransport') + pl.legend(loc="lower left") + + pl.subplot(2, 4, 6) + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.3) + pl.scatter(transp_Xs_sinkhorn[:, 0], transp_Xs_sinkhorn[:, 1], c=ys, + marker='+', label='Transp samples', s=30) + pl.xticks([]) + pl.yticks([]) + pl.title('Transported samples\nSinkhornTransport') + + pl.subplot(2, 4, 7) + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.3) + pl.scatter(transp_Xs_lpl1[:, 0], transp_Xs_lpl1[:, 1], c=ys, + marker='+', label='Transp samples', s=30) + pl.xticks([]) + pl.yticks([]) + pl.title('Transported samples\nSinkhornLpl1Transport') + + pl.subplot(2, 4, 8) + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.3) + pl.scatter(transp_Xs_l1l2[:, 0], transp_Xs_l1l2[:, 1], c=ys, + marker='+', label='Transp samples', s=30) + pl.xticks([]) + pl.yticks([]) + pl.title('Transported samples\nSinkhornL1l2Transport') + pl.tight_layout() + + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_classes_003.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 1.906 seconds) + + + +.. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_otda_classes.py ` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_otda_classes.ipynb ` + +.. rst-class:: sphx-glr-signature + + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_color_images.ipynb b/docs/source/auto_examples/plot_otda_color_images.ipynb new file mode 100644 index 0000000..c45c307 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_color_images.ipynb @@ -0,0 +1,144 @@ +{ + "nbformat_minor": 0, + "nbformat": 4, + "cells": [ + { + "execution_count": null, + "cell_type": "code", + "source": [ + "%matplotlib inline" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "\n========================================================\nOT for domain adaptation with image color adaptation [6]\n========================================================\n\nThis example presents a way of transferring colors between two image\nwith Optimal Transport as introduced in [6]\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014).\nRegularized discrete optimal transport.\nSIAM Journal on Imaging Sciences, 7(3), 1853-1882.\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# Authors: Remi Flamary \n# Stanislas Chambon \n#\n# License: MIT License\n\nimport numpy as np\nfrom scipy import ndimage\nimport matplotlib.pylab as pl\nimport ot\n\n\nr = np.random.RandomState(42)\n\n\ndef im2mat(I):\n \"\"\"Converts and image to matrix (one pixel per line)\"\"\"\n return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))\n\n\ndef mat2im(X, shape):\n \"\"\"Converts back a matrix to an image\"\"\"\n return X.reshape(shape)\n\n\ndef minmax(I):\n return np.clip(I, 0, 1)" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "generate data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# Loading images\nI1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256\nI2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256\n\nX1 = im2mat(I1)\nX2 = im2mat(I2)\n\n# training samples\nnb = 1000\nidx1 = r.randint(X1.shape[0], size=(nb,))\nidx2 = r.randint(X2.shape[0], size=(nb,))\n\nXs = X1[idx1, :]\nXt = X2[idx2, :]" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# EMDTransport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\n\n# SinkhornTransport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\n\n# prediction between images (using out of sample prediction as in [6])\ntransp_Xs_emd = ot_emd.transform(Xs=X1)\ntransp_Xt_emd = ot_emd.inverse_transform(Xt=X2)\n\ntransp_Xs_sinkhorn = ot_emd.transform(Xs=X1)\ntransp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2)\n\nI1t = minmax(mat2im(transp_Xs_emd, I1.shape))\nI2t = minmax(mat2im(transp_Xt_emd, I2.shape))\n\nI1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape))\nI2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape))" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "plot original image\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(1, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "scatter plot of colors\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(2, figsize=(6.4, 3))\n\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 2], c=Xs)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 2], c=Xt)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "plot new images\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(3, figsize=(8, 4))\n\npl.subplot(2, 3, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(2, 3, 2)\npl.imshow(I1t)\npl.axis('off')\npl.title('Image 1 Adapt')\n\npl.subplot(2, 3, 3)\npl.imshow(I1te)\npl.axis('off')\npl.title('Image 1 Adapt (reg)')\n\npl.subplot(2, 3, 4)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')\n\npl.subplot(2, 3, 5)\npl.imshow(I2t)\npl.axis('off')\npl.title('Image 2 Adapt')\n\npl.subplot(2, 3, 6)\npl.imshow(I2te)\npl.axis('off')\npl.title('Image 2 Adapt (reg)')\npl.tight_layout()\n\npl.show()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "name": "python2", + "language": "python" + }, + "language_info": { + "mimetype": "text/x-python", + "nbconvert_exporter": "python", + "name": "python", + "file_extension": ".py", + "version": "2.7.12", + "pygments_lexer": "ipython2", + "codemirror_mode": { + "version": 2, + "name": "ipython" + } + } + } +} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_otda_color_images.py b/docs/source/auto_examples/plot_otda_color_images.py new file mode 100644 index 0000000..46ad44b --- /dev/null +++ b/docs/source/auto_examples/plot_otda_color_images.py @@ -0,0 +1,165 @@ +# -*- coding: utf-8 -*- +""" +======================================================== +OT for domain adaptation with image color adaptation [6] +======================================================== + +This example presents a way of transferring colors between two image +with Optimal Transport as introduced in [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. +""" + +# Authors: Remi Flamary +# Stanislas Chambon +# +# License: MIT License + +import numpy as np +from scipy import ndimage +import matplotlib.pylab as pl +import ot + + +r = np.random.RandomState(42) + + +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) + + +############################################################################## +# 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 + +X1 = im2mat(I1) +X2 = im2mat(I2) + +# training samples +nb = 1000 +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) + +# SinkhornTransport +ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) +ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + +# prediction between images (using out of sample prediction as in [6]) +transp_Xs_emd = ot_emd.transform(Xs=X1) +transp_Xt_emd = ot_emd.inverse_transform(Xt=X2) + +transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) +transp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2) + +I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) +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 +############################################################################## + +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') + + +############################################################################## +# scatter plot of colors +############################################################################## + +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() + + +############################################################################## +# plot new images +############################################################################## + +pl.figure(3, 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/docs/source/auto_examples/plot_otda_color_images.rst b/docs/source/auto_examples/plot_otda_color_images.rst new file mode 100644 index 0000000..e3989c8 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_color_images.rst @@ -0,0 +1,257 @@ + + +.. _sphx_glr_auto_examples_plot_otda_color_images.py: + + +======================================================== +OT for domain adaptation with image color adaptation [6] +======================================================== + +This example presents a way of transferring colors between two image +with Optimal Transport as introduced in [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. + + + +.. code-block:: python + + + # Authors: Remi Flamary + # Stanislas Chambon + # + # License: MIT License + + import numpy as np + from scipy import ndimage + import matplotlib.pylab as pl + import ot + + + r = np.random.RandomState(42) + + + 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) + + + + + + + + +generate data +############################################################################# + + + +.. code-block:: python + + + # Loading images + I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 + I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256 + + X1 = im2mat(I1) + X2 = im2mat(I2) + + # training samples + nb = 1000 + 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 +############################################################################# + + + +.. code-block:: python + + + # EMDTransport + ot_emd = ot.da.EMDTransport() + ot_emd.fit(Xs=Xs, Xt=Xt) + + # SinkhornTransport + ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) + ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + + # prediction between images (using out of sample prediction as in [6]) + transp_Xs_emd = ot_emd.transform(Xs=X1) + transp_Xt_emd = ot_emd.inverse_transform(Xt=X2) + + transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) + transp_Xt_sinkhorn = ot_emd.inverse_transform(Xt=X2) + + I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) + 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 +############################################################################# + + + +.. code-block:: python + + + 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') + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_color_images_001.png + :align: center + + + + +scatter plot of colors +############################################################################# + + + +.. code-block:: python + + + 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() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_color_images_003.png + :align: center + + + + +plot new images +############################################################################# + + + +.. code-block:: python + + + pl.figure(3, 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() + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_color_images_005.png + :align: center + + + + +**Total running time of the script:** ( 3 minutes 16.043 seconds) + + + +.. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_otda_color_images.py ` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_otda_color_images.ipynb ` + +.. rst-class:: sphx-glr-signature + + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_d2.ipynb b/docs/source/auto_examples/plot_otda_d2.ipynb new file mode 100644 index 0000000..2331f8c --- /dev/null +++ b/docs/source/auto_examples/plot_otda_d2.ipynb @@ -0,0 +1,144 @@ +{ + "nbformat_minor": 0, + "nbformat": 4, + "cells": [ + { + "execution_count": null, + "cell_type": "code", + "source": [ + "%matplotlib inline" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "\n# OT for empirical distributions\n\n\nThis example introduces a domain adaptation in a 2D setting. It explicits\nthe problem of domain adaptation and introduces some optimal transport\napproaches to solve it.\n\nQuantities such as optimal couplings, greater coupling coefficients and\ntransported samples are represented in order to give a visual understanding\nof what the transport methods are doing.\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# Authors: Remi Flamary \n# Stanislas Chambon \n#\n# License: MIT License\n\nimport matplotlib.pylab as pl\nimport ot" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "generate data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "n_samples_source = 150\nn_samples_target = 150\n\nXs, ys = ot.datasets.get_data_classif('3gauss', n_samples_source)\nXt, yt = ot.datasets.get_data_classif('3gauss2', n_samples_target)\n\n# Cost matrix\nM = ot.dist(Xs, Xt, metric='sqeuclidean')" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# EMD Transport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\n\n# Sinkhorn Transport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\n\n# Sinkhorn Transport with Group lasso regularization\not_lpl1 = ot.da.SinkhornLpl1Transport(reg_e=1e-1, reg_cl=1e0)\not_lpl1.fit(Xs=Xs, ys=ys, Xt=Xt)\n\n# transport source samples onto target samples\ntransp_Xs_emd = ot_emd.transform(Xs=Xs)\ntransp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs)\ntransp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs)" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Fig 1 : plots source and target samples + matrix of pairwise distance\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(1, figsize=(10, 10))\npl.subplot(2, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples')\npl.xticks([])\npl.yticks([])\npl.legend(loc=0)\npl.title('Source samples')\n\npl.subplot(2, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples')\npl.xticks([])\npl.yticks([])\npl.legend(loc=0)\npl.title('Target samples')\n\npl.subplot(2, 2, 3)\npl.imshow(M, interpolation='nearest')\npl.xticks([])\npl.yticks([])\npl.title('Matrix of pairwise distances')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Fig 2 : plots optimal couplings for the different methods\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(2, figsize=(10, 6))\n\npl.subplot(2, 3, 1)\npl.imshow(ot_emd.coupling_, interpolation='nearest')\npl.xticks([])\npl.yticks([])\npl.title('Optimal coupling\\nEMDTransport')\n\npl.subplot(2, 3, 2)\npl.imshow(ot_sinkhorn.coupling_, interpolation='nearest')\npl.xticks([])\npl.yticks([])\npl.title('Optimal coupling\\nSinkhornTransport')\n\npl.subplot(2, 3, 3)\npl.imshow(ot_lpl1.coupling_, interpolation='nearest')\npl.xticks([])\npl.yticks([])\npl.title('Optimal coupling\\nSinkhornLpl1Transport')\n\npl.subplot(2, 3, 4)\not.plot.plot2D_samples_mat(Xs, Xt, ot_emd.coupling_, c=[.5, .5, 1])\npl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples')\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples')\npl.xticks([])\npl.yticks([])\npl.title('Main coupling coefficients\\nEMDTransport')\n\npl.subplot(2, 3, 5)\not.plot.plot2D_samples_mat(Xs, Xt, ot_sinkhorn.coupling_, c=[.5, .5, 1])\npl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples')\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples')\npl.xticks([])\npl.yticks([])\npl.title('Main coupling coefficients\\nSinkhornTransport')\n\npl.subplot(2, 3, 6)\not.plot.plot2D_samples_mat(Xs, Xt, ot_lpl1.coupling_, c=[.5, .5, 1])\npl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples')\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples')\npl.xticks([])\npl.yticks([])\npl.title('Main coupling coefficients\\nSinkhornLpl1Transport')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Fig 3 : plot transported samples\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# display transported samples\npl.figure(4, figsize=(10, 4))\npl.subplot(1, 3, 1)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=0.5)\npl.scatter(transp_Xs_emd[:, 0], transp_Xs_emd[:, 1], c=ys,\n marker='+', label='Transp samples', s=30)\npl.title('Transported samples\\nEmdTransport')\npl.legend(loc=0)\npl.xticks([])\npl.yticks([])\n\npl.subplot(1, 3, 2)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=0.5)\npl.scatter(transp_Xs_sinkhorn[:, 0], transp_Xs_sinkhorn[:, 1], c=ys,\n marker='+', label='Transp samples', s=30)\npl.title('Transported samples\\nSinkhornTransport')\npl.xticks([])\npl.yticks([])\n\npl.subplot(1, 3, 3)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=0.5)\npl.scatter(transp_Xs_lpl1[:, 0], transp_Xs_lpl1[:, 1], c=ys,\n marker='+', label='Transp samples', s=30)\npl.title('Transported samples\\nSinkhornLpl1Transport')\npl.xticks([])\npl.yticks([])\n\npl.tight_layout()\npl.show()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "name": "python2", + "language": "python" + }, + "language_info": { + "mimetype": "text/x-python", + "nbconvert_exporter": "python", + "name": "python", + "file_extension": ".py", + "version": "2.7.12", + "pygments_lexer": "ipython2", + "codemirror_mode": { + "version": 2, + "name": "ipython" + } + } + } +} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_otda_d2.py b/docs/source/auto_examples/plot_otda_d2.py new file mode 100644 index 0000000..3daa0a6 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_d2.py @@ -0,0 +1,173 @@ +# -*- coding: utf-8 -*- +""" +============================== +OT for empirical distributions +============================== + +This example introduces a domain adaptation in a 2D setting. It explicits +the problem of domain adaptation and introduces some optimal transport +approaches to solve it. + +Quantities such as optimal couplings, greater coupling coefficients and +transported samples are represented in order to give a visual understanding +of what the transport methods are doing. +""" + +# Authors: Remi Flamary +# Stanislas Chambon +# +# License: MIT License + +import matplotlib.pylab as pl +import ot + + +############################################################################## +# generate data +############################################################################## + +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, metric='sqeuclidean') + + +############################################################################## +# Instantiate the different transport algorithms and fit them +############################################################################## + +# EMD Transport +ot_emd = ot.da.EMDTransport() +ot_emd.fit(Xs=Xs, Xt=Xt) + +# Sinkhorn Transport +ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) +ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + +# Sinkhorn Transport with Group lasso regularization +ot_lpl1 = ot.da.SinkhornLpl1Transport(reg_e=1e-1, reg_cl=1e0) +ot_lpl1.fit(Xs=Xs, ys=ys, Xt=Xt) + +# transport source samples onto target samples +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 +############################################################################## + +pl.figure(1, figsize=(10, 10)) +pl.subplot(2, 2, 1) +pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples') +pl.xticks([]) +pl.yticks([]) +pl.legend(loc=0) +pl.title('Source samples') + +pl.subplot(2, 2, 2) +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') +pl.xticks([]) +pl.yticks([]) +pl.legend(loc=0) +pl.title('Target samples') + +pl.subplot(2, 2, 3) +pl.imshow(M, interpolation='nearest') +pl.xticks([]) +pl.yticks([]) +pl.title('Matrix of pairwise distances') +pl.tight_layout() + + +############################################################################## +# Fig 2 : plots optimal couplings for the different methods +############################################################################## + +pl.figure(2, figsize=(10, 6)) + +pl.subplot(2, 3, 1) +pl.imshow(ot_emd.coupling_, interpolation='nearest') +pl.xticks([]) +pl.yticks([]) +pl.title('Optimal coupling\nEMDTransport') + +pl.subplot(2, 3, 2) +pl.imshow(ot_sinkhorn.coupling_, interpolation='nearest') +pl.xticks([]) +pl.yticks([]) +pl.title('Optimal coupling\nSinkhornTransport') + +pl.subplot(2, 3, 3) +pl.imshow(ot_lpl1.coupling_, interpolation='nearest') +pl.xticks([]) +pl.yticks([]) +pl.title('Optimal coupling\nSinkhornLpl1Transport') + +pl.subplot(2, 3, 4) +ot.plot.plot2D_samples_mat(Xs, Xt, ot_emd.coupling_, 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.xticks([]) +pl.yticks([]) +pl.title('Main coupling coefficients\nEMDTransport') + +pl.subplot(2, 3, 5) +ot.plot.plot2D_samples_mat(Xs, Xt, ot_sinkhorn.coupling_, 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.xticks([]) +pl.yticks([]) +pl.title('Main coupling coefficients\nSinkhornTransport') + +pl.subplot(2, 3, 6) +ot.plot.plot2D_samples_mat(Xs, Xt, ot_lpl1.coupling_, 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.xticks([]) +pl.yticks([]) +pl.title('Main coupling coefficients\nSinkhornLpl1Transport') +pl.tight_layout() + + +############################################################################## +# Fig 3 : plot transported samples +############################################################################## + +# display transported samples +pl.figure(4, figsize=(10, 4)) +pl.subplot(1, 3, 1) +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.5) +pl.scatter(transp_Xs_emd[:, 0], transp_Xs_emd[:, 1], c=ys, + marker='+', label='Transp samples', s=30) +pl.title('Transported samples\nEmdTransport') +pl.legend(loc=0) +pl.xticks([]) +pl.yticks([]) + +pl.subplot(1, 3, 2) +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.5) +pl.scatter(transp_Xs_sinkhorn[:, 0], transp_Xs_sinkhorn[:, 1], c=ys, + marker='+', label='Transp samples', s=30) +pl.title('Transported samples\nSinkhornTransport') +pl.xticks([]) +pl.yticks([]) + +pl.subplot(1, 3, 3) +pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.5) +pl.scatter(transp_Xs_lpl1[:, 0], transp_Xs_lpl1[:, 1], c=ys, + marker='+', label='Transp samples', s=30) +pl.title('Transported samples\nSinkhornLpl1Transport') +pl.xticks([]) +pl.yticks([]) + +pl.tight_layout() +pl.show() diff --git a/docs/source/auto_examples/plot_otda_d2.rst b/docs/source/auto_examples/plot_otda_d2.rst new file mode 100644 index 0000000..20b76ba --- /dev/null +++ b/docs/source/auto_examples/plot_otda_d2.rst @@ -0,0 +1,265 @@ + + +.. _sphx_glr_auto_examples_plot_otda_d2.py: + + +============================== +OT for empirical distributions +============================== + +This example introduces a domain adaptation in a 2D setting. It explicits +the problem of domain adaptation and introduces some optimal transport +approaches to solve it. + +Quantities such as optimal couplings, greater coupling coefficients and +transported samples are represented in order to give a visual understanding +of what the transport methods are doing. + + + +.. code-block:: python + + + # Authors: Remi Flamary + # Stanislas Chambon + # + # License: MIT License + + import matplotlib.pylab as pl + import ot + + + + + + + + +generate data +############################################################################# + + + +.. code-block:: python + + + 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, metric='sqeuclidean') + + + + + + + + +Instantiate the different transport algorithms and fit them +############################################################################# + + + +.. code-block:: python + + + # EMD Transport + ot_emd = ot.da.EMDTransport() + ot_emd.fit(Xs=Xs, Xt=Xt) + + # Sinkhorn Transport + ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) + ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + + # Sinkhorn Transport with Group lasso regularization + ot_lpl1 = ot.da.SinkhornLpl1Transport(reg_e=1e-1, reg_cl=1e0) + ot_lpl1.fit(Xs=Xs, ys=ys, Xt=Xt) + + # transport source samples onto target samples + 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 +############################################################################# + + + +.. code-block:: python + + + pl.figure(1, figsize=(10, 10)) + pl.subplot(2, 2, 1) + pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples') + pl.xticks([]) + pl.yticks([]) + pl.legend(loc=0) + pl.title('Source samples') + + pl.subplot(2, 2, 2) + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples') + pl.xticks([]) + pl.yticks([]) + pl.legend(loc=0) + pl.title('Target samples') + + pl.subplot(2, 2, 3) + pl.imshow(M, interpolation='nearest') + pl.xticks([]) + pl.yticks([]) + pl.title('Matrix of pairwise distances') + pl.tight_layout() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_d2_001.png + :align: center + + + + +Fig 2 : plots optimal couplings for the different methods +############################################################################# + + + +.. code-block:: python + + + pl.figure(2, figsize=(10, 6)) + + pl.subplot(2, 3, 1) + pl.imshow(ot_emd.coupling_, interpolation='nearest') + pl.xticks([]) + pl.yticks([]) + pl.title('Optimal coupling\nEMDTransport') + + pl.subplot(2, 3, 2) + pl.imshow(ot_sinkhorn.coupling_, interpolation='nearest') + pl.xticks([]) + pl.yticks([]) + pl.title('Optimal coupling\nSinkhornTransport') + + pl.subplot(2, 3, 3) + pl.imshow(ot_lpl1.coupling_, interpolation='nearest') + pl.xticks([]) + pl.yticks([]) + pl.title('Optimal coupling\nSinkhornLpl1Transport') + + pl.subplot(2, 3, 4) + ot.plot.plot2D_samples_mat(Xs, Xt, ot_emd.coupling_, 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.xticks([]) + pl.yticks([]) + pl.title('Main coupling coefficients\nEMDTransport') + + pl.subplot(2, 3, 5) + ot.plot.plot2D_samples_mat(Xs, Xt, ot_sinkhorn.coupling_, 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.xticks([]) + pl.yticks([]) + pl.title('Main coupling coefficients\nSinkhornTransport') + + pl.subplot(2, 3, 6) + ot.plot.plot2D_samples_mat(Xs, Xt, ot_lpl1.coupling_, 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.xticks([]) + pl.yticks([]) + pl.title('Main coupling coefficients\nSinkhornLpl1Transport') + pl.tight_layout() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_d2_003.png + :align: center + + + + +Fig 3 : plot transported samples +############################################################################# + + + +.. code-block:: python + + + # display transported samples + pl.figure(4, figsize=(10, 4)) + pl.subplot(1, 3, 1) + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.5) + pl.scatter(transp_Xs_emd[:, 0], transp_Xs_emd[:, 1], c=ys, + marker='+', label='Transp samples', s=30) + pl.title('Transported samples\nEmdTransport') + pl.legend(loc=0) + pl.xticks([]) + pl.yticks([]) + + pl.subplot(1, 3, 2) + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.5) + pl.scatter(transp_Xs_sinkhorn[:, 0], transp_Xs_sinkhorn[:, 1], c=ys, + marker='+', label='Transp samples', s=30) + pl.title('Transported samples\nSinkhornTransport') + pl.xticks([]) + pl.yticks([]) + + pl.subplot(1, 3, 3) + pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', + label='Target samples', alpha=0.5) + pl.scatter(transp_Xs_lpl1[:, 0], transp_Xs_lpl1[:, 1], c=ys, + marker='+', label='Transp samples', s=30) + pl.title('Transported samples\nSinkhornLpl1Transport') + pl.xticks([]) + pl.yticks([]) + + pl.tight_layout() + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_d2_006.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 46.009 seconds) + + + +.. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_otda_d2.py ` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_otda_d2.ipynb ` + +.. rst-class:: sphx-glr-signature + + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_mapping.ipynb b/docs/source/auto_examples/plot_otda_mapping.ipynb new file mode 100644 index 0000000..0b5ca5c --- /dev/null +++ b/docs/source/auto_examples/plot_otda_mapping.ipynb @@ -0,0 +1,126 @@ +{ + "nbformat_minor": 0, + "nbformat": 4, + "cells": [ + { + "execution_count": null, + "cell_type": "code", + "source": [ + "%matplotlib inline" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "\n===============================================\nOT mapping estimation for domain adaptation [8]\n===============================================\n\nThis example presents how to use MappingTransport to estimate at the same\ntime both the coupling transport and approximate the transport map with either\na linear or a kernelized mapping as introduced in [8]\n\n[8] M. Perrot, N. Courty, R. Flamary, A. Habrard,\n \"Mapping estimation for discrete optimal transport\",\n Neural Information Processing Systems (NIPS), 2016.\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# Authors: Remi Flamary \n# Stanislas Chambon \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "generate data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "n_source_samples = 100\nn_target_samples = 100\ntheta = 2 * np.pi / 20\nnoise_level = 0.1\n\nXs, ys = ot.datasets.get_data_classif(\n 'gaussrot', n_source_samples, nz=noise_level)\nXs_new, _ = ot.datasets.get_data_classif(\n 'gaussrot', n_source_samples, nz=noise_level)\nXt, yt = ot.datasets.get_data_classif(\n 'gaussrot', n_target_samples, theta=theta, nz=noise_level)\n\n# one of the target mode changes its variance (no linear mapping)\nXt[yt == 2] *= 3\nXt = Xt + 4" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Instantiate the different transport algorithms and fit them\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# MappingTransport with linear kernel\not_mapping_linear = ot.da.MappingTransport(\n kernel=\"linear\", mu=1e0, eta=1e-8, bias=True,\n max_iter=20, verbose=True)\n\not_mapping_linear.fit(Xs=Xs, Xt=Xt)\n\n# for original source samples, transform applies barycentric mapping\ntransp_Xs_linear = ot_mapping_linear.transform(Xs=Xs)\n\n# for out of source samples, transform applies the linear mapping\ntransp_Xs_linear_new = ot_mapping_linear.transform(Xs=Xs_new)\n\n\n# MappingTransport with gaussian kernel\not_mapping_gaussian = ot.da.MappingTransport(\n kernel=\"gaussian\", eta=1e-5, mu=1e-1, bias=True, sigma=1,\n max_iter=10, verbose=True)\not_mapping_gaussian.fit(Xs=Xs, Xt=Xt)\n\n# for original source samples, transform applies barycentric mapping\ntransp_Xs_gaussian = ot_mapping_gaussian.transform(Xs=Xs)\n\n# for out of source samples, transform applies the gaussian mapping\ntransp_Xs_gaussian_new = ot_mapping_gaussian.transform(Xs=Xs_new)" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "plot data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(1, (10, 5))\npl.clf()\npl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker='+', label='Source samples')\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o', label='Target samples')\npl.legend(loc=0)\npl.title('Source and target distributions')" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "plot transported samples\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(2)\npl.clf()\npl.subplot(2, 2, 1)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=.2)\npl.scatter(transp_Xs_linear[:, 0], transp_Xs_linear[:, 1], c=ys, marker='+',\n label='Mapped source samples')\npl.title(\"Bary. mapping (linear)\")\npl.legend(loc=0)\n\npl.subplot(2, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=.2)\npl.scatter(transp_Xs_linear_new[:, 0], transp_Xs_linear_new[:, 1],\n c=ys, marker='+', label='Learned mapping')\npl.title(\"Estim. mapping (linear)\")\n\npl.subplot(2, 2, 3)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=.2)\npl.scatter(transp_Xs_gaussian[:, 0], transp_Xs_gaussian[:, 1], c=ys,\n marker='+', label='barycentric mapping')\npl.title(\"Bary. mapping (kernel)\")\n\npl.subplot(2, 2, 4)\npl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker='o',\n label='Target samples', alpha=.2)\npl.scatter(transp_Xs_gaussian_new[:, 0], transp_Xs_gaussian_new[:, 1], c=ys,\n marker='+', label='Learned mapping')\npl.title(\"Estim. mapping (kernel)\")\npl.tight_layout()\n\npl.show()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "name": "python2", + "language": "python" + }, + "language_info": { + "mimetype": "text/x-python", + "nbconvert_exporter": "python", + "name": "python", + "file_extension": ".py", + "version": "2.7.12", + "pygments_lexer": "ipython2", + "codemirror_mode": { + "version": 2, + "name": "ipython" + } + } + } +} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_otda_mapping.py b/docs/source/auto_examples/plot_otda_mapping.py new file mode 100644 index 0000000..09d2cb4 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_mapping.py @@ -0,0 +1,126 @@ +# -*- 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 +# Stanislas Chambon +# +# License: MIT License + +import numpy as np +import matplotlib.pylab as pl +import ot + + +############################################################################## +# generate data +############################################################################## + +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_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_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, + 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() diff --git a/docs/source/auto_examples/plot_otda_mapping.rst b/docs/source/auto_examples/plot_otda_mapping.rst new file mode 100644 index 0000000..088da31 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_mapping.rst @@ -0,0 +1,231 @@ + + +.. _sphx_glr_auto_examples_plot_otda_mapping.py: + + +=============================================== +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. + + + +.. code-block:: python + + + # Authors: Remi Flamary + # Stanislas Chambon + # + # License: MIT License + + import numpy as np + import matplotlib.pylab as pl + import ot + + + + + + + + +generate data +############################################################################# + + + +.. code-block:: python + + + 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_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_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 +############################################################################# + + + +.. code-block:: python + + + # 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) + + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + It. |Loss |Delta loss + -------------------------------- + 0|4.273804e+03|0.000000e+00 + 1|4.264510e+03|-2.174580e-03 + 2|4.264209e+03|-7.047095e-05 + 3|4.264078e+03|-3.069822e-05 + 4|4.264018e+03|-1.412924e-05 + 5|4.263961e+03|-1.341165e-05 + 6|4.263946e+03|-3.586522e-06 + It. |Loss |Delta loss + -------------------------------- + 0|4.294523e+02|0.000000e+00 + 1|4.247737e+02|-1.089443e-02 + 2|4.245516e+02|-5.228765e-04 + 3|4.244430e+02|-2.557417e-04 + 4|4.243724e+02|-1.663904e-04 + 5|4.243196e+02|-1.244111e-04 + 6|4.242808e+02|-9.132500e-05 + 7|4.242497e+02|-7.331710e-05 + 8|4.242271e+02|-5.326612e-05 + 9|4.242063e+02|-4.916026e-05 + 10|4.241906e+02|-3.699617e-05 + + +plot data +############################################################################# + + + +.. code-block:: python + + + 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') + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_mapping_001.png + :align: center + + + + +plot transported samples +############################################################################# + + + +.. code-block:: python + + + 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() + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_mapping_003.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 0.853 seconds) + + + +.. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_otda_mapping.py ` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_otda_mapping.ipynb ` + +.. rst-class:: sphx-glr-signature + + `Generated by Sphinx-Gallery `_ diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb new file mode 100644 index 0000000..4b2ec02 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb @@ -0,0 +1,144 @@ +{ + "nbformat_minor": 0, + "nbformat": 4, + "cells": [ + { + "execution_count": null, + "cell_type": "code", + "source": [ + "%matplotlib inline" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "\n====================================================================================\nOT for domain adaptation with image color adaptation [6] with mapping estimation [8]\n====================================================================================\n\n[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized\n discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3),\n 1853-1882.\n[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, \"Mapping estimation for\n discrete optimal transport\", Neural Information Processing Systems (NIPS),\n 2016.\n\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# Authors: Remi Flamary \n# Stanislas Chambon \n#\n# License: MIT License\n\nimport numpy as np\nfrom scipy import ndimage\nimport matplotlib.pylab as pl\nimport ot\n\nr = np.random.RandomState(42)\n\n\ndef im2mat(I):\n \"\"\"Converts and image to matrix (one pixel per line)\"\"\"\n return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))\n\n\ndef mat2im(X, shape):\n \"\"\"Converts back a matrix to an image\"\"\"\n return X.reshape(shape)\n\n\ndef minmax(I):\n return np.clip(I, 0, 1)" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Generate data\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# Loading images\nI1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256\nI2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256\n\n\nX1 = im2mat(I1)\nX2 = im2mat(I2)\n\n# training samples\nnb = 1000\nidx1 = r.randint(X1.shape[0], size=(nb,))\nidx2 = r.randint(X2.shape[0], size=(nb,))\n\nXs = X1[idx1, :]\nXt = X2[idx2, :]" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "Domain adaptation for pixel distribution transfer\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "# EMDTransport\not_emd = ot.da.EMDTransport()\not_emd.fit(Xs=Xs, Xt=Xt)\ntransp_Xs_emd = ot_emd.transform(Xs=X1)\nImage_emd = minmax(mat2im(transp_Xs_emd, I1.shape))\n\n# SinkhornTransport\not_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)\not_sinkhorn.fit(Xs=Xs, Xt=Xt)\ntransp_Xs_sinkhorn = ot_emd.transform(Xs=X1)\nImage_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape))\n\not_mapping_linear = ot.da.MappingTransport(\n mu=1e0, eta=1e-8, bias=True, max_iter=20, verbose=True)\not_mapping_linear.fit(Xs=Xs, Xt=Xt)\n\nX1tl = ot_mapping_linear.transform(Xs=X1)\nImage_mapping_linear = minmax(mat2im(X1tl, I1.shape))\n\not_mapping_gaussian = ot.da.MappingTransport(\n mu=1e0, eta=1e-2, sigma=1, bias=False, max_iter=10, verbose=True)\not_mapping_gaussian.fit(Xs=Xs, Xt=Xt)\n\nX1tn = ot_mapping_gaussian.transform(Xs=X1) # use the estimated mapping\nImage_mapping_gaussian = minmax(mat2im(X1tn, I1.shape))" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "plot original images\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(1, figsize=(6.4, 3))\npl.subplot(1, 2, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.imshow(I2)\npl.axis('off')\npl.title('Image 2')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "plot pixel values distribution\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(2, figsize=(6.4, 5))\n\npl.subplot(1, 2, 1)\npl.scatter(Xs[:, 0], Xs[:, 2], c=Xs)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 1')\n\npl.subplot(1, 2, 2)\npl.scatter(Xt[:, 0], Xt[:, 2], c=Xt)\npl.axis([0, 1, 0, 1])\npl.xlabel('Red')\npl.ylabel('Blue')\npl.title('Image 2')\npl.tight_layout()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + }, + { + "source": [ + "plot transformed images\n#############################################################################\n\n" + ], + "cell_type": "markdown", + "metadata": {} + }, + { + "execution_count": null, + "cell_type": "code", + "source": [ + "pl.figure(2, figsize=(10, 5))\n\npl.subplot(2, 3, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Im. 1')\n\npl.subplot(2, 3, 4)\npl.imshow(I2)\npl.axis('off')\npl.title('Im. 2')\n\npl.subplot(2, 3, 2)\npl.imshow(Image_emd)\npl.axis('off')\npl.title('EmdTransport')\n\npl.subplot(2, 3, 5)\npl.imshow(Image_sinkhorn)\npl.axis('off')\npl.title('SinkhornTransport')\n\npl.subplot(2, 3, 3)\npl.imshow(Image_mapping_linear)\npl.axis('off')\npl.title('MappingTransport (linear)')\n\npl.subplot(2, 3, 6)\npl.imshow(Image_mapping_gaussian)\npl.axis('off')\npl.title('MappingTransport (gaussian)')\npl.tight_layout()\n\npl.show()" + ], + "outputs": [], + "metadata": { + "collapsed": false + } + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "name": "python2", + "language": "python" + }, + "language_info": { + "mimetype": "text/x-python", + "nbconvert_exporter": "python", + "name": "python", + "file_extension": ".py", + "version": "2.7.12", + "pygments_lexer": "ipython2", + "codemirror_mode": { + "version": 2, + "name": "ipython" + } + } + } +} \ No newline at end of file diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.py b/docs/source/auto_examples/plot_otda_mapping_colors_images.py new file mode 100644 index 0000000..936206c --- /dev/null +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.py @@ -0,0 +1,171 @@ +# -*- 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. + +""" + +# Authors: Remi Flamary +# Stanislas Chambon +# +# License: MIT License + +import numpy as np +from scipy import ndimage +import matplotlib.pylab as pl +import ot + +r = np.random.RandomState(42) + + +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) + + +############################################################################## +# 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 + + +X1 = im2mat(I1) +X2 = im2mat(I2) + +# training samples +nb = 1000 +idx1 = r.randint(X1.shape[0], size=(nb,)) +idx2 = r.randint(X2.shape[0], size=(nb,)) + +Xs = X1[idx1, :] +Xt = X2[idx2, :] + + +############################################################################## +# Domain adaptation for pixel distribution transfer +############################################################################## + +# EMDTransport +ot_emd = ot.da.EMDTransport() +ot_emd.fit(Xs=Xs, Xt=Xt) +transp_Xs_emd = ot_emd.transform(Xs=X1) +Image_emd = minmax(mat2im(transp_Xs_emd, I1.shape)) + +# SinkhornTransport +ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) +ot_sinkhorn.fit(Xs=Xs, Xt=Xt) +transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) +Image_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) + +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(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(Xs=X1) # use the estimated mapping +Image_mapping_gaussian = minmax(mat2im(X1tn, I1.shape)) + + +############################################################################## +# plot original 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() + + +############################################################################## +# plot pixel values distribution +############################################################################## + +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() + + +############################################################################## +# plot transformed images +############################################################################## + +pl.figure(2, figsize=(10, 5)) + +pl.subplot(2, 3, 1) +pl.imshow(I1) +pl.axis('off') +pl.title('Im. 1') + +pl.subplot(2, 3, 4) +pl.imshow(I2) +pl.axis('off') +pl.title('Im. 2') + +pl.subplot(2, 3, 2) +pl.imshow(Image_emd) +pl.axis('off') +pl.title('EmdTransport') + +pl.subplot(2, 3, 5) +pl.imshow(Image_sinkhorn) +pl.axis('off') +pl.title('SinkhornTransport') + +pl.subplot(2, 3, 3) +pl.imshow(Image_mapping_linear) +pl.axis('off') +pl.title('MappingTransport (linear)') + +pl.subplot(2, 3, 6) +pl.imshow(Image_mapping_gaussian) +pl.axis('off') +pl.title('MappingTransport (gaussian)') +pl.tight_layout() + +pl.show() diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.rst b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst new file mode 100644 index 0000000..1107067 --- /dev/null +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst @@ -0,0 +1,302 @@ + + +.. _sphx_glr_auto_examples_plot_otda_mapping_colors_images.py: + + +==================================================================================== +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. + + + + +.. code-block:: python + + + # Authors: Remi Flamary + # Stanislas Chambon + # + # License: MIT License + + import numpy as np + from scipy import ndimage + import matplotlib.pylab as pl + import ot + + r = np.random.RandomState(42) + + + 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) + + + + + + + + +Generate data +############################################################################# + + + +.. code-block:: python + + + # Loading images + I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256 + I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256 + + + X1 = im2mat(I1) + X2 = im2mat(I2) + + # training samples + nb = 1000 + idx1 = r.randint(X1.shape[0], size=(nb,)) + idx2 = r.randint(X2.shape[0], size=(nb,)) + + Xs = X1[idx1, :] + Xt = X2[idx2, :] + + + + + + + + +Domain adaptation for pixel distribution transfer +############################################################################# + + + +.. code-block:: python + + + # EMDTransport + ot_emd = ot.da.EMDTransport() + ot_emd.fit(Xs=Xs, Xt=Xt) + transp_Xs_emd = ot_emd.transform(Xs=X1) + Image_emd = minmax(mat2im(transp_Xs_emd, I1.shape)) + + # SinkhornTransport + ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) + ot_sinkhorn.fit(Xs=Xs, Xt=Xt) + transp_Xs_sinkhorn = ot_emd.transform(Xs=X1) + Image_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) + + 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(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(Xs=X1) # use the estimated mapping + Image_mapping_gaussian = minmax(mat2im(X1tn, I1.shape)) + + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + It. |Loss |Delta loss + -------------------------------- + 0|3.680514e+02|0.000000e+00 + 1|3.592359e+02|-2.395185e-02 + 2|3.590581e+02|-4.947749e-04 + 3|3.589663e+02|-2.556471e-04 + 4|3.589095e+02|-1.582289e-04 + 5|3.588707e+02|-1.081994e-04 + 6|3.588423e+02|-7.911661e-05 + 7|3.588206e+02|-6.055473e-05 + 8|3.588034e+02|-4.778202e-05 + 9|3.587895e+02|-3.886420e-05 + 10|3.587781e+02|-3.182249e-05 + 11|3.587684e+02|-2.695669e-05 + 12|3.587602e+02|-2.298642e-05 + 13|3.587530e+02|-1.993240e-05 + 14|3.587468e+02|-1.736014e-05 + 15|3.587413e+02|-1.518037e-05 + 16|3.587365e+02|-1.358038e-05 + 17|3.587321e+02|-1.215346e-05 + 18|3.587282e+02|-1.091639e-05 + 19|3.587278e+02|-9.877929e-07 + It. |Loss |Delta loss + -------------------------------- + 0|3.784725e+02|0.000000e+00 + 1|3.646380e+02|-3.655332e-02 + 2|3.642858e+02|-9.660434e-04 + 3|3.641516e+02|-3.683776e-04 + 4|3.640785e+02|-2.008220e-04 + 5|3.640320e+02|-1.276966e-04 + 6|3.639999e+02|-8.796173e-05 + 7|3.639764e+02|-6.455658e-05 + 8|3.639583e+02|-4.976436e-05 + 9|3.639440e+02|-3.946556e-05 + 10|3.639322e+02|-3.222132e-05 + + +plot original images +############################################################################# + + + +.. code-block:: python + + + 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:: /auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_001.png + :align: center + + + + +plot pixel values distribution +############################################################################# + + + +.. code-block:: python + + + 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() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_003.png + :align: center + + + + +plot transformed images +############################################################################# + + + +.. code-block:: python + + + pl.figure(2, figsize=(10, 5)) + + pl.subplot(2, 3, 1) + pl.imshow(I1) + pl.axis('off') + pl.title('Im. 1') + + pl.subplot(2, 3, 4) + pl.imshow(I2) + pl.axis('off') + pl.title('Im. 2') + + pl.subplot(2, 3, 2) + pl.imshow(Image_emd) + pl.axis('off') + pl.title('EmdTransport') + + pl.subplot(2, 3, 5) + pl.imshow(Image_sinkhorn) + pl.axis('off') + pl.title('SinkhornTransport') + + pl.subplot(2, 3, 3) + pl.imshow(Image_mapping_linear) + pl.axis('off') + pl.title('MappingTransport (linear)') + + pl.subplot(2, 3, 6) + pl.imshow(Image_mapping_gaussian) + pl.axis('off') + pl.title('MappingTransport (gaussian)') + pl.tight_layout() + + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_otda_mapping_colors_images_004.png + :align: center + + + + +**Total running time of the script:** ( 2 minutes 45.618 seconds) + + + +.. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_otda_mapping_colors_images.py ` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_otda_mapping_colors_images.ipynb ` + +.. rst-class:: sphx-glr-signature + + `Generated by Sphinx-Gallery `_ -- cgit v1.2.3