diff --git a/notebooks/Models/PCAModel.ipynb b/notebooks/Models/PCAModel.ipynb deleted file mode 100644 index 5eadd80..0000000 --- a/notebooks/Models/PCAModel.ipynb +++ /dev/null @@ -1,337 +0,0 @@ -{ - "metadata": { - "name": "", - "signature": "sha256:59bdc8b29e9c5506be2fee5281740cd79bc54064f8be6a16d77004047dddfebe" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Principal Component Analysis Models" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Import the aligned face dataset and build a list of Image objects from it" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import scipy.io as sio\n", - "import numpy as np\n", - "from menpo.image import MaskedImage\n", - "import menpo.io as pio\n", - "\n", - "im_db = sio.loadmat(pio.data_path_to('alignedbwfaces.mat'))\n", - "imagedata = im_db['images']\n", - "mask = im_db['mask']\n", - "images = []\n", - "for i in range(imagedata.shape[-1]):\n", - " images.append(MaskedImage(imagedata[...,i], mask=mask))" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`images` is a Python list of our `menpo.image.Image` class. We can grab one of them and view it" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "%matplotlib inline\n", - "image = images[0]\n", - "image.view()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "the `view()` method is a quick and easy visualization on all our types. For images, it's roughly equivalent to this" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import matplotlib.pyplot as plt\n", - "plt.gray()\n", - "plt.imshow(image.pixels[...,0])" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "but does some legwork to give a sensible result for B+W images too. See the `menpo.images.Images.ipynb` notebook for more examples of how our `Image` class works." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Set aside the last 10 images for testing, use the rest for constructing the model" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "training_images = images[:-10]\n", - "test_images = images[-10:]\n", - "\n", - "from menpo.model import PCAModel\n", - "pca = PCAModel(training_images)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Firstly, lets take a look at the mean" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "pca.mean.view()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note that the method above return `Image` instances! \n" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "type(pca.mean)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This makes working with our models really convienient. Of course statistical models generally only work with vectors of data, so how is this possible? It's all done through the `Vectorizable` interface (see `menpo.vectorizable.ipynb` for details). If you ever want to see the low level vectors, just call the `mean_vector` property." - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "pca.mean_vector" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "What else can we see? How about the proportion of variance captured in each of component" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "import matplotlib.pyplot as plt\n", - "plt.plot(pca.eigenvalues_ratio)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "or cumulative variance captured in the first 40 components" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "plt.plot(np.cumsum(pca.eigenvalues_ratio[:40]))" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Or just the components themselves" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "print 'shape of all the components: {}'.format(pca.components.shape)\n", - "print 'pca.components[:5] grabs the first 5 components: {}'.format(pca.components[:5].shape)" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now lets use our model. We can project an in-sample image onto the PCA and check the reconstruction is perfect" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "insample_image = training_images[9]\n", - "weightings = pca.project(insample_image)\n", - "rebuilt_image = pca.instance(weightings)\n", - "print 'if the reconstruction is close, an image of the reconstruction is displayed'\n", - "if np.allclose(insample_image.pixels, rebuilt_image.pixels):\n", - " rebuilt_image.view()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "print \"Examining the numerical error\"\n", - "plt.imshow((insample_image.pixels - rebuilt_image.pixels)[...,0])" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now lets limit the number of components used in the reconstruction a little..." - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "rebuilt_image_50_comps = pca.instance(weightings[:50])\n", - "rebuilt_image_50_comps.view()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now lets try an out of sample image" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "test_image = test_images[0]\n", - "test_image.view()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "weights = pca.project(test_image)\n", - "reconstructed_image = pca.instance(weights)\n", - "reconstructed_image.view()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This operation of projecting to learn weights followed by reconstruction of an instance is fairly common, so we have some syntactic sugar to make things a little more natural." - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "reconstructed_image_alt = pca.reconstruct(test_image)\n", - "print \"Does the easier reconstruct syntax yield the same result as before?: {}\".format(\n", - "np.all(reconstructed_image_alt.pixels == reconstructed_image.pixels))" - ], - "language": "python", - "metadata": {}, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Note that you can also pass in a number of Components to limit the reconstruction power" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "reconstructed_image_limited = pca.reconstruct(test_image, n_components=50)\n", - "reconstructed_image_limited.view()" - ], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -} \ No newline at end of file