tensorflow手写字体识别,入门示例

此篇包含了手写字体识别的全连接神经网络和卷积神经网络模型建模,模型保存成pb格式,以及使用tensorflow迁移学习的方式。

#!/usr/bin/python

# -*- coding:utf-8 -*-
import input_data as input
import tensorflow as tf
from tensorflow.python.framework import graph_util

from tensorflow.python.platform import gfile
import numpy as np

def testMnist():

    mnist = input.read_data_sets("/home/myjob/Downloads/Mnist/", one_hot=True)
    x = tf.placeholder("float32",[None,784])
    w = tf.Variable(tf.zeros([784,10]))
    b = tf.Variable(tf.zeros([10]))

    y = tf.nn.softmax(tf.matmul(x,w)+b)
    y_ = tf.placeholder("float32",[None,10])
    cross_entry = tf.reduce_sum(y_*tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entry)
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        for i in range(1000):
            batch_xs, batch_ys = mnist.train.next_batch(100)
            yy = sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

        print "over:"

        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float32"))
        print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})

    print mnist

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)

    return tf.Variable(initial)

def bias_Variable(shape):

    initial = tf.constant(0.1,shape=shape)

    return initial
def conv2d(x,W):

    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding="SAME")
def padding_2d(x):

    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")

def mnist_conv2d():

    mnist = input.read_data_sets("/home/myjob/Downloads/Mnist/", one_hot=True)
    x = tf.placeholder("float32", [None, 784],name='input_x')
    y_ = tf.placeholder("float32", [None, 10],name='input_y')
    w_conv1 = weight_variable([5,5,1,32])

    b_conv1 = bias_Variable([32])

    x_image = tf.reshape(x,[-1,28,28,1])
    h_conv1 = tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
    h_pool1 = padding_2d(h_conv1)
    w_conv2 = weight_variable([5,5,32,64])
    b_conv2 = bias_Variable([64])
    h_conv2 = tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
    h_pool2 = padding_2d(h_conv2)
    w_fc1 = weight_variable([7*7*64,1024])
    b_fc1 = bias_Variable([1024])
    h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1,name="fc1")
    keep_prob = tf.placeholder("float32",name='keep_prob')
    h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
    w_fc2 = weight_variable([1024,10])
    b_fc2 = bias_Variable([10])
    y_conv2d = tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2)+b_fc2,name="out")
    cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv2d))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y_conv2d, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float32"))
    init = tf.global_variables_initializer()
    saver = tf.train.Saver()
    model_path = "/home/myjob/Downloads/Mnist/model.ckpt"
    model_path_pb = "/home/myjob/Downloads/Mnist/"
    with tf.Session() as sess:
        sess.run(init)
        for i in range(101):
            batch = mnist.train.next_batch(64)
            if i % 2 == 0:
                train_accuracy = accuracy.eval(feed_dict={
                    x: batch[0], y_: batch[1], keep_prob: 1.0})
                print "step %d, training accuracy %g" % (i, train_accuracy)
                saver_path = saver.save(sess,model_path)
                constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['out'])
                with tf.gfile.FastGFile(model_path_pb +'model1.pb', mode='wb') as f:
                    f.write(constant_graph.SerializeToString())

                result = sess.run(y_conv2d,feed_dict={x:mnist.test.images[0:1],keep_prob:1.0})
                index = tf.argmax(result,1)
                # print result
                # print sess.run(index)
            train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})


            v = sess.graph.get_tensor_by_name('Variable_1:0')
            print "========================================"
            print sess.run(v[0])

        print "test accuracy %g" % accuracy.eval(feed_dict={
            x: mnist.test.images[0:100], y_: mnist.test.labels[0:100], keep_prob: 1.0})

def test_model():

    meta_path = '/home/myjob/Downloads/Mnist/model.ckpt.meta'
    model_path = '/home/myjob/Downloads/Mnist/model.ckpt'

    mnist = input.read_data_sets("/home/myjob/Downloads/Mnist/", one_hot=True)
    tf.reset_default_graph()

    saver = tf.train.import_meta_graph(meta_path)
    with tf.Session() as sess:

        tf.train.Saver().restore(sess, model_path)
        graph = tf.get_default_graph()
        out = graph.get_tensor_by_name('out:0')
        input_x = graph.get_operation_by_name('input_x').outputs[0]
        keep_prob = graph.get_operation_by_name('keep_prob').outputs[0]

        result = sess.run(out, feed_dict={input_x: mnist.test.images[0:10], keep_prob:1.0})
        index = tf.argmax(result,1)
        print mnist.test.labels[0:10]
        print sess.run(index)
