Keras Applications

Keras Applications

Keras Applications are deep learning models that are made available alongside pre-trained weights. These models can be used for prediction, feature extraction, and fine-tuning.

Weights are downloaded automatically when instantiating a model. They are stored at ~/.keras/models/.

Upon instantiation, the models will be built according to the image data format set in your Keras configuration file at ~/.keras/keras.json. For instance, if you have set image_data_format=channels_last, then any model loaded from this repository will get built according to the data format convention “Height-Width-Depth”.

Available models

ModelSize (MB)Top-1 AccuracyTop-5 AccuracyParametersDepthTime (ms) per inference step (CPU)Time (ms) per inference step (GPU)
Xception8879.0%94.5%22.9M81109.48.1
VGG1652871.3%90.1%138.4M1669.54.2
VGG1954971.3%90.0%143.7M1984.84.4
ResNet509874.9%92.1%25.6M10758.24.6
ResNet50V29876.0%93.0%25.6M10345.64.4
ResNet10117176.4%92.8%44.7M20989.65.2
ResNet101V217177.2%93.8%44.7M20572.75.4
ResNet15223276.6%93.1%60.4M311127.46.5
ResNet152V223278.0%94.2%60.4M307107.56.6
InceptionV39277.9%93.7%23.9M18942.26.9
InceptionResNetV221580.3%95.3%55.9M449130.210.0
MobileNet1670.4%89.5%4.3M5522.63.4
MobileNetV21471.3%90.1%3.5M10525.93.8
DenseNet1213375.0%92.3%8.1M24277.15.4
DenseNet1695776.2%93.2%14.3M33896.46.3
DenseNet2018077.3%93.6%20.2M402127.26.7
NASNetMobile2374.4%91.9%5.3M38927.06.7
NASNetLarge34382.5%96.0%88.9M533344.520.0
EfficientNetB02977.1%93.3%5.3M13246.04.9
EfficientNetB13179.1%94.4%7.9M18660.25.6
EfficientNetB23680.1%94.9%9.2M18680.86.5
EfficientNetB34881.6%95.7%12.3M210140.08.8
EfficientNetB47582.9%96.4%19.5M258308.315.1
EfficientNetB511883.6%96.7%30.6M312579.225.3
EfficientNetB616684.0%96.8%43.3M360958.140.4
EfficientNetB725684.3%97.0%66.7M4381578.961.6
EfficientNetV2B02978.7%94.3%7.2M---
EfficientNetV2B13479.8%95.0%8.2M---
EfficientNetV2B24280.5%95.1%10.2M---
EfficientNetV2B35982.0%95.8%14.5M---
EfficientNetV2S8883.9%96.7%21.6M---
EfficientNetV2M22085.3%97.4%54.4M---
EfficientNetV2L47985.7%97.5%119.0M---
ConvNeXtTiny109.4281.3%-28.6M---
ConvNeXtSmall192.2982.3%-50.2M---
ConvNeXtBase338.5885.3%-88.5M---
ConvNeXtLarge755.0786.3%-197.7M---
ConvNeXtXLarge131086.7%-350.1M---

The top-1 and top-5 accuracy refers to the model’s performance on the ImageNet validation dataset.

Depth refers to the topological depth of the network. This includes activation layers, batch normalization layers etc.

Time per inference step is the average of 30 batches and 10 repetitions.

  • CPU: AMD EPYC Processor (with IBPB) (92 core)
  • RAM: 1.7T
  • GPU: Tesla A100
  • Batch size: 32

Depth counts the number of layers with parameters.

Usage examples for image classification models

Classify ImageNet classes with ResNet50

import keras
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
img_path = 'elephant.jpg'
img = keras.utils.load_img(img_path, target_size=(224, 224))
x = keras.utils.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])

Extract features with VGG16

import keras
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
model = VGG16(weights='imagenet', include_top=False)
img_path = 'elephant.jpg'
img = keras.utils.load_img(img_path, target_size=(224, 224))
x = keras.utils.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)

Extract features from an arbitrary intermediate layer with VGG19

from keras.applications.vgg19 import VGG19
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 = 'elephant.jpg'
img = keras.utils.load_img(img_path, target_size=(224, 224))
x = keras.utils.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
block4_pool_features = model.predict(x)

Fine-tune InceptionV3 on a new set of classes

from keras.applications.inception_v3 import InceptionV3
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(200, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
for layer in base_model.layers:
    layer.trainable = False
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.fit(...)
for i, layer in enumerate(base_model.layers):
   print(i, layer.name)
for layer in model.layers[:249]:
   layer.trainable = False
for layer in model.layers[249:]:
   layer.trainable = True
from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')
model.fit(...)

Build InceptionV3 over a custom input tensor

from keras.applications.inception_v3 import InceptionV3
from keras.layers import Input
input_tensor = Input(shape=(224, 224, 3))
model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)