Developing a complex depth learning model using Keras + TensorFlow

This post was last edited by Oner at 2017-5-25 19:37

Question guide: 1. Why Choose Keras. 2. How to install Keras and TensorFlow as the back end. 3. What is the Keras sequence model? 4. How to use the Keras to save and resume the pre-training model. 5. How to use the Keras API to develop VGG convolution neural networks. 6. How to use the Keras API to build and run the Squeezenet convolution neural network.

Keras is a highly available Python API that can help you quickly build and train your own depth learning model, and its backend is TensorFlow or Theano. This article assumes that you are already familiar with TensorFlow and convolution neural network, if you are not familiar with, then you can take a look at this 10-minute introductory tensorflow tutorial [http://cv-tricks.com/artificial-... ensorflow-tutorial/] and convolution neural network tutorial [http://cv-tricks.com/tensorflow-... age-classification/], and then come back and read this article. In this tutorial, we will study the following aspects: why Choose Keras. Why Keras is considered to be the future of deep learning. Install Keras Step by step on Ubuntu. Keras tensorflow Tutorial: Keras basic knowledge. Understanding the Keras sequence model

4.1 Practical examples Explain linear regression problems using Keras to save and reply to a pre-trained model Keras API

6.1 Using the Keras API to develop a VGG convolution neural network

6.2 Using the Keras API to build and run the Squeezenet convolution neural network

1. Why Choose Keras.

Keras is a framework developed by Google's engineer, François Chollet [Https://twitter.com/fchollet], to help you develop rapid prototyping on Theano. Later, this is extended to TensorFlow and can also be used as a back end. And recently, TensorFlow decided to provide it as part of the contrib file.

Keras is thought to be the future of building neural networks, and here are some of the reasons it is popular: lightweight and fast development: Keras is designed to eliminate boilerplate code. Several lines of Keras code can achieve more functionality than native TensorFlow code. You can also easily implement CNN and RNN, and let them run on the CPU or GPU. The "winner" of the framework: Keras is an API that runs on top of other depth learning frameworks. This frame can be either TensorFlow or Theano. Microsoft also plans to make CNTK as a backend for Keras. At present, the neural network framework world is very dispersed, and develops very fast. Specifically, you can take a look at Karpathy's tweets:

Imagine how painful it is for us to learn a new framework every year. So far, TensorFlow seems to have become a trend, and more and more frameworks are starting to support Keras, which may become a standard.

At present, Keras is the fastest growing kind of deep learning framework. Because the different depth learning framework can be used as the backend, it also makes it a big reason for popularity. You can imagine a scenario where if you read a very interesting paper, and you want to test the model on your own dataset. Let's assume again that you are very familiar with TensorFlow, but you know very little about Theano. Well, you have to use TensorFlow to reproduce the paper, but the cycle is very long. However, if the code is now written in Keras, you can use the code simply by modifying the backend to TensorFlow. This will be a great impetus to the development of the community.

2. How to install Keras and TensorFlow as back end

A) Dependent installation

Install h5py for model Save and load: [Python]? 1 pip Install H5py

There are also some dependency packs to install. [Python]? 1 2 pip install numpy scipy pip Install pillow If you have not installed TensorFlow, then you can follow this tutorial to install TensorFlow first. Once you've installed the TensorFlow, you just need to use the PIP to easily install Keras. [Python]? The 1 sudo pip install Keras uses the following command to view the Keras version. [Python]? 1 2 >>> Import kerasusing TensorFlow backend. >>> keras.__version__ ' 2.0.4 '

Once the Keras is installed, you need to modify the backend file, which is to determine whether you need tensorflow as the backend or Theano as the backend, and the modified configuration file is located in ~/.keras/keras.json. The specific configuration is as follows: [Python]? 1 2 3 4 5 6 {"Floatx": "float32", "Epsilon": 1e-07, "backend": "TensorFlow", "Image_data_form At ":" Channels_last "}

