|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 2, |
| 6 | + "metadata": { |
| 7 | + "collapsed": false |
| 8 | + }, |
| 9 | + "outputs": [], |
| 10 | + "source": [ |
| 11 | + "# The AWS/GPU situation is very annoying. \n", |
| 12 | + "\n", |
| 13 | + "#I just used this pre-built AMI:\n", |
| 14 | + "## https://gist.github.com/erikbern/78ba519b97b440e10640" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 1, |
| 20 | + "metadata": { |
| 21 | + "collapsed": false |
| 22 | + }, |
| 23 | + "outputs": [ |
| 24 | + { |
| 25 | + "name": "stderr", |
| 2
EDBE
6 | + "output_type": "stream", |
| 27 | + "text": [ |
| 28 | + "usage: __main__.py [-h] [--batch_size BATCH_SIZE] [--num_batches NUM_BATCHES]\n", |
| 29 | + "__main__.py: error: unrecognized arguments: -f /root/.local/share/jupyter/runtime/kernel-64f89975-b1c6-4e61-86ac-9eb0feaeafe5.json\n" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "ename": "SystemExit", |
| 34 | + "evalue": "2", |
| 35 | + "output_type": "error", |
| 36 | + "traceback": [ |
| 37 | + "An exception has occurred, use %tb to see the full traceback.\n", |
| 38 | + "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 2\n" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "name": "stderr", |
| 43 | + "output_type": "stream", |
| 44 | + "text": [ |
| 45 | + "To exit: use 'exit', 'quit', or Ctrl-D.\n" |
| 46 | + ] |
| 47 | + } |
| 48 | + ], |
| 49 | + "source": [ |
| 50 | + "from datetime import datetime\n", |
| 51 | + "import math\n", |
| 52 | + "import time\n", |
| 53 | + "\n", |
| 54 | + "import tensorflow.python.platform\n", |
| 55 | + "import tensorflow as tf\n", |
| 56 | + "from tensorflow.python.ops import array_ops\n", |
| 57 | + "\n", |
| 58 | + "FLAGS = tf.app.flags.FLAGS\n", |
| 59 | + "\n", |
| 60 | + "tf.app.flags.DEFINE_integer('batch_size', 128,\n", |
| 61 | + " \"\"\"Batch size.\"\"\")\n", |
| 62 | + "tf.app.flags.DEFINE_integer('num_batches', 100,\n", |
| 63 | + " \"\"\"Number of batches to run.\"\"\")\n", |
| 64 | + "\n", |
| 65 | + "parameters = []\n", |
| 66 | + "\n", |
| 67 | + "conv_counter = 1\n", |
| 68 | + "pool_counter = 1\n", |
| 69 | + "affine_counter = 1\n", |
| 70 | + "\n", |
| 71 | + "def _conv(inpOp, nIn, nOut, kH, kW, dH, dW, padType):\n", |
| 72 | + " global conv_counter\n", |
| 73 | + " global parameters\n", |
| 74 | + " name = 'conv' + str(conv_counter)\n", |
| 75 | + " conv_counter += 1\n", |
| 76 | + " with tf.name_scope(name) as scope:\n", |
| 77 | + " kernel = tf.Variable(tf.truncated_normal([kH, kW, nIn, nOut],\n", |
| 78 | + " dtype=tf.float32,\n", |
| 79 | + " stddev=1e-1), name='weights')\n", |
| 80 | + " conv = tf.nn.conv2d(inpOp, kernel, [1, dH, dW, 1], padding=padType)\n", |
| 81 | + " biases = tf.Variable(tf.constant(0.0, shape=[nOut], dtype=tf.float32),\n", |
| 82 | + " trainable=True, name='biases')\n", |
| 83 | + " bias = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())\n", |
| 84 | + " conv1 = tf.nn.relu(bias, name=scope)\n", |
| 85 | + " parameters += [kernel, biases]\n", |
| 86 | + " return conv1\n", |
| 87 | + "\n", |
| 88 | + "def _affine(inpOp, nIn, nOut):\n", |
| 89 | + " global affine_counter\n", |
| 90 | + " global parameters\n", |
| 91 | + " name = 'affine' + str(affine_counter)\n", |
| 92 | + " affine_counter += 1\n", |
| 93 | + " with tf.name_scope(name) as scope:\n", |
| 94 | + " kernel = tf.Variable(tf.truncated_normal([nIn, nOut],\n", |
| 95 | + " dtype=tf.float32,\n", |
| 96 | + " stddev=1e-1), name='weights')\n", |
| 97 | + " biases = tf.Variable(tf.constant(0.0, shape=[nOut], dtype=tf.float32),\n", |
| 98 | + " trainable=True, name='biases')\n", |
| 99 | + " affine1 = tf.nn.relu_layer(inpOp, kernel, biases, name=name)\n", |
| 100 | + " parameters += [kernel, biases]\n", |
| 101 | + " return affine1\n", |
| 102 | + "\n", |
| 103 | + "def _mpool(inpOp, kH, kW, dH, dW, padding):\n", |
| 104 | + " global pool_counter\n", |
| 105 | + " global parameters\n", |
| 106 | + " name = 'pool' + str(pool_counter)\n", |
| 107 | + " pool_counter += 1\n", |
| 108 | + " return tf.nn.max_pool(inpOp,\n", |
| 109 | + " ksize=[1, kH, kW, 1],\n", |
| 110 | + " strides=[1, dH, dW, 1],\n", |
| 111 | + " padding=padding,\n", |
| 112 | + " name=name)\n", |
| 113 | + "\n", |
| 114 | + "def _apool(inpOp, kH, kW, dH, dW, padding):\n", |
| 115 | + " global pool_counter\n", |
| 116 | + " global parameters\n", |
| 117 | + " name = 'pool' + str(pool_counter)\n", |
| 118 | + " pool_counter += 1\n", |
| 119 | + " return tf.nn.avg_pool(inpOp,\n", |
| 120 | + " ksize=[1, kH, kW, 1],\n", |
| 121 | + " strides=[1, dH, dW, 1],\n", |
| 122 | + " padding=padding,\n", |
| 123 | + " name=name)\n", |
| 124 | + "\n", |
| 125 | + "def _inception(inp, inSize, o1s, o2s1, o2s2, o3s1, o3s2, o4s1, o4s2):\n", |
| 126 | + " conv1 = _conv(inp, inSize, o1s, 1, 1, 1, 1, 'SAME')\n", |
| 127 | + "\n", |
| 128 | + " conv3_ = _conv(inp, inSize, o2s1, 1, 1, 1, 1, 'SAME')\n", |
| 129 | + " conv3 = _conv(conv3_, o2s1, o2s2, 3, 3, 1, 1, 'SAME')\n", |
| 130 | + "\n", |
| 131 | + " conv5_ = _conv(inp, inSize, o3s1, 1, 1, 1, 1, 'SAME')\n", |
| 132 | + " conv5 = _conv(conv5_, o3s1, o3s2, 5, 5, 1, 1, 'SAME')\n", |
| 133 | + "\n", |
| 134 | + " pool_ = _mpool(inp, o4s1, o4s1, 1, 1, 'SAME')\n", |
| 135 | + " pool = _conv(pool_, inSize, o4s2, 1, 1, 1, 1, 'SAME')\n", |
| 136 | + "\n", |
| 137 | + " incept = array_ops.concat(3, [conv1, conv3, conv5, pool])\n", |
| 138 | + " return incept\n", |
| 139 | + "\n", |
| 140 | + "\n", |
| 141 | + "def loss(logits, labels):\n", |
| 142 | + " batch_size = tf.size(labels)\n", |
| 143 | + " labels = tf.expand_dims(labels, 1)\n", |
| 144 | + " indices = tf.expand_dims(tf.range(0, batch_size, 1), 1)\n", |
| 145 | + " concated = tf.concat(1, [indices, labels])\n", |
| 146 | + " onehot_labels = tf.sparse_to_dense(\n", |
| 147 | + " concated, tf.pack([batch_size, 1000]), 1.0, 0.0)\n", |
| 148 | + " cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits,\n", |
| 149 | + " onehot_labels,\n", |
| 150 | + " name='xentropy')\n", |
| 151 | + " loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')\n", |
| 152 | + " return loss\n", |
| 153 | + "\n", |
| 154 | + "def inference(images):\n", |
| 155 | + " conv1 = _conv (images, 3, 64, 7, 7, 2, 2, 'SAME')\n", |
| 156 | + " pool1 = _mpool(conv1, 3, 3, 2, 2, 'SAME')\n", |
| 157 | + " conv2 = _conv (pool1, 64, 64, 1, 1, 1, 1, 'SAME')\n", |
| 158 | + " conv3 = _conv (conv2, 64, 192, 3, 3, 1, 1, 'SAME')\n", |
| 159 | + " pool3 = _mpool(conv3, 3, 3, 2, 2, 'SAME')\n", |
| 160 | + "\n", |
| 161 | + " incept3a = _inception(pool3, 192, 64, 96, 128, 16, 32, 3, 32)\n", |
| 162 | + " incept3b = _inception(incept3a, 256, 128, 128, 192, 32, 96, 3, 64)\n", |
| 163 | + " pool4 = _mpool(incept3b, 3, 3, 2, 2, 'SAME')\n", |
| 164 | + " incept4a = _inception(pool4, 480, 192, 96, 208, 16, 48, 3, 64)\n", |
| 165 | + " incept4b = _inception(incept4a, 512, 160, 112, 224, 24, 64, 3, 64)\n", |
| 166 | + " incept4c = _inception(incept4b, 512, 128, 128, 256, 24, 64, 3, 64)\n", |
| 167 | + " incept4d = _inception(incept4c, 512, 112, 144, 288, 32, 64, 3, 64)\n", |
| 168 | + " incept4e = _inception(incept4d, 528, 256, 160, 320, 32, 128, 3, 128)\n", |
| 169 | + " pool5 = _mpool(incept4e, 3, 3, 2, 2, 'SAME')\n", |
| 170 | + " incept5a = _inception(pool5, 832, 256, 160, 320, 32, 128, 3, 128)\n", |
| 171 | + " incept5b = _inception(incept5a, 832, 384, 192, 384, 48, 128, 3, 128)\n", |
| 172 | + " pool6 = _apool(incept5b, 7, 7, 1, 1, 'VALID')\n", |
| 173 | + "\n", |
| 174 | + " resh1 = tf.reshape(pool6, [-1, 1024])\n", |
| 175 | + " affn1 = _affine(resh1, 1024, 1000)\n", |
| 176 | + "\n", |
| 177 | + " return affn1\n", |
| 178 | + "\n", |
| 179 | + "\n", |
| 180 | + "def time_tensorflow_run(session, target, info_string):\n", |
| 181 | + " num_steps_burn_in = 10\n", |
| 182 | + " total_duration = 0.0\n", |
| 183 | + " total_duration_squared = 0.0\n", |
| 184 | + " if not isinstance(target, list):\n", |
| 185 | + " target = [target]\n", |
| 186 | + " for i in xrange(FLAGS.num_batches + num_steps_burn_in):\n", |
| 187 | + " start_time = time.time()\n", |
| 188 | + " _ = session.run(tf.group(*target))\n", |
| 189 | + " duration = time.time() - start_time\n", |
| 190 | + " if i > num_steps_burn_in:\n", |
| 191 | + " if not i % 10:\n", |
| 192 | + " print ('%s: step %d, duration = %.3f' %\n", |
| 193 | + " (datetime.now(), i - num_steps_burn_in, duration))\n", |
| 194 | + " total_duration += duration\n", |
| 195 | + " total_duration_squared += duration * duration\n", |
| 196 | + " mn = total_duration / FLAGS.num_batches\n", |
| 197 | + " vr = total_duration_squared / FLAGS.num_batches - mn * mn\n", |
| 198 | + " sd = math.sqrt(vr)\n", |
| 199 | + " print ('%s: %s across %d steps, %.3f +/- %.3f sec / batch' %\n", |
| 200 | + " (datetime.now(), info_string, FLAGS.num_batches, mn, sd))\n", |
| 201 | + "\n", |
| 202 | + "def run_benchmark():\n", |
| 203 | + " global parameters\n", |
| 204 | + " with tf.Graph().as_default():\n", |
| 205 | + " # Generate some dummy images.\n", |
| 206 | + " image_size = 224\n", |
| 207 | + " images = tf.Variable(tf.random_normal([FLAGS.batch_size,\n", |
| 208 | + " image_size,\n", |
| 209 | + "
B41A
span> image_size, 3],\n", |
| 210 | + " dtype=tf.float32,\n", |
| 211 | + " stddev=1e-1))\n", |
| 212 | + "\n", |
| 213 | + " labels = tf.Variable(tf.ones([FLAGS.batch_size],\n", |
| 214 | + " dtype=tf.int32))\n", |
| 215 | + "\n", |
| 216 | + " # Build a Graph that computes the logits predictions from the\n", |
| 217 | + " # inference model.\n", |
| 218 | + " last_layer = inference(images)\n", |
| 219 | + "\n", |
| 220 | + " # Build an initialization operation.\n", |
| 221 | + " init = tf.initialize_all_variables()\n", |
| 222 | + "\n", |
| 223 | + " # Start running operations on the Graph.\n", |
| 224 | + " config = tf.ConfigProto()\n", |
| 225 | + " config.gpu_options.allocator_type = 'BFC'\n", |
| 226 | + " sess = tf.Session(config=config)\n", |
| 227 | + " sess.run(init)\n", |
| 228 | + "\n", |
| 229 | + " # Run the forward benchmark.\n", |
| 230 | + " time_tensorflow_run(sess, last_layer, \"Forward\")\n", |
| 231 | + "\n", |
| 232 | + " # Add a simple objective so we can calculate the backward pass.\n", |
| 233 | + " objective = loss(last_layer, labels)\n", |
| 234 | + " # Compute the gradient with respect to all the parameters.\n", |
| 235 | + " grad = tf.gradients(objective, parameters)\n", |
| 236 | + " # Run the backward benchmark.\n", |
| 237 | + " time_tensorflow_run(sess, grad, \"Forward-backward\")\n", |
| 238 | + "\n", |
| 239 | + "\n", |
| 240 | + "def main(_):\n", |
| 241 | + " run_benchmark()\n", |
| 242 | + "\n", |
| 243 | + "\n", |
| 244 | + "if __name__ == '__main__':\n", |
| 245 | + " tf.app.run()" |
| 246 | + ] |
| 247 | + }, |
| 248 | + { |
| 249 | + "cell_type": "code", |
| 250 | + "execution_count": null, |
| 251 | + "metadata": { |
| 252 | + "collapsed": true |
| 253 | + }, |
| 254 | + "outputs": [], |
| 255 | + "source": [] |
| 256 | + } |
| 257 | + ], |
| 258 | + "metadata": { |
| 259 | + "kernelspec": { |
| 260 | + "display_name": "Python 2", |
| 261 | + "language": "python", |
| 262 | + "name": "python2" |
| 263 | + }, |
| 264 | + "language_info": { |
| 265 | + "codemirror_mode": { |
| 266 | + "name": "ipython", |
| 267 | + "version": 2 |
| 268 | + }, |
| 269 | + "file_extension": ".py", |
| 270 | + "mimetype": "text/x-python", |
| 271 | + "name": "python", |
| 272 | + "nbconvert_exporter": "python", |
| 273 | + "pygments_lexer": "ipython2", |
| 274 | + "version": "2.7.6" |
| 275 | + } |
| 276 | + }, |
| 277 | + "nbformat": 4, |
| 278 | + "nbformat_minor": 0 |
| 279 | +} |
0 commit comments