python 工具箱
As a follow-up of my previous introductory article on optimal transport and a first part of this guide provided by Aurelie Boisbunon here, I will present below how you can solve different tasks with Optimal Transport (OT) in practice using the Python Optimal Transport (POT) toolbox.
作为后续我的最优运输之前的介绍性文章 ,并提供本指南的第一部分奥莱丽亚Boisbunon 在这里 ,我将介绍以下如何在实践中使用的解决最优传输(OT)不同任务的Python优化运输( POT)工具箱。
To start with, let us install POT using pip from the terminal by simply running
首先,让我们简单地从终端使用pip安装POT
pip3 install ot
And voilà! If everything went well, you now have POT installed and ready to use on your computer. Let me now explain how you can reproduce the results from my previous article.
和瞧! 如果一切顺利,那么您现在已经安装了POT,可以在计算机上使用了。 现在让我解释一下如何重现上一篇文章的结果。
颜色转移 (Color transfer)
In this application our goal is to transfer the color style of one image onto another image in the smoothest way possible. To do this, we will follow the example from the official webpage of the POT library and start by defining several supplementary functions needed when working with images:
在此应用程序中,我们的目标是以尽可能平滑的方式将一个图像的色彩样式转移到另一图像上。 为此,我们将遵循POT库官方网页上的示例 ,并先定义使用图像时所需的几个补充功能:
import numpy as np
import matplotlib.pylab as pl
import otr = np.random.RandomState(42)
def im2mat(img):
"""Converts an image to a matrix (one pixel per line)"""
return img.reshape((img.shape[0] * img.shape[1], img.shape[2]))
def mat2im(X, shape):
"""Converts a matrix back to an image"""
return X.reshape(shape)
So, first three lines here are just imports for numpy, matplotlib.pylab and ot packages. Then, we have two functions that allow us to convert an image represented by a 3d matrix (some people call them tensors) where the first dimension is the height of the image, the second one is its width, while the third is given by RGB coordinates of the pixels. Let’s now load some images to see what it means.
因此,这里的前三行只是numpy,matplotlib.pylab和ot软件包的导入。 然后,我们有两个函数可以转换由3d矩阵(有些人称为张量)表示的图像,其中第一维是图像的高度,第二维是图像的宽度,而第三维是RGB给定的像素的坐标。 现在,让我们加载一些图像以查看其含义。
I1 = pl.imread('../../data/ocean_day.jpg').astype(np.float64)/256I2 = pl.imread('../../data/ocean_sunset.jpg').astype(np.float64)/256
Here they are, the daytime ocea