python-kerasHow can I use the Python Keras Applications library to build a neural network?
Keras Applications is a library of pre-trained models with deep learning algorithms, developed with a high-level API for building and training models. It can be used to build a neural network in Python.
Example code
from keras.applications import MobileNet
# Create the base model of MobileNet
model = MobileNet(weights='imagenet', include_top=False)
# Add a new top layer
x = model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
preds = Dense(2, activation='softmax')(x)
# Create a new model
model_2 = Model(inputs=model.input, outputs=preds)
# Freeze the layers
for layer in model_2.layers[:20]:
layer.trainable=False
for layer in model_2.layers[20:]:
layer.trainable=True
# Compile the model
model_2.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
This code creates a new model based on MobileNet, adds a new top layer, freezes layers, and compiles the model.
Code explanation
from keras.applications import MobileNet
: imports the MobileNet model from the Keras Applications library.model = MobileNet(weights='imagenet', include_top=False)
: creates the base model of MobileNet using the pre-trained weights from ImageNet and excluding the top layer.x = model.output
: creates a new output layer.x = GlobalAveragePooling2D()(x)
: applies global average pooling to the output layer.x = Dense(1024, activation='relu')(x)
: adds a dense layer with 1024 neurons and ReLU activation.x = Dense(1024, activation='relu')(x)
: adds a dense layer with 1024 neurons and ReLU activation.x = Dense(512, activation='relu')(x)
: adds a dense layer with 512 neurons and ReLU activation.preds = Dense(2, activation='softmax')(x)
: adds a dense layer with 2 neurons and softmax activation.model_2 = Model(inputs=model.input, outputs=preds)
: creates a new model with the inputs from the original model and the new output layer.for layer in model_2.layers[:20]:
: freezes the first 20 layers of the model.for layer in model_2.layers[20:]:
: allows the remaining layers to be trained.model_2.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
: compiles the model with the Adam optimizer, categorical cross entropy loss function, and accuracy metric.
Helpful links
More of Python Keras
- How do I use zero padding in Python Keras?
- How do I use Python Keras to zip a file?
- How do I use Python Keras to perform Optical Character Recognition (OCR)?
- How do I use Python Keras to create a Zoom application?
- How do I save weights in a Python Keras model?
- How can I use Python Keras on Windows?
- How do I install the Python Keras .whl file?
- How can I use Python with Keras to build a deep learning model?
- How can I improve the validation accuracy of my Keras model using Python?
- How can I install the python module tensorflow.keras in R?
See more codes...