――It's very easy to read and the comments are carefully written, so even I, a Python beginner, could understand what I was doing.
main.py
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import sys
import cv2
import random
import numpy as np
import tensorflow as tf
import tensorflow.python.platform
#Number of identification labels(This time Kyo:0,Kaoru: 1,Shinya:2 so 3)
NUM_CLASSES = 3
#Image size when learning(px)
IMAGE_SIZE = 28
#Number of dimensions of the image(28* 28*Color(?))
IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*3
#Set the path of data required for learning and the scale of learning
#TensorFlow built-in function that can register parameter settings, default values and help screen explanations
flags = tf.app.flags
FLAGS = flags.FLAGS
#Training data
flags.DEFINE_string('train', '/workspace/dir/train/data.txt', 'File name of train data')
#Verification test data
flags.DEFINE_string('test', '/workspace/dir/test/data.txt', 'File name of train data')
#Folder where data is placed
flags.DEFINE_string('train_dir', /workspace/dir/data', 'Directory to put the training data.')
#Number of data learning training trials
flags.DEFINE_integer('max_steps', 100, 'Number of steps to run trainer.')
#How many images to use in one learning
flags.DEFINE_integer('batch_size', 20, 'Batch size Must divide evenly into the dataset sizes.')
#If the learning rate is too small, learning will not proceed, and if it is too large, the error will not converge or diverge.
flags.DEFINE_float('learning_rate', 1e-4, 'Initial learning rate.')
#AI learning model part(neural network)To create
# images_placeholder:Image placeholder, keep_prob:dropout rate place_holder becomes an argument
#Outputs and returns the probability of each label for the input image
####
###Add learning model processing here
####
if __name__ == '__main__':
#Open file
f = open(FLAGS.train, 'r')
#Array to put data
train_image = []
train_label = []
for line in f:
#Separated with spaces except line breaks
line = line.rstrip()
l = line.split()
#Read data and reduce to 28x28
img = cv2.imread(l[0])
img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
#0 after lining up-Set to a float value of 1
train_image.append(img.flatten().astype(np.float32)/255.0)
#Label 1-of-Prepare with k method
tmp = np.zeros(NUM_CLASSES)
tmp[int(l[1])] = 1
train_label.append(tmp)
#Convert to numpy format
train_image = np.asarray(train_image)
train_label = np.asarray(train_label)
f.close()
f = open(FLAGS.test, 'r')
test_image = []
test_label = []
for line in f:
line = line.rstrip()
l = line.split()
img = cv2.imread(l[0])
img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
test_image.append(img.flatten().astype(np.float32)/255.0)
tmp = np.zeros(NUM_CLASSES)
tmp[int(l[1])] = 1
test_label.append(tmp)
test_image = np.asarray(test_image)
test_label = np.asarray(test_label)
f.close()
#Specify the scope to be output to the graph of TensorBoard
with tf.Graph().as_default():
#Tensor for inserting images(28*28*3(IMAGE_PIXELS)Any number of dimensional images(None)I have a minute)
images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS))
#Tensor to put a label(3(NUM_CLASSES)Any number of dimensional labels(None)Enter minutes)
labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES))
#Temporary Tensor to put dropout rate
keep_prob = tf.placeholder("float")
# inference()To create a model
logits = inference(images_placeholder, keep_prob)
# loss()To calculate the loss
loss_value = loss(logits, labels_placeholder)
# training()To train and adjust the parameters of the learning model
train_op = training(loss_value, FLAGS.learning_rate)
#Accuracy calculation
acc = accuracy(logits, labels_placeholder)
#Ready to save
saver = tf.train.Saver()
#Creating a Session(TensorFlow calculations must be done in an absolute Session)
sess = tf.Session()
#Variable initialization(Initialize when starting Session)
sess.run(tf.global_variables_initializer())
#TensorBoard display settings(Tensor Board Declarative?)
summary_op = tf.summary.merge_all()
# train_Specify the path to output the TensorBoard log with dir
summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph)
#Actually max_Execute training as many times as step
for step in range(FLAGS.max_steps):
for i in range(len(train_image)/FLAGS.batch_size):
# batch_Training for size images
batch = FLAGS.batch_size*i
# feed_Specify the data to put in the placeholder with dict
sess.run(train_op, feed_dict={
images_placeholder: train_image[batch:batch+FLAGS.batch_size],
labels_placeholder: train_label[batch:batch+FLAGS.batch_size],
keep_prob: 0.5})
#Calculate the accuracy after each step
train_accuracy = sess.run(acc, feed_dict={
images_placeholder: train_image,
labels_placeholder: train_label,
keep_prob: 1.0})
print "step %d, training accuracy %g"%(step, train_accuracy)
#Add a value to be displayed on the TensorBoard after each step
summary_str = sess.run(summary_op, feed_dict={
images_placeholder: train_image,
labels_placeholder: train_label,
keep_prob: 1.0})
summary_writer.add_summary(summary_str, step)
#Display accuracy for test data after training
print "test accuracy %g"%sess.run(acc, feed_dict={
images_placeholder: test_image,
labels_placeholder: test_label,
keep_prob: 1.0})
#Learn the data and save the final model
# "model.ckpt"Is the output file name
save_path = saver.save(sess, "model2.ckpt")
--The TensorFlow function in the original source code is old, so replace it. http://qiita.com/shu223/items/ef160cbe1e9d9f57c248 http://qiita.com/TokyoMickey/items/f6a9251f5a59120e39f8 --If the number of identification labels is 1, it was useless. (At first, I thought I could only go to Kyo)
-I made a Dir en gray face classifier using TensorFlow --(1) Introduction -I made a face classifier for Dir en gray using TensorFlow-② Environment construction -I made a face classifier for Dir en gray using TensorFlow-③ Image collection -I made a face classifier for Dir en gray using TensorFlow-④ Face extraction -I made a face classifier for Dir en gray using TensorFlow-⑤ Learning data preparation -I made a Dir en gray face classifier using TensorFlow-⑥ Learning program -I made a face classifier for Dir en gray using TensorFlow-⑦ Learning model -I made a face classifier for Dir en gray using TensorFlow-⑧ Learning execution -I made a Dir en gray face classifier using TensorFlow --⑨ Data visualization -I made a Dir en gray face classifier using TensorFlow --⑩ Face classification test -I made a Dir en gray face classifier using TensorFlow-⑪ Web release preparation -I made a Dir en gray face classifier using TensorFlow --⑫ Web release -I tried to make a Dir en gray face classifier using TensorFlow --⑬ Playing (final)
Recommended Posts