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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
| {
| "cells": [
| {
| "cell_type": "code",
| "execution_count": 1,
| "metadata": {
| "collapsed": true
| },
| "outputs": [],
| "source": [
| "%matplotlib inline\n",
| "import numpy as np\n",
| "import torch\n",
| "from torch.utils.serialization import load_lua\n",
| "import os\n",
| "import scipy.io as sio\n",
| "import cv2\n",
| "import math\n",
| "from matplotlib import pyplot as plt\n",
| "from torch.utils.data.dataset import Dataset\n",
| "\n",
| "from sklearn.decomposition import PCA"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": 2,
| "metadata": {
| "collapsed": true
| },
| "outputs": [],
| "source": [
| "TRAIN_DATA_DIR = '/Data/nruiz9/data/facial_landmarks/300W_LP/'"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": 4,
| "metadata": {
| "collapsed": false
| },
| "outputs": [],
| "source": [
| "shape_params_list = []\n",
| "names = []\n",
| "\n",
| "with open(os.path.join(TRAIN_DATA_DIR, 'filename_list_filtered.txt')) as f:\n",
| " for idx, line in enumerate(f):\n",
| " original_line = line\n",
| " line = line.strip('\\n')\n",
| " mat_path = os.path.join(TRAIN_DATA_DIR, line + '.mat')\n",
| " mat = sio.loadmat(mat_path)\n",
| "\n",
| " shape_params_list.append(np.array(mat['Shape_Para'][:,0]))\n",
| " names.append(line)"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": 5,
| "metadata": {
| "collapsed": false
| },
| "outputs": [
| {
| "name": "stdout",
| "output_type": "stream",
| "text": [
| "122415\n"
| ]
| }
| ],
| "source": [
| "X = [sp for sp in shape_params_list]\n",
| "print len(X)"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": 6,
| "metadata": {
| "collapsed": false
| },
| "outputs": [
| {
| "name": "stdout",
| "output_type": "stream",
| "text": [
| "[ 0.4954199 0.13912562 0.11082269 0.07658631 0.04858431 0.02813001\n",
| " 0.01758898 0.01631346 0.01002331 0.00814171]\n",
| "0.950736293253\n"
| ]
| }
| ],
| "source": [
| "pca = PCA(n_components=10)\n",
| "pca.fit(X)\n",
| "print(pca.explained_variance_ratio_)\n",
| "print sum(pca.explained_variance_ratio_)"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": 7,
| "metadata": {
| "collapsed": false
| },
| "outputs": [
| {
| "name": "stdout",
| "output_type": "stream",
| "text": [
| "122415\n"
| ]
| }
| ],
| "source": [
| "new_X = pca.transform(X)\n",
| "print len(new_X)"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": 8,
| "metadata": {
| "collapsed": false
| },
| "outputs": [
| {
| "name": "stdout",
| "output_type": "stream",
| "text": [
| "[-2556323.53165033 -1140679.77655202 -1371614.65446089 -1119583.33472875\n",
| " -754535.15912456 -821857.44375021 -534676.82835068 -499987.22801558\n",
| " -426309.71017172 -446477.70288007]\n",
| "[ 5002830.39467843 1820495.74291976 1441834.85901925 1429397.04589404\n",
| " 1223356.93869817 924078.41303862 760271.63357968 805551.96259901\n",
| " 466004.54029864 545186.01838144]\n",
| "[-176340.23999369 440.18163591 -2284.73154146 -6407.94592961\n",
| " -11806.29047248 -2078.74081526 -3059.95275478 -5356.3932487\n",
| " -3081.66281823 2027.99147229]\n",
| "[ 7559153.92632876 2961175.51947178 2813449.51348013 2548980.38062279\n",
| " 1977892.09782273 1745935.85678882 1294948.46193037 1305539.19061459\n",
| " 892314.25047037 991663.72126151]\n",
| "[ 7559153.92632876 2961175.51947178 2813449.51348013 2548980.38062279\n",
| " 1977892.09782273 1745935.85678882 1294948.46193037 1305539.19061459\n",
| " 892314.25047037 991663.72126151]\n",
| "(122415, 10)\n"
| ]
| }
| ],
| "source": [
| "print np.amin(new_X, 0)\n",
| "print np.amax(new_X, 0)\n",
| "print np.median(new_X, 0)\n",
| "\n",
| "print np.abs(np.amax(new_X, 0) - np.amin(new_X, 0))\n",
| "print np.ptp(new_X, 0)\n",
| "print new_X.shape"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": 9,
| "metadata": {
| "collapsed": false
| },
| "outputs": [
| {
| "name": "stdout",
| "output_type": "stream",
| "text": [
| "(10, 122415)\n",
| "[0 0 0 0 0 0 0 0 0 0]\n",
| "[59 59 59 59 59 59 59 59 59 59]\n"
| ]
| }
| ],
| "source": [
| "maxs = np.amax(new_X, 0)\n",
| "mins = np.amin(new_X, 0)\n",
| "dividers = 60\n",
| "step_sizes = np.ptp(new_X, 0) / (dividers - 2)\n",
| "\n",
| "bins = []\n",
| "for idx in xrange(new_X.shape[1]):\n",
| " rng = range(int(mins[idx]), int(maxs[idx]) + 1, int(step_sizes[idx]))\n",
| " bins.append(np.digitize(new_X[:,idx], rng))\n",
| " \n",
| "bins = np.array(bins)\n",
| "print bins.shape\n",
| "print np.amin(bins, 1)\n",
| "print np.amax(bins, 1)"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": 10,
| "metadata": {
| "collapsed": false
| },
| "outputs": [
| {
| "name": "stdout",
| "output_type": "stream",
| "text": [
| "AFW_Flip/AFW_5083671561_5_5 AFW_Flip/AFW_1648807314_2_0\n"
| ]
| }
| ],
| "source": [
| "print names[0], names[1]"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": 12,
| "metadata": {
| "collapsed": false
| },
| "outputs": [],
| "source": [
| "# Save the new PCA binned representation\n",
| "idx = 0\n",
| "for name in names:\n",
| " pose_path = os.path.join(TRAIN_DATA_DIR, name + '_shape.npy')\n",
| " np.save(pose_path, bins[:,idx])\n",
| " idx += 1"
| ]
| },
| {
| "cell_type": "code",
| "execution_count": null,
| "metadata": {
| "collapsed": true
| },
| "outputs": [],
| "source": []
| }
| ],
| "metadata": {
| "anaconda-cloud": {},
| "kernelspec": {
| "display_name": "Python [conda root]",
| "language": "python",
| "name": "conda-root-py"
| },
| "language_info": {
| "codemirror_mode": {
| "name": "ipython",
| "version": 2
| },
| "file_extension": ".py",
| "mimetype": "text/x-python",
| "name": "python",
| "nbconvert_exporter": "python",
| "pygments_lexer": "ipython2",
| "version": "2.7.12"
| }
| },
| "nbformat": 4,
| "nbformat_minor": 1
| }
|
|