Friday, November 17, 2023

Optimal Transport for Image Processing

Optimal Transport for Image Processing

Image processing: Transforming MNIST digits using optimal transport

The goal of this post is to create the following transformation which maps a number 2 to number 4 using Optimal Transport, where the two numbers are hand written digit images, see MNIST digit dataset. The Python code is available on my github.

Girl in a jacket

Here, we briefly recap the Optimal Transport, which aims to move (mass of) points from “source” to the (mass of) points in the “target”.

Mathematically, it is a linear programming problem:
minPRn×ni,jCijPijs.t.Pij0,i,j=1,...,njPij=ai,i=1,...,niPij=bj,j=1,...,n(OT)\begin{aligned} \min_{P \in \mathbb R^{n \times n}} \hspace{0.5cm} & \sum_{i,j} C_{ij}P_{ij}\\ s.t. \hspace{0.5cm} & P_{ij}\geq 0, \forall i, j =1,..., n\\ & \sum_j P_{ij} = a_i, \forall i=1,...,n\\ & \sum_i P_{ij} = b_j, \forall j=1,...,n \end{aligned}\tag{OT}

In this formulation, aa represents the vector of resources, bb is the vector of targets, and CijC_{ij} denotes the cost associated with moving a unit from resource aia_i to target bjb_j. The matrix PP is the solution, representing the plan, with PijP_{ij} indicating the quantity of product being transported from aia_i to bjb_j. In practical applications, the cost matrix CC is often constructed using squared distance:

Cij=xi(1)xj(2)22C_{ij} = ||x^{(1)}_i-x^{(2)}_j||_2^2

Here x(1)x^{(1)} and x(2)x^{(2)} (in Rn×2\mathbb R^{n\times 2}) denote the locations of resources aRna \in \mathbb R^n and targets bRnb\in \mathbb R^n, respectively.

For simplicity, we require that i=1nai=i=1nbi\sum_{i=1}^n a_i=\sum_{i=1}^n b_i.

Returning to the images labeled 2 and 4, we extract nn pixels from each image, assigning unit mass to each pixel. Subsequently, we apply Optimal Transport to determine the corresponding pairs (assuming that such pairing exists) and employ linear interpolation between these points. This process results in the creation of the GIF image showcased at the outset of this blog post.

Popular Posts