Aidemy 2020/11/10
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
-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.
-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
・ 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.
-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) __ 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.
-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" __.
-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 __.
-There are no particular changes in compilation and learning. Only one point, when performing transfer learning, the optimization function should be __ "SGD" __.
-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