Multi-backend deep learning framework that provides a unified, high-level API for building and training neural networks across JAX, TensorFlow, PyTorch, and OpenVINO backends.
—
Ready-to-use pre-trained models for computer vision tasks. Keras provides a comprehensive collection of state-of-the-art models pre-trained on ImageNet, enabling transfer learning and feature extraction for various applications.
VGG (Visual Geometry Group) models with deep convolutional architectures using small 3x3 filters.
def VGG16(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""
VGG16 model with 16 layers.
Parameters:
- include_top: Whether to include fully connected layers at top
- weights: 'imagenet' (pre-training on ImageNet) or None (random initialization)
- input_tensor: Optional tensor to use as image input
- input_shape: Shape of input images (only if include_top=False)
- pooling: Pooling mode for feature extraction ('avg', 'max', or None)
- classes: Number of classes to classify (only if include_top=True and weights=None)
- classifier_activation: Activation for final classification layer
Returns:
Keras Model instance
"""
def VGG19(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""VGG19 model with 19 layers."""Residual Networks with skip connections that enable training of very deep networks.
def ResNet50(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""
ResNet50 model with 50 layers.
Parameters:
- include_top: Whether to include fully connected layers at top
- weights: 'imagenet' or None
- input_tensor: Optional tensor to use as image input
- input_shape: Shape of input images (only if include_top=False)
- pooling: Pooling mode for feature extraction
- classes: Number of classes to classify
- classifier_activation: Activation for final classification layer
Returns:
Keras Model instance
"""
def ResNet101(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""ResNet101 model with 101 layers."""
def ResNet152(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""ResNet152 model with 152 layers."""
def ResNet50V2(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""ResNet50V2 model with improved residual blocks."""
def ResNet101V2(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""ResNet101V2 model with improved residual blocks."""
def ResNet152V2(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""ResNet152V2 model with improved residual blocks."""Inception architectures using multi-scale convolutional features and efficient factorized convolutions.
def InceptionV3(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""
InceptionV3 model.
Parameters:
- include_top: Whether to include fully connected layers at top
- weights: 'imagenet' or None
- input_tensor: Optional tensor to use as image input
- input_shape: Shape of input images (only if include_top=False)
- pooling: Pooling mode for feature extraction
- classes: Number of classes to classify
- classifier_activation: Activation for final classification layer
Returns:
Keras Model instance
"""
def InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""InceptionResNetV2 combining Inception and ResNet architectures."""
def Xception(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""
Xception model with depthwise separable convolutions.
Parameters:
- include_top: Whether to include fully connected layers at top
- weights: 'imagenet' or None
- input_tensor: Optional tensor to use as image input
- input_shape: Shape of input images (only if include_top=False)
- pooling: Pooling mode for feature extraction
- classes: Number of classes to classify
- classifier_activation: Activation for final classification layer
Returns:
Keras Model instance
"""Efficient models designed for mobile and embedded devices using depthwise separable convolutions.
def MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3,
include_top=True, weights='imagenet', input_tensor=None,
pooling=None, classes=1000, classifier_activation='softmax'):
"""
MobileNet model optimized for mobile devices.
Parameters:
- input_shape: Shape of input images
- alpha: Width multiplier for network
- depth_multiplier: Depth multiplier for depthwise convolution
- dropout: Dropout rate
- include_top: Whether to include fully connected layers at top
- weights: 'imagenet' or None
- input_tensor: Optional tensor to use as image input
- pooling: Pooling mode for feature extraction
- classes: Number of classes to classify
- classifier_activation: Activation for final classification layer
Returns:
Keras Model instance
"""
def MobileNetV2(input_shape=None, alpha=1.0, include_top=True, weights='imagenet',
input_tensor=None, pooling=None, classes=1000, classifier_activation='softmax'):
"""MobileNetV2 with inverted residuals and linear bottlenecks."""
def MobileNetV3Large(input_shape=None, alpha=1.0, minimalistic=False, include_top=True,
weights='imagenet', input_tensor=None, classes=1000, pooling=None,
dropout_rate=0.2, classifier_activation='softmax'):
"""MobileNetV3Large optimized for high resource use cases."""
def MobileNetV3Small(input_shape=None, alpha=1.0, minimalistic=False, include_top=True,
weights='imagenet', input_tensor=None, classes=1000, pooling=None,
dropout_rate=0.2, classifier_activation='softmax'):
"""MobileNetV3Small optimized for low resource use cases."""EfficientNet family of models that systematically scale network width, depth, and resolution.
def EfficientNetB0(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', **kwargs):
"""
EfficientNetB0 model.
Parameters:
- include_top: Whether to include fully connected layers at top
- weights: 'imagenet' or None
- input_tensor: Optional tensor to use as image input
- input_shape: Shape of input images (only if include_top=False)
- pooling: Pooling mode for feature extraction
- classes: Number of classes to classify
- classifier_activation: Activation for final classification layer
Returns:
Keras Model instance
"""
def EfficientNetB1(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', **kwargs):
"""EfficientNetB1 model."""
def EfficientNetB2(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', **kwargs):
"""EfficientNetB2 model."""
def EfficientNetB3(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', **kwargs):
"""EfficientNetB3 model."""
def EfficientNetB4(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', **kwargs):
"""EfficientNetB4 model."""
def EfficientNetB5(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', **kwargs):
"""EfficientNetB5 model."""
def EfficientNetB6(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', **kwargs):
"""EfficientNetB6 model."""
def EfficientNetB7(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', **kwargs):
"""EfficientNetB7 model."""Second generation EfficientNet models with improved training efficiency and better parameter efficiency.
def EfficientNetV2B0(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', include_preprocessing=True):
"""EfficientNetV2B0 model."""
def EfficientNetV2B1(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', include_preprocessing=True):
"""EfficientNetV2B1 model."""
def EfficientNetV2B2(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', include_preprocessing=True):
"""EfficientNetV2B2 model."""
def EfficientNetV2B3(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', include_preprocessing=True):
"""EfficientNetV2B3 model."""
def EfficientNetV2S(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', include_preprocessing=True):
"""EfficientNetV2S model."""
def EfficientNetV2M(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', include_preprocessing=True):
"""EfficientNetV2M model."""
def EfficientNetV2L(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000,
classifier_activation='softmax', include_preprocessing=True):
"""EfficientNetV2L model."""Densely connected networks where each layer connects to every other layer in a feed-forward fashion.
def DenseNet121(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000):
"""
DenseNet121 model with 121 layers.
Parameters:
- include_top: Whether to include fully connected layers at top
- weights: 'imagenet' or None
- input_tensor: Optional tensor to use as image input
- input_shape: Shape of input images (only if include_top=False)
- pooling: Pooling mode for feature extraction
- classes: Number of classes to classify
Returns:
Keras Model instance
"""
def DenseNet169(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000):
"""DenseNet169 model with 169 layers."""
def DenseNet201(include_top=True, weights='imagenet', input_tensor=None,
input_shape=None, pooling=None, classes=1000):
"""DenseNet201 model with 201 layers."""Modern ConvNet architectures that compete with Vision Transformers.
def ConvNeXtTiny(model_name='convnext_tiny', include_top=True, include_preprocessing=True,
weights='imagenet', input_tensor=None, input_shape=None, pooling=None,
classes=1000, classifier_activation='softmax'):
"""ConvNeXtTiny model."""
def ConvNeXtSmall(model_name='convnext_small', include_top=True, include_preprocessing=True,
weights='imagenet', input_tensor=None, input_shape=None, pooling=None,
classes=1000, classifier_activation='softmax'):
"""ConvNeXtSmall model."""
def ConvNeXtBase(model_name='convnext_base', include_top=True, include_preprocessing=True,
weights='imagenet', input_tensor=None, input_shape=None, pooling=None,
classes=1000, classifier_activation='softmax'):
"""ConvNeXtBase model."""
def ConvNeXtLarge(model_name='convnext_large', include_top=True, include_preprocessing=True,
weights='imagenet', input_tensor=None, input_shape=None, pooling=None,
classes=1000, classifier_activation='softmax'):
"""ConvNeXtLarge model."""
def ConvNeXtXLarge(model_name='convnext_xlarge', include_top=True, include_preprocessing=True,
weights='imagenet', input_tensor=None, input_shape=None, pooling=None,
classes=1000, classifier_activation='softmax'):
"""ConvNeXtXLarge model."""Neural Architecture Search networks that were discovered through automated architecture search.
def NASNetMobile(input_shape=None, include_top=True, weights='imagenet',
input_tensor=None, pooling=None, classes=1000):
"""
NASNetMobile model optimized for mobile devices.
Parameters:
- input_shape: Shape of input images
- include_top: Whether to include fully connected layers at top
- weights: 'imagenet' or None
- input_tensor: Optional tensor to use as image input
- pooling: Pooling mode for feature extraction
- classes: Number of classes to classify
Returns:
Keras Model instance
"""
def NASNetLarge(input_shape=None, include_top=True, weights='imagenet',
input_tensor=None, pooling=None, classes=1000):
"""NASNetLarge model for high-accuracy applications."""Functions for preprocessing images and decoding predictions from pre-trained models.
def preprocess_input(x, data_format=None):
"""
Preprocess images for pre-trained models.
Parameters:
- x: Input images as numpy array
- data_format: Data format ('channels_first' or 'channels_last')
Returns:
Preprocessed images
"""
def decode_predictions(preds, top=5):
"""
Decode predictions from ImageNet models.
Parameters:
- preds: Numpy array of predictions
- top: Number of top predictions to return
Returns:
List of tuples (class_name, class_description, score)
"""import keras
from keras.applications import ResNet50
from keras.applications.resnet50 import preprocess_input, decode_predictions
from keras.utils import load_img, img_to_array
import numpy as np
# Load pre-trained ResNet50 model
model = ResNet50(weights='imagenet')
# Load and preprocess image
img_path = 'elephant.jpg'
img = load_img(img_path, target_size=(224, 224))
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# Make prediction
preds = model.predict(x)
predictions = decode_predictions(preds, top=3)[0]
for class_name, description, score in predictions:
print(f'{description}: {score:.4f}')import keras
from keras import layers
from keras.applications import VGG16
# Load pre-trained VGG16 without top layers
base_model = VGG16(
weights='imagenet',
include_top=False,
input_shape=(224, 224, 3)
)
# Freeze base model layers
base_model.trainable = False
# Add custom classification head
model = keras.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(128, activation='relu'),
layers.Dropout(0.2),
layers.Dense(num_classes, activation='softmax')
])
# Compile model
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Train on custom dataset
# model.fit(train_data, epochs=10)import keras
from keras.applications import MobileNetV2
import numpy as np
# Load model without classification layers
feature_extractor = MobileNetV2(
weights='imagenet',
include_top=False,
pooling='avg'
)
# Extract features from images
def extract_features(images):
# Preprocess images
processed_images = keras.applications.mobilenet_v2.preprocess_input(images)
# Extract features
features = feature_extractor.predict(processed_images)
return features
# Use for similarity search, clustering, etc.
# features = extract_features(image_batch)import keras
from keras.applications import EfficientNetB0
# Load pre-trained model
base_model = EfficientNetB0(
weights='imagenet',
include_top=False,
input_shape=(224, 224, 3)
)
# Initially freeze all layers
base_model.trainable = False
# Add custom head
model = keras.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(num_classes, activation='softmax')
])
# Train with frozen features
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# model.fit(train_data, epochs=10)
# Fine-tune: unfreeze top layers
base_model.trainable = True
# Freeze bottom layers, fine-tune top layers
for layer in base_model.layers[:-20]:
layer.trainable = False
# Use lower learning rate for fine-tuning
model.compile(
optimizer=keras.optimizers.Adam(1e-5),
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Continue training
# model.fit(train_data, epochs=10)Install with Tessl CLI
npx tessl i tessl/pypi-keras