{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# First Sip of Caffe\n", "\n", "This is a quick check to make sure we can brew." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "Before we begin, let's verify [WebSockets](http://en.wikipedia.org/wiki/WebSocket) are working on your system. To do this, execute the cell block below by giving it focus (clicking on it with your mouse), and hitting Ctrl-Enter, or pressing the play button in the toolbar above. If all goes well, you should see some output returned below the grey cell. If not, please consult the [Self-paced Lab Troubleshooting FAQ](https://developer.nvidia.com/self-paced-labs-faq#Troubleshooting) to debug the issue. *This section courtesy the NVIDIA Caffe introduction.*" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print \"The answer should be three: \" + str(1+2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's execute the cell below to display information about the GPUs running on the server." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "!nvidia-smi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now load the `caffe` Python module and switch to GPU mode for computation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "caffe_root = '../' # this file should be run from {caffe_root}/examples (otherwise change this line)\n", "\n", "# make sure that caffe is in our import path\n", "import sys\n", "sys.path.insert(0, caffe_root + 'python')\n", "\n", "# load caffe and switch to the GPU\n", "import caffe\n", "caffe.set_mode_gpu()\n", "caffe.set_device(0)\n", "\n", "# import standard array and plotting toolkit\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next step is to collect the materials we'll need for our first sip:\n", "\n", "1. a pre-trained network for off-the-shelf use\n", "2. the category names to understand the output" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# make sure we have the CaffeNet model weights\n", "import os\n", "if os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):\n", " print 'CaffeNet found.'\n", "else:\n", " print 'Downloading pre-trained CaffeNet model...'\n", " !../scripts/download_model_binary.py ../models/bvlc_reference_caffenet\n", "\n", "# load ImageNet labels (for understanding the output)\n", "labels_file = caffe_root + 'data/ilsvrc12/synset_words.txt'\n", "if not os.path.exists(labels_file):\n", " !../data/ilsvrc12/get_ilsvrc_aux.sh\n", "labels = np.loadtxt(labels_file, str, delimiter='\\t')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With `caffe` ready, let's load our pre-trained CaffeNet (a variant of the ILSVRC2012-winning AlexNet by Krizhevsky et al.)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# load and run the net\n", "model_def = caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'\n", "model_weights = caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'\n", "\n", "net = caffe.Net(model_def, # defines the structure of the model\n", " model_weights, # contains the trained weights\n", " caffe.TEST) # for deployment (in contrast to caffe.TRAIN)\n", "\n", "print 'Loaded net.'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before checking the net let's define an input pre-processor, `transformer`, to help feed an image into the net." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# configure input pre-processing\n", "mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')\n", "mu = mu.mean(1).mean(1) # average over pixels to obtain the mean (BGR) pixel values\n", "transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})\n", "transformer.set_transpose('data', (2,0,1)) # move image channels to outermost dimension\n", "transformer.set_mean('data', mu) # subtract the dataset-mean value in each channel\n", "transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255]\n", "transformer.set_channel_swap('data', (2,1,0)) # swap channels from RGB to BGR\n", "print 'Configured input.'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Time for an image:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# download an image\n", "image = caffe.io.load_image(caffe_root + 'examples/images/coffee.jpg')\n", "transformed_image = transformer.preprocess('data', image)\n", "plt.imshow(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What a fine cup of coffee. Let's classify!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "net.blobs['data'].data[...] = transformed_image\n", "net.forward()\n", "output_prob = net.blobs['prob'].data[0]\n", "\n", "print 'What does the net say?\\n', labels[output_prob.argmax()]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Happy brewing!" ] } ], "metadata": { "description": "Use the pre-trained ImageNet model to classify images with the Python interface.", "example_name": "ImageNet classification", "include_in_docs": true, "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "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.10" }, "priority": 1 }, "nbformat": 4, "nbformat_minor": 0 }