[PYTHON] Deep learning image recognition 3 after model creation

Aidemy 2020/11/10

Introduction

Hello, it is Yope! I am a liberal arts student, but I was interested in the possibilities of AI, so I went to the AI-specialized school "Aidemy" to study. I would like to share the knowledge gained here with you, and I am summarizing it on Qiita. I am very happy that many people have read the previous summary article. Thank you! This is the third post for deep learning and image recognition. Nice to meet you.

What to learn this time ・ Convenient functions of Keras ・ About the method to improve the accuracy of the model

useful function

Display of learning process

-There is a way to visualize how the accuracy has changed as the learning progresses. For example, if "model.fit ()" is stored in the variable "history", the accuracy can be changed by __ "history.history ['acc']" __. The rest can be visualized by __ "plt.plot ()" __.

-Also, in the previous Chapter, the calculation of generalization accuracy was performed with __ "model.evaluate (X_test, y_test)" __, but in the argument of "model.fit ()" __ "validation_data = (X_test)" By adding ", y_test)" __, __ generalization accuracy is calculated in the same way __. -Similarly, when acquiring the change in generalization accuracy, it is recommended to use __ "history.history ['val_acc']" __ to visualize it.

・ Code![Screenshot 2020-11-09 16.39.21.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/698700/6865a878-5afe-94ef- 79b2-be27e002b530.png)

・ Result![Screenshot 2020-11-09 16.39.32.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/698700/e1669f65-d751-4b17- 4a83-d54edadfa214.png)

Early Stopping ・ Even if the number of learnings is too large, the accuracy will drop when it exceeds a certain level. In such cases, it is important to stop __learning early __. __ "Early Stopping" __ can be used in such cases. -Early Stopping has verification data in addition to training and test data, takes an error between the evaluation of that data and the evaluation of the training data for each learning, and stops learning at the timing when the error spreads. That is. -As a code, first create an instance as __ "es = EarlyStopping ()" __. All you have to do is set __ "callbacks = [es]" __ in the argument of model.fit (). -As an instance argument, specify what to monitor as an evaluation error with __ "monitor" __, and stop learning if the monitored value is not improved by how many epochs with __ "patience" __ For __ "mode" , "improve the model" indicates whether the specified value is "increased" or "decreased", respectively, __ "'max'" and "'min". Specify with'”. If you set this to __ "'auto'" __, it will guess automatically.

Model structure output

-When you want to check the structure of the model, there is a __ method to output this as a __ image. The only way to do this is __ "plot_model (model, to_file ='model.png')" __. -Also, if you want to set a name for each layer, add __ "name" __ to the argument when you use __ "model.add ()" __ to create a layer below.

·code

·result

How to improve accuracy

optimisation

・ It is often better to change the __learning rate as learning progresses. This is called __ "optimization of learning rate" __, and Adam, RMSprop, Adagrad, etc. are used.

Drop out

-Although it has appeared many times before, dropout can be mentioned as a method used to prevent overfitting and improve accuracy. This is a technique that makes it easier to generalize by randomly removing data.

Fine tuning (transfer learning)

-Fine tuning __ (transfer learning) __ has been learned before, but it is a method of learning using a part of __ other models __. In terms of image recognition, only the last fully connected layer is changed and the convolutional layer is used as it is. -As an image recognition model, Keras can download and use the five image classification models __ "Xception, VGG16, VGG19, ResNet50, Inception V3" __, and the weights learned by ImageNet. The following is an example of creating a model of VGG16.

スクリーンショット 2020-11-09 18.19.55.png

-For the above code, first give the input form with __ "input_tensor" __. After defining this, actually create a VGG model __. It can be created with "VGG16 ()" __. As an argument, __ "include_top" __ specifies whether to use the fully connected layer of the original model with True or False, and __ "weights ='imagenet'" __ specifies to use the weights learned by imagenet. , __ "input_tensor" __ Describe the input form defined earlier.

・ Once this is done, the next step is to create a separate model __ (top_model) __ near the output layer (fully connected layer). Since the input layer of top_model receives the output of the VGG model (convolutional layer), it is set as __ "Flatten ()" __. By setting input_shape to __ "vgg16.output_shape [1:]" __, the output shape (two-dimensional) of the VGG model can be obtained. -The rest is just like adding layers like a normal model. After adding up to the output layer, the next step is to create a final model with __ "input is vgg16.input, output is top_model with vgg output" __.

スクリーンショット 2020-11-09 18.44.49.png

-This is the end of adding layers, and from here on, we will compile and learn, but before that, set the weight of the __VGG model part so that it will not be changed __.

スクリーンショット 2020-11-09 18.44.58.png

-There are no particular changes in compilation and learning. Only one point, when performing transfer learning, the optimization function should be __ "SGD" __.

Summary

-Learning accuracy can be visualized by changing __ "history ['acc']" __ to __ "plt.plot ()" __. -Even if model.evaluate () is not used, model __ generalization accuracy is calculated __ by setting __ "validation_data" __ in model, fit (). -When using EarlyStopping, create an instance and then specify it with the argument __ "callbacks" __ of model.fit (). -If you want to visualize the model structure as an image, you can do it with __ "plot_model (model, to_file)" __. -As a method to improve accuracy, there are optimization and dropout, and transfer learning may be performed. For example, in the case of image recognition, the convolutional layer is transferred with VGG16, the model near the output layer is implemented by oneself, and the entire model is implemented by combining.

This time is over. Thank you for reading until the end.

Recommended Posts

Deep learning image recognition 3 after model creation
Deep learning image recognition 2 model implementation
Image recognition model using deep learning in 2016
Deep learning image recognition 1 theory
Implementation of Deep Learning model for image recognition
Read & implement Deep Residual Learning for Image Recognition
Learning model creation, learning and reasoning
Real-time image recognition on mobile devices with TensorFlow learning model
Deep Learning Model Lightening Library Distiller
[AI] Deep Learning for Image Denoising
Image recognition
Deep Learning
Deep learning learned by implementation 2 (image classification)
Image alignment: from SIFT to deep learning
Let's make Godzilla's image recognition model preprocessing, learning and deployment feel good
Deep Learning Memorandum
Start Deep learning
Inflated learning image
Face image dataset sorting using machine learning model (# 3)
Do image recognition with Caffe model Chainer Yo!
Python Deep Learning
Deep learning × Python
Model construction for face image dataset sorting-VGG19 transfer learning (# 2)
Deep learning image analysis starting with Kaggle and Keras
[Anomaly detection] Detect image distortion by deep distance learning
First Deep Learning ~ Struggle ~
Python: Deep Learning Practices
Deep learning / activation functions
Deep Learning from scratch
Image recognition with keras
Deep learning / cross entropy
First Deep Learning ~ Preparation ~
[AI] Deep Metric Learning
Pepper Tutorial (7): Image Recognition
Python: Deep Learning Tuning
Does TensorFlow change the image of deep learning? What I thought after touching a little
Deep learning / softmax function
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
[Deep learning] Image classification with convolutional neural network [DW day 4]
Classify CIFAR-10 image datasets using various models of deep learning
I tried to divide with a deep learning language model
Introduction to Deep Learning for the first time (Chainer) Japanese character recognition Chapter 3 [Character recognition using a model]