The article directories

    • instructions
    • The official model
      • Use and Download
    • Store files and locations
      • To predict
      • The complete code
    • Building a New network
      • Feature extraction
    • Extract any intermediate layer features
      • Fine tuning neural network
      • Automatic input tensor
    • Other recommended

It takes 20 minutes to read this document. After finishing, you will learn to use the core functions of the Applications module, and be able to use the training model in the module to make predictions, or fine-tune the neural network provided by the module to extract any middle-layer features. The following is the class library version:

Keras 2.3.1 Keras-Applications 1.0.8 Keras-base 2.3.1 Keras-Preprocessing 1.1.0 tensorflow 2.2.0 Tensorflow-Base 2.2.0 Tensorflow - estimator 2.2.0 tensorflow - gpu 2.2.0Copy the code

instructions

Application is a special module in KERAS, which provides us with a variety of classical neural networks that have been built and models trained on specific data sets. At the same time, with the help of this module, we can also extract the neural network structure, which can be directly used or adjusted for training our own model. Here is the code in the initialization file (__init__.py) in the keras.Applications module.

from .vgg16 import VGG16
from .vgg19 import VGG19
from .resnet50 import ResNet50
from .inception_v3 import InceptionV3
from .inception_resnet_v2 import InceptionResNetV2
from .xception import Xception
from .mobilenet import MobileNet
from .mobilenet_v2 import MobileNetV2
from .densenet import DenseNet121, DenseNet169, DenseNet201
from .nasnet import NASNetMobile, NASNetLarge
from .resnet import ResNet101, ResNet152
from .resnet_v2 import ResNet50V2, ResNet101V2, ResNet152V2
Copy the code

This code directly tells us what neural networks are available for the current version.

The official model

Use and Download

The code above tells us the reference format of the neural network in the Applications module. The referenced function gets the official model with the weights argument.

from keras.applications.resnet50 import ResNet50
model = ResNet50(weights='imagenet')
Copy the code

Using the above code, Keras will check if the model exists, and if it does not, it will go to Github to download the model. The download address for the above code is github.com/fchollet/de…

Store files and locations

Model after downloading the default storage location is hidden folder under the current account, the path to ~ /. Keras/models, the absolute path for himself (for example)/home/fonttian /. Keras/models. We can also download the model and store it there. In addition to the model, however, there are other files, such as imagenet_class_index.json. Json of the data set used, and there are a thousand classes in this file, which corresponds to classes=1000 in the ResNet50 function.

To predict

I’ll show you the code first, using the official example here.

img_path = '.. /Images/elephant.jpg'
img = image.load_img(img_path, target_size=(224.224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
Decode the result into a list of tuples (class, description, probability)
# (A list represents a sample in the batch)
print('Predicted:', decode_predictions(preds, top=3) [0])
# Predicted: [(u' N02504013 ', U 'Indian_elephant', 0.82658225), (U 'N01871265 ', U' Tusker ', 0.1122357), (U 'N02504458 ', U 'African_elephant', 0.061040461)]
Copy the code

The above code can be divided into two modules, the first is to load data and process, here the data loading function image.load_img is the official function, the size parameter is used to adjust the size of the image. The loaded images continue to be adjusted using Numpy. The second module is the prediction module, which uses model.predict to make predictions. The predictions are converted into tuple lists (class, description, probability) using the official decoding function decode_Predictions. At the same time, use the parameter TOP to select how many items to output.

The complete code

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights='imagenet')

img_path = '.. /Images/elephant.jpg'
img = image.load_img(img_path, target_size=(224.224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
Decode the result into a list of tuples (class, description, probability)
# (A list represents a sample in the batch)
print('Predicted:', decode_predictions(preds, top=3) [0])
# Predicted: [(u' N02504013 ', U 'Indian_elephant', 0.82658225), (U 'N01871265 ', U' Tusker ', 0.1122357), (U 'N02504458 ', U 'African_elephant', 0.061040461)]
Copy the code

From the complete code, we can clearly see that the Applications module essentially returns a Model object, so how to operate the model should be flexible. Here are some more flexible ways to use the Model object. At the same time, it was clear that this code could also be ported to model objects we created ourselves.

Building a New network

Feature extraction

The neural networks built with Keras are all classical neural networks that have been constructed, such as VGG16, which is a classic classification neural network. However, feature extraction requires neural network without classifier, which can be controlled by parameter include_TOP. This parameter is interpreted as follows in KERas:

include_top: whether to include the ? fully-connectedlayers at the top of the network.
Copy the code

Because different neural networks use different classifiers, sometimes there’s a number that represents the number of layers of a fully connected neural network, so I’ll use a question mark instead.

There are many official examples given, first of all, VGG16 feature extraction examples:

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=False)

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)
Copy the code

Now the book example becomes the feature, the essence is the matrix. Not the previous category.

Extract any intermediate layer features

In addition, the neural networks in all applications essentially return the neural networks in the defined Keras. Therefore, we can also obtain arbitrary middle-layer features as a general neural network. The specific method is as follows. Here is an example of extracting arbitrary middle-layer features given by VGG19:

from keras.applications.vgg19 import VGG19
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from keras.models import Model
import numpy as np

base_model = VGG19(weights='imagenet')
model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)

