|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Pycaffe tutorial\n", |
| 8 | + "\n", |
| 9 | + "This tutorial assumes that you already know how to use Caffe. If you don't,\n", |
| 10 | + "consult the official caffe tutorials at:\n", |
| 11 | + "- http://caffe.berkeleyvision.org/tutorial/\n", |
| 12 | + "- http://caffe.berkeleyvision.org/gathered/examples/mnist.html\n", |
| 13 | + "\n", |
| 14 | + "Also assumed is a basic knowledge of Python. If it is not the case, have a look\n", |
| 15 | + "at the official python tutorials here: https://docs.python.org/2/tutorial/\n", |
| 16 | + "\n", |
| 17 | + "Caffe uses Python 2.7, make sure you use are using the correct version by running\n", |
| 18 | + "\n", |
| 19 | + "`$ python --version`\n", |
| 20 | + "\n", |
| 21 | + "\n", |
| 22 | + "\n", |
| 23 | + "The objective of this tutorial is to present basic and more advanced uses of\n", |
| 24 | + "the pycaffe interface, to use Caffe with Python.\n", |
| 25 | + "\n", |
| 26 | + "In this part of the tutorial, we will learn to start a standard training of\n", |
| 27 | + "Caffe, with Python. We will be using the data/networks provided in\n", |
| 28 | + "http://caffe.berkeleyvision.org/gathered/examples/mnist.html\n", |
| 29 | + "\n", |
| 30 | + "Set the following variable to your caffe root:" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": 2, |
| 36 | + "metadata": { |
| 37 | + "collapsed": true |
| 38 | + }, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "CAFFE_ROOT=\"/caffe\"\n", |
| 42 | + "\n", |
| 43 | + "import os\n", |
| 44 | + "os.chdir(CAFFE_ROOT) # change the current directory to the caffe root, to help\n", |
| 45 | + " # with the relative paths" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "metadata": {}, |
| 51 | + "source": [ |
| 52 | + "The first step is to import the caffe library:" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": 3, |
| 58 | + "metadata": { |
| 59 | + "collapsed": false |
| 60 | + }, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "import caffe" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "markdown", |
| 68 | + "metadata": {}, |
| 69 | + "source": [ |
| 70 | + "Then, we decide whether to use CPU or GPU for the training.\n", |
| 71 | + "\n", |
| 72 | + "Training with a GPU\n", |
| 73 | + "is faster, if the layers provide a GPU implementation. If you don't have a\n", |
| 74 | + "compatible graphics card or your layers are not implemented for GPU, use the\n", |
| 75 | + "CPU mode." |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 4, |
| 81 | + "metadata": { |
| 82 | + "collapsed": false |
| 83 | + }, |
| 84 | + "outputs": [ |
| 85 | + { |
| 86 | + "name": "stdout", |
| 87 | + "output_type": "stream", |
| 88 | + "text": [ |
| 89 | + "Initialized caffe\n" |
| 90 | + ] |
| 91 | + } |
| 92 | + ], |
| 93 | + "source": [ |
| 94 | + "USE_GPU = True\n", |
| 95 | + "\n", |
| 96 | + "if USE_GPU:\n", |
| 97 | + " caffe.set_device(0) # Or the index of the GPU you want to use\n", |
| 98 | + " caffe.set_mode_gpu()\n", |
| 99 | + " # Multi-GPU training is not available from Python, see\n", |
| 100 | + " # https://github.com/BVLC/caffe/issues/2936\n", |
| 101 | + "else:\n", |
| 102 | + " caffe.set_mode_cpu()\n", |
| 103 | + "\n", |
| 104 | + "print(\"Initialized caffe\")" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "markdown", |
| 109 | + "metadata": {}, |
| 110 | + "source": [ |
| 111 | + "Next, as we have a solver file, we will load it (paths are relative to the caffe root)." |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": 5, |
| 117 | + "metadata": { |
| 118 | + "collapsed": false |
| 119 | + }, |
| 120 | + "outputs": [], |
| 121 | + "source": [ |
| 122 | + "solver_file = \"examples/mnist/lenet_solver.prototxt\"\n", |
| 123 | + "\n", |
| 124 | + "solver = caffe.SGDSolver(solver_file)" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "Now, just run the training." |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 6, |
| 137 | + "metadata": { |
| 138 | + "collapsed": true |
| 139 | + }, |
| 140 | + "outputs": [], |
| 141 | + "source": [ |
| 142 | + "solver.solve()" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "markdown", |
| 147 | + "metadata": {}, |
| 148 | + "source": [ |
| 149 | + "Your network is now trained, and ready to go! The output is in `examples/mnist/lenet_iter_10000.caffemodel`.\n", |
| 150 | + "\n", |
| 151 | + "If you want to resume training from a snapshot, it's very simple too:" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": 7, |
| 157 | + "metadata": { |
| 158 | + "collapsed": true |
| 159 | + }, |
| 160 | + "outputs": [], |
| 161 | + "source": [ |
| 162 | + "snapshot_file = \"examples/mnist/lenet_iter_5000.solverstate\"\n", |
| 163 | + "solver.solve(snapshot_file)" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "code", |
| 168 | + "execution_count": 8, |
| 169 | + "metadata": { |
| 170 | + "collapsed": false |
| 171 | + }, |
| 172 | + "outputs": [ |
| 173 | + { |
| 174 | + "data": { |
| 175 | + "text/plain": [ |
| 176 | + "[('iter', 10000),\n", |
| 177 | + " ('net', <caffe._caffe.Net at 0x1f9d488>),\n", |
| 178 | + " ('restore',\n", |
| 179 | + " <bound method SGDSolver.restore of <caffe._caffe.SGDSolver object at 0x1fb19f0>>),\n", |
| 180 | + " ('set_iter',\n", |
| 181 | + " <bound method SGDSolver.set_iter of <caffe._caffe.SGDSolver object at 0x1fb19f0>>),\n", |
| 182 | + " ('snapshot',\n", |
| 183 | + " <bound method SGDSolver.snapshot of <caffe._caffe.SGDSolver object at 0x1fb19f0>>),\n", |
| 184 | + " ('solve',\n", |
| 185 | + " <bound method SGDSolver.solve of <caffe._caffe.SGDSolver object at 0x1fb19f0>>),\n", |
| 186 | + " ('step',\n", |
| 187 | + " <bound method SGDSolver.step of <caffe._caffe.SGDSolver object at 0x1fb19f0>>),\n", |
| 188 | + " ('test_nets', <caffe._caffe.NetVec at 0x49f4de0>)]" |
| 189 | + ] |
| 190 | + }, |
| 191 | + "execution_count": 8, |
| 192 | + "metadata": {}, |
| 193 | + "output_type": "execute_result" |
| 194 | + } |
| 195 | + ], |
| 196 | + "source": [ |
| 197 | + "[(e, getattr(solver, e)) for e in dir(solver) if not e.startswith(\"__\")]" |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "cell_type": "markdown", |
| 202 | + "metadata": {}, |
| 203 | + "source": [ |
| 204 | + "And there you go! This is the Python equivalent of\n", |
| 205 | + "`example/mnist/train_lenet.sh`, or\n", |
| 206 | + "\n", |
| 207 | + "`$ caffe train --solver=examples/mnist/lenet_solver.prototxt`\n", |
| 208 | + "\n", |
| 209 | + "But you can do much more than that with python. The next part covers network manipulation." |
| 210 | + ] |
| 211 | + } |
| 212 | + ], |
| 213 | + "metadata": { |
| 214 | + "kernelspec": { |
| 215 | + "display_name": "Python 2", |
| 216 | + "language": "python", |
| 217 | + "name": "python2" |
| 218 | + }, |
| 219 | + "language_info": { |
| 220 | + "codemirror_mode": { |
| 221 | + "name": "ipython", |
| 222 | + "version": 2 |
| 223 | + }, |
| 224 | + "file_extension": ".py", |
| 225 | + "mimetype": "text/x-python", |
| 226 | + "name": "python", |
| 227 | + "nbconvert_exporter": "python", |
| 228 | + "pygments_lexer": "ipython2", |
| 229 | + "version": "2.7.5" |
| 230 | + } |
| 231 | + }, |
| 232 | + "nbformat": 4, |
| 233 | + "nbformat_minor": 0 |
| 234 | +} |
0 commit comments