11# -*- coding: utf-8 -*-
22"""
3- ========================================================
4- OT for domain adaptation with image color adaptation [6]
5- ========================================================
3+ =============================
4+ OT for image color adaptation
5+ =============================
66
77This example presents a way of transferring colors between two image
88with Optimal Transport as introduced in [6]
@@ -41,7 +41,7 @@ def minmax(I):
4141
4242
4343##############################################################################
44- # generate data
44+ # Generate data
4545##############################################################################
4646
4747# Loading images
@@ -61,33 +61,7 @@ def minmax(I):
6161
6262
6363##############################################################################
64- # Instantiate the different transport algorithms and fit them
65- ##############################################################################
66-
67- # EMDTransport
68- ot_emd = ot .da .EMDTransport ()
69- ot_emd .fit (Xs = Xs , Xt = Xt )
70-
71- # SinkhornTransport
72- ot_sinkhorn = ot .da .SinkhornTransport (reg_e = 1e-1 )
73- ot_sinkhorn .fit (Xs = Xs , Xt = Xt )
74-
75- # prediction between images (using out of sample prediction as in [6])
76- transp_Xs_emd = ot_emd .transform (Xs = X1 )
77- transp_Xt_emd = ot_emd .inverse_transform (Xt = X2 )
78-
79- transp_Xs_sinkhorn = ot_emd .transform (Xs = X1 )
80- transp_Xt_sinkhorn = ot_emd .inverse_transform (Xt = X2 )
81-
82- I1t = minmax (mat2im (transp_Xs_emd , I1 .shape ))
83- I2t = minmax (mat2im (transp_Xt_emd , I2 .shape ))
84-
85- I1te = minmax (mat2im (transp_Xs_sinkhorn , I1 .shape ))
86- I2te = minmax (mat2im (transp_Xt_sinkhorn , I2 .shape ))
87-
88-
89- ##############################################################################
90- # plot original image
64+ # Plot original image
9165##############################################################################
9266
9367pl .figure (1 , figsize = (6.4 , 3 ))
@@ -104,7 +78,7 @@ def minmax(I):
10478
10579
10680##############################################################################
107- # scatter plot of colors
81+ # Scatter plot of colors
10882##############################################################################
10983
11084pl .figure (2 , figsize = (6.4 , 3 ))
@@ -126,7 +100,33 @@ def minmax(I):
126100
127101
128102##############################################################################
129- # plot new images
103+ # Instantiate the different transport algorithms and fit them
104+ ##############################################################################
105+
106+ # EMDTransport
107+ ot_emd = ot .da .EMDTransport ()
108+ ot_emd .fit (Xs = Xs , Xt = Xt )
109+
110+ # SinkhornTransport
111+ ot_sinkhorn = ot .da .SinkhornTransport (reg_e = 1e-1 )
112+ ot_sinkhorn .fit (Xs = Xs , Xt = Xt )
113+
114+ # prediction between images (using out of sample prediction as in [6])
115+ transp_Xs_emd = ot_emd .transform (Xs = X1 )
116+ transp_Xt_emd = ot_emd .inverse_transform (Xt = X2 )
117+
118+ transp_Xs_sinkhorn = ot_emd .transform (Xs = X1 )
119+ transp_Xt_sinkhorn = ot_emd .inverse_transform (Xt = X2 )
120+
121+ I1t = minmax (mat2im (transp_Xs_emd , I1 .shape ))
122+ I2t = minmax (mat2im (transp_Xt_emd , I2 .shape ))
123+
124+ I1te = minmax (mat2im (transp_Xs_sinkhorn , I1 .shape ))
125+ I2te = minmax (mat2im (transp_Xt_sinkhorn , I2 .shape ))
126+
127+
128+ ##############################################################################
129+ # Plot new images
130130##############################################################################
131131
132132pl .figure (3 , figsize = (8 , 4 ))
0 commit comments