img_path = '.. /Images/elephant.jpg'
img = image.load_img(img_path, target_size=(224.224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

block4_pool_features = model.predict(x)
print(block4_pool_features)
Copy the code

This is easy to understand, is to use the Model parameters to process the original complete Model, the key parameter is the outputs,

The real problem here is how to get the core parameter, which is the name of get_layer (get_layer also has an index parameter). There is no good way to get the core parameter, and the most effective way is to look at keras source code directly. This part of the code is located in Keras_Applications. If you click CTRL to view it in Pycharm, or go to Github to download the source code, here is the Github address of Keras-Applications.

Fine tuning neural network

With this code we are very close to the essence of the Applications module, where we can do much more pure things, such as using only part of the neural network provided by the module, while building the rest of the important parts ourselves. The specific approach is just the same. First, the include_TOP parameter is used to remove the classifier, and then the Model function is used to obtain the output we want. For example, the feature extraction of any intermediate layer in the previous part is actually a part of a normal neural network intercepted and run. And then we fine-tune it to get the model we need.

Here is an example of Keras – fine-tuning InceptionV3, which we use to illustrate:

(1) Construct neural network

# Build a pre-training model without classifier
base_model = InceptionV3(weights='imagenet', include_top=False)

Add global average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)

Add a full connection layer
x = Dense(1024, activation='relu')(x)

# Add a classifier, suppose we have 200 classes
predictions = Dense(200, activation='softmax')(x)

# Build the complete model we need to train
model = Model(inputs=base_model.input, outputs=predictions)
Copy the code

The first half of this step is the same as before, which is to obtain the neural network without classification. Then, we added a classifier by ourselves. Here, we first added the average pooling layer of the whole play, and then added a two-layer neural network as a classifier (200 classes). Then use the Model to build the complete Model.

(2) Lock layer, training new classifier

Here we use the pre-trained model, and then we need to use the lock layer. Trainable =False, which is inherent to Network in Keras and inherits from the layer in keras.engine.base_layer. This is a very common class in Keras. We can use this method to lock all convolutional layers of V3, and then train the unlocked layers.

# first, we only train the top few layers (randomly initialized layers)
Lock all convolutional layers of InceptionV3
for layer in base_model.layers:
    layer.trainable = False
    
Build the model (be sure to operate behind the lock layer)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# Train generations on new data sets
model.fit_generator(...)

# Now that the top layer should be trained, let's start fine-tuning the convolutional layer for Inception V3.
Copy the code

(3) Training top layer

Now that we’ve trained our top layers, we can let go and train the neurons behind us. The specific code is as follows:

# Now that the top layer should be trained, let's start fine-tuning the convolutional layer for Inception V3.
# We will lock the bottom layers and train the rest of the top layers.

Let's look at the name and number of each layer and see how many layers we should lock:
for i, layer in enumerate(base_model.layers):
   print(i, layer.name)

# We choose to train the top two Inception blocks
Lock the first 249 layers and release the second.
for layer in model.layers[:249]:
   layer.trainable = False
for layer in model.layers[249:]:
   layer.trainable = True

We need to recompile the model for the above changes to take effect
# Let's set a very low learning rate and use SGD for fine tuning
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

# We continue training the model and this time we train the last two Inception blocks
# and two fully connected layers
model.fit_generator(...)
Copy the code

(4) All codes

from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K

# Build a pre-training model without classifier
base_model = InceptionV3(weights='imagenet', include_top=False)

Add global average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)

Add a full connection layer
x = Dense(1024, activation='relu')(x)

# Add a classifier, suppose we have 200 classes
predictions = Dense(200, activation='softmax')(x)

# Build the complete model we need to train
model = Model(inputs=base_model.input, outputs=predictions)

# first, we only train the top few layers (randomly initialized layers)
Lock all convolutional layers of InceptionV3
for layer in base_model.layers:
    layer.trainable = False

Build the model (be sure to operate behind the lock layer)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

# Train generations on new data sets
model.fit_generator(...)

# Now that the top layer should be trained, let's start fine-tuning the convolutional layer for Inception V3.
# We will lock the bottom layers and train the rest of the top layers.

Let's look at the name and number of each layer and see how many layers we should lock:
for i, layer in enumerate(base_model.layers):
   print(i, layer.name)

# We choose to train the top two Inception blocks
Lock the first 249 layers and release the second.
for layer in model.layers[:249]:
   layer.trainable = False
for layer in model.layers[249:]:
   layer.trainable = True

We need to recompile the model for the above changes to take effect
# Let's set a very low learning rate and use SGD for fine tuning
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')

# We continue training the model and this time we train the last two Inception blocks
# and two fully connected layers
model.fit_generator(...)
Copy the code

Automatic input tensor

from keras.applications.inception_v3 import InceptionV3
from keras.layers import Input

# This may also be the output of a different Keras model or layer
input_tensor = Input(shape=(224.224.3))  K.image_data_format() == 'channels_last'

model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)
Copy the code

Other recommended

  1. Python ML environment construction and learning materials recommended
  2. PyTorch official Chinese document CSDN drainage
  3. Hyperopt official Chinese document guide
  4. Pyhanlp User Guide CSDN Blog Column (Official Recommendation)