[PYTHON] I made a Dir en gray face classifier using TensorFlow --⑥ Learning program

Introduction

――It's very easy to read and the comments are carefully written, so even I, a Python beginner, could understand what I was doing.

program

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")

Supplements and excuses

--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)

All page links

-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

I made a Dir en gray face classifier using TensorFlow --⑥ Learning program
I made a Dir en gray face classifier using TensorFlow --- ⑧ Learning execution
I made a Dir en gray face classifier using TensorFlow --- ⑦ Learning model
I made a Dir en gray face classifier using TensorFlow --(1) Introduction
I made a Dir en gray face classifier using TensorFlow-④ Face extraction
I made a Dir en gray face classifier using TensorFlow --⑩ Face classification test
I made a Dir en gray face classifier using TensorFlow --⑬ Playing (final)
I made a Dir en gray face classifier using TensorFlow --⑫ Web release
I made a Dir en gray face classifier using TensorFlow --② Environment construction
I made a Dir en gray face classifier using TensorFlow --⑪ Web release preparation
[Python] I made a classifier for irises [Machine learning]
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
I made a VGG16 model using TensorFlow (on the way)
I made a C ++ learning site
I made a Line-bot using Python!
Make a face recognizer using TensorFlow
Face detection using a cascade classifier
I made a payroll program in Python!
I tried playing a ○ ✕ game using TensorFlow
Beginner: I made a launcher using dictionary
A story about simple machine learning using TensorFlow
I made a Caesar cryptographic program in Python.
I tried to make a ○ ✕ game using TensorFlow
I made a login / logout process using Python Bottle.
I made a vim learning game "PacVim" with Go
I made a prime number generation program in Python 2
I made a school festival introduction game using Ren’py
PyTorch Learning Note 2 (I tried using a pre-trained model)
〇✕ I made a game
I made a quick feed reader using feedparser in Python
I tried using Tensorboard, a visualization tool for machine learning
I made a learning kit for word2vec / doc2vec / GloVe / fastText
I made a face diagnosis AI for a female professional golfer ③
I made a muscle training estimation app using Qore SDK
I made an original program guide using the NHK program guide API.
I made a Chatbot using LINE Messaging API and Python
I tried face recognition using Face ++
I made a python text
I tried using magenta / TensorFlow
I made a discord bot
I made a program to solve (hint) Saizeriya's spot the difference
I made a game called Battle Ship using pygame and tkinter
I made a program that solves the spot the difference in seconds
Don't you want to say that you made a face recognition program?
I tried scoring a transvestite contest using Face ++'s Detect API
I made a prime number table output program in various languages
I made a poker game server chat-holdem using websocket with python
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
I made a program that automatically calculates the zodiac with tkinter
[Kaggle] I made a collection of questions using the Titanic tutorial