Tensorflow trained to recognize custom picture

Many are just getting started TensorFlow entry or machine learning students want to be able to train the model through their designated picture source, then identify and classify your specified picture. However, TensorFlow official introductory tutorial, gives no clear method of how to customize data entry training model. Now, we refer to the official introductory course "Deep MNIST for Experts" of the contents of a (Portal: https://www.tensorflow.org/get_started/mnist/pros ), describes how to customize the picture input to TensorFlow training model.

In the code section of "Deep MNISTfor Experts", the program will TensorFlow comes mnist picture data collection mnist.train.images as a training input, mnist.test.images as input validation. After learning this section, we will marvel ultra-high recognition rate convolutional neural network, but for students just learning TensorFlow, the heart may produce a question mark: how to replace designated for their picture source mnist data set? For example, I want to change the picture source their own C drive inside the picture, it should be how to adjust the code?

We look at this section of the curriculum related to the code mnist picture call:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
batch = mnist.train.next_batch(50)
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

To implement a custom image input, you need to first set of pictures ready for collection. To save time, we mnist handwritten set of numbers one by one parsed, stored on their local hard disk, saved as bmp format, then put the local hard disk handwritten picture read out one by one, consisting of a collection then enter the neural network. See mnist way to extract handwritten digits episodes of " How to Export handwritten digital pictures from TensorFlow of mnist data sets ."

After you export pictures to a local mnist handwritten digital collective, you can follow the following python code to achieve a custom image of training:

Import os 

Import numpy AS NP
 Import tensorflow AS TF 

from PIL Import Image 

# first picture directory traversal is to get the total picture 
input_count = 0
 for i in the Range (0, 10 ): 
    dir = ' ./mnist_digits_images/%s/ ' i%   # here you can change your own image directory, i is the classification label 
    for RT, dirs, Files in os.walk (dir):
         for filename in Files: 
            input_count + = 1 # define the dimension and each dimension corresponds to the length of the array


= np.array input_images ([[0] * 784 for I in Range (input_count)]) 
input_labels = np.array ([[0] * 10 for I in Range (input_count)]) 

# the second pass image directory to generate picture data and label 
index = 0
 for i in the Range (0, 10 ): 
    dir = ' ./mnist_digits_images/%s/ ' % i   # here you can change your own image directory, i is the classification label 
    for RT, dirs, Files in os.walk (the dir):
         for filename in Files: 
            filename = the dir +filename 
            IMG = Image.open (filename) 
            width = img.size [0] 
            height = img.size [. 1 ]
             for H in Range (0, height):
                 for W in Range (0, width):
                     # By such processing the digital thin lines, help improve the recognition accuracy 
                    IF img.getpixel ((W, H))> 230 : 
                        input_images [index] [W + H width *] = 0   # previously been converted into a picture dimension 
                    the else : 
                        input_images [index] [W + H * width] =. 1
            input_labels [index] [I] =. 1 
            index +. 1 = # define input nodes, values of the matrix corresponds to a set of image pixels and the image tag (i.e., numbers represented) 
X = tf.placeholder (tf.float32, Shape = [None, 784 ]) 
Y_ = tf.placeholder (tf.float32, Shape = [None, 10 ]) 
x_image = tf.reshape (X, [-1, 28, 28,. 1 ]) # define a first convolutional layer variables and OPS 
W_conv1 = tf.Variable (tf.truncated_normal ([. 7,. 7,. 1, 32], STDDEV = 0.1 )) 
b_conv1 = tf.Variable (tf.constant (0.1, Shape = [32 ])) 
L1_conv = TF. nn.conv2d (x_image, W_conv1, Strides = [. 1,. 1,. 1,. 1], padding = ' SAME ' ) 
L1_relu





= tf.nn.relu(L1_conv + b_conv1)
L1_pool = tf.nn.max_pool(L1_relu, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

# 定义第二个卷积层的variables和ops
W_conv2 = tf.Variable(tf.truncated_normal([3, 3, 32, 64], stddev=0.1))
b_conv2 = tf.Variable(tf.constant(0.1, shape=[64]))

L2_conv = tf.nn.conv2d(L1_pool, W_conv2, strides=[1, 1, 1, 1], padding='SAME')
L2_relu = tf.nn.relu(L2_conv + b_conv2)
L2_pool = tf.nn.max_pool(L2_relu, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

# 全连接层
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1))
b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))

h_pool2_flat = tf.reshape(L2_pool, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

# dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

# readout层
W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1))
b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]))

y_conv Tf.matmul = (h_fc1_drop, W_fc2) + b_fc2 

# define and optimize the training OP 
cross_entropy = tf.reduce_mean (tf.nn.softmax_cross_entropy_with_logits (= Y_ Labels, logits = y_conv)) 
train_step = tf.train.AdamOptimizer ((1E- . 4 )). Minimize (cross_entropy) 
correct_prediction = tf.equal (tf.argmax (y_conv,. 1), tf.argmax (Y_,. 1 )) 
Accuracy = tf.reduce_mean (tf.cast (correct_prediction, tf.float32)) 

with tf.Session () AS Sess: 
    sess.run (tf.global_variables_initializer ()) 

    Print ( " total read% s input images, tags% s " % (input_count, input_count)) 

    #Set the number of input and each training iterations op, where the total number in order to support any image that defines a remainder remainder, for example, if the number of each training input op is 60, the total number of images is 150, then the two previous each input 60, the last entry 30 (residue 30) 
    the batch_size = 60 
    Iterations = 100 
    batches_count = int (input_count / the batch_size) 
    rEMAINDER = input_count% the batch_size
     Print ( " data set into% s batches foregoing batch% s data the last batch of data% s " % (batches_count + 1 , batch_size, REMAINDER)) 

    # perform iterative training 
    for IT in the Range (the iterations):
         # the key here is to make the input array into np.array 
        for the n- in the Range (batches_count): 
            train_step.run (feed_dict= {X: input_images [n-* the batch_size: (n-+. 1) * the batch_size], 
                                      Y_: input_labels [n- * the batch_size: (n-+. 1) * the batch_size], keep_prob: 0.5 })
         IF REMAINDER> 0: 
            start_index = batches_count * the batch_size; 
            train_step.run ( 
                feed_dict = {X: input_images [start_index: input_count -. 1], Y_: input_labels [start_index: input_count -. 1 ], 
                           keep_prob: 0.5 }) 

        # every five iterations is completed, it is determined whether the accuracy has reached 100 % to exit the iteration loop 
        iterate_accuracy = 0
         IF IT == 5% 0:
            iterate_accuracy = accuracy.eval(feed_dict={x: input_images, y_: input_labels, keep_prob: 1.0})
            print('iteration %d: accuracy %s' % (it, iterate_accuracy))
            if iterate_accuracy >= 1:
                break;

    print('完成训练!')

 Python code execution result of the shots are as follows:

 

For related code and model building code above, please refer to the contents of the official "Deep MNIST for Experts" will be an understanding. In this context, it is important to know is how to integrate local picture source feed_dict become an acceptable format. One of the most critical is that these two lines:

# Define the corresponding dimension of the array and the length of each dimension 
input_images np.array = ([[0] * 784 for I in Range (input_count)]) 
input_labels = np.array ([[0] * 10 for I in Range (input_count )])

They correspond to two placeholder feed_dict of:

x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

 

Transfer from https://www.jb51.net/article/166937.htm

Guess you like

Origin www.cnblogs.com/answerThe/p/11453458.html