[PYTHON] Create an AI that identifies Zuckerberg's face by deep learning ② (AI model construction)

This is a continuation of the previous article "Creating AI to identify Zuckerberg's face by deep learning ① (learning data preparation)". This article is Part 2.

What we make is "AI that identifies Zuckerberg's face". Use TensorFlow from Google's deep learning library. Sample videos of what I made this time are here.

zuck4.png zgif2.gif

Previous article has collected face image data to be trained by TensorFlow, so this time we will build an AI model of TensorFlow! The work of the TensorFlow part is rough

** "(1) Design of TensorFlow learning model → (2) Train and train face data → (3) Make it possible to judge the face of any image using the learning result" **

I will proceed with the flow. This time, we will describe the procedure of ** "Designing a learning model of TensorFlow (designing a neural network)" **.

Then, since we collected face image data for learning last time, the learning model design is as follows!

② Design a learning model for TensorFlow

1. Before using TensorFlow

If you don't have the prerequisite knowledge to actually do deep learning with TensorFlow, the following will be meaningless, so I will introduce the ones that are especially helpful and strongly memorable when I am a beginner.

[Deep learning related]

Neural network and deep learning It was a great learning experience for me to explain in detail what is happening behind the scenes at the mathematical formula level. I think you should read it after you reach the point where you've learned about deep learning, but you don't really understand it. However, it is too detailed and I understand only about 10%. ② [Does artificial intelligence exceed humans (book)](https://www.amazon.co.jp/%E4%BA%BA%E5%B7%A5%E7%9F%A5%E8%83%BD % E3% 81% AF% E4% BA% BA% E9% 96% 93% E3% 82% 92% E8% B6% 85% E3% 81% 88% E3% 82% 8B% E3% 81% 8B-% E8% A7% 92% E5% B7% 9D% EF% BC% A5% EF% BC% B0% EF% BC% B5% EF% BC% A2% E9% 81% B8% E6% 9B% B8-% E6 % 9D% BE% E5% B0% BE-% E8% B1% 8A-ebook / dp / B00UAAK07S) A book by Mr. Matsuo of the University of Tokyo that is like "This person is speaking of deep learning!" In Japan (??). I read it about two years ago and it was on my Kindle so I read it again. It is very easy to understand because it explains "What is deep learning?" So that anyone can understand it without using any difficult words or mathematical formulas. It may be the easiest to understand.

[TensorFlow related]

Sugyan memo He has written various examples using TensorFlow and is beyond the memo level. (If you overlooked this note, Interview article will be recommended as an advertisement every time you open Facebook. W) ④ TensorFlow tutorial Japanese translation He has translated the official TensorFlow tutorial into Japanese. I am grateful that both beginners and advanced students have translated it into Japanese. ⑤ I'm not a programmer or a data scientist in particular, but I touched Tensorflow for a month, so it's super easy to understandSince I touched Tensorflow for 2 months, I explained the convolutional neural network in an easy-to-understand manner with 95.04% of "handwritten hiragana" identification The above two explained the TensorFlow tutorial, which was very helpful during learning! ⑦ Momokuro member face recognition by TensorFlowIdentify the anime Yuruyuri production company with TensorFlow This time, I have referred to the above two at the code level. I couldn't make something that would work without this article. I am deeply grateful m (_ _) m

Looking at it like this, there are surprisingly many people in Japan who are touching TensorFlow. It was the impression that. Thank you to everyone who helped me m (_ _) m

2. Design a learning model for TensorFlow

Now, let's finally write the processing of TensorFlow. TensorFlow's ** "Learning Model Design" ** is considered to be the most important part of deep learning. Perhaps it is no exaggeration to say that this ** "designing a learning model (neural network) is deep learning" **.

Before rushing into here, please try TensorFlow tutorial etc. and make sure that TensorFlow is installed at least and it works normally. (It's even better to try the advanced CNN tutorial)

TensorFlow setup and tutorials are omitted here. Reference: TensorFlow MNIST For ML Beginners Tutorial Implementation

The structure of the TensorFlow learning model (neural network) this time will be ** convolutional neural network (CNN) ** explained in the expert edition of the TensorFlow tutorial. Since we will proceed with the structure of one layer of hidden layers, I think that it has exactly the same structure as the tutorial **.

As for the directory structure, I put the face image data folder and the processing files related to tensorflow into a folder called tensorflow, which was created by installing tensorflow according to the formula. (This time, the processing related to learning model design and learning implementation is put together in a file called `main.py```. The code below is a part of `main.py```.)

Directory structure


/tensoflow
  main.py(I will write the learning model and learning process here)
  eval.py(A file that returns the case law results of any image)
  /data(Face data collected in the previous article)
    /train
      /zuckerbuerg
      /elonmusk
      /billgates
      data.txt
    /test
      /zuckerbuerg
      /elonmusk
      /billgates
      data.txt
After that, the folders and files created when tensorflow was installed are in the tensorflow folder.

(I tried my best to leave a comment in the code, but I do not understand it perfectly, and I would appreciate it if you could point out that there is a misunderstanding m (_ _) m Anyway, I got the impression that the functions provided by TensorFlow did all the detailed calculations. )

main.py(Learning model part of)


  #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
  def inference(images_placeholder, keep_prob):

    #Weight with standard deviation 0.Initialize with a normal distribution of 1
    def weight_variable(shape):
      initial = tf.truncated_normal(shape, stddev=0.1)
      return tf.Variable(initial)

    #Bias standard deviation 0.Initialize with a normal distribution of 1
    def bias_variable(shape):
      initial = tf.constant(0.1, shape=shape)
      return tf.Variable(initial)

    #Create a convolution layer
    def conv2d(x, W):
      return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

    #Create a pooling layer
    def max_pool_2x2(x):
      return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                            strides=[1, 2, 2, 1], padding='SAME')

    #28px image data input in vector format*Return to 28px image(Perhaps)。
    #This time it's a color image, so 3(1 for monochrome)
    x_image = tf.reshape(images_placeholder, [-1, IMAGE_SIZE, IMAGE_SIZE, 3])

    #Create the first layer of the convolution layer
    with tf.name_scope('conv1') as scope:
      #The argument is[width, height, input, filters]。
      # 5px*Filtering the image in the 5px range
      #Input is 3 because this time it is a color image(Perhaps)
      #Detect 32 features
      W_conv1 = weight_variable([5, 5, 3, 32])
      #Substitute the bias value
      b_conv1 = bias_variable([32])
      #The parts that are likely to be useful as features are left, and the parts that are unlikely to be used as features are
      #Understanding that it is not treated as a feature as 0(Relu function)
      h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)

    #Creation of pooling layer 1
    # 2*Make a frame of 2 sizes and set the features in the frame to 1.*Converts to a size of 1(Compressing features)。
    #That frame 2*Understanding that slide 2 at a time to apply compression work to the entire image
    #Roughly summarize the features subdivided by rough understanding in a slightly better way(Compress)
    with tf.name_scope('pool1') as scope:
      h_pool1 = max_pool_2x2(h_conv1)

    #Creation of the second layer of the convolution layer
    with tf.name_scope('conv2') as scope:
      #Filtering is performed again with the output on the first layer as the input on the second layer.
      # 5px*Image in the range of 5px(?)Is filtering
      #The input is 32 because the output of 32 features of the first layer is input.
      #Detect 64 features
      W_conv2 = weight_variable([5, 5, 32, 64])
      #Substitute the bias value(Same as the first layer)
      b_conv2 = bias_variable([64])
      #Arrangement of detected features(Same as the first layer)
      h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)

    #Creation of pooling layer 2(Same as booring layer 1)
    with tf.name_scope('pool2') as scope:
      h_pool2 = max_pool_2x2(h_conv2)

    #Creation of fully connected layer 1
    with tf.name_scope('fc1') as scope:
      W_fc1 = weight_variable([7*7*64, 1024])
      b_fc1 = bias_variable([1024])
      #Convert image analysis to vector results
      h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
      #Like the first and second, it activates the detected features.
      h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
      #dropout settings
      #It's optimized only for training data, so it's not really usable
      #It seems to play a role in preventing "overfitting" that becomes AI
      h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    #Creation of fully connected layer 2(Read layer)
    with tf.name_scope('fc2') as scope:
      W_fc2 = weight_variable([1024, NUM_CLASSES])
      b_fc2 = bias_variable([NUM_CLASSES])

    #Normalization with softmax function
    #Convert the output of the neural network so far to the probability of each label
    with tf.name_scope('softmax') as scope:
      y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

    #Probability of each label(Something like?)return it. Add all the calculated probabilities of each label to get 1.
    return y_conv

