Debian Codegraph @

examples/domain-adaptation/plot_otda_linear_mapping.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """
4 ============================
5 Linear OT mapping estimation
6 ============================
7
8
9 """
10
11 # Author: Remi Flamary <[email protected]>
12 #
13 # License: MIT License
14
15 # sphinx_gallery_thumbnail_number = 2
16
17 # %%
18 import os
19 from pathlib import Path
20
21 import numpy as np
22 from matplotlib import pyplot as plt
23 import ot
24
25 ##############################################################################
26 # Generate data
27 # -------------
28
29 n = 1000
30 d = 2
31 sigma = 0.1
32
33 rng = np.random.RandomState(42)
34
35 # source samples
36 angles = rng.rand(n, 1) * 2 * np.pi
37 xs = np.concatenate((np.sin(angles), np.cos(angles)), axis=1) + sigma * rng.randn(n, 2)
38 xs[: n // 2, 1] += 2
39
40
41 # target samples
42 anglet = rng.rand(n, 1) * 2 * np.pi
43 xt = np.concatenate((np.sin(anglet), np.cos(anglet)), axis=1) + sigma * rng.randn(n, 2)
44 xt[: n // 2, 1] += 2
45
46
47 A = np.array([[1.5, 0.7], [0.7, 1.5]])
48 b = np.array([[4, 2]])
49 xt = xt.dot(A) + b
50
51 ##############################################################################
52 # Plot data
53 # ---------
54
55 plt.figure(1, (5, 5))
56 plt.plot(xs[:, 0], xs[:, 1], "+")
57 plt.plot(xt[:, 0], xt[:, 1], "o")
58 plt.legend(("Source", "Target"))
59 plt.title("Source and target distributions")
60 plt.show()
61
62 ##############################################################################
63 # Estimate linear mapping and transport
64 # -------------------------------------
65
66
67 # Gaussian (linear) Monge mapping estimation
68 Ae, be = ot.gaussian.empirical_bures_wasserstein_mapping(xs, xt)
69
70 xst = xs.dot(Ae) + be
71
72 # Gaussian (linear) GW mapping estimation
73 Agw, bgw = ot.gaussian.empirical_gaussian_gromov_wasserstein_mapping(xs, xt)
74
75 xstgw = xs.dot(Agw) + bgw
76
77 ##############################################################################
78 # Plot transported samples
79 # ------------------------
80
81 plt.figure(2, (10, 5))
82 plt.clf()
83 plt.subplot(1, 2, 1)
84 plt.plot(xs[:, 0], xs[:, 1], "+")
85 plt.plot(xt[:, 0], xt[:, 1], "o")
86 plt.plot(xst[:, 0], xst[:, 1], "+")
87 plt.legend(("Source", "Target", "Transp. Monge"), loc=0)
88 plt.title("Transported samples with Monge")
89 plt.subplot(1, 2, 2)
90 plt.plot(xs[:, 0], xs[:, 1], "+")
91 plt.plot(xt[:, 0], xt[:, 1], "o")
92 plt.plot(xstgw[:, 0], xstgw[:, 1], "+")
93 plt.legend(("Source", "Target", "Transp. GW"), loc=0)
94 plt.title("Transported samples with Gaussian GW")
95 plt.show()
96
97 ##############################################################################
98 # Load image data
99 # ---------------
100
101
102 def im2mat(img):
103 """Converts and image to matrix (one pixel per line)"""
104 return img.reshape((img.shape[0] * img.shape[1], img.shape[2]))
105
106
107 def mat2im(X, shape):
108 """Converts back a matrix to an image"""
109 return X.reshape(shape)
110
111
112 def minmax(img):
113 return np.clip(img, 0, 1)
114
115
116 # Loading images
117 this_file = os.path.realpath("__file__")
118 data_path = os.path.join(Path(this_file).parent.parent.parent, "data")
119
120 I1 = plt.imread(os.path.join(data_path, "ocean_day.jpg")).astype(np.float64) / 256
121 I2 = plt.imread(os.path.join(data_path, "ocean_sunset.jpg")).astype(np.float64) / 256
122
123
124 X1 = im2mat(I1)
125 X2 = im2mat(I2)
126
127 ##############################################################################
128 # Estimate mapping and adapt
129 # ----------------------------
130
131 # Monge mapping
132 mapping = ot.da.LinearTransport()
133 mapping.fit(Xs=X1, Xt=X2)
134
135
136 xst = mapping.transform(Xs=X1)
137 xts = mapping.inverse_transform(Xt=X2)
138
139 I1t = minmax(mat2im(xst, I1.shape))
140 I2t = minmax(mat2im(xts, I2.shape))
141
142 # gaussian GW mapping
143
144 mapping = ot.da.LinearGWTransport()
145 mapping.fit(Xs=X1, Xt=X2)
146
147
148 xstgw = mapping.transform(Xs=X1)
149 xtsgw = mapping.inverse_transform(Xt=X2)
150
151 I1tgw = minmax(mat2im(xstgw, I1.shape))
152 I2tgw = minmax(mat2im(xtsgw, I2.shape))
153
154 # %%
155
156
157 ##############################################################################
158 # Plot transformed images
159 # -----------------------
160
161 plt.figure(3, figsize=(14, 7))
162
163 plt.subplot(2, 3, 1)
164 plt.imshow(I1)
165 plt.axis("off")
166 plt.title("Im. 1")
167
168 plt.subplot(2, 3, 4)
169 plt.imshow(I2)
170 plt.axis("off")
171 plt.title("Im. 2")
172
173 plt.subplot(2, 3, 2)
174 plt.imshow(I1t)
175 plt.axis("off")
176 plt.title("Monge mapping Im. 1")
177
178 plt.subplot(2, 3, 5)
179 plt.imshow(I2t)
180 plt.axis("off")
181 plt.title("Inverse Monge mapping Im. 2")
182
183 plt.subplot(2, 3, 3)
184 plt.imshow(I1tgw)
185 plt.axis("off")
186 plt.title("Gaussian GW mapping Im. 1")
187
188 plt.subplot(2, 3, 6)
189 plt.imshow(I2tgw)
190 plt.axis("off")
191 plt.title("Inverse Gaussian GW mapping Im. 2")