为了账号安全,请及时绑定邮箱和手机立即绑定

TensorFlow与Flask结合打造手写体数字识别

Mr_Ricky 算法工程师
难度中级
时长 2小时 1分
学习人数
综合评分7.90
44人评价 查看评价
8.0 内容实用
8.0 简洁易懂
7.7 逻辑清晰
  • 在卷积层中调用模型文件

    from mnist import model
    data = read_data_sets('MNIST_data', one_hot=True)
    with tf.variable_scope('convolutional'):
        x = tf.placeholder(tf.float32, [None, 784])
        keep_prob = tf.placeholder(tf.float32)
        y, variables = model.convolutional(x,keep_prob)
    
    _y = tf.placeholder(tf.float32, [None, 10])
    cross_entropy = -tf.reduce_sum(_y * tf.log(y))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(_y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    saver=tf.train.Saver(variables)
    with tf.Session() as sess:
        merged_summary_op = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter('/tmp/mnist_log/1', sess.graph)
        summary_writer.add_graph(sess.graph)


    查看全部
  • 完成剩余的卷积神经网络模型

    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 = max_pool_2x2(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)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
    
    W_fc2 = weight_variable([1024, 10])
    b_fc2 = bias_variable([10])
    y = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
    
    return y, [W_conv1, b_conv1, W_conv2, b_conv2, W_fc1, b_fc1, W_fc2, b_fc2]


    查看全部
  • 简单构建cnn的网络

    def convolutional(x, keep_prob):
        def conv2d(x, W):
            return tf.nn.conv2d([1, 1, 1, 1], padding='SAME')
    
        def max_pool_2x2(x):
            return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    
        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 tf.Variable(initial)
    
        x_image = tf.reshape(x, [-1, 28, 28, 1])
        W_conb1 = weight_variable([5, 5, 1, 32])
        b_conv1 = bias_variable([32])
        h_conv1 = tf.nn.relu(conv2d(x_image, W_conb1) + b_conv1)
        h_pool1 = max_pool_2x2(h_conv1)


    查看全部
  • 完成线性模型的构建

    import tensorflow as tf
    import os
    from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
    
    from mnist import model
    
    data = read_data_sets('MNIST_data', one_hot=True)
    with tf.variable_scope('regression'):
        x = tf.placeholder(tf.float32, [None, 784])
        y, variables = model.regression(x)
    
    _y = tf.placeholder('float', [None, 10])
    cross_entropy = -tf.reduce_sum(_y * tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(_y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    saver=tf.train.Saver(variables)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for _ in range(1000):
            batch_xs,batch_ys=data.train.next_batch(100)
            sess.run(train_step,feed_dict={x:batch_xs,_y:batch_ys})
        print(sess.run(accuracy,feed_dict={x:data.test.images,_y:data.test.labels}))
    
        path=saver.save(sess,os.path.join(os.path.dirname(__file__),'data','regression.ckpt'),write_meta_graph=False,write_state=False)
        print('Saver:'+path)

    最终保存了训练好的模型


    查看全部
  • 进行模型的构建

    先是线性模型

    import tensorflow as tf
    from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
    
    from mnist import model
    
    data = read_data_sets('MNIST_data', one_hot=True)
    with tf.variable_scope('regression'):
        x = tf.placeholder(tf.float32, [None, 784])
        y, variables = model.regression(x)
    
    _y = tf.placeholder('float', [None, 10])
    cross_entropy = -tf.reduce_sum(_y * tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(_y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


    查看全部
  • 使用tensorflow提供的函数来

    下载官方的mnist数据集


    查看全部
  • flask一种轻量的web框架

     训练好模型

    使用flask来调用模型

    查看全部
  • mnist数据集是手写数字数据集

    包含0到9的手写数字图片

    可以用来训练深度学习网络


    查看全部
    0 采集 收起 来源:mnist数据集

    2020-07-01

  • tensorflow是一个深度学习库

    支持cnn rnn lstm等

    可以实现语音识别和图像识别


    查看全部
    0 采集 收起 来源:TensorFlow框架

    2020-07-01

  • 将人工智能与现有的技术相结合

    可以进一步提高使用体验

    tensorflow与flask结合

    查看全部
    0 采集 收起 来源:课程介绍

    2020-07-01

  • MNIST

    查看全部
  • 整合步骤。

    查看全部
  • MNIST
    查看全部
    0 采集 收起 来源:mnist数据集

    2020-04-15

  • MNIST

    查看全部
    0 采集 收起 来源:mnist数据集

    2020-04-15

  • TensorFlow

    查看全部
    0 采集 收起 来源:TensorFlow框架

    2020-04-15

举报

0/150
提交
取消
课程须知
1、Python基础 2、TensorFlow基础
老师告诉你能学到什么?
1、如何使用tensorflow训练一个mnist数据集; 2、如何将mnist数据集使用flask打包成api接口; 3、如何通过前端web框架调用模型接口;

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!