Recognition of TensorFlow learning the realization of a single picture (python handwritten digit) __python

Source: Internet
Author: User
Tags constant

Let's say we've installed the TensorFlow.

Generally in the installation of good TensorFlow, will run its demo, and the most common demo is handwritten digit recognition of the demo, that is, mnist data set.

However, we just ran its demo, maybe a lot of people will have the same ideas as I do, if you bring a digital picture, how to use our training network model to identify, the following we will be Mnist demo to achieve it.

1. Training model

First we need to train the model and save the model MODEL.CKPT to the specified folder.

Saver = Tf.train.Saver () 

saver.save (Sess, "model_data/model.ckpt")

Add the above two lines of code to the training code, the training is completed after the model can be saved, if this part of the problem, you can check the data Baidu, TensorFlow How to save the training model, where we will not wordy.

2. Test model

After we've trained the model, we'll save it in the Model_data folder, and you'll notice that there are 4 files in the folder.


Then we can test the model, put the images to be tested under the Images folder, and execute

#-*-coding:utf-8-*-import cv2 import tensorflow as TF import numpy as NP from sys import path Path.append ('.
/..') From common import extract_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 TF. Variable (initial) #输入特征x with convolution kernel W for convolution operations, strides for convolution kernel moving step length, #padding表示是否需要补齐边缘像素使输出图像大小不变 def conv2d (x, W): return tf.nn.conv2d (x, W, strides=[1, 1, 1, 1], padding= ' SAME ') #对x进行最大池化操作, ksize the range of the pooling, Def max_pool_2x2 (x): Return Tf.nn.max _pool (x, Ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding= ' SAME ') def main (): #定义会话 sess = tf. InteractiveSession () #声明输入图片数据, category x = Tf.placeholder (' float ', [none,784]) x_img = Tf.reshape (x, [ -1,28,28,1]) W_con V1 = weight_variable ([5, 5, 1,]) B_CONV1 = bias_variable ([+]) W_conv2 = Weight_variable ([5,5,32,64]) B_conv2 = bias _variable ([]) W_FC1 = weight_variable ([7*7*64,1024]) B_FC1 = bias_variable ([1024]) W_FC2 = Weight_variable ([1024,10]) B_FC2 = Bias_variable ([ten]) saver = Tf.train.Save R (WRITE_VERSION=TF.TRAIN.SAVERDEF.V1) Saver.restore (sess, ' model_data/model.ckpt ') #进行卷积操作, and add relu activation function H_CONV1 = tf. Nn.relu (conv2d (X_IMG,W_CONV1) + b_conv1) #进行最大池化 h_pool1 = max_pool_2x2 (h_conv1) #同理第二层卷积层 h_conv2 = Tf.nn.relu (conv2 D (h_pool1,w_conv2) + b_conv2) H_pool2 = max_pool_2x2 (h_conv2) #将卷积的产出展开 H_pool2_flat = Tf.reshape (h_pool2,[-1,7*7*64) ) #神经网络计算 and add the relu activation function h_fc1 = Tf.nn.relu (Tf.matmul (H_POOL2_FLAT,W_FC1) + b_fc1) #输出层, using Softmax for multiple classifications Y_conv=tf.nn.soft Max (Tf.matmul (H_FC1, W_FC2) + b_fc2) # Mnist_data_set = Extract_mnist. Mnistdataset ('.. /.. /data/') # x_img, y = mnist_data_set.next_train_batch (1) im = Cv2.imread (' images/888.jpg ', Cv2. Imread_grayscale). Astype (Np.float32) im = Cv2.resize (IM, (28,28), Interpolation=cv2. inter_cubic) #图片预处理 #img_gray = Cv2.cvtcolor (IM, cv2. Color_bgr2gray). Astype (Np.float32) #数据从0 ~255 to -0.5~0.5 Img_gRay = (IM-(255/2.0))/255 #cv2. Imshow (' Out ', Img_gray) #cv2. Waitkey (0) x_img = Np.reshape (Img_gray, [-1, 784])  Print x_img output = Sess.run (Y_conv, feed_dict = {x:x_img}) print ' Y_con: ', ' \ n ', output print ' The Predict is

 : ', Np.argmax (output) #关闭会话 sess.close () if __name__ = = ' __main__ ': Main ()

