|
| 1 | +# This file is adapted from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/alexnet/alexnet_benchmark.py |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +import math |
| 5 | +import time |
| 6 | + |
| 7 | +from six.moves import xrange # pylint: disable=redefined-builtin |
| 8 | +import tensorflow as tf |
| 9 | + |
| 10 | +BATCH_SIZE = 128 |
| 11 | +IMAGE_SIZE = 227 |
| 12 | +NUM_CHANNELS = 3 |
| 13 | +SEED = 66478 |
| 14 | + |
| 15 | +def print_activations(t): |
| 16 | + print(t.op.name, ' ', t.get_shape().as_list()) |
| 17 | + |
| 18 | + |
| 19 | +def inference(images): |
| 20 | + # conv1 |
| 21 | + with tf.name_scope('conv1') as scope: |
| 22 | + kernel = tf.Variable(tf.truncated_normal([11, 11, 3, 64], dtype=tf.float32, |
| 23 | + stddev=1e-1, seed=SEED), name='weights') |
| 24 | + conv = tf.nn.conv2d(images, kernel, [1, 4, 4, 1], padding='SAME') |
| 25 | + biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), |
| 26 | + trainable=True, name='biases') |
| 27 | + bias = tf.nn.bias_add(conv, biases) |
| 28 | + conv1 = tf.nn.relu(bias, name=scope) |
| 29 | + print_activations(conv1) |
| 30 | + |
| 31 | + # lrn1 |
| 32 | + # TODO(shlens, jiayq): Add a GPU version of local response normalization. |
| 33 | + |
| 34 | + # pool1 |
| 35 | + pool1 = tf.nn.max_pool(conv1, |
| 36 | + ksize=[1, 3, 3, 1], |
| 37 | + strides=[1, 2, 2, 1], |
| 38 | + padding='VALID', |
| 39 | + name='pool1') |
| 40 | + print_activations(pool1) |
| 41 | + |
| 42 | + # conv2 |
| 43 | + with tf.name_scope('conv2') as scope: |
| 44 | + kernel = tf.Variable(tf.truncated_normal([5, 5, 64, 192], |
| 45 | + dtype=tf.float32, |
| 46 | + stddev=1e-1, |
| 47 | + seed=SEED), name='weights') |
| 48 | + conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding='SAME') |
| 49 | + biases = tf.Variable(tf.constant(0.0, shape=[192], dtype=tf.float32), |
| 50 | + trainable=True, name='biases') |
| 51 | + bias = tf.nn.bias_add(conv, biases) |
| 52 | + conv2 = tf.nn.relu(bias, name=scope) |
| 53 | + print_activations(conv2) |
| 54 | + |
| 55 | + # pool2 |
| 56 | + pool2 = tf.nn.max_pool(conv2, |
| 57 | + ksize=[1, 3, 3, 1], |
| 58 | + strides=[1, 2, 2, 1], |
| 59 | + padding='VALID', |
| 60 | + name='pool2') |
| 61 | + print_activations(pool2) |
| 62 | + |
| 63 | + # conv3 |
| 64 | + with tf.name_scope('conv3') as scope: |
| 65 | + kernel = tf.Variable(tf.truncated_normal([3, 3, 192, 384], |
| 66 | + dtype=tf.float32, |
| 67 | + stddev=1e-1, |
| 68 | + seed=SEED), name='weights') |
| 69 | + conv = tf.nn.conv2d(pool2, kernel, [1, 1, 1, 1], padding='SAME') |
| 70 | + biases = tf.Variable(tf.constant(0.0, shape=[384], dtype=tf.float32), |
| 71 | + trainable=True, name='biases') |
| 72 | + bias = tf.nn.bias_add(conv, biases) |
| 73 | + conv3 = tf.nn.relu(bias, name=scope) |
| 74 | + print_activations(conv3) |
| 75 | + |
| 76 | + # conv4 |
| 77 | + with tf.name_scope('conv4') as scope: |
| 78 | + kernel = tf.Variable(tf.truncated_normal([3, 3, 384, 256], |
| 79 | + dtype=tf.float32, |
| 80 | + stddev=1e-1, |
| 81 | + seed=SEED), name='weights') |
| 82 | + conv = tf.nn.conv2d(conv3, kernel, [1, 1, 1, 1], padding='SAME') |
| 83 | + biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), |
| 84 | + trainable=True, name='biases') |
| 85 | + bias = tf.nn.bias_add(conv, biases) |
| 86 | + conv4 = tf.nn.relu(bias, name=scope) |
| 87 | + print_activations(conv4) |
| 88 | + |
| 89 | + # conv5 |
| 90 | + with tf.name_scope('conv5') as scope: |
| 91 | + kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], |
| 92 | + dtype=tf.float32, |
| 93 | + stddev=1e-1, |
| 94 | + seed=SEED), name='weights') |
| 95 | + conv = tf.nn.conv2d(conv4, kernel, [1, 1, 1, 1], padding='SAME') |
| 96 | + biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), |
| 97 | + trainable=True, name='biases') |
| 98 | + bias = tf.nn.bias_add(conv, biases) |
| 99 | + conv5 = tf.nn.relu(bias, name=scope) |
| 100 | + print_activations(conv5) |
| 101 | + |
| 102 | + # pool5 |
| 103 | + pool5 = tf.nn.max_pool(conv5, |
| 104 | + ksize=[1, 3, 3, 1], |
| 105 | + strides=[1, 2, 2, 1], |
| 106 | + padding='VALID', |
| 107 | + name='pool5') |
| 108 | + print_activations(pool5) |
| 109 | + |
| 110 | + fc6W = tf.Variable( |
| 111 | + tf.truncated_normal([9216, 4096], |
| 112 | + stddev=0.1, |
| 113 | + seed=SEED), |
| 114 | + name="fc6W") |
| 115 | + fc6b = tf.Variable(tf.zeros([4096]), name="fc6b") |
| 116 | + fc6 = tf.nn.relu_layer(tf.reshape(pool5, [BATCH_SIZE, 9216]), fc6W, fc6b, name="fc6") |
| 117 | + |
| 118 | + fc7W = tf.Variable( |
| 119 | + tf.truncated_normal([4096, 4096], |
| 120 | + stddev=0.1, |
| 121 | + seed=SEED), |
| 122 | + name="fc7W") |
| 123 | + fc7b = tf.Variable(tf.zeros([4096]), name="fc7b") |
| 124 | + fc7 = tf.nn.relu_layer(fc6, fc7W, fc7b, name="fc7") |
| 125 | + |
| 126 | + fc8W = tf.Variable( |
| 127 | + tf.truncated_normal([4096, 1000], |
| 128 | + stddev=0.1, |
| 129 | + seed=SEED), |
| 130 | + name="fc8W") |
| 131 | + fc8b = tf.Variable(tf.zeros([1000]), name="fc8b") |
| 132 | + fc8 = tf.nn.xw_plus_b(fc7, fc8W, fc8b, name="fc8") |
| 133 | + |
| 134 | + return fc8 |
| 135 | + |
| 136 | + |
| 137 | +sess = tf.InteractiveSession(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) |
| 138 | + |
| 139 | +with tf.device('/gpu:0'): |
| 140 | + # Generate some dummy images. |
| 141 | + # Note that our padding definition is slightly different the cuda-convnet. |
| 142 | + # In order to force the model to start with the same activations sizes, |
| 143 | + # we add 3 to the image_size and employ VALID padding above. |
| 144 | + images = tf.placeholder( |
| 145 | + tf.float32, |
| 146 | + shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS), |
| 147 | + name="data") |
| 148 | + labels = tf.placeholder(tf.int32, shape=(BATCH_SIZE,), name="label") |
| 149 | + labels = tf.to_int64(labels) |
| 150 | + |
| 151 | + # Build a Graph that computes the logits predictions from the |
| 152 | + # inference model. |
| 153 | + logits = inference(images) |
| 154 | + |
| 155 | + loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( |
| 156 | + logits, labels), name="loss") |
| 157 | + |
| 158 | + # Use simple momentum for the optimization. |
| 159 | + optimizer = tf.train.MomentumOptimizer(0.01, |
| 160 | + 0.9).minimize(loss, |
| 161 | + name="train//step") |
| 162 | + |
| 163 | + # Predictions for the current training minibatch. |
| 164 | + probs = tf.nn.softmax(logits, name="probs") |
| 165 | + prediction = tf.arg_max(probs, 1, name="prediction") |
| 166 | + correct_prediction = tf.equal(prediction, labels, name="correct_prediction") |
| 167 | + accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"), name="accuracy") |
| 168 | + |
| 169 | + # Build an initialization operation. |
| 170 | + tf.initialize_variables(tf.all_variables(), name="init//all_vars") |
| 171 | + |
| 172 | + # this code traverses the graph and adds Assign nodes for each variable |
| 173 | + variables = [node for node in sess.graph_def.node if node.op == "Variable"] |
| 174 | + for v in variables: |
| 175 | + n = sess.graph.as_graph_element(v.name + ":0") |
| 176 | + dtype = tf.as_dtype(sess.graph.get_operation_by_name(v.name).get_attr("dtype")) |
| 177 | + update_placeholder = tf.placeholder(dtype, n.get_shape().as_list(), name=(v.name + "//update_placeholder")) |
| 178 | + tf.assign(n, update_placeholder, name=(v.name + "//assign")) |
| 179 | + |
| 180 | + from google.protobuf.text_format import MessageToString |
| 181 | + print MessageToString(sess.graph_def) |
| 182 | + filename = "alexnet_graph.pb" |
| 183 | + s = sess.graph_def.SerializeToString() |
| 184 | + f = open(filename, "wb") |
| 185 | + f.write(s) |
0 commit comments