Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "I'd like to train VGG network as example, but it just ran too slow on my computer.\n",
- "\n",
- "I have only mac, so I can only run tensorflow with CPU :(\n",
- "\n",
- "So, I refer to our good old friend, handwriting recognition with MNIST.\n",
- "\n",
- "Bear with me, plz. I just want to show you a full process converting a graph to a single reusable file."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "from math import sqrt\n",
- "import numpy as np\n",
- "import tensorflow as tf\n",
- "from tensorflow.examples.tutorials.mnist.input_data import read_data_sets\n",
- "\n",
- "%matplotlib inline\n",
- "import matplotlib.pyplot as plt"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "# helper function\n",
- "def accuracy(y, y_):\n",
- " return (y == y_).mean()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Extracting mnist_data/train-images-idx3-ubyte.gz\n",
- "Extracting mnist_data/train-labels-idx1-ubyte.gz\n",
- "Extracting mnist_data/t10k-images-idx3-ubyte.gz\n",
- "Extracting mnist_data/t10k-labels-idx1-ubyte.gz\n"
- ]
- }
- ],
- "source": [
- "mnist = read_data_sets(\"mnist_data\", one_hot=True)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "# build a trainable graph\n",
- "train_graph = tf.Graph()\n",
- "hidden_shape = [100, 100]\n",
- "learning_rate = 0.05\n",
- "\n",
- "with train_graph.as_default():\n",
- " with tf.name_scope(\"input\"):\n",
- " image_batch = tf.placeholder(tf.float32, \n",
- " shape=[None, 784],\n",
- " name=\"image_batch\")\n",
- " layer_shapes = [784] + hidden_shape + [10]\n",
- " last_layer = image_batch\n",
- " with tf.name_scope(\"hidden\"):\n",
- " for in_shape, out_shape in zip(layer_shapes[:-1], layer_shapes[1:]):\n",
- " sig = sqrt(6)/(in_shape + out_shape)\n",
- " W_init = 2*sig*np.random.rand(in_shape, out_shape)-sig\n",
- " W = tf.Variable(np.random.randn(in_shape, out_shape),\n",
- " dtype=tf.float32,\n",
- " name=\"weight\")\n",
- " bias = tf.Variable(np.random.randn(out_shape),\n",
- " dtype=tf.float32,\n",
- " name=\"bias\")\n",
- " zscore = tf.matmul(last_layer, W) + bias\n",
- " last_layer = tf.nn.sigmoid(zscore)\n",
- " \n",
- " with tf.name_scope(\"output\"):\n",
- " prob = tf.nn.softmax(zscore, \n",
- " name=\"probability\")\n",
- " predict = tf.arg_max(prob, \n",
- " 1, \n",
- " name=\"prediction\")\n",
- " target = tf.placeholder(tf.float32,\n",
- " shape=[None, 10],\n",
- " name=\"target\")\n",
- " loss = -tf.reduce_mean(target*tf.log(prob))\n",
- " train_op = tf.train.AdagradOptimizer(learning_rate).minimize(loss)\n",
- " saver = tf.train.Saver()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Initialized\n",
- "Iteration 1000: 0.16 47.45%\n",
- "Iteration 2000: 0.12 62.55%\n",
- "Iteration 3000: 0.10 69.13%\n",
- "Iteration 4000: 0.08 73.12%\n",
- "Iteration 5000: 0.10 76.01%\n",
- "Iteration 6000: 0.06 77.92%\n",
- "Iteration 7000: 0.07 79.24%\n",
- "Iteration 8000: 0.06 80.53%\n",
- "Iteration 9000: 0.06 81.49%\n",
- "Iteration 10000: 0.07 82.52%\n",
- "Iteration 11000: 0.05 83.25%\n",
- "Iteration 12000: 0.06 83.75%\n",
- "Iteration 13000: 0.04 84.27%\n",
- "Iteration 14000: 0.06 84.71%\n",
- "Iteration 15000: 0.06 84.95%\n",
- "Iteration 16000: 0.05 85.33%\n",
- "Iteration 17000: 0.03 85.62%\n",
- "Iteration 18000: 0.05 86.05%\n",
- "Iteration 19000: 0.04 86.28%\n",
- "Iteration 20000: 0.05 86.50%\n"
- ]
- }
- ],
- "source": [
- "# Training and save\n",
- "num_iters = 20000\n",
- "batch_size = 300\n",
- "\n",
- "with tf.Session(graph=train_graph) as sess:\n",
- " tf.global_variables_initializer().run()\n",
- " print(\"Initialized\")\n",
- " \n",
- " for step in range(num_iters):\n",
- " images, labels = mnist.train.next_batch(batch_size)\n",
- " feed_dict = { image_batch: images,\n",
- " target: labels }\n",
- " l, _ = sess.run([loss, train_op], feed_dict=feed_dict)\n",
- " \n",
- " if (step+1) % 1000 == 0:\n",
- " pred = sess.run(predict, feed_dict={image_batch:mnist.test.images})\n",
- " acc = accuracy(pred, np.argmax(mnist.test.labels, axis=1))\n",
- " print(\"Iteration {}: {:.2f} {:.2f}%\".format(step+1, l, acc*100))\n",
- " \n",
- " saver.save(sess, save_path=\"model/mnist_example.chkp\", global_step=step)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Looking good enough, ready to save the graph"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "checkpoint\r\n",
- "mnist_example.chkp-19999.data-00000-of-00001\r\n",
- "mnist_example.chkp-19999.index\r\n",
- "mnist_example.chkp-19999.meta\r\n"
- ]
- }
- ],
- "source": [
- "# saved data\n",
- "!ls model/"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "# import the graph_util\n",
- "# We'll use it to convert all variables in graph to constants\n",
- "from tensorflow.python.framework import graph_util"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'model/mnist_example.chkp-19999'"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# A CheckpointState object from tensorflow.python.training.checkpoint_state_pb2\n",
- "# Yes, it's produced by protobuf.\n",
- "checkpoint = tf.train.get_checkpoint_state(\"model/\")\n",
- "checkpoint.model_checkpoint_path"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'model/mnist_example.chkp-19999.meta'"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# meta file contains the meta data of the saved session\n",
- "meta_file_path = checkpoint.model_checkpoint_path+\".meta\"\n",
- "meta_file_path"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "# restore the saver from the meta file\n",
- "restore_graph = tf.Graph()\n",
- "with restore_graph.as_default():\n",
- " saver = tf.train.import_meta_graph(meta_file_path, clear_devices=True)\n",
- "restore_graph_def = restore_graph.as_graph_def()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "INFO:tensorflow:Restoring parameters from model/mnist_example.chkp-19999\n",
- "INFO:tensorflow:Froze 6 variables.\n",
- "Converted 6 variables to const ops.\n"
- ]
- }
- ],
- "source": [
- "# restore the session with restored saver\n",
- "# convert graph to \"freezed\" graph\n",
- "# Note that you need to specify the layers you want to output, \n",
- "# tensorflow will extract a subgraph which involves the layers you want and freeze it.\n",
- "with tf.Session(graph=restore_graph) as sess:\n",
- " saver.restore(sess, checkpoint.model_checkpoint_path)\n",
- " out_graph_def = graph_util.convert_variables_to_constants(sess,\n",
- " restore_graph_def,\n",
- " [predict.op.name])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "# write graph to disk just like what you will do with any protobuf object\n",
- "with tf.gfile.GFile(\"my_mnist.pb\", \"wb\") as fid:\n",
- " fid.write(out_graph_def.SerializeToString())"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "------\n",
- "\n",
- "Testing time!"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "graph = tf.Graph()\n",
- "graph_def = graph.as_graph_def()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "with tf.gfile.GFile(\"my_mnist.pb\", \"rb\") as rf:\n",
- " graph_def.ParseFromString(rf.read())\n",
- "with graph.as_default():\n",
- " tf.import_graph_def(graph_def, name=\"\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [],
- "source": [
- "images_tensor = graph.get_tensor_by_name(\"input/image_batch:0\")\n",
- "predict_tensor = graph.get_tensor_by_name(\"output/prediction:0\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "86.5\n"
- ]
- }
- ],
- "source": [
- "with tf.Session(graph=graph) as sess:\n",
- " tf.global_variables_initializer().run()\n",
- " pred = sess.run(predict_tensor, \n",
- " feed_dict={images_tensor:mnist.test.images})\n",
- " print(accuracy(np.argmax(mnist.test.labels, axis=1), pred)*100)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.1"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment