summaryrefslogtreecommitdiff
path: root/docs/source/auto_examples/plot_otda_linear_mapping.ipynb
blob: 027b6cbf36e20e4aee494cdb5139c509a33269a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Linear OT mapping estimation\n\n\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Author: Remi Flamary <remi.flamary@unice.fr>\n#\n# License: MIT License\n\nimport numpy as np\nimport pylab as pl\nimport ot"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate data\n-------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n = 1000\nd = 2\nsigma = .1\n\n# source samples\nangles = np.random.rand(n, 1) * 2 * np.pi\nxs = np.concatenate((np.sin(angles), np.cos(angles)),\n                    axis=1) + sigma * np.random.randn(n, 2)\nxs[:n // 2, 1] += 2\n\n\n# target samples\nanglet = np.random.rand(n, 1) * 2 * np.pi\nxt = np.concatenate((np.sin(anglet), np.cos(anglet)),\n                    axis=1) + sigma * np.random.randn(n, 2)\nxt[:n // 2, 1] += 2\n\n\nA = np.array([[1.5, .7], [.7, 1.5]])\nb = np.array([[4, 2]])\nxt = xt.dot(A) + b"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot data\n---------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl.figure(1, (5, 5))\npl.plot(xs[:, 0], xs[:, 1], '+')\npl.plot(xt[:, 0], xt[:, 1], 'o')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Estimate linear mapping and transport\n-------------------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "Ae, be = ot.da.OT_mapping_linear(xs, xt)\n\nxst = xs.dot(Ae) + be"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot transported samples\n------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl.figure(1, (5, 5))\npl.clf()\npl.plot(xs[:, 0], xs[:, 1], '+')\npl.plot(xt[:, 0], xt[:, 1], 'o')\npl.plot(xst[:, 0], xst[:, 1], '+')\n\npl.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load image data\n---------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def im2mat(I):\n    \"\"\"Converts and image to matrix (one pixel per line)\"\"\"\n    return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))\n\n\ndef mat2im(X, shape):\n    \"\"\"Converts back a matrix to an image\"\"\"\n    return X.reshape(shape)\n\n\ndef minmax(I):\n    return np.clip(I, 0, 1)\n\n\n# Loading images\nI1 = pl.imread('../data/ocean_day.jpg').astype(np.float64) / 256\nI2 = pl.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256\n\n\nX1 = im2mat(I1)\nX2 = im2mat(I2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Estimate mapping and adapt\n----------------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mapping = ot.da.LinearTransport()\n\nmapping.fit(Xs=X1, Xt=X2)\n\n\nxst = mapping.transform(Xs=X1)\nxts = mapping.inverse_transform(Xt=X2)\n\nI1t = minmax(mat2im(xst, I1.shape))\nI2t = minmax(mat2im(xts, I2.shape))\n\n# %%"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot transformed images\n-----------------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl.figure(2, figsize=(10, 7))\n\npl.subplot(2, 2, 1)\npl.imshow(I1)\npl.axis('off')\npl.title('Im. 1')\n\npl.subplot(2, 2, 2)\npl.imshow(I2)\npl.axis('off')\npl.title('Im. 2')\n\npl.subplot(2, 2, 3)\npl.imshow(I1t)\npl.axis('off')\npl.title('Mapping Im. 1')\n\npl.subplot(2, 2, 4)\npl.imshow(I2t)\npl.axis('off')\npl.title('Inverse mapping Im. 2')"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}