Note that the parameter image_data_format is channels_last, which means that the back end is TensorFlow. Because, in the TensorFlow image storage Way is [height, width, channels], but in Theano is completely different, namely [channels, height, width]. Therefore, if you do not set this parameter correctly, then the intermediate result of your model will be very strange. For Theano, this parameter is Channels_first.

So you are ready to use Keras to build the model and TensorFlow as the backend.

3. Keras Basic Knowledge

The main data structure in Keras is model, which defines a complete diagram. You can add any network structure to a diagram that already exists. [Python]? There are two different modeling methods for the 1 import keras Keras: Sequential models: This method is used to implement some simple models. All you have to do is add layers to some existing models. The API for functional Api:keras is very powerful, and you can use these APIs to construct more complex models, such as multiple output models, a direction-free graph, and so on.

In the next section of this article, we will study the theories and examples of the Keras sequential models and functional APIs.

4. Keras Sequential Models

In this section, I will introduce the theory of Keras sequential models in the future. I will quickly explain how it works and will use specific code to explain it. After that, we'll solve a simple linear regression problem that you can read while running the code to deepen the impression.

The following code is how to start importing and building a sequence model. [Python]? 1 2 from keras.models import sequential models = sequential ()

Next we can add dense (full connected layer) to the model, activation,conv2d,maxpooling2d function. [Python]? 1 2 3 from keras.layers import dense, activation, conv2d, maxpooling2d, Flatten, dropout Model.add (64, (3, 3), Activation = ' Relu ', Input_shape = (M, m)) # This ads a convolutional layer with filters of size 3 * 3 T o Graph

Here's how to add some of the most popular layers to your network. I've written a lot about layers in the convolution neural network tutorial [http://cv-tricks.com/tensorflow-... age-classification/].

1. Convolution layer

Here we use a convolution layer, 64 convolution cores, the dimension is 33, and then activate using the Relu activation function, the dimension of the input data is ' 100100*32 '. Note that if you are the first convolution layer, you must add the dimension of the input data, the following parameters can be omitted. [Python]? 1 Model.add (conv2d, (3, 3), activation = ' Relu ', Input_shape = (100, 100, 32))

2. Maxpooling Layer

Specify the type of layer, and specify the size of the red, and then automatically complete the red operation, cool. [Python]? 1 Model.add (maxpooling2d (pool_size = (2, 2))

3. Fully connected Layer

This layer is called the dense layer in keras, we just need to set the dimension of the output layer, and then Keras will help us to do it automatically. [Python]? 1 Model.add (dense (256, activation = ' relu '))

4. Dropout

[Python]? 1 Model.add (Dropout (0.5)) 5. Flat layer

Model.add (Flatten ())

4. Data entry

The first layer of the network needs to be read into the training data. So we need to develop the dimensions of the input data. Therefore, the Input_shape parameter is used to develop the dimension size of the input data. [Python]? 1 Model.add (conv2d, (3, 3), activation = ' Relu ', Input_shape = (224, 224, 3)) In this example, the first layer of data entry is a convolution layer, the size of the input data is 224*224*3.

The above steps help you build a model using a sequence model. Next, let's learn the most important part. Once you specify a network architecture, you also need to specify the optimizer and the loss function. We use the compile function in Keras to achieve this function. For example, in the following code, we use Rmsprop as the optimizer, binary_crossentropy as the loss function value. [Python]? 1 model. Compile (loss = ' binary_crossentropy ', optimizer = ' Rmsprop ') if you want to use a random gradient to drop, then you need to select the appropriate initial value and the Super parameter: [Python]? 1 2 3 from keras.optimizers import sgd sgd = SGD (lr = 0.01, decay = 1e-6, momentum = 0.9, Nesterov = True) model. Compile (loss = ' categorical_crossentropy ', optimizer = SGD)

Now we've built the model. Next, let's enter data into the model, which is implemented through the FIT function in the Keras. You can also specify Batch_size and epochs in this function to train. [Python]? 1