Note
Click here to download the full example code
OT for image color adaptation with labels - OptimalTransportGridSearch¶
Using sinkhorn L1l2
import numpy as np
import matplotlib.pylab as pl
import ot
import pottok
Generate data¶
# Loading X and y
Xs,ys,Xt,yt = pottok.datasets.load_pottoks()
Xs = Xs/255
Xt = Xt/255
# Loading images array
brown_pottok,black_pottok = pottok.datasets.load_pottoks(return_X_y=False)
brown_pottok = brown_pottok/255
black_pottok = black_pottok/255
Optimal transport with SinkhornL1l2 with gridsearch¶
gridsearch_transport = pottok.OptimalTransportGridSearch(transport_function=ot.da.SinkhornTransport,
params=dict(reg_e=[1e-1,1e0]))
gridsearch_transport.preprocessing(Xs=Xs,Xt=Xt,scaler=False)
gridsearch_transport.fit_circular()
Out:
Learning Optimal Transport with SinkhornTransport algorithm.
Xs and Xt are not scaled
[[0.91372549 0.87058824 0.68235294]
[0.92156863 0.8745098 0.68627451]
[0.92156863 0.87843137 0.69019608]
...
[0.74901961 0.74509804 0.42352941]
[0.74901961 0.74509804 0.44313725]
[0.75686275 0.75294118 0.43529412]]
mean_squared_error is : 0.005541744312379894
mean_squared_error is : 0.026163703713251486
Best grid is {'reg_e': 0.1}
Best score is 0.005541744312379894
<ot.da.SinkhornTransport object at 0x7fe996052950>
Plot images¶
X1tl = gridsearch_transport.predict_transfer(brown_pottok.reshape(-1,3))
Image_mapping_gs = X1tl.reshape(*brown_pottok.shape)
pl.figure(1, figsize=(10,8))
pl.subplot(2, 2, 1)
pl.imshow(brown_pottok)
pl.axis('off')
pl.title('Brown pottok (Source)')
pl.subplot(2, 2, 3)
pl.imshow(black_pottok)
pl.axis('off')
pl.title('Black pottok (Target)')
pl.subplot(2, 2, 4)
pl.imshow(Image_mapping_gs)
pl.axis('off')
pl.title('Sinkhorn (Source to Target with no labels)')
pl.show()
Out:
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Total running time of the script: ( 0 minutes 48.767 seconds)