natanielruiz
2017-08-11 b215992b7a187782cf494ab9f291195ffde9278e
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 77,
   "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": 78,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "TRAIN_DATA_DIR = '/Data/nruiz9/data/facial_landmarks/300W_LP/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "shape_params_dict = dict()\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_dict[line] = np.array(mat['Shape_Para'][:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "122415\n"
     ]
    }
   ],
   "source": [
    "X = [shape_params_dict[datum] for datum in shape_params_dict]\n",
    "print len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "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": 82,
   "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": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2556323.53165033 -1140679.77655202 -1371614.65446089 -1119583.33472875\n",
      "  -754535.15912458  -821857.44375049  -534676.82841282  -499987.22775606\n",
      "  -426309.70779568  -446477.723748  ]\n",
      "[ 5002830.39467843  1820495.74291969  1441834.85901925  1429397.04589405\n",
      "  1223356.93869825   924078.41304442   760271.63359562   805551.96296291\n",
      "   466004.54589496   545186.01453756]\n",
      "[-176340.23999371     440.18163592   -2284.73154146   -6407.94592961\n",
      "  -11806.29047245   -2078.74081487   -3059.95282595   -5356.39323994\n",
      "   -3081.66667384    2027.97251627]\n",
      "[ 7559153.92632876  2961175.51947171  2813449.51348013  2548980.3806228\n",
      "  1977892.09782283  1745935.8567949   1294948.46200844  1305539.19071897\n",
      "   892314.25369063   991663.73828556]\n",
      "[ 7559153.92632876  2961175.51947171  2813449.51348013  2548980.3806228\n",
      "  1977892.09782283  1745935.8567949   1294948.46200844  1305539.19071897\n",
      "   892314.25369063   991663.73828556]\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": 84,
   "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": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Save the new PCA binned representation\n",
    "idx = 0\n",
    "for name in shape_params_dict:\n",
    "    pose_path = os.path.join(TRAIN_DATA_DIR, line + '_pose.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
}