#迁移学习
def transfer():

    mnist = input.read_data_sets("/home/myjob/Downloads/Mnist/", one_hot=True)
    model_path_pb = "/home/myjob/Downloads/Mnist/"

    y_ = tf.placeholder("float32", [None, 10], name='input_y')

    with tf.Session() as sess:

        with gfile.FastGFile(model_path_pb + 'model.pb', 'rb') as f:

            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            sess.graph.as_default()
            tf.import_graph_def(graph_def, name='')  # 导入计算图

        input_x = sess.graph.get_tensor_by_name('input_x:0')
        h_fc = sess.graph.get_tensor_by_name('fc1:0')
        # h_fc
        hfc_sg = tf.stop_gradient(h_fc)
        
        w_fc1 = weight_variable([1024, 1024])
        b_fc1 = bias_Variable([1024])
        h_fc1 = tf.nn.relu(tf.matmul(hfc_sg, w_fc1) + b_fc1, name="fc2")
        w_fc2 = weight_variable([1024, 10])
        b_fc2 = bias_Variable([10])
        y_conv2d = tf.nn.softmax(tf.matmul(h_fc1, w_fc2) + b_fc2, name="outt")
        cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv2d))
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
        correct_prediction = tf.equal(tf.argmax(y_conv2d, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float32"))
        init = tf.global_variables_initializer()
        with tf.Session() as sess:
            sess.run(init)

            for i in range(101):
                batch = mnist.train.next_batch(64)
                if i % 2 == 0:
                    train_accuracy = accuracy.eval(feed_dict={
                        input_x: batch[0], y_: batch[1]})

                    print "step %d, training accuracy %g" % (i, train_accuracy)
                    constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['outt'])
                    with tf.gfile.FastGFile(model_path_pb + 'model_transform.pb', mode='wb') as f:
                        f.write(constant_graph.SerializeToString())
                train_step.run(feed_dict={input_x:batch[0], y_: batch[1]})
#迁移学习
def transfer1():

    mnist = input.read_data_sets("/home/myjob/Downloads/Mnist/", one_hot=True)
    model_path_pb = "/home/myjob/Downloads/Mnist/"
    x = tf.placeholder("float32", [None, 1024], name='x')
    y_ = tf.placeholder("float32", [None, 10], name='input_y')

    with tf.Session() as sess:
        with gfile.FastGFile(model_path_pb + 'model.pb', 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            sess.graph.as_default()
            tf.import_graph_def(graph_def, name='')  # 导入计算图

        input_x = sess.graph.get_tensor_by_name('input_x:0')
        h_fc = sess.graph.get_tensor_by_name('fc1:0')
        w_fc1 = weight_variable([1024, 1024])
        b_fc1 = bias_Variable([1024])
        h_fc1 = tf.nn.relu(tf.matmul(x, w_fc1) + b_fc1, name="fc2")
        w_fc2 = weight_variable([1024, 10])
        b_fc2 = bias_Variable([10])
        y_conv2d = tf.nn.softmax(tf.matmul(h_fc1, w_fc2) + b_fc2, name="outt")

        cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv2d))
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
        correct_prediction = tf.equal(tf.argmax(y_conv2d, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float32"))
        init = tf.global_variables_initializer()
        with tf.Session() as sess:
            sess.run(init)
            for i in range(101):
                batch = mnist.train.next_batch(64)
                if i % 2 == 0:
                    result = sess.run(h_fc,feed_dict={input_x: batch[0]})

                    train_accuracy = accuracy.eval(feed_dict={
                        x: result, y_: batch[1]})
                    print "step %d, training accuracy %g" % (i, train_accuracy)
                    constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['outt'])
                    with tf.gfile.FastGFile(model_path_pb + 'model_transform1.pb', mode='wb') as f:
                        f.write(constant_graph.SerializeToString())
                result = sess.run(h_fc, feed_dict={input_x: batch[0]})
                train_step.run(feed_dict={x: result, y_: batch[1]})
                v = sess.graph.get_tensor_by_name('Variable_1:0')
                print "========================================"
                print sess.run(v[0])
def test_transform():

    mnist = input.read_data_sets("/home/myjob/Downloads/Mnist/", one_hot=True)
    model_path_pb = "/home/myjob/Downloads/Mnist/"
    with tf.Session() as sess:
        with gfile.FastGFile(model_path_pb + 'model_transform.pb', 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            sess.graph.as_default()
            tf.import_graph_def(graph_def, name='')  # 导入计算图
    input_x = sess.graph.get_tensor_by_name('input_x:0')
    op = sess.graph.get_tensor_by_name('outt:0')
    with tf.Session() as sess:
        ops = sess.graph.get_operations()
        print ops
        result = sess.run(op, feed_dict={input_x: mnist.test.images[0:10]})
        index = tf.argmax(result, 1)
        print mnist.test.labels[0:10]
        print sess.run(index)
if __name__ == '__main__':

    # test_model()
    # mnist_conv2d()
    test_model_pb()
    # test_transform()
    # test_pb_pb()
    print "ss"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值