From ae1ede4ed31973213b5945721b7b9fe8e4992a1c Mon Sep 17 00:00:00 2001 From: RĂ©mi Flamary Date: Mon, 24 Sep 2018 11:00:14 +0200 Subject: update doc+notebooks for convolutional --- .../source/auto_examples/auto_examples_jupyter.zip | Bin 119618 -> 123577 bytes docs/source/auto_examples/auto_examples_python.zip | Bin 79365 -> 81978 bytes .../sphx_glr_plot_convolutional_barycenter_001.png | Bin 0 -> 319138 bytes ...phx_glr_plot_convolutional_barycenter_thumb.png | Bin 0 -> 54369 bytes docs/source/auto_examples/index.rst | 20 +++ .../plot_convolutional_barycenter.ipynb | 90 ++++++++++++ .../auto_examples/plot_convolutional_barycenter.py | 92 +++++++++++++ .../plot_convolutional_barycenter.rst | 151 +++++++++++++++++++++ 8 files changed, 353 insertions(+) create mode 100644 docs/source/auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png create mode 100644 docs/source/auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png create mode 100644 docs/source/auto_examples/plot_convolutional_barycenter.ipynb create mode 100644 docs/source/auto_examples/plot_convolutional_barycenter.py create mode 100644 docs/source/auto_examples/plot_convolutional_barycenter.rst (limited to 'docs/source') diff --git a/docs/source/auto_examples/auto_examples_jupyter.zip b/docs/source/auto_examples/auto_examples_jupyter.zip index c6a7e90..304bb06 100644 Binary files a/docs/source/auto_examples/auto_examples_jupyter.zip and b/docs/source/auto_examples/auto_examples_jupyter.zip differ diff --git a/docs/source/auto_examples/auto_examples_python.zip b/docs/source/auto_examples/auto_examples_python.zip index 28ff08e..3be8a76 100644 Binary files a/docs/source/auto_examples/auto_examples_python.zip and b/docs/source/auto_examples/auto_examples_python.zip differ 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 new file mode 100644 index 0000000..14a72a3 Binary files /dev/null and b/docs/source/auto_examples/images/sphx_glr_plot_convolutional_barycenter_001.png differ 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 new file mode 100644 index 0000000..af8aad2 Binary files /dev/null and b/docs/source/auto_examples/images/thumb/sphx_glr_plot_convolutional_barycenter_thumb.png differ diff --git a/docs/source/auto_examples/index.rst b/docs/source/auto_examples/index.rst index 5cbfba6..259fca1 100644 --- a/docs/source/auto_examples/index.rst +++ b/docs/source/auto_examples/index.rst @@ -147,6 +147,26 @@ This is a gallery of all the POT example files. /auto_examples/plot_compute_emd +.. raw:: html + +
+ +.. 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 + +
+ + +.. toctree:: + :hidden: + + /auto_examples/plot_convolutional_barycenter + .. raw:: html
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 \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 +# +# 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 + # + # 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 ` + + + + .. container:: sphx-glr-download + + :download:`Download Jupyter notebook: plot_convolutional_barycenter.ipynb ` + + +.. only:: html + + .. rst-class:: sphx-glr-signature + + `Gallery generated by Sphinx-Gallery `_ -- cgit v1.2.3