mnist_testdemo/mnist/model.py
import tensorflow as tf
# Y=W*x+b 线性模型
def regression(x):
W = tf.Variable(tf.zeros([784, 10]), name="W")
b = tf.Variable(tf.zeros([10]), name="b")
y = tf.nn.softmax(tf.matmul(x, W) + b)
return y, [W, b]
# 卷积模型
def convolutional(x, keep_prob):
# 卷积层
def conv2d(x, W):
return tf.nn.conv2d(x, W, [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_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(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 = max_pool_2x2(h_conv2)
# full connection
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]
mnist_testdemo/mnist/input_data.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import gzip
import os
import tempfile
import numpy
from six.moves import urllib
from six.moves import xrange
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
mnist_testdemo/mnist/regression.py
import os
import input_data
import model
import tensorflow as tf
# 从input_data中下载数据到MNIST_data
data = input_data.read_data_sets('MNIST_data', one_hot=True)
# create model
with tf.variable_scope("regression"):
# 用户输入占位符
x = tf.placeholder(tf.float32, [None, 784])
y, variables = model.regression(x)
# train
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(20000):
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("Saved:", path)
生成mnist_testdemo/mnist/data/regression.ckpt.data-00000-of-00001和mnist_testdemo/mnist/data/regression.ckpt.index
mnist_testdemo/mnist/convolutional.py
import os
import model
import tensorflow as tf
import input_data
data = input_data.read_data_sets('MNIST_data', one_hot=True)
#model
with tf.variable_scope("convolutional"):
x = tf.placeholder(tf.float32, [None, 784], name='x')
keep_prob = tf.placeholder(tf.float32)
y, variables = model.convolutional(x, keep_prob)
#train
y_ = tf.placeholder(tf.float32, [None, 10], name='y')
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()
summay_writer = tf.summary.FileWriter('./mnist_log/1', sess.graph)
summay_writer.add_graph(sess.graph)
sess.run(tf.global_variables_initializer())
for i in range(20000):
batch = data.train.next_batch(50)
if i % 100 == 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))
sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print(sess.run(accuracy, feed_dict={x: data.test.images, y_: data.test.labels, keep_prob: 1.0}))
path = saver.save(
sess, os.path.join(os.path.dirname(__file__), 'data', 'convalutional.ckpt'),
write_meta_graph=False, write_state=False)
print("Saved:", path)
生成 mnist_testdemo/mnist/data/convalutional.ckpt.data-00000-of-00001和mnist_testdemo/mnist/data/convalutional.ckpt.index
mnist_testdemo/main.py
# -*- coding:utf-8 -*-
import numpy as np
import tensorflow as tf
from flask import Flask, jsonify, render_template, request
import pprint
from mnist import model
x = tf.placeholder("float", [None, 784])
sess = tf.Session()
# 取出训练好的线性模型
with tf.variable_scope("regression"):
y1, variables = model.regression(x)
saver = tf.train.Saver(variables)
saver.restore(sess, "mnist/data/regression.ckpt")
# 取出训练好的卷积模型
with tf.variable_scope("convolutional"):
keep_prob = tf.placeholder("float")
y2, variables = model.convolutional(x, keep_prob)
saver = tf.train.Saver(variables)
saver.restore(sess, "mnist/data/convalutional.ckpt")
# 根据输入调用线性模型并返回识别结果
def regression(input):
return sess.run(y1, feed_dict={x: input}).flatten().tolist()
# 根据输入调用卷积模型并返回识别结果
def convolutional(input):
return sess.run(y2, feed_dict={x: input, keep_prob: 1.0}).flatten().tolist()
app = Flask(__name__)
@app.route('/api/mnist', methods=['POST'])
def mnist():
# pprint.pprint(request.json)
input = ((255 - np.array(request.json, dtype=np.uint8)) / 255.0).reshape(1, 784)
output1 = regression(input)
output2 = convolutional(input)
pprint.pprint(output1)
pprint.pprint(output2)
return jsonify(results=[output1, output2])
@app.route('/')
def main():
return render_template('index.html')
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=8889)
drawInput() {
var ctx = this.input.getContext('2d');
var img = new Image();
img.onload = () => {
var inputs = [];
var small = document.createElement('canvas').getContext('2d');
small.drawImage(img, 0, 0, img.width, img.height, 0, 0, 28, 28);
var data = small.getImageData(0, 0, 28, 28).data;
for (var i = 0; i < 28; i++) {
for (var j = 0; j < 28; j++) {
var n = 4 * (i * 28 + j);
inputs[i * 28 + j] = (data[n + 0] + data[n + 1] + data[n + 2]) / 3;
ctx.fillStyle = 'rgb(' + [data[n + 0], data[n + 1], data[n + 2]].join(',') + ')';
ctx.fillRect(j * 5, i * 5, 5, 5);
}
}
if (Math.min(...inputs) === 255) {
return;
}
$.ajax({
url: '/api/mnist',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(inputs),
success: (data) => {
data = JSON.parse(data);
for (let i = 0; i < 2; i++) {
var max = 0;
var max_index = 0;
for (let j = 0; j < 10; j++) {
var value = Math.round(data.results[i][j] * 1000);
if (value > max) {
max = value;
max_index = j;
}
var digits = String(value).length;
for (var k = 0; k < 3 - digits; k++) {
value = '0' + value;
}
var text = '0.' + value;
if (value > 999) {
text = '1.000';
}
$('#output tr').eq(j + 1).find('td').eq(i).text(text);
}
for (let j = 0; j < 10; j++) {
if (j === max_index) {
$('#output tr').eq(j + 1).find('td').eq(i).addClass('success');
} else {
$('#output tr').eq(j + 1).find('td').eq(i).removeClass('success');
}
}
}
}
});
};
img.src = this.canvas.toDataURL();
}
参考:https://blog.csdn.net/welggy/article/details/100154164
训练运行查看CPU性能
可以看到使用CPU训练利用率很高,而且训练速度也是超级慢
使用GPU训练的话相对来讲就快多了