diff options
author | tvayer <titouan.vayer@gmail.com> | 2019-05-29 14:24:05 +0200 |
---|---|---|
committer | tvayer <titouan.vayer@gmail.com> | 2019-05-29 14:24:05 +0200 |
commit | 63bbeb34e48f02c97a762dab5232158d90a5cffc (patch) | |
tree | 853026b5854b6e4b01fdf750db139985b3dd596f /docs/source | |
parent | f70aabfcc11f92181e0dc987b341bad8ec030d75 (diff) | |
parent | f66ab58c7c895011fd37bafd3e848828399c56c4 (diff) |
Merge remote-tracking branch 'rflamary/master'
merge pot
Diffstat (limited to 'docs/source')
58 files changed, 2641 insertions, 302 deletions
diff --git a/docs/source/all.rst b/docs/source/all.rst index bbb9833..32930fd 100644 --- a/docs/source/all.rst +++ b/docs/source/all.rst @@ -19,7 +19,7 @@ ot.bregman .. automodule:: ot.bregman :members: - + ot.smooth ----- .. automodule:: ot.smooth @@ -43,6 +43,12 @@ ot.da .. automodule:: ot.da :members: + +ot.gpu +-------- + +.. automodule:: ot.gpu + :members: ot.dr -------- @@ -68,3 +74,9 @@ ot.plot .. automodule:: ot.plot :members: + +ot.stochastic +------------- + +.. automodule:: ot.stochastic + :members: diff --git a/docs/source/auto_examples/auto_examples_jupyter.zip b/docs/source/auto_examples/auto_examples_jupyter.zip Binary files differindex 8102274..88e1e9b 100644 --- a/docs/source/auto_examples/auto_examples_jupyter.zip +++ b/docs/source/auto_examples/auto_examples_jupyter.zip diff --git a/docs/source/auto_examples/auto_examples_python.zip b/docs/source/auto_examples/auto_examples_python.zip Binary files differindex d685070..120a586 100644 --- a/docs/source/auto_examples/auto_examples_python.zip +++ b/docs/source/auto_examples/auto_examples_python.zip diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png Binary files differnew file mode 100644 index 0000000..6e74d89 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png Binary files differnew file mode 100644 index 0000000..0407e44 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png Binary files differnew file mode 100644 index 0000000..4421bc7 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png Binary files differnew file mode 100644 index 0000000..52638e3 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png Binary files differnew file mode 100644 index 0000000..c5078cf --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png Binary files differnew file mode 100644 index 0000000..58e87b6 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png Binary files differindex 81cee52..d8db85e 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_003.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_005.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_005.png Binary files differindex eac9230..81cee52 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_005.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_005.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_006.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_006.png Binary files differindex 2e29ff9..bfa0873 100644 --- a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_006.png +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_1D_006.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_003.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_003.png Binary files differnew file mode 100644 index 0000000..eb04b1a --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_003.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_004.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_004.png Binary files differnew file mode 100644 index 0000000..a9f44ba --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_004.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_006.png b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_006.png Binary files differnew file mode 100644 index 0000000..e53928e --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_barycenter_lp_vs_entropic_006.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png b/docs/source/auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png Binary files differnew file mode 100644 index 0000000..14a72a3 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png b/docs/source/auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png Binary files differnew file mode 100644 index 0000000..d7bc78a --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png 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 Binary files differindex 95f882a..7de991a 100644 --- 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 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 Binary files differindex aa1a5d3..aac929b 100644 --- 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 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 Binary files differindex d219bb3..5b8101b 100644 --- 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 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 Binary files differindex 33134fc..d77e68a 100644 --- 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 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 Binary files differindex 42197e3..1199903 100644 --- 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 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 Binary files differindex d9101da..1c73e43 100644 --- 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 diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_004.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_004.png Binary files differnew file mode 100644 index 0000000..8aada91 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_004.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_005.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_005.png Binary files differnew file mode 100644 index 0000000..42e5007 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_005.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_006.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_006.png Binary files differnew file mode 100644 index 0000000..335ea95 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_006.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png Binary files differnew file mode 100644 index 0000000..cda643b --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_007.png diff --git a/docs/source/auto_examples/images/sphx_glr_plot_stochastic_008.png b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_008.png Binary files differnew file mode 100644 index 0000000..335ea95 --- /dev/null +++ b/docs/source/auto_examples/images/sphx_glr_plot_stochastic_008.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png Binary files differnew file mode 100644 index 0000000..4679eb6 --- /dev/null +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png Binary files differnew file mode 100644 index 0000000..af8aad2 --- /dev/null +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png Binary files differnew file mode 100644 index 0000000..0861d4d --- /dev/null +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png 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 Binary files differindex a919055..4d90437 100644 --- 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 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 Binary files differindex f7fd217..61a5137 100644 --- 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 diff --git a/docs/source/auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png b/docs/source/auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png Binary files differnew file mode 100644 index 0000000..609339d --- /dev/null +++ b/docs/source/auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png diff --git a/docs/source/auto_examples/index.rst b/docs/source/auto_examples/index.rst index 69fb320..17a9710 100644 --- a/docs/source/auto_examples/index.rst +++ b/docs/source/auto_examples/index.rst @@ -49,6 +49,46 @@ This is a gallery of all the POT example files. .. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="Illustration of 2D Wasserstein barycenters if discributions that are weighted sum of diracs."> + +.. only:: html + + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_free_support_barycenter_thumb.png + + :ref:`sphx_glr_auto_examples_plot_free_support_barycenter.py` + +.. raw:: html + + </div> + + +.. toctree:: + :hidden: + + /auto_examples/plot_free_support_barycenter + +.. raw:: html + + <div class="sphx-glr-thumbcontainer" tooltip="This example illustrates the computation of EMD, Sinkhorn and smooth OT plans and their visuali..."> + +.. only:: html + + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_OT_1D_smooth_thumb.png + + :ref:`sphx_glr_auto_examples_plot_OT_1D_smooth.py` + +.. raw:: html + + </div> + + +.. toctree:: + :hidden: + + /auto_examples/plot_OT_1D_smooth + +.. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="This example is designed to show how to use the Gromov-Wassertsein distance computation in POT...."> .. only:: html @@ -109,6 +149,26 @@ This is a gallery of all the POT example files. .. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="This example is designed to illustrate how the Convolutional Wasserstein Barycenter function of..."> + +.. only:: html + + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png + + :ref:`sphx_glr_auto_examples_plot_convolutional_barycenter.py` + +.. raw:: html + + </div> + + +.. toctree:: + :hidden: + + /auto_examples/plot_convolutional_barycenter + +.. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip=" "> .. only:: html @@ -149,13 +209,13 @@ This is a gallery of all the POT example files. .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="This example presents a way of transferring colors between two image with Optimal Transport as ..."> + <div class="sphx-glr-thumbcontainer" tooltip="This example is designed to show how to use the stochatic optimization algorithms for descrete ..."> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_stochastic_thumb.png - :ref:`sphx_glr_auto_examples_plot_otda_color_images.py` + :ref:`sphx_glr_auto_examples_plot_stochastic.py` .. raw:: html @@ -165,17 +225,17 @@ This is a gallery of all the POT example files. .. toctree:: :hidden: - /auto_examples/plot_otda_color_images + /auto_examples/plot_stochastic .. raw:: html - <div class="sphx-glr-thumbcontainer" tooltip="OT for domain adaptation with image color adaptation [6] with mapping estimation [8]."> + <div class="sphx-glr-thumbcontainer" tooltip="This example presents a way of transferring colors between two images with Optimal Transport as..."> .. only:: html - .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_color_images_thumb.png - :ref:`sphx_glr_auto_examples_plot_otda_mapping_colors_images.py` + :ref:`sphx_glr_auto_examples_plot_otda_color_images.py` .. raw:: html @@ -185,7 +245,7 @@ This is a gallery of all the POT example files. .. toctree:: :hidden: - /auto_examples/plot_otda_mapping_colors_images + /auto_examples/plot_otda_color_images .. raw:: html @@ -209,6 +269,26 @@ This is a gallery of all the POT example files. .. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="OT for domain adaptation with image color adaptation [6] with mapping estimation [8]."> + +.. only:: html + + .. figure:: /auto_examples/images/thumb/sphx_glr_plot_otda_mapping_colors_images_thumb.png + + :ref:`sphx_glr_auto_examples_plot_otda_mapping_colors_images.py` + +.. raw:: html + + </div> + + +.. toctree:: + :hidden: + + /auto_examples/plot_otda_mapping_colors_images + +.. raw:: html + <div class="sphx-glr-thumbcontainer" tooltip="This example presents how to use MappingTransport to estimate at the same time both the couplin..."> .. only:: html diff --git a/docs/source/auto_examples/plot_OT_1D_smooth.ipynb b/docs/source/auto_examples/plot_OT_1D_smooth.ipynb new file mode 100644 index 0000000..d523f6a --- /dev/null +++ b/docs/source/auto_examples/plot_OT_1D_smooth.ipynb @@ -0,0 +1,144 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n# 1D smooth optimal transport\n\n\nThis example illustrates the computation of EMD, Sinkhorn and smooth OT plans\nand their visualization.\n\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\nimport ot.plot\nfrom ot.datasets import make_1D_gauss as gauss" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate data\n-------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% parameters\n\nn = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na = gauss(n, m=20, s=5) # m= mean, s= std\nb = gauss(n, m=60, s=10)\n\n# loss matrix\nM = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)))\nM /= M.max()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot distributions and loss matrix\n----------------------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% plot the distributions\n\npl.figure(1, figsize=(6.4, 3))\npl.plot(x, a, 'b', label='Source distribution')\npl.plot(x, b, 'r', label='Target distribution')\npl.legend()\n\n#%% plot distributions and loss matrix\n\npl.figure(2, figsize=(5, 5))\not.plot.plot1D_mat(a, b, M, 'Cost matrix M')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Solve EMD\n---------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% EMD\n\nG0 = ot.emd(a, b, M)\n\npl.figure(3, figsize=(5, 5))\not.plot.plot1D_mat(a, b, G0, 'OT matrix G0')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Solve Sinkhorn\n--------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% Sinkhorn\n\nlambd = 2e-3\nGs = ot.sinkhorn(a, b, M, lambd, verbose=True)\n\npl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn')\n\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Solve Smooth OT\n--------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% Smooth OT with KL regularization\n\nlambd = 2e-3\nGsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='kl')\n\npl.figure(5, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT KL reg.')\n\npl.show()\n\n\n#%% Smooth OT with KL regularization\n\nlambd = 1e-1\nGsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='l2')\n\npl.figure(6, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT l2 reg.')\n\npl.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}
\ No newline at end of file diff --git a/docs/source/auto_examples/plot_OT_1D_smooth.py b/docs/source/auto_examples/plot_OT_1D_smooth.py new file mode 100644 index 0000000..b690751 --- /dev/null +++ b/docs/source/auto_examples/plot_OT_1D_smooth.py @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- +""" +=========================== +1D smooth optimal transport +=========================== + +This example illustrates the computation of EMD, Sinkhorn and smooth OT plans +and their visualization. + +""" + +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + +import numpy as np +import matplotlib.pylab as pl +import ot +import ot.plot +from ot.datasets import make_1D_gauss as gauss + +############################################################################## +# Generate data +# ------------- + + +#%% parameters + +n = 100 # nb bins + +# bin positions +x = np.arange(n, dtype=np.float64) + +# Gaussian distributions +a = gauss(n, m=20, s=5) # m= mean, s= std +b = gauss(n, m=60, s=10) + +# loss matrix +M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) +M /= M.max() + + +############################################################################## +# Plot distributions and loss matrix +# ---------------------------------- + +#%% plot the distributions + +pl.figure(1, figsize=(6.4, 3)) +pl.plot(x, a, 'b', label='Source distribution') +pl.plot(x, b, 'r', label='Target distribution') +pl.legend() + +#%% plot distributions and loss matrix + +pl.figure(2, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') + +############################################################################## +# Solve EMD +# --------- + + +#%% EMD + +G0 = ot.emd(a, b, M) + +pl.figure(3, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') + +############################################################################## +# Solve Sinkhorn +# -------------- + + +#%% Sinkhorn + +lambd = 2e-3 +Gs = ot.sinkhorn(a, b, M, lambd, verbose=True) + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn') + +pl.show() + +############################################################################## +# Solve Smooth OT +# -------------- + + +#%% Smooth OT with KL regularization + +lambd = 2e-3 +Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='kl') + +pl.figure(5, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT KL reg.') + +pl.show() + + +#%% Smooth OT with KL regularization + +lambd = 1e-1 +Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='l2') + +pl.figure(6, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT l2 reg.') + +pl.show() diff --git a/docs/source/auto_examples/plot_OT_1D_smooth.rst b/docs/source/auto_examples/plot_OT_1D_smooth.rst new file mode 100644 index 0000000..5a0ebd3 --- /dev/null +++ b/docs/source/auto_examples/plot_OT_1D_smooth.rst @@ -0,0 +1,242 @@ + + +.. _sphx_glr_auto_examples_plot_OT_1D_smooth.py: + + +=========================== +1D smooth optimal transport +=========================== + +This example illustrates the computation of EMD, Sinkhorn and smooth OT plans +and their visualization. + + + + +.. code-block:: python + + + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License + + import numpy as np + import matplotlib.pylab as pl + import ot + import ot.plot + from ot.datasets import make_1D_gauss as gauss + + + + + + + +Generate data +------------- + + + +.. code-block:: python + + + + #%% parameters + + n = 100 # nb bins + + # bin positions + x = np.arange(n, dtype=np.float64) + + # Gaussian distributions + a = gauss(n, m=20, s=5) # m= mean, s= std + b = gauss(n, m=60, s=10) + + # loss matrix + M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) + M /= M.max() + + + + + + + + +Plot distributions and loss matrix +---------------------------------- + + + +.. code-block:: python + + + #%% plot the distributions + + pl.figure(1, figsize=(6.4, 3)) + pl.plot(x, a, 'b', label='Source distribution') + pl.plot(x, b, 'r', label='Target distribution') + pl.legend() + + #%% plot distributions and loss matrix + + pl.figure(2, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') + + + + +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_001.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_002.png + :scale: 47 + + + + +Solve EMD +--------- + + + +.. code-block:: python + + + + #%% EMD + + G0 = ot.emd(a, b, M) + + pl.figure(3, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') + + + + +.. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_005.png + :align: center + + + + +Solve Sinkhorn +-------------- + + + +.. code-block:: python + + + + #%% Sinkhorn + + lambd = 2e-3 + Gs = ot.sinkhorn(a, b, M, lambd, verbose=True) + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn') + + pl.show() + + + + +.. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_007.png + :align: center + + +.. rst-class:: sphx-glr-script-out + + Out:: + + It. |Err + ------------------- + 0|7.958844e-02| + 10|5.921715e-03| + 20|1.238266e-04| + 30|2.469780e-06| + 40|4.919966e-08| + 50|9.800197e-10| + + +Solve Smooth OT +-------------- + + + +.. code-block:: python + + + + #%% Smooth OT with KL regularization + + lambd = 2e-3 + Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='kl') + + pl.figure(5, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT KL reg.') + + pl.show() + + + #%% Smooth OT with KL regularization + + lambd = 1e-1 + Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='l2') + + pl.figure(6, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT l2 reg.') + + pl.show() + + + +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_009.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_OT_1D_smooth_010.png + :scale: 47 + + + + +**Total running time of the script:** ( 0 minutes 1.053 seconds) + + + +.. only :: html + + .. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_OT_1D_smooth.py <plot_OT_1D_smooth.py>` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_OT_1D_smooth.ipynb <plot_OT_1D_smooth.ipynb>` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ diff --git a/docs/source/auto_examples/plot_barycenter_1D.ipynb b/docs/source/auto_examples/plot_barycenter_1D.ipynb index 5866088..fc60e1f 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.ipynb +++ b/docs/source/auto_examples/plot_barycenter_1D.ipynb @@ -26,7 +26,79 @@ }, "outputs": [], "source": [ - "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\n# necessary for 3d plot even if not used\nfrom mpl_toolkits.mplot3d import Axes3D # noqa\nfrom matplotlib.collections import PolyCollection\n\n#\n# Generate data\n# -------------\n\n#%% parameters\n\nn = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std\na2 = ot.datasets.make_1D_gauss(n, m=60, s=8)\n\n# creating matrix A containing all distributions\nA = np.vstack((a1, a2)).T\nn_distributions = A.shape[1]\n\n# loss matrix + normalization\nM = ot.utils.dist0(n)\nM /= M.max()\n\n#\n# Plot data\n# ---------\n\n#%% plot the distributions\n\npl.figure(1, figsize=(6.4, 3))\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\npl.tight_layout()\n\n#\n# Barycenter computation\n# ----------------------\n\n#%% barycenter computation\n\nalpha = 0.2 # 0<=alpha<=1\nweights = np.array([1 - alpha, alpha])\n\n# l2bary\nbary_l2 = A.dot(weights)\n\n# wasserstein\nreg = 1e-3\nbary_wass = ot.bregman.barycenter(A, M, reg, weights)\n\npl.figure(2)\npl.clf()\npl.subplot(2, 1, 1)\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\n\npl.subplot(2, 1, 2)\npl.plot(x, bary_l2, 'r', label='l2')\npl.plot(x, bary_wass, 'g', label='Wasserstein')\npl.legend()\npl.title('Barycenters')\npl.tight_layout()\n\n#\n# Barycentric interpolation\n# -------------------------\n\n#%% barycenter interpolation\n\nn_alpha = 11\nalpha_list = np.linspace(0, 1, n_alpha)\n\n\nB_l2 = np.zeros((n, n_alpha))\n\nB_wass = np.copy(B_l2)\n\nfor i in range(0, n_alpha):\n alpha = alpha_list[i]\n weights = np.array([1 - alpha, alpha])\n B_l2[:, i] = A.dot(weights)\n B_wass[:, i] = ot.bregman.barycenter(A, M, reg, weights)\n\n#%% plot interpolation\n\npl.figure(3)\n\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_l2[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with l2')\npl.tight_layout()\n\npl.figure(4)\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_wass[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with Wasserstein')\npl.tight_layout()\n\npl.show()" + "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\n# necessary for 3d plot even if not used\nfrom mpl_toolkits.mplot3d import Axes3D # noqa\nfrom matplotlib.collections import PolyCollection" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate data\n-------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% parameters\n\nn = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std\na2 = ot.datasets.make_1D_gauss(n, m=60, s=8)\n\n# creating matrix A containing all distributions\nA = np.vstack((a1, a2)).T\nn_distributions = A.shape[1]\n\n# loss matrix + normalization\nM = ot.utils.dist0(n)\nM /= M.max()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot data\n---------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% plot the distributions\n\npl.figure(1, figsize=(6.4, 3))\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\npl.tight_layout()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Barycenter computation\n----------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% barycenter computation\n\nalpha = 0.2 # 0<=alpha<=1\nweights = np.array([1 - alpha, alpha])\n\n# l2bary\nbary_l2 = A.dot(weights)\n\n# wasserstein\nreg = 1e-3\nbary_wass = ot.bregman.barycenter(A, M, reg, weights)\n\npl.figure(2)\npl.clf()\npl.subplot(2, 1, 1)\nfor i in range(n_distributions):\n pl.plot(x, A[:, i])\npl.title('Distributions')\n\npl.subplot(2, 1, 2)\npl.plot(x, bary_l2, 'r', label='l2')\npl.plot(x, bary_wass, 'g', label='Wasserstein')\npl.legend()\npl.title('Barycenters')\npl.tight_layout()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Barycentric interpolation\n-------------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#%% barycenter interpolation\n\nn_alpha = 11\nalpha_list = np.linspace(0, 1, n_alpha)\n\n\nB_l2 = np.zeros((n, n_alpha))\n\nB_wass = np.copy(B_l2)\n\nfor i in range(0, n_alpha):\n alpha = alpha_list[i]\n weights = np.array([1 - alpha, alpha])\n B_l2[:, i] = A.dot(weights)\n B_wass[:, i] = ot.bregman.barycenter(A, M, reg, weights)\n\n#%% plot interpolation\n\npl.figure(3)\n\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_l2[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with l2')\npl.tight_layout()\n\npl.figure(4)\ncmap = pl.cm.get_cmap('viridis')\nverts = []\nzs = alpha_list\nfor i, z in enumerate(zs):\n ys = B_wass[:, i]\n verts.append(list(zip(x, ys)))\n\nax = pl.gcf().gca(projection='3d')\n\npoly = PolyCollection(verts, facecolors=[cmap(a) for a in alpha_list])\npoly.set_alpha(0.7)\nax.add_collection3d(poly, zs=zs, zdir='y')\nax.set_xlabel('x')\nax.set_xlim3d(0, n)\nax.set_ylabel('$\\\\alpha$')\nax.set_ylim3d(0, 1)\nax.set_zlabel('')\nax.set_zlim3d(0, B_l2.max() * 1.01)\npl.title('Barycenter interpolation with Wasserstein')\npl.tight_layout()\n\npl.show()" ] } ], diff --git a/docs/source/auto_examples/plot_barycenter_1D.py b/docs/source/auto_examples/plot_barycenter_1D.py index 5ed9f3f..6864301 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.py +++ b/docs/source/auto_examples/plot_barycenter_1D.py @@ -25,7 +25,7 @@ import ot from mpl_toolkits.mplot3d import Axes3D # noqa from matplotlib.collections import PolyCollection -# +############################################################################## # Generate data # ------------- @@ -48,7 +48,7 @@ n_distributions = A.shape[1] M = ot.utils.dist0(n) M /= M.max() -# +############################################################################## # Plot data # --------- @@ -60,7 +60,7 @@ for i in range(n_distributions): pl.title('Distributions') pl.tight_layout() -# +############################################################################## # Barycenter computation # ---------------------- @@ -90,7 +90,7 @@ pl.legend() pl.title('Barycenters') pl.tight_layout() -# +############################################################################## # Barycentric interpolation # ------------------------- diff --git a/docs/source/auto_examples/plot_barycenter_1D.rst b/docs/source/auto_examples/plot_barycenter_1D.rst index b314dc1..66ac042 100644 --- a/docs/source/auto_examples/plot_barycenter_1D.rst +++ b/docs/source/auto_examples/plot_barycenter_1D.rst @@ -18,52 +18,34 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. +.. code-block:: python -.. rst-class:: sphx-glr-horizontal - - - * - .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_001.png - :scale: 47 + # Author: Remi Flamary <remi.flamary@unice.fr> + # + # License: MIT License - * + import numpy as np + import matplotlib.pylab as pl + import ot + # necessary for 3d plot even if not used + from mpl_toolkits.mplot3d import Axes3D # noqa + from matplotlib.collections import PolyCollection - .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_002.png - :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_003.png - :scale: 47 - * - .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_004.png - :scale: 47 +Generate data +------------- .. code-block:: python - # Author: Remi Flamary <remi.flamary@unice.fr> - # - # License: MIT License - - import numpy as np - import matplotlib.pylab as pl - import ot - # necessary for 3d plot even if not used - from mpl_toolkits.mplot3d import Axes3D # noqa - from matplotlib.collections import PolyCollection - - # - # Generate data - # ------------- - #%% parameters n = 100 # nb bins @@ -83,9 +65,19 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. M = ot.utils.dist0(n) M /= M.max() - # - # Plot data - # --------- + + + + + + +Plot data +--------- + + + +.. code-block:: python + #%% plot the distributions @@ -95,9 +87,22 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. pl.title('Distributions') pl.tight_layout() - # - # Barycenter computation - # ---------------------- + + + +.. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_001.png + :align: center + + + + +Barycenter computation +---------------------- + + + +.. code-block:: python + #%% barycenter computation @@ -125,9 +130,22 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. pl.title('Barycenters') pl.tight_layout() - # - # Barycentric interpolation - # ------------------------- + + + +.. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_003.png + :align: center + + + + +Barycentric interpolation +------------------------- + + + +.. code-block:: python + #%% barycenter interpolation @@ -194,7 +212,25 @@ SIAM Journal on Scientific Computing, 37(2), A1111-A1138. pl.show() -**Total running time of the script:** ( 0 minutes 0.363 seconds) + + +.. rst-class:: sphx-glr-horizontal + + + * + + .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_005.png + :scale: 47 + + * + + .. image:: /auto_examples/images/sphx_glr_plot_barycenter_1D_006.png + :scale: 47 + + + + +**Total running time of the script:** ( 0 minutes 0.413 seconds) diff --git a/docs/source/auto_examples/plot_convolutional_barycenter.ipynb b/docs/source/auto_examples/plot_convolutional_barycenter.ipynb new file mode 100644 index 0000000..4981ba3 --- /dev/null +++ b/docs/source/auto_examples/plot_convolutional_barycenter.ipynb @@ -0,0 +1,90 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n# Convolutional Wasserstein Barycenter example\n\n\nThis example is designed to illustrate how the Convolutional Wasserstein Barycenter\nfunction of POT works.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Author: Nicolas Courty <ncourty@irisa.fr>\n#\n# License: MIT License\n\n\nimport numpy as np\nimport pylab as pl\nimport ot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Data preparation\n----------------\n\nThe four distributions are constructed from 4 simple images\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "f1 = 1 - pl.imread('../data/redcross.png')[:, :, 2]\nf2 = 1 - pl.imread('../data/duck.png')[:, :, 2]\nf3 = 1 - pl.imread('../data/heart.png')[:, :, 2]\nf4 = 1 - pl.imread('../data/tooth.png')[:, :, 2]\n\nA = []\nf1 = f1 / np.sum(f1)\nf2 = f2 / np.sum(f2)\nf3 = f3 / np.sum(f3)\nf4 = f4 / np.sum(f4)\nA.append(f1)\nA.append(f2)\nA.append(f3)\nA.append(f4)\nA = np.array(A)\n\nnb_images = 5\n\n# those are the four corners coordinates that will be interpolated by bilinear\n# interpolation\nv1 = np.array((1, 0, 0, 0))\nv2 = np.array((0, 1, 0, 0))\nv3 = np.array((0, 0, 1, 0))\nv4 = np.array((0, 0, 0, 1))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Barycenter computation and visualization\n----------------------------------------\n\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(figsize=(10, 10))\npl.title('Convolutional Wasserstein Barycenters in POT')\ncm = 'Blues'\n# regularization parameter\nreg = 0.004\nfor i in range(nb_images):\n for j in range(nb_images):\n pl.subplot(nb_images, nb_images, i * nb_images + j + 1)\n tx = float(i) / (nb_images - 1)\n ty = float(j) / (nb_images - 1)\n\n # weights are constructed by bilinear interpolation\n tmp1 = (1 - tx) * v1 + tx * v2\n tmp2 = (1 - tx) * v3 + tx * v4\n weights = (1 - ty) * tmp1 + ty * tmp2\n\n if i == 0 and j == 0:\n pl.imshow(f1, cmap=cm)\n pl.axis('off')\n elif i == 0 and j == (nb_images - 1):\n pl.imshow(f3, cmap=cm)\n pl.axis('off')\n elif i == (nb_images - 1) and j == 0:\n pl.imshow(f2, cmap=cm)\n pl.axis('off')\n elif i == (nb_images - 1) and j == (nb_images - 1):\n pl.imshow(f4, cmap=cm)\n pl.axis('off')\n else:\n # call to barycenter computation\n pl.imshow(ot.bregman.convolutional_barycenter2d(A, reg, weights), cmap=cm)\n pl.axis('off')\npl.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}
\ No newline at end of file diff --git a/docs/source/auto_examples/plot_convolutional_barycenter.py b/docs/source/auto_examples/plot_convolutional_barycenter.py new file mode 100644 index 0000000..e74db04 --- /dev/null +++ b/docs/source/auto_examples/plot_convolutional_barycenter.py @@ -0,0 +1,92 @@ + +#%% +# -*- coding: utf-8 -*- +""" +============================================ +Convolutional Wasserstein Barycenter example +============================================ + +This example is designed to illustrate how the Convolutional Wasserstein Barycenter +function of POT works. +""" + +# Author: Nicolas Courty <ncourty@irisa.fr> +# +# License: MIT License + + +import numpy as np +import pylab as pl +import ot + +############################################################################## +# Data preparation +# ---------------- +# +# The four distributions are constructed from 4 simple images + + +f1 = 1 - pl.imread('../data/redcross.png')[:, :, 2] +f2 = 1 - pl.imread('../data/duck.png')[:, :, 2] +f3 = 1 - pl.imread('../data/heart.png')[:, :, 2] +f4 = 1 - pl.imread('../data/tooth.png')[:, :, 2] + +A = [] +f1 = f1 / np.sum(f1) +f2 = f2 / np.sum(f2) +f3 = f3 / np.sum(f3) +f4 = f4 / np.sum(f4) +A.append(f1) +A.append(f2) +A.append(f3) +A.append(f4) +A = np.array(A) + +nb_images = 5 + +# those are the four corners coordinates that will be interpolated by bilinear +# interpolation +v1 = np.array((1, 0, 0, 0)) +v2 = np.array((0, 1, 0, 0)) +v3 = np.array((0, 0, 1, 0)) +v4 = np.array((0, 0, 0, 1)) + + +############################################################################## +# Barycenter computation and visualization +# ---------------------------------------- +# + +pl.figure(figsize=(10, 10)) +pl.title('Convolutional Wasserstein Barycenters in POT') +cm = 'Blues' +# regularization parameter +reg = 0.004 +for i in range(nb_images): + for j in range(nb_images): + pl.subplot(nb_images, nb_images, i * nb_images + j + 1) + tx = float(i) / (nb_images - 1) + ty = float(j) / (nb_images - 1) + + # weights are constructed by bilinear interpolation + tmp1 = (1 - tx) * v1 + tx * v2 + tmp2 = (1 - tx) * v3 + tx * v4 + weights = (1 - ty) * tmp1 + ty * tmp2 + + if i == 0 and j == 0: + pl.imshow(f1, cmap=cm) + pl.axis('off') + elif i == 0 and j == (nb_images - 1): + pl.imshow(f3, cmap=cm) + pl.axis('off') + elif i == (nb_images - 1) and j == 0: + pl.imshow(f2, cmap=cm) + pl.axis('off') + elif i == (nb_images - 1) and j == (nb_images - 1): + pl.imshow(f4, cmap=cm) + pl.axis('off') + else: + # call to barycenter computation + pl.imshow(ot.bregman.convolutional_barycenter2d(A, reg, weights), cmap=cm) + pl.axis('off') +pl.show() diff --git a/docs/source/auto_examples/plot_convolutional_barycenter.rst b/docs/source/auto_examples/plot_convolutional_barycenter.rst new file mode 100644 index 0000000..a28db2f --- /dev/null +++ b/docs/source/auto_examples/plot_convolutional_barycenter.rst @@ -0,0 +1,151 @@ + + +.. _sphx_glr_auto_examples_plot_convolutional_barycenter.py: + + +============================================ +Convolutional Wasserstein Barycenter example +============================================ + +This example is designed to illustrate how the Convolutional Wasserstein Barycenter +function of POT works. + + + +.. code-block:: python + + + # Author: Nicolas Courty <ncourty@irisa.fr> + # + # License: MIT License + + + import numpy as np + import pylab as pl + import ot + + + + + + + +Data preparation +---------------- + +The four distributions are constructed from 4 simple images + + + +.. code-block:: python + + + + f1 = 1 - pl.imread('../data/redcross.png')[:, :, 2] + f2 = 1 - pl.imread('../data/duck.png')[:, :, 2] + f3 = 1 - pl.imread('../data/heart.png')[:, :, 2] + f4 = 1 - pl.imread('../data/tooth.png')[:, :, 2] + + A = [] + f1 = f1 / np.sum(f1) + f2 = f2 / np.sum(f2) + f3 = f3 / np.sum(f3) + f4 = f4 / np.sum(f4) + A.append(f1) + A.append(f2) + A.append(f3) + A.append(f4) + A = np.array(A) + + nb_images = 5 + + # those are the four corners coordinates that will be interpolated by bilinear + # interpolation + v1 = np.array((1, 0, 0, 0)) + v2 = np.array((0, 1, 0, 0)) + v3 = np.array((0, 0, 1, 0)) + v4 = np.array((0, 0, 0, 1)) + + + + + + + + +Barycenter computation and visualization +---------------------------------------- + + + + +.. code-block:: python + + + pl.figure(figsize=(10, 10)) + pl.title('Convolutional Wasserstein Barycenters in POT') + cm = 'Blues' + # regularization parameter + reg = 0.004 + for i in range(nb_images): + for j in range(nb_images): + pl.subplot(nb_images, nb_images, i * nb_images + j + 1) + tx = float(i) / (nb_images - 1) + ty = float(j) / (nb_images - 1) + + # weights are constructed by bilinear interpolation + tmp1 = (1 - tx) * v1 + tx * v2 + tmp2 = (1 - tx) * v3 + tx * v4 + weights = (1 - ty) * tmp1 + ty * tmp2 + + if i == 0 and j == 0: + pl.imshow(f1, cmap=cm) + pl.axis('off') + elif i == 0 and j == (nb_images - 1): + pl.imshow(f3, cmap=cm) + pl.axis('off') + elif i == (nb_images - 1) and j == 0: + pl.imshow(f2, cmap=cm) + pl.axis('off') + elif i == (nb_images - 1) and j == (nb_images - 1): + pl.imshow(f4, cmap=cm) + pl.axis('off') + else: + # call to barycenter computation + pl.imshow(ot.bregman.convolutional_barycenter2d(A, reg, weights), cmap=cm) + pl.axis('off') + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png + :align: center + + + + +**Total running time of the script:** ( 1 minutes 11.608 seconds) + + + +.. only :: html + + .. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_convolutional_barycenter.py <plot_convolutional_barycenter.py>` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_convolutional_barycenter.ipynb <plot_convolutional_barycenter.ipynb>` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ diff --git a/docs/source/auto_examples/plot_free_support_barycenter.ipynb b/docs/source/auto_examples/plot_free_support_barycenter.ipynb new file mode 100644 index 0000000..05a81c8 --- /dev/null +++ b/docs/source/auto_examples/plot_free_support_barycenter.ipynb @@ -0,0 +1,108 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n# 2D free support Wasserstein barycenters of distributions\n\n\nIllustration of 2D Wasserstein barycenters if discributions that are weighted\nsum of diracs.\n\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Author: Vivien Seguy <vivien.seguy@iip.ist.i.kyoto-u.ac.jp>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate data\n -------------\n%% parameters and data generation\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "N = 3\nd = 2\nmeasures_locations = []\nmeasures_weights = []\n\nfor i in range(N):\n\n n_i = np.random.randint(low=1, high=20) # nb samples\n\n mu_i = np.random.normal(0., 4., (d,)) # Gaussian mean\n\n A_i = np.random.rand(d, d)\n cov_i = np.dot(A_i, A_i.transpose()) # Gaussian covariance matrix\n\n x_i = ot.datasets.make_2D_samples_gauss(n_i, mu_i, cov_i) # Dirac locations\n b_i = np.random.uniform(0., 1., (n_i,))\n b_i = b_i / np.sum(b_i) # Dirac weights\n\n measures_locations.append(x_i)\n measures_weights.append(b_i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compute free support barycenter\n-------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "k = 10 # number of Diracs of the barycenter\nX_init = np.random.normal(0., 1., (k, d)) # initial Dirac locations\nb = np.ones((k,)) / k # weights of the barycenter (it will not be optimized, only the locations are optimized)\n\nX = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot data\n---------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(1)\nfor (x_i, b_i) in zip(measures_locations, measures_weights):\n color = np.random.randint(low=1, high=10 * N)\n pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure')\npl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter')\npl.title('Data measures and their barycenter')\npl.legend(loc=0)\npl.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}
\ No newline at end of file diff --git a/docs/source/auto_examples/plot_free_support_barycenter.py b/docs/source/auto_examples/plot_free_support_barycenter.py new file mode 100644 index 0000000..b6efc59 --- /dev/null +++ b/docs/source/auto_examples/plot_free_support_barycenter.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +""" +==================================================== +2D free support Wasserstein barycenters of distributions +==================================================== + +Illustration of 2D Wasserstein barycenters if discributions that are weighted +sum of diracs. + +""" + +# Author: Vivien Seguy <vivien.seguy@iip.ist.i.kyoto-u.ac.jp> +# +# License: MIT License + +import numpy as np +import matplotlib.pylab as pl +import ot + + +############################################################################## +# Generate data +# ------------- +#%% parameters and data generation +N = 3 +d = 2 +measures_locations = [] +measures_weights = [] + +for i in range(N): + + n_i = np.random.randint(low=1, high=20) # nb samples + + mu_i = np.random.normal(0., 4., (d,)) # Gaussian mean + + A_i = np.random.rand(d, d) + cov_i = np.dot(A_i, A_i.transpose()) # Gaussian covariance matrix + + x_i = ot.datasets.make_2D_samples_gauss(n_i, mu_i, cov_i) # Dirac locations + b_i = np.random.uniform(0., 1., (n_i,)) + b_i = b_i / np.sum(b_i) # Dirac weights + + measures_locations.append(x_i) + measures_weights.append(b_i) + + +############################################################################## +# Compute free support barycenter +# ------------- + +k = 10 # number of Diracs of the barycenter +X_init = np.random.normal(0., 1., (k, d)) # initial Dirac locations +b = np.ones((k,)) / k # weights of the barycenter (it will not be optimized, only the locations are optimized) + +X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b) + + +############################################################################## +# Plot data +# --------- + +pl.figure(1) +for (x_i, b_i) in zip(measures_locations, measures_weights): + color = np.random.randint(low=1, high=10 * N) + pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure') +pl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter') +pl.title('Data measures and their barycenter') +pl.legend(loc=0) +pl.show() diff --git a/docs/source/auto_examples/plot_free_support_barycenter.rst b/docs/source/auto_examples/plot_free_support_barycenter.rst new file mode 100644 index 0000000..d1b3b80 --- /dev/null +++ b/docs/source/auto_examples/plot_free_support_barycenter.rst @@ -0,0 +1,140 @@ + + +.. _sphx_glr_auto_examples_plot_free_support_barycenter.py: + + +==================================================== +2D free support Wasserstein barycenters of distributions +==================================================== + +Illustration of 2D Wasserstein barycenters if discributions that are weighted +sum of diracs. + + + + +.. code-block:: python + + + # Author: Vivien Seguy <vivien.seguy@iip.ist.i.kyoto-u.ac.jp> + # + # License: MIT License + + import numpy as np + import matplotlib.pylab as pl + import ot + + + + + + + + +Generate data + ------------- +%% parameters and data generation + + + +.. code-block:: python + + N = 3 + d = 2 + measures_locations = [] + measures_weights = [] + + for i in range(N): + + n_i = np.random.randint(low=1, high=20) # nb samples + + mu_i = np.random.normal(0., 4., (d,)) # Gaussian mean + + A_i = np.random.rand(d, d) + cov_i = np.dot(A_i, A_i.transpose()) # Gaussian covariance matrix + + x_i = ot.datasets.make_2D_samples_gauss(n_i, mu_i, cov_i) # Dirac locations + b_i = np.random.uniform(0., 1., (n_i,)) + b_i = b_i / np.sum(b_i) # Dirac weights + + measures_locations.append(x_i) + measures_weights.append(b_i) + + + + + + + + +Compute free support barycenter +------------- + + + +.. code-block:: python + + + k = 10 # number of Diracs of the barycenter + X_init = np.random.normal(0., 1., (k, d)) # initial Dirac locations + b = np.ones((k,)) / k # weights of the barycenter (it will not be optimized, only the locations are optimized) + + X = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b) + + + + + + + + +Plot data +--------- + + + +.. code-block:: python + + + pl.figure(1) + for (x_i, b_i) in zip(measures_locations, measures_weights): + color = np.random.randint(low=1, high=10 * N) + pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure') + pl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter') + pl.title('Data measures and their barycenter') + pl.legend(loc=0) + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_free_support_barycenter_001.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 0.129 seconds) + + + +.. only :: html + + .. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_free_support_barycenter.py <plot_free_support_barycenter.py>` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_free_support_barycenter.ipynb <plot_free_support_barycenter.ipynb>` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ diff --git a/docs/source/auto_examples/plot_otda_color_images.ipynb b/docs/source/auto_examples/plot_otda_color_images.ipynb index 2daf406..103bdec 100644 --- a/docs/source/auto_examples/plot_otda_color_images.ipynb +++ b/docs/source/auto_examples/plot_otda_color_images.ipynb @@ -1,144 +1,144 @@ { - "nbformat_minor": 0, - "nbformat": 4, "cells": [ { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, - { + }, + "outputs": [], "source": [ - "\n# OT for image color adaptation\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": {} - }, + "%matplotlib inline" + ] + }, { - "execution_count": null, - "cell_type": "code", + "cell_type": "markdown", + "metadata": {}, "source": [ - "# Authors: Remi Flamary <remi.flamary@unice.fr>\n# Stanislas Chambon <stan.chambon@gmail.com>\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": [], + "\n# OT for image color adaptation\n\n\nThis example presents a way of transferring colors between two images\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": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "# Authors: Remi Flamary <remi.flamary@unice.fr>\n# Stanislas Chambon <stan.chambon@gmail.com>\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 an 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)" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "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, :]" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "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')" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "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()" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "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_sinkhorn.transform(Xs=X1)\ntransp_Xt_sinkhorn = ot_sinkhorn.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))" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } + }, + "outputs": [], + "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()" + ] } - ], + ], "metadata": { "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, "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" - } + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.7" } - } + }, + "nbformat": 4, + "nbformat_minor": 0 }
\ 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 index e77aec0..62383a2 100644 --- a/docs/source/auto_examples/plot_otda_color_images.py +++ b/docs/source/auto_examples/plot_otda_color_images.py @@ -4,7 +4,7 @@ OT for image color adaptation ============================= -This example presents a way of transferring colors between two image +This example presents a way of transferring colors between two images with Optimal Transport as introduced in [6] [6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). @@ -27,7 +27,7 @@ r = np.random.RandomState(42) def im2mat(I): - """Converts and image to matrix (one pixel per line)""" + """Converts an image to matrix (one pixel per line)""" return I.reshape((I.shape[0] * I.shape[1], I.shape[2])) @@ -115,8 +115,8 @@ ot_sinkhorn.fit(Xs=Xs, Xt=Xt) 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) +transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1) +transp_Xt_sinkhorn = ot_sinkhorn.inverse_transform(Xt=X2) I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) diff --git a/docs/source/auto_examples/plot_otda_color_images.rst b/docs/source/auto_examples/plot_otda_color_images.rst index 9c31ba7..ab0406e 100644 --- a/docs/source/auto_examples/plot_otda_color_images.rst +++ b/docs/source/auto_examples/plot_otda_color_images.rst @@ -7,7 +7,7 @@ OT for image color adaptation ============================= -This example presents a way of transferring colors between two image +This example presents a way of transferring colors between two images with Optimal Transport as introduced in [6] [6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). @@ -34,7 +34,7 @@ SIAM Journal on Imaging Sciences, 7(3), 1853-1882. def im2mat(I): - """Converts and image to matrix (one pixel per line)""" + """Converts an image to matrix (one pixel per line)""" return I.reshape((I.shape[0] * I.shape[1], I.shape[2])) @@ -168,8 +168,8 @@ Instantiate the different transport algorithms and fit them 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) + transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1) + transp_Xt_sinkhorn = ot_sinkhorn.inverse_transform(Xt=X2) I1t = minmax(mat2im(transp_Xs_emd, I1.shape)) I2t = minmax(mat2im(transp_Xt_emd, I2.shape)) @@ -235,11 +235,13 @@ Plot new images -**Total running time of the script:** ( 3 minutes 16.469 seconds) +**Total running time of the script:** ( 3 minutes 55.541 seconds) -.. container:: sphx-glr-footer +.. only :: html + + .. container:: sphx-glr-footer .. container:: sphx-glr-download @@ -252,6 +254,9 @@ Plot new images :download:`Download Jupyter notebook: plot_otda_color_images.ipynb <plot_otda_color_images.ipynb>` -.. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery <http://sphinx-gallery.readthedocs.io>`_ +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb index 56caa8a..baffef4 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.ipynb @@ -1,144 +1,144 @@ { - "nbformat_minor": 0, - "nbformat": 4, "cells": [ { - "execution_count": null, - "cell_type": "code", - "source": [ - "%matplotlib inline" - ], - "outputs": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "source": [ "\n# OT for image color adaptation with mapping estimation\n\n\nOT for domain adaptation with image color adaptation [6] with mapping\nestimation [8].\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 <remi.flamary@unice.fr>\n# Stanislas Chambon <stan.chambon@gmail.com>\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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "source": [ + "# Authors: Remi Flamary <remi.flamary@unice.fr>\n# Stanislas Chambon <stan.chambon@gmail.com>\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)" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "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, :]" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "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_sinkhorn.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))" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "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()" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } - }, + }, + "outputs": [], + "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()" + ] + }, { + "cell_type": "markdown", + "metadata": {}, "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": [], + "cell_type": "code", + "execution_count": null, "metadata": { "collapsed": false - } + }, + "outputs": [], + "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()" + ] } - ], + ], "metadata": { "kernelspec": { - "display_name": "Python 2", - "name": "python2", - "language": "python" - }, + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, "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" - } + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.7" } - } + }, + "nbformat": 4, + "nbformat_minor": 0 }
\ 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 index 5f1e844..a20eca8 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.py +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.py @@ -77,7 +77,7 @@ 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) +transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1) Image_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) ot_mapping_linear = ot.da.MappingTransport( diff --git a/docs/source/auto_examples/plot_otda_mapping_colors_images.rst b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst index 8394fb0..2afdc8a 100644 --- a/docs/source/auto_examples/plot_otda_mapping_colors_images.rst +++ b/docs/source/auto_examples/plot_otda_mapping_colors_images.rst @@ -104,7 +104,7 @@ Domain adaptation for pixel distribution transfer # 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) + transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1) Image_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) ot_mapping_linear = ot.da.MappingTransport( @@ -132,39 +132,39 @@ Domain adaptation for pixel distribution transfer It. |Loss |Delta loss -------------------------------- - 0|3.680518e+02|0.000000e+00 - 1|3.592439e+02|-2.393116e-02 - 2|3.590632e+02|-5.030248e-04 - 3|3.589698e+02|-2.601358e-04 - 4|3.589118e+02|-1.614977e-04 - 5|3.588724e+02|-1.097608e-04 - 6|3.588436e+02|-8.035205e-05 - 7|3.588215e+02|-6.141923e-05 - 8|3.588042e+02|-4.832627e-05 - 9|3.587902e+02|-3.909574e-05 - 10|3.587786e+02|-3.225418e-05 - 11|3.587688e+02|-2.712592e-05 - 12|3.587605e+02|-2.314041e-05 - 13|3.587534e+02|-1.991287e-05 - 14|3.587471e+02|-1.744348e-05 - 15|3.587416e+02|-1.544523e-05 - 16|3.587367e+02|-1.364654e-05 - 17|3.587323e+02|-1.230435e-05 - 18|3.587284e+02|-1.093370e-05 - 19|3.587276e+02|-2.052728e-06 + 0|3.680534e+02|0.000000e+00 + 1|3.592501e+02|-2.391854e-02 + 2|3.590682e+02|-5.061555e-04 + 3|3.589745e+02|-2.610227e-04 + 4|3.589167e+02|-1.611644e-04 + 5|3.588768e+02|-1.109242e-04 + 6|3.588482e+02|-7.972733e-05 + 7|3.588261e+02|-6.166174e-05 + 8|3.588086e+02|-4.871697e-05 + 9|3.587946e+02|-3.919056e-05 + 10|3.587830e+02|-3.228124e-05 + 11|3.587731e+02|-2.744744e-05 + 12|3.587648e+02|-2.334451e-05 + 13|3.587576e+02|-1.995629e-05 + 14|3.587513e+02|-1.761058e-05 + 15|3.587457e+02|-1.542568e-05 + 16|3.587408e+02|-1.366315e-05 + 17|3.587365e+02|-1.221732e-05 + 18|3.587325e+02|-1.102488e-05 + 19|3.587303e+02|-6.062107e-06 It. |Loss |Delta loss -------------------------------- - 0|3.784758e+02|0.000000e+00 - 1|3.646352e+02|-3.656911e-02 - 2|3.642861e+02|-9.574714e-04 - 3|3.641523e+02|-3.672061e-04 - 4|3.640788e+02|-2.020990e-04 - 5|3.640321e+02|-1.282701e-04 - 6|3.640002e+02|-8.751240e-05 - 7|3.639765e+02|-6.521203e-05 - 8|3.639582e+02|-5.007767e-05 - 9|3.639439e+02|-3.938917e-05 - 10|3.639323e+02|-3.187865e-05 + 0|3.784871e+02|0.000000e+00 + 1|3.646491e+02|-3.656142e-02 + 2|3.642975e+02|-9.642655e-04 + 3|3.641626e+02|-3.702413e-04 + 4|3.640888e+02|-2.026301e-04 + 5|3.640419e+02|-1.289607e-04 + 6|3.640097e+02|-8.831646e-05 + 7|3.639861e+02|-6.487612e-05 + 8|3.639679e+02|-4.994063e-05 + 9|3.639536e+02|-3.941436e-05 + 10|3.639419e+02|-3.209753e-05 Plot original images @@ -283,11 +283,13 @@ Plot transformed images -**Total running time of the script:** ( 2 minutes 52.212 seconds) +**Total running time of the script:** ( 3 minutes 14.206 seconds) -.. container:: sphx-glr-footer +.. only :: html + + .. container:: sphx-glr-footer .. container:: sphx-glr-download @@ -300,6 +302,9 @@ Plot transformed images :download:`Download Jupyter notebook: plot_otda_mapping_colors_images.ipynb <plot_otda_mapping_colors_images.ipynb>` -.. rst-class:: sphx-glr-signature - `Generated by Sphinx-Gallery <http://sphinx-gallery.readthedocs.io>`_ +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ diff --git a/docs/source/auto_examples/plot_stochastic.ipynb b/docs/source/auto_examples/plot_stochastic.ipynb new file mode 100644 index 0000000..7f6ff3d --- /dev/null +++ b/docs/source/auto_examples/plot_stochastic.ipynb @@ -0,0 +1,295 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n# Stochastic examples\n\n\nThis example is designed to show how to use the stochatic optimization\nalgorithms for descrete and semicontinous measures from the POT library.\n\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Author: Kilian Fatras <kilian.fatras@gmail.com>\n#\n# License: MIT License\n\nimport matplotlib.pylab as pl\nimport numpy as np\nimport ot\nimport ot.plot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM\n############################################################################\n############################################################################\n DISCRETE CASE:\n\n Sample two discrete measures for the discrete case\n ---------------------------------------------\n\n Define 2 discrete measures a and b, the points where are defined the source\n and the target measures and finally the cost matrix c.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "n_source = 7\nn_target = 4\nreg = 1\nnumItermax = 1000\n\na = ot.utils.unif(n_source)\nb = ot.utils.unif(n_target)\n\nrng = np.random.RandomState(0)\nX_source = rng.randn(n_source, 2)\nY_target = rng.randn(n_target, 2)\nM = ot.dist(X_source, Y_target)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Call the \"SAG\" method to find the transportation matrix in the discrete case\n---------------------------------------------\n\nDefine the method \"SAG\", call ot.solve_semi_dual_entropic and plot the\nresults.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "method = \"SAG\"\nsag_pi = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method,\n numItermax)\nprint(sag_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "SEMICONTINOUS CASE:\n\nSample one general measure a, one discrete measures b for the semicontinous\ncase\n---------------------------------------------\n\nDefine one general measure a, one discrete measures b, the points where\nare defined the source and the target measures and finally the cost matrix c.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "n_source = 7\nn_target = 4\nreg = 1\nnumItermax = 1000\nlog = True\n\na = ot.utils.unif(n_source)\nb = ot.utils.unif(n_target)\n\nrng = np.random.RandomState(0)\nX_source = rng.randn(n_source, 2)\nY_target = rng.randn(n_target, 2)\nM = ot.dist(X_source, Y_target)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Call the \"ASGD\" method to find the transportation matrix in the semicontinous\ncase\n---------------------------------------------\n\nDefine the method \"ASGD\", call ot.solve_semi_dual_entropic and plot the\nresults.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "method = \"ASGD\"\nasgd_pi, log_asgd = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method,\n numItermax, log=log)\nprint(log_asgd['alpha'], log_asgd['beta'])\nprint(asgd_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compare the results with the Sinkhorn algorithm\n---------------------------------------------\n\nCall the Sinkhorn algorithm from POT\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "sinkhorn_pi = ot.sinkhorn(a, b, M, reg)\nprint(sinkhorn_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "PLOT TRANSPORTATION MATRIX\n#############################################################################\n\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot SAG results\n----------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, sag_pi, 'semi-dual : OT matrix SAG')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot ASGD results\n-----------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, asgd_pi, 'semi-dual : OT matrix ASGD')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot Sinkhorn results\n---------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM\n############################################################################\n############################################################################\n SEMICONTINOUS CASE:\n\n Sample one general measure a, one discrete measures b for the semicontinous\n case\n ---------------------------------------------\n\n Define one general measure a, one discrete measures b, the points where\n are defined the source and the target measures and finally the cost matrix c.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "n_source = 7\nn_target = 4\nreg = 1\nnumItermax = 100000\nlr = 0.1\nbatch_size = 3\nlog = True\n\na = ot.utils.unif(n_source)\nb = ot.utils.unif(n_target)\n\nrng = np.random.RandomState(0)\nX_source = rng.randn(n_source, 2)\nY_target = rng.randn(n_target, 2)\nM = ot.dist(X_source, Y_target)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Call the \"SGD\" dual method to find the transportation matrix in the\nsemicontinous case\n---------------------------------------------\n\nCall ot.solve_dual_entropic and plot the results.\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "sgd_dual_pi, log_sgd = ot.stochastic.solve_dual_entropic(a, b, M, reg,\n batch_size, numItermax,\n lr, log=log)\nprint(log_sgd['alpha'], log_sgd['beta'])\nprint(sgd_dual_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compare the results with the Sinkhorn algorithm\n---------------------------------------------\n\nCall the Sinkhorn algorithm from POT\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "sinkhorn_pi = ot.sinkhorn(a, b, M, reg)\nprint(sinkhorn_pi)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot SGD results\n-----------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, sgd_dual_pi, 'dual : OT matrix SGD')\npl.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Plot Sinkhorn results\n---------------------\n\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn')\npl.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.7" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}
\ No newline at end of file diff --git a/docs/source/auto_examples/plot_stochastic.py b/docs/source/auto_examples/plot_stochastic.py new file mode 100644 index 0000000..742f8d9 --- /dev/null +++ b/docs/source/auto_examples/plot_stochastic.py @@ -0,0 +1,208 @@ +""" +========================== +Stochastic examples +========================== + +This example is designed to show how to use the stochatic optimization +algorithms for descrete and semicontinous measures from the POT library. + +""" + +# Author: Kilian Fatras <kilian.fatras@gmail.com> +# +# License: MIT License + +import matplotlib.pylab as pl +import numpy as np +import ot +import ot.plot + + +############################################################################# +# COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM +############################################################################# +############################################################################# +# DISCRETE CASE: +# +# Sample two discrete measures for the discrete case +# --------------------------------------------- +# +# Define 2 discrete measures a and b, the points where are defined the source +# and the target measures and finally the cost matrix c. + +n_source = 7 +n_target = 4 +reg = 1 +numItermax = 1000 + +a = ot.utils.unif(n_source) +b = ot.utils.unif(n_target) + +rng = np.random.RandomState(0) +X_source = rng.randn(n_source, 2) +Y_target = rng.randn(n_target, 2) +M = ot.dist(X_source, Y_target) + +############################################################################# +# +# Call the "SAG" method to find the transportation matrix in the discrete case +# --------------------------------------------- +# +# Define the method "SAG", call ot.solve_semi_dual_entropic and plot the +# results. + +method = "SAG" +sag_pi = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method, + numItermax) +print(sag_pi) + +############################################################################# +# SEMICONTINOUS CASE: +# +# Sample one general measure a, one discrete measures b for the semicontinous +# case +# --------------------------------------------- +# +# Define one general measure a, one discrete measures b, the points where +# are defined the source and the target measures and finally the cost matrix c. + +n_source = 7 +n_target = 4 +reg = 1 +numItermax = 1000 +log = True + +a = ot.utils.unif(n_source) +b = ot.utils.unif(n_target) + +rng = np.random.RandomState(0) +X_source = rng.randn(n_source, 2) +Y_target = rng.randn(n_target, 2) +M = ot.dist(X_source, Y_target) + +############################################################################# +# +# Call the "ASGD" method to find the transportation matrix in the semicontinous +# case +# --------------------------------------------- +# +# Define the method "ASGD", call ot.solve_semi_dual_entropic and plot the +# results. + +method = "ASGD" +asgd_pi, log_asgd = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method, + numItermax, log=log) +print(log_asgd['alpha'], log_asgd['beta']) +print(asgd_pi) + +############################################################################# +# +# Compare the results with the Sinkhorn algorithm +# --------------------------------------------- +# +# Call the Sinkhorn algorithm from POT + +sinkhorn_pi = ot.sinkhorn(a, b, M, reg) +print(sinkhorn_pi) + + +############################################################################## +# PLOT TRANSPORTATION MATRIX +############################################################################## + +############################################################################## +# Plot SAG results +# ---------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, sag_pi, 'semi-dual : OT matrix SAG') +pl.show() + + +############################################################################## +# Plot ASGD results +# ----------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, asgd_pi, 'semi-dual : OT matrix ASGD') +pl.show() + + +############################################################################## +# Plot Sinkhorn results +# --------------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn') +pl.show() + + +############################################################################# +# COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM +############################################################################# +############################################################################# +# SEMICONTINOUS CASE: +# +# Sample one general measure a, one discrete measures b for the semicontinous +# case +# --------------------------------------------- +# +# Define one general measure a, one discrete measures b, the points where +# are defined the source and the target measures and finally the cost matrix c. + +n_source = 7 +n_target = 4 +reg = 1 +numItermax = 100000 +lr = 0.1 +batch_size = 3 +log = True + +a = ot.utils.unif(n_source) +b = ot.utils.unif(n_target) + +rng = np.random.RandomState(0) +X_source = rng.randn(n_source, 2) +Y_target = rng.randn(n_target, 2) +M = ot.dist(X_source, Y_target) + +############################################################################# +# +# Call the "SGD" dual method to find the transportation matrix in the +# semicontinous case +# --------------------------------------------- +# +# Call ot.solve_dual_entropic and plot the results. + +sgd_dual_pi, log_sgd = ot.stochastic.solve_dual_entropic(a, b, M, reg, + batch_size, numItermax, + lr, log=log) +print(log_sgd['alpha'], log_sgd['beta']) +print(sgd_dual_pi) + +############################################################################# +# +# Compare the results with the Sinkhorn algorithm +# --------------------------------------------- +# +# Call the Sinkhorn algorithm from POT + +sinkhorn_pi = ot.sinkhorn(a, b, M, reg) +print(sinkhorn_pi) + +############################################################################## +# Plot SGD results +# ----------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, sgd_dual_pi, 'dual : OT matrix SGD') +pl.show() + + +############################################################################## +# Plot Sinkhorn results +# --------------------- + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn') +pl.show() diff --git a/docs/source/auto_examples/plot_stochastic.rst b/docs/source/auto_examples/plot_stochastic.rst new file mode 100644 index 0000000..d531045 --- /dev/null +++ b/docs/source/auto_examples/plot_stochastic.rst @@ -0,0 +1,446 @@ + + +.. _sphx_glr_auto_examples_plot_stochastic.py: + + +========================== +Stochastic examples +========================== + +This example is designed to show how to use the stochatic optimization +algorithms for descrete and semicontinous measures from the POT library. + + + + +.. code-block:: python + + + # Author: Kilian Fatras <kilian.fatras@gmail.com> + # + # License: MIT License + + import matplotlib.pylab as pl + import numpy as np + import ot + import ot.plot + + + + + + + + +COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM +############################################################################ +############################################################################ + DISCRETE CASE: + + Sample two discrete measures for the discrete case + --------------------------------------------- + + Define 2 discrete measures a and b, the points where are defined the source + and the target measures and finally the cost matrix c. + + + +.. code-block:: python + + + n_source = 7 + n_target = 4 + reg = 1 + numItermax = 1000 + + a = ot.utils.unif(n_source) + b = ot.utils.unif(n_target) + + rng = np.random.RandomState(0) + X_source = rng.randn(n_source, 2) + Y_target = rng.randn(n_target, 2) + M = ot.dist(X_source, Y_target) + + + + + + + +Call the "SAG" method to find the transportation matrix in the discrete case +--------------------------------------------- + +Define the method "SAG", call ot.solve_semi_dual_entropic and plot the +results. + + + +.. code-block:: python + + + method = "SAG" + sag_pi = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method, + numItermax) + print(sag_pi) + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [[2.55553509e-02 9.96395660e-02 1.76579142e-02 4.31178196e-06] + [1.21640234e-01 1.25357448e-02 1.30225078e-03 7.37891338e-03] + [3.56123975e-03 7.61451746e-02 6.31505947e-02 1.33831456e-07] + [2.61515202e-02 3.34246014e-02 8.28734709e-02 4.07550428e-04] + [9.85500870e-03 7.52288517e-04 1.08262628e-02 1.21423583e-01] + [2.16904253e-02 9.03825797e-04 1.87178503e-03 1.18391107e-01] + [4.15462212e-02 2.65987989e-02 7.23177216e-02 2.39440107e-03]] + + +SEMICONTINOUS CASE: + +Sample one general measure a, one discrete measures b for the semicontinous +case +--------------------------------------------- + +Define one general measure a, one discrete measures b, the points where +are defined the source and the target measures and finally the cost matrix c. + + + +.. code-block:: python + + + n_source = 7 + n_target = 4 + reg = 1 + numItermax = 1000 + log = True + + a = ot.utils.unif(n_source) + b = ot.utils.unif(n_target) + + rng = np.random.RandomState(0) + X_source = rng.randn(n_source, 2) + Y_target = rng.randn(n_target, 2) + M = ot.dist(X_source, Y_target) + + + + + + + +Call the "ASGD" method to find the transportation matrix in the semicontinous +case +--------------------------------------------- + +Define the method "ASGD", call ot.solve_semi_dual_entropic and plot the +results. + + + +.. code-block:: python + + + method = "ASGD" + asgd_pi, log_asgd = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method, + numItermax, log=log) + print(log_asgd['alpha'], log_asgd['beta']) + print(asgd_pi) + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [3.98220325 7.76235856 3.97645524 2.72051681 1.23219313 3.07696856 + 2.84476972] [-2.65544161 -2.50838395 -0.9397765 6.10360206] + [[2.34528761e-02 1.00491956e-01 1.89058354e-02 6.47543413e-06] + [1.16616747e-01 1.32074516e-02 1.45653361e-03 1.15764107e-02] + [3.16154850e-03 7.42892944e-02 6.54061055e-02 1.94426150e-07] + [2.33152216e-02 3.27486992e-02 8.61986263e-02 5.94595747e-04] + [6.34131496e-03 5.31975896e-04 8.12724003e-03 1.27856612e-01] + [1.41744829e-02 6.49096245e-04 1.42704389e-03 1.26606520e-01] + [3.73127657e-02 2.62526499e-02 7.57727161e-02 3.51901117e-03]] + + +Compare the results with the Sinkhorn algorithm +--------------------------------------------- + +Call the Sinkhorn algorithm from POT + + + +.. code-block:: python + + + sinkhorn_pi = ot.sinkhorn(a, b, M, reg) + print(sinkhorn_pi) + + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [[2.55535622e-02 9.96413843e-02 1.76578860e-02 4.31043335e-06] + [1.21640742e-01 1.25369034e-02 1.30234529e-03 7.37715259e-03] + [3.56096458e-03 7.61460101e-02 6.31500344e-02 1.33788624e-07] + [2.61499607e-02 3.34255577e-02 8.28741973e-02 4.07427179e-04] + [9.85698720e-03 7.52505948e-04 1.08291770e-02 1.21418473e-01] + [2.16947591e-02 9.04086158e-04 1.87228707e-03 1.18386011e-01] + [4.15442692e-02 2.65998963e-02 7.23192701e-02 2.39370724e-03]] + + +PLOT TRANSPORTATION MATRIX +############################################################################# + + +Plot SAG results +---------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, sag_pi, 'semi-dual : OT matrix SAG') + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_004.png + :align: center + + + + +Plot ASGD results +----------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, asgd_pi, 'semi-dual : OT matrix ASGD') + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_005.png + :align: center + + + + +Plot Sinkhorn results +--------------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn') + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_006.png + :align: center + + + + +COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM +############################################################################ +############################################################################ + SEMICONTINOUS CASE: + + Sample one general measure a, one discrete measures b for the semicontinous + case + --------------------------------------------- + + Define one general measure a, one discrete measures b, the points where + are defined the source and the target measures and finally the cost matrix c. + + + +.. code-block:: python + + + n_source = 7 + n_target = 4 + reg = 1 + numItermax = 100000 + lr = 0.1 + batch_size = 3 + log = True + + a = ot.utils.unif(n_source) + b = ot.utils.unif(n_target) + + rng = np.random.RandomState(0) + X_source = rng.randn(n_source, 2) + Y_target = rng.randn(n_target, 2) + M = ot.dist(X_source, Y_target) + + + + + + + +Call the "SGD" dual method to find the transportation matrix in the +semicontinous case +--------------------------------------------- + +Call ot.solve_dual_entropic and plot the results. + + + +.. code-block:: python + + + sgd_dual_pi, log_sgd = ot.stochastic.solve_dual_entropic(a, b, M, reg, + batch_size, numItermax, + lr, log=log) + print(log_sgd['alpha'], log_sgd['beta']) + print(sgd_dual_pi) + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [0.92449986 2.75486107 1.07923806 0.02741145 0.61355413 1.81961594 + 0.12072562] [0.33831611 0.46806842 1.5640451 4.96947652] + [[2.20001105e-02 9.26497883e-02 1.08654588e-02 9.78995555e-08] + [1.55669974e-02 1.73279561e-03 1.19120878e-04 2.49058251e-05] + [3.48198483e-03 8.04151063e-02 4.41335396e-02 3.45115752e-09] + [3.14927954e-02 4.34760520e-02 7.13338154e-02 1.29442395e-05] + [6.81836550e-02 5.62182457e-03 5.35386584e-02 2.21568095e-02] + [8.04671052e-02 3.62163462e-03 4.96331605e-03 1.15837801e-02] + [4.88644009e-02 3.37903481e-02 6.07955004e-02 7.42743505e-05]] + + +Compare the results with the Sinkhorn algorithm +--------------------------------------------- + +Call the Sinkhorn algorithm from POT + + + +.. code-block:: python + + + sinkhorn_pi = ot.sinkhorn(a, b, M, reg) + print(sinkhorn_pi) + + + + + +.. rst-class:: sphx-glr-script-out + + Out:: + + [[2.55535622e-02 9.96413843e-02 1.76578860e-02 4.31043335e-06] + [1.21640742e-01 1.25369034e-02 1.30234529e-03 7.37715259e-03] + [3.56096458e-03 7.61460101e-02 6.31500344e-02 1.33788624e-07] + [2.61499607e-02 3.34255577e-02 8.28741973e-02 4.07427179e-04] + [9.85698720e-03 7.52505948e-04 1.08291770e-02 1.21418473e-01] + [2.16947591e-02 9.04086158e-04 1.87228707e-03 1.18386011e-01] + [4.15442692e-02 2.65998963e-02 7.23192701e-02 2.39370724e-03]] + + +Plot SGD results +----------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, sgd_dual_pi, 'dual : OT matrix SGD') + pl.show() + + + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_007.png + :align: center + + + + +Plot Sinkhorn results +--------------------- + + + +.. code-block:: python + + + pl.figure(4, figsize=(5, 5)) + ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn') + pl.show() + + + +.. image:: /auto_examples/images/sphx_glr_plot_stochastic_008.png + :align: center + + + + +**Total running time of the script:** ( 0 minutes 20.889 seconds) + + + +.. only :: html + + .. container:: sphx-glr-footer + + + .. container:: sphx-glr-download + + :download:`Download Python source code: plot_stochastic.py <plot_stochastic.py>` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_stochastic.ipynb <plot_stochastic.ipynb>` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_ diff --git a/docs/source/conf.py b/docs/source/conf.py index 114245d..433eca6 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -31,7 +31,7 @@ class Mock(MagicMock): @classmethod def __getattr__(cls, name): return MagicMock() -MOCK_MODULES = ['ot.lp.emd_wrap','autograd','pymanopt','cudamat','autograd.numpy','pymanopt.manifolds','pymanopt.solvers'] +MOCK_MODULES = ['ot.lp.emd_wrap','autograd','pymanopt','cupy','autograd.numpy','pymanopt.manifolds','pymanopt.solvers'] # 'autograd.numpy','pymanopt.manifolds','pymanopt.solvers', sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES) # !!!! diff --git a/docs/source/readme.rst b/docs/source/readme.rst index 5d37f64..e7c2bd1 100644 --- a/docs/source/readme.rst +++ b/docs/source/readme.rst @@ -2,7 +2,7 @@ POT: Python Optimal Transport ============================= |PyPI version| |Anaconda Cloud| |Build Status| |Documentation Status| -|Anaconda downloads| |License| +|Downloads| |Anaconda downloads| |License| This open source Python library provide several solvers for optimization problems related to Optimal Transport for signal, image processing and @@ -13,13 +13,14 @@ It provides the following solvers: - OT Network Flow solver for the linear program/ Earth Movers Distance [1]. - Entropic regularization OT solver with Sinkhorn Knopp Algorithm [2] - and stabilized version [9][10] with optional GPU implementation - (requires cudamat). + and stabilized version [9][10] and greedy SInkhorn [22] with optional + GPU implementation (requires cudamat). - Smooth optimal transport solvers (dual and semi-dual) for KL and squared L2 regularizations [17]. - Non regularized Wasserstein barycenters [16] with LP solver (only small scale). -- Bregman projections for Wasserstein barycenter [3] and unmixing [4]. +- Bregman projections for Wasserstein barycenter [3], convolutional + barycenter [21] and unmixing [4]. - Optimal transport for domain adaptation with group lasso regularization [5] - Conditional gradient [6] and Generalized conditional gradient for @@ -29,6 +30,9 @@ It provides the following solvers: pymanopt). - Gromov-Wasserstein distances and barycenters ([13] and regularized [12]) +- Stochastic Optimization for Large-scale Optimal Transport (semi-dual + problem [18] and dual problem [19]) +- Non regularized free support Wasserstein barycenters [20]. Some demonstrations (both in Python and Jupyter Notebook format) are available in the examples folder. @@ -104,7 +108,7 @@ Dependencies Some sub-modules require additional dependences which are discussed below -- **ot.dr** (Wasserstein dimensionality rediuction) depends on autograd +- **ot.dr** (Wasserstein dimensionality reduction) depends on autograd and pymanopt that can be installed with: :: @@ -219,6 +223,9 @@ The contributors to this library are: - `Stanislas Chambon <https://slasnista.github.io/>`__ - `Antoine Rolet <https://arolet.github.io/>`__ - Erwan Vautier (Gromov-Wasserstein) +- `Kilian Fatras <https://kilianfatras.github.io/>`__ +- `Alain + Rakotomamonjy <https://sites.google.com/site/alainrakotomamonjy/home>`__ This toolbox benefit a lot from open source research and we would like to thank the following persons for providing some code (in various @@ -334,6 +341,31 @@ Optimal Transport <https://arxiv.org/abs/1710.06276>`__. Proceedings of the Twenty-First International Conference on Artificial Intelligence and Statistics (AISTATS). +[18] Genevay, A., Cuturi, M., Peyré, G. & Bach, F. (2016) `Stochastic +Optimization for Large-scale Optimal +Transport <https://arxiv.org/abs/1605.08527>`__. Advances in Neural +Information Processing Systems (2016). + +[19] Seguy, V., Bhushan Damodaran, B., Flamary, R., Courty, N., Rolet, +A.& Blondel, M. `Large-scale Optimal Transport and Mapping +Estimation <https://arxiv.org/pdf/1711.02283.pdf>`__. International +Conference on Learning Representation (2018) + +[20] Cuturi, M. and Doucet, A. (2014) `Fast Computation of Wasserstein +Barycenters <http://proceedings.mlr.press/v32/cuturi14.html>`__. +International Conference in Machine Learning + +[21] Solomon, J., De Goes, F., Peyré, G., Cuturi, M., Butscher, A., +Nguyen, A. & Guibas, L. (2015). `Convolutional wasserstein distances: +Efficient optimal transportation on geometric +domains <https://dl.acm.org/citation.cfm?id=2766963>`__. ACM +Transactions on Graphics (TOG), 34(4), 66. + +[22] J. Altschuler, J.Weed, P. Rigollet, (2017) `Near-linear time +approximation algorithms for optimal transport via Sinkhorn +iteration <https://papers.nips.cc/paper/6792-near-linear-time-approximation-algorithms-for-optimal-transport-via-sinkhorn-iteration.pdf>`__, +Advances in Neural Information Processing Systems (NIPS) 31 + .. |PyPI version| image:: https://badge.fury.io/py/POT.svg :target: https://badge.fury.io/py/POT .. |Anaconda Cloud| image:: https://anaconda.org/conda-forge/pot/badges/version.svg @@ -342,6 +374,8 @@ Statistics (AISTATS). :target: https://travis-ci.org/rflamary/POT .. |Documentation Status| image:: https://readthedocs.org/projects/pot/badge/?version=latest :target: http://pot.readthedocs.io/en/latest/?badge=latest +.. |Downloads| image:: https://pepy.tech/badge/pot + :target: https://pepy.tech/project/pot .. |Anaconda downloads| image:: https://anaconda.org/conda-forge/pot/badges/downloads.svg :target: https://anaconda.org/conda-forge/pot .. |License| image:: https://anaconda.org/conda-forge/pot/badges/license.svg |