This is the only learning model design, but what we are doing

** (1) Input what you want to characterize and convey it to the top (next) layer → (2) Learn various things according to the learning model of that layer to detect and output the features → (3) The previous layer The output (feature) of is transmitted as an input to the next layer, and ① to ③ are repeated for the number of layers → ④ The probability that the first input at the end matches the prepared label is calculated. return**

I'm aware that it's like that.

In TensorFlow, the data is in ** tensor format ** (data like a matrix), and the data flows steadily to each layer, so it's ** "TensorFlow" **. And, as this ** hierarchy gets deeper, we learn more and more features **, so it's ** "Deep Learning" **. I see! Easy-to-understand naming. w

3. Visualize the designed learning model (neural network)

A feature called ** TensorBoard ** that comes with TensorFlow allows you to see it visually with this designed neural network structure.

Access 6006!


#### **`./For data, specify the directory where the training data is stored. I put the images in a directory called data.`**

This TensorBoard seems to be one of the characteristic functions of TensorFlow, and it is certainly amazing to see various results easily. You can find out more about TensorBoard by google. You may not be able to see the graph until you actually train the data (?)

スクリーンショット 2017-05-08 22.19.21.png

Hmmmmmmmm. surely

** Reshape the image input as an image placeholder → input it to the 1st layer (conv1) → 1st pooling (pool1) → pass it to the 2nd layer (conv2) → …… **

