Tensorflow 2: Run mnist examples

What is mnist

MNIST is like the "Hello World" of machine learning. Its a database of handwritten digits (0-9), with which you can try out a few machine learning algorithms.


Download the examples

Download the related examples from below link:

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/tutorials/mnist


Download the dataset of mnist

Download the following files from link http://yann.lecun.com/exdb/mnist/, and then copy them to /tmp/tensorflow/mnist/input_data/

train-images-idx3-ubyte.gz:  training set images (9912422 bytes) 
train-labels-idx1-ubyte.gz:  training set labels (28881 bytes) 
t10k-images-idx3-ubyte.gz:   test set images (1648877 bytes) 

t10k-labels-idx1-ubyte.gz:   test set labels (4542 bytes)


Run mnist examples

1. fully_connected_feed.py : Trains and evaluates the MNIST network using a feed dictionary

   command: python3.5 fully_connected_feed.py

2. mnist_with_summaries.py: A simple MNIST cassifier which displays summaries in Tensorboard

   command: python3.5 mnist_with_summaries.py --log_dir=/tmp/test_log/

            tensorboard --logdir=/tmp/test_log/

   and then connect to tensorboard via chrome: 127.0.0.1:6006




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是基于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窗口,包含一个用于绘制手写数字的画布、一个用于清除画布的按钮、一个用于识别手写数字并显示结果的按钮,以及一个用于显示识别结果的标签。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值