基于TensorFlow的CNN实现Mnist手写数字识别,带有可视化界面
时间: 2024-05-13 19:16:48 浏览: 195
好的,以下是基于TensorFlow的CNN实现Mnist手写数字识别的代码,带有可视化界面。
首先需要安装必要的库,包括TensorFlow、Tkinter和Pillow:
```
pip install tensorflow
pip install tkinter
pip install pillow
```
然后,我们需要下载Mnist数据集。可以在TensorFlow的官方GitHub页面找到下载链接,或者使用以下代码下载:
```
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
```
接下来,我们可以开始构建CNN模型。下面的代码展示了一个简单的CNN模型:
```
import tensorflow as tf
# Define parameters
learning_rate = 0.001
training_iters = 20000
batch_size = 128
display_step = 10
# Network parameters
n_input = 784
n_classes = 10
dropout = 0.75
# Create placeholders
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32)
# Create convnet
def conv2d(x, W, b, strides=1):
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
def maxpool2d(x, k=2):
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')
def conv_net(x, weights, biases, dropout):
x = tf.reshape(x, shape=[-1, 28, 28, 1])
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
conv1 = maxpool2d(conv1, k=2)
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
conv2 = maxpool2d(conv2, k=2)
fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
fc1 = tf.nn.dropout(fc1, dropout)
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
return out
# Initialize weights and biases
weights = {
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
'out': tf.Variable(tf.random_normal([1024, n_classes]))
}
biases = {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bd1': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
# Construct model
pred = conv_net(x, weights, biases, keep_prob)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Evaluate model
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
```
接下来,我们可以开始训练模型,同时在训练过程中使用Tkinter创建一个可视化界面,用于展示模型的训练过程和识别结果。以下是完整的代码:
```
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import tkinter as tk
from PIL import Image, ImageDraw
# Define parameters
learning_rate = 0.001
training_iters = 20000
batch_size = 128
display_step = 10
# Network parameters
n_input = 784
n_classes = 10
dropout = 0.75
# Create placeholders
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32)
# Create convnet
def conv2d(x, W, b, strides=1):
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
def maxpool2d(x, k=2):
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')
def conv_net(x, weights, biases, dropout):
x = tf.reshape(x, shape=[-1, 28, 28, 1])
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
conv1 = maxpool2d(conv1, k=2)
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
conv2 = maxpool2d(conv2, k=2)
fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
fc1 = tf.nn.dropout(fc1, dropout)
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
return out
# Initialize weights and biases
weights = {
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
'out': tf.Variable(tf.random_normal([1024, n_classes]))
}
biases = {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bd1': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
# Construct model
pred = conv_net(x, weights, biases, keep_prob)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Evaluate model
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# Start training
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
step = 1
while step * batch_size < training_iters:
batch_x, batch_y = mnist.train.next_batch(batch_size)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, keep_prob: dropout})
if step % display_step == 0:
acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y, keep_prob: 1.})
print("Step " + str(step*batch_size) + ", Training Accuracy= " + "{:.5f}".format(acc))
step += 1
print("Optimization Finished!")
# Create Tkinter GUI
root = tk.Tk()
root.title("Mnist Digit Recognition")
# Create canvas for drawing
canvas_width = 200
canvas_height = 200
canvas = tk.Canvas(root, width=canvas_width, height=canvas_height, bg="white")
canvas.pack()
# Create PIL image for drawing
image = Image.new("L", (canvas_width, canvas_height), 0)
draw = ImageDraw.Draw(image)
# Define function for classifying drawn digit
def classify_digit():
# Resize image to 28x28
digit_image = image.resize((28, 28))
# Convert image to numpy array
digit_array = tf.keras.preprocessing.image.img_to_array(digit_image)
digit_array = digit_array.reshape((1, 784))
digit_array = digit_array.astype('float32')
digit_array /= 255
# Classify digit using trained model
prediction = sess.run(tf.argmax(pred, 1), feed_dict={x: digit_array, keep_prob: 1.})
# Display prediction
prediction_label.config(text="Prediction: " + str(prediction[0]))
# Define function for clearing canvas
def clear_canvas():
canvas.delete("all")
draw.rectangle((0, 0, canvas_width, canvas_height), fill=0)
# Create buttons and labels
classify_button = tk.Button(root, text="Classify", command=classify_digit)
classify_button.pack(side="top")
clear_button = tk.Button(root, text="Clear", command=clear_canvas)
clear_button.pack(side="top")
prediction_label = tk.Label(root, text="")
prediction_label.pack(side="bottom")
# Define canvas event handlers
def on_left_button_down(event):
canvas.bind("<B1-Motion>", on_mouse_move)
def on_left_button_up(event):
canvas.unbind("<B1-Motion>")
def on_mouse_move(event):
x, y = event.x, event.y
canvas.create_oval(x-10, y-10, x+10, y+10, fill="black")
draw.ellipse((x-10, y-10, x+10, y+10), fill=255)
canvas.bind("<Button-1>", on_left_button_down)
canvas.bind("<ButtonRelease-1>", on_left_button_up)
root.mainloop()
```
在训练过程中,程序会打印出每个batch的训练准确率。在训练完成后,程序会创建一个Tkinter窗口,包含一个用于绘制手写数字的画布、一个用于清除画布的按钮、一个用于识别手写数字并显示结果的按钮,以及一个用于显示识别结果的标签。
阅读全文