| 1 |
|
| 2 |
""" |
| 3 |
===================================================== |
| 4 |
OT for image color adaptation with mapping estimation |
| 5 |
===================================================== |
| 6 |
|
| 7 |
OT for domain adaptation with image color adaptation [6] with mapping |
| 8 |
estimation [8]. |
| 9 |
|
| 10 |
[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized |
| 11 |
discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882. |
| 12 |
|
| 13 |
[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for |
| 14 |
discrete optimal transport", Neural Information Processing Systems (NIPS), 2016. |
| 15 |
|
| 16 |
""" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import os |
| 25 |
from pathlib import Path |
| 26 |
|
| 27 |
import numpy as np |
| 28 |
from matplotlib import pyplot as plt |
| 29 |
import ot |
| 30 |
|
| 31 |
rng = np.random.RandomState(42) |
| 32 |
|
| 33 |
|
| 34 |
def im2mat(img): |
| 35 |
"""Converts and image to matrix (one pixel per line)""" |
| 36 |
return img.reshape((img.shape[0] * img.shape[1], img.shape[2])) |
| 37 |
|
| 38 |
|
| 39 |
def mat2im(X, shape): |
| 40 |
"""Converts back a matrix to an image""" |
| 41 |
return X.reshape(shape) |
| 42 |
|
| 43 |
|
| 44 |
def minmax(img): |
| 45 |
return np.clip(img, 0, 1) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
this_file = os.path.realpath("__file__") |
| 54 |
data_path = os.path.join(Path(this_file).parent.parent.parent, "data") |
| 55 |
|
| 56 |
I1 = plt.imread(os.path.join(data_path, "ocean_day.jpg")).astype(np.float64) / 256 |
| 57 |
I2 = plt.imread(os.path.join(data_path, "ocean_sunset.jpg")).astype(np.float64) / 256 |
| 58 |
|
| 59 |
X1 = im2mat(I1) |
| 60 |
X2 = im2mat(I2) |
| 61 |
|
| 62 |
|
| 63 |
nb = 500 |
| 64 |
idx1 = rng.randint(X1.shape[0], size=(nb,)) |
| 65 |
idx2 = rng.randint(X2.shape[0], size=(nb,)) |
| 66 |
|
| 67 |
Xs = X1[idx1, :] |
| 68 |
Xt = X2[idx2, :] |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
ot_emd = ot.da.EMDTransport() |
| 77 |
ot_emd.fit(Xs=Xs, Xt=Xt) |
| 78 |
transp_Xs_emd = ot_emd.transform(Xs=X1) |
| 79 |
Image_emd = minmax(mat2im(transp_Xs_emd, I1.shape)) |
| 80 |
|
| 81 |
|
| 82 |
ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) |
| 83 |
ot_sinkhorn.fit(Xs=Xs, Xt=Xt) |
| 84 |
transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1) |
| 85 |
Image_sinkhorn = minmax(mat2im(transp_Xs_sinkhorn, I1.shape)) |
| 86 |
|
| 87 |
ot_mapping_linear = ot.da.MappingTransport( |
| 88 |
mu=1e0, eta=1e-8, bias=True, max_iter=20, verbose=True |
| 89 |
) |
| 90 |
ot_mapping_linear.fit(Xs=Xs, Xt=Xt) |
| 91 |
|
| 92 |
X1tl = ot_mapping_linear.transform(Xs=X1) |
| 93 |
Image_mapping_linear = minmax(mat2im(X1tl, I1.shape)) |
| 94 |
|
| 95 |
ot_mapping_gaussian = ot.da.MappingTransport( |
| 96 |
mu=1e0, eta=1e-2, sigma=1, bias=False, max_iter=10, verbose=True |
| 97 |
) |
| 98 |
ot_mapping_gaussian.fit(Xs=Xs, Xt=Xt) |
| 99 |
|
| 100 |
X1tn = ot_mapping_gaussian.transform(Xs=X1) |
| 101 |
Image_mapping_gaussian = minmax(mat2im(X1tn, I1.shape)) |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
plt.figure(1, figsize=(6.4, 3)) |
| 109 |
plt.subplot(1, 2, 1) |
| 110 |
plt.imshow(I1) |
| 111 |
plt.axis("off") |
| 112 |
plt.title("Image 1") |
| 113 |
|
| 114 |
plt.subplot(1, 2, 2) |
| 115 |
plt.imshow(I2) |
| 116 |
plt.axis("off") |
| 117 |
plt.title("Image 2") |
| 118 |
plt.tight_layout() |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
plt.figure(2, figsize=(6.4, 5)) |
| 126 |
|
| 127 |
plt.subplot(1, 2, 1) |
| 128 |
plt.scatter(Xs[:, 0], Xs[:, 2], c=Xs) |
| 129 |
plt.axis([0, 1, 0, 1]) |
| 130 |
plt.xlabel("Red") |
| 131 |
plt.ylabel("Blue") |
| 132 |
plt.title("Image 1") |
| 133 |
|
| 134 |
plt.subplot(1, 2, 2) |
| 135 |
plt.scatter(Xt[:, 0], Xt[:, 2], c=Xt) |
| 136 |
plt.axis([0, 1, 0, 1]) |
| 137 |
plt.xlabel("Red") |
| 138 |
plt.ylabel("Blue") |
| 139 |
plt.title("Image 2") |
| 140 |
plt.tight_layout() |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
plt.figure(2, figsize=(10, 5)) |
| 148 |
|
| 149 |
plt.subplot(2, 3, 1) |
| 150 |
plt.imshow(I1) |
| 151 |
plt.axis("off") |
| 152 |
plt.title("Im. 1") |
| 153 |
|
| 154 |
plt.subplot(2, 3, 4) |
| 155 |
plt.imshow(I2) |
| 156 |
plt.axis("off") |
| 157 |
plt.title("Im. 2") |
| 158 |
|
| 159 |
plt.subplot(2, 3, 2) |
| 160 |
plt.imshow(Image_emd) |
| 161 |
plt.axis("off") |
| 162 |
plt.title("EmdTransport") |
| 163 |
|
| 164 |
plt.subplot(2, 3, 5) |
| 165 |
plt.imshow(Image_sinkhorn) |
| 166 |
plt.axis("off") |
| 167 |
plt.title("SinkhornTransport") |
| 168 |
|
| 169 |
plt.subplot(2, 3, 3) |
| 170 |
plt.imshow(Image_mapping_linear) |
| 171 |
plt.axis("off") |
| 172 |
plt.title("MappingTransport (linear)") |
| 173 |
|
| 174 |
plt.subplot(2, 3, 6) |
| 175 |
plt.imshow(Image_mapping_gaussian) |
| 176 |
plt.axis("off") |
| 177 |
plt.title("MappingTransport (gaussian)") |
| 178 |
plt.tight_layout() |
| 179 |
|
| 180 |
plt.show() |