| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
============================================ |
| 5 |
Convolutional Wasserstein Barycenter example |
| 6 |
============================================ |
| 7 |
|
| 8 |
This example is designed to illustrate how the Convolutional Wasserstein |
| 9 |
Barycenter function of POT works. |
| 10 |
""" |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import os |
| 16 |
from pathlib import Path |
| 17 |
|
| 18 |
import numpy as np |
| 19 |
import matplotlib.pyplot as plt |
| 20 |
import ot |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
this_file = os.path.realpath("__file__") |
| 29 |
data_path = os.path.join(Path(this_file).parent.parent.parent, "data") |
| 30 |
|
| 31 |
f1 = 1 - plt.imread(os.path.join(data_path, "redcross.png"))[::2, ::2, 2] |
| 32 |
f2 = 1 - plt.imread(os.path.join(data_path, "tooth.png"))[::2, ::2, 2] |
| 33 |
f3 = 1 - plt.imread(os.path.join(data_path, "heart.png"))[::2, ::2, 2] |
| 34 |
f4 = 1 - plt.imread(os.path.join(data_path, "duck.png"))[::2, ::2, 2] |
| 35 |
|
| 36 |
f1 = f1 / np.sum(f1) |
| 37 |
f2 = f2 / np.sum(f2) |
| 38 |
f3 = f3 / np.sum(f3) |
| 39 |
f4 = f4 / np.sum(f4) |
| 40 |
A = np.array([f1, f2, f3, f4]) |
| 41 |
|
| 42 |
nb_images = 5 |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
v1 = np.array((1, 0, 0, 0)) |
| 47 |
v2 = np.array((0, 1, 0, 0)) |
| 48 |
v3 = np.array((0, 0, 1, 0)) |
| 49 |
v4 = np.array((0, 0, 0, 1)) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
fig, axes = plt.subplots(nb_images, nb_images, figsize=(7, 7)) |
| 58 |
plt.suptitle("Convolutional Wasserstein Barycenters in POT") |
| 59 |
cm = "Blues" |
| 60 |
|
| 61 |
reg = 0.004 |
| 62 |
for i in range(nb_images): |
| 63 |
for j in range(nb_images): |
| 64 |
tx = float(i) / (nb_images - 1) |
| 65 |
ty = float(j) / (nb_images - 1) |
| 66 |
|
| 67 |
|
| 68 |
tmp1 = (1 - tx) * v1 + tx * v2 |
| 69 |
tmp2 = (1 - tx) * v3 + tx * v4 |
| 70 |
weights = (1 - ty) * tmp1 + ty * tmp2 |
| 71 |
|
| 72 |
if i == 0 and j == 0: |
| 73 |
axes[i, j].imshow(f1, cmap=cm) |
| 74 |
elif i == 0 and j == (nb_images - 1): |
| 75 |
axes[i, j].imshow(f3, cmap=cm) |
| 76 |
elif i == (nb_images - 1) and j == 0: |
| 77 |
axes[i, j].imshow(f2, cmap=cm) |
| 78 |
elif i == (nb_images - 1) and j == (nb_images - 1): |
| 79 |
axes[i, j].imshow(f4, cmap=cm) |
| 80 |
else: |
| 81 |
|
| 82 |
axes[i, j].imshow( |
| 83 |
ot.bregman.convolutional_barycenter2d(A, reg, weights), cmap=cm |
| 84 |
) |
| 85 |
axes[i, j].axis("off") |
| 86 |
plt.tight_layout() |
| 87 |
plt.show() |