OK, put on the effect chart.


Output:




And then put a cifar10 on the end, feel my input data is a bit problematic, because the direct read CIFAR10 data test is no problem, but instead of their own pictures to do preprocessing after the input results have problems, (reference: CV2 read the data is BGR order, PiL read the data is RGB order, CIFAR10 data is RGB order), which child shoes can be pointed out remember the message told me

#-*-Coding:utf-8-*-from-sys import path import numpy as NP import TensorFlow as TF import time import cv2 from PiL Import Image path.append ('..
/..')  From common import extract_cifar10 from common import inspect_image #初始化单个卷积核上的参数 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) #卷积操作 def conv2d (X, W): Return tf.nn.conv2d (x, W, strides=[1, 1, 1, 1], padding= ' SAME ') def main (): #定义会话 sess = tf.

	InteractiveSession () #声明输入图片数据, category x = Tf.placeholder (' float ', [none,32,32,3]) Y_ = Tf.placeholder (' float ', [none,10]) #第一层卷积层 w_conv1 = Weight_variable ([5, 5, 3,]) B_CONV1 = Bias_variable ([]) #进行卷积操作, and add relu activation function CONV1 = Tf.nn.relu (conv2d (X,W_CONV1) + b_conv1) # pool1 pool1 = Tf.nn.max_pool (Conv1, Ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],padding= ' SA ME ', name= ' pool1 ') # Norm1 Norm1 = TF.NN.LRN (pool1, 4, bias=1.0, alpha=0.001/9.0, beta=0.75,name= ' Norm1 ') #第二层卷积层 w_conv2 = Weight_variable ([5,5,64,64]) b_conv2 = Bi As_variable ([]) Conv2 = Tf.nn.relu (conv2d (norm1,w_conv2) + b_conv2) # Norm2 Norm2 = TF.NN.LRN (Conv2, 4, bias=1.0, ALP ha=0.001/9.0, beta=0.75,name= ' Norm2 ') # pool2 pool2 = Tf.nn.max_pool (Norm2, Ksize=[1, 3, 3, 1],strides=[1, 2, 2, 1], p Adding= ' SAME ', name= ' pool2 ') #全连接层 #权值参数 w_fc1 = weight_variable ([8*8*64,384]) #偏置值 B_FC1 = bias_variable ([384]) #将 The output of the convolution expands Pool2_flat = Tf.reshape (pool2,[-1,8*8*64]) #神经网络计算 and adds the Relu activation function FC1 = Tf.nn.relu (Tf.matmul (POOL2_FLAT,W_FC1) + b _FC1) #全连接第二层 #权值参数 w_fc2 = weight_variable ([384,192]) #偏置值 B_FC2 = Bias_variable ([)] #神经网络计算 and add relu activation function FC2 = Tf.nn.relu (Tf.matmul (FC1,W_FC2) + b_fc2) #输出层, using Softmax for multiple classifications W_FC2 = Weight_variable ([192,10]) B_FC2 = Bias_variabl E ([ten]) Y_conv=tf.maximum (Tf.nn.softmax (Tf.matmul (FC2, W_FC2) + b_fc2), 1e-30) # saver = Tf.train.Saver () saver.restor E (sess, ' Model_data/modEl.ckpt ') #input im = Image.open (' images/dog8.jpg ') im.show () im = Im.resize ((32,32)) # r, g, B = im.split () # IM = Image.merge ("RGB", (r,g,b)) print im.size, Im.mode im = Np.array (IM). Astype (Np.float32) im = Np.reshape (IM, [ -1,3 2*32*3]) im = (im-(255/2.0))/255 BATCH_XS = Np.reshape (IM, [ -1,32,32,3]) #print batch_xs #获取cifar10数据 # cifar1 0_data_set = Extract_cifar10. Cifar10dataset ('.. /.. /data/') # Batch_xs, Batch_ys = Cifar10_data_set.next_train_batch (1) # print Batch_ys output = Sess.run (Y_conv, Feed_d ICT={X:BATCH_XS}) Print output print ' The Out of is: ', Np.argmax (output) #关闭会话 sess.close () if __name__ = = ' __main __ ': Main ()


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.