It seems that the neural network structure of the designed learning model is exactly the same. great. Easy to understand.

The name of each layer (such as conv1) is named in the `with tf.name_scope ('conv1') as scope:` part of the learning model code above. It seems that the setting of TensorBorad itself is done in the part of `summary_writer = tf.train.SummaryWriter (FLAGS.train_dir, sess.graph_def)` that appears in the code later.

The hidden layer of this learning model is one layer, but the world's strongest AI Go software "AlphaGo" seems to be ridiculously complicated when looking at this neural network structure. (I think it was in an article somewhere.)

③ Let the learning model actually learn the data

This is the end of ** designing a learning model **, which seems to be the most important thing in using TensorFlow. (Although it is the same as this tutorial.) Next, let's actually use this designed learning model to learn the large amount of face data collected earlier. The continuation is likely to be long again, so from the article below ↓

Click here for the following ** Part 3 **: Create AI to identify Zuckerberg's face by deep learning ③ (Data learning)


** "Creating an AI that identifies Zuckerberg's face with deep learning" ** Part 1: Create AI to identify Zuckerberg's face by deep learning ① (Learning data preparation) Part 2: Making AI that identifies Zuckerberg's face by deep learning ② (AI model construction) Part 3: Create AI to identify Zuckerberg's face by deep learning ③ (Data learning) Part 4: Create AI to identify Zuckerberg's face by deep learning ④ (WEB construction)

GitHub:https://github.com/AkiyoshiOkano/zuckerberg-detect-ai

Bonus judgment result

Judgment result of playing with the Zuckerberg detector

Mr. Bill Gates who became able to judge by the flow of development gates5.png


Also Mr. Ilone Mask elon4.png


Judging President Trump スクリーンショット 2017-05-07 1.00.21.png

Recommended Posts

Create an AI that identifies Zuckerberg's face by deep learning ② (AI model construction)
Create an AI that identifies Zuckerberg's face by deep learning ④ (WEB construction)
Create an AI that identifies Zuckerberg's face by deep learning ① (Learning data preparation)
Create AI to identify Zuckerberg's face by deep learning ③ (Data learning)
Model construction for face image dataset sorting-VGG19 transfer learning (# 2)
[AI] Deep Metric Learning
Explain & actually move the SMIS model that can "virtually try on clothes" by deep learning
I tried to make Othello AI that I learned 7.2 million hands by deep learning with Chainer
Create an API that returns data from a model using turicreate
About the shortest path to create an image recognition model by machine learning and implement an Android application