Install & Compatibility
Where this runs
tested against v1.0.8 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
py 3.9
✕ build_error
✓ 22.4s
2334MB installed
● package 2334MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
ResNet50
✓ from keras_applications.resnet50 import ResNet50
✗ from tensorflow.keras.applications.resnet50 import ResNet50
This quickstart demonstrates how to load a pre-trained `ResNet50` model, preprocess a sample image, and make predictions using the `tf.keras.applications` module. Weights are automatically downloaded upon first instantiation. The example includes creating a dummy image for immediate execution.
import numpy as np
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
import os
# Create a dummy image for demonstration
dummy_image_path = 'dummy_elephant.jpg'
if not os.path.exists(dummy_image_path):
try:
from PIL import Image
img_data = np.random.randint(0, 255, size=(224, 224, 3), dtype=np.uint8)
dummy_img = Image.fromarray(img_data)
dummy_img.save(dummy_image_path)
print(f"Created dummy image: {dummy_image_path}")
except ImportError:
print("Pillow not installed. Skipping dummy image creation.")
print("Please provide a real image path to run the example.")
dummy_image_path = None
if dummy_image_path:
# Load the pre-trained ResNet50 model
# weights='imagenet' downloads weights if not already present
model = ResNet50(weights='imagenet')
print("ResNet50 model loaded successfully.")
# Load an image and resize it to the target size expected by ResNet50
img = image.load_img(dummy_image_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0) # Add batch dimension
# Preprocess the image for the model (e.g., channel reordering, mean subtraction)
x = preprocess_input(x)
# Make predictions
preds = model.predict(x)
# Decode the top 3 predictions
decoded_preds = decode_predictions(preds, top=3)[0]
print('Predicted:', decoded_preds)
# Clean up dummy image
os.remove(dummy_image_path)
else:
print("Quickstart example requires Pillow to create a dummy image.")
Debug
Known issues
breakingThe standalone `keras-applications` GitHub repository and Python package are officially deprecated. All Keras Application models have been integrated into the core Keras library and the TensorFlow pip package. Direct imports from `keras_applications` may not work or might refer to an outdated version.fixMigrate your imports to `from tensorflow.keras.applications import ...` if using TensorFlow with Keras 2.x/3.x, or `from keras.applications import ...` if using standalone Keras 3 with another backend. Remove `pip install keras-applications` and rely on `tensorflow` or `keras` installation.
affects: <=1.0.8 (standalone package)
gotchaEach Keras Application model expects a specific type of input preprocessing. Failing to call the correct `preprocess_input` function for the chosen model (e.g., converting RGB to BGR, zero-centering pixels, or scaling to [-1, 1]) will lead to unstable training, incorrect feature extraction, or poor prediction accuracy when using pre-trained weights.fixAlways import and apply the `preprocess_input` function specific to the model you are using (e.g., `from tensorflow.keras.applications.vgg16 import preprocess_input`).
affects: All versions, especially when using pre-trained weights.
gotchaWith TensorFlow 2.x, Keras is deeply integrated as `tf.keras`. Using `import keras` (standalone Keras 2) and `tf.keras` interchangeably or in the same environment can lead to confusion and `AttributeError` or `ModuleNotFoundError` issues, especially when loading saved models. Keras 3 further changes import paths for multi-backend support.fixStandardize on `from tensorflow.keras import ...` or `import tensorflow.keras as keras` for TensorFlow-based projects. For Keras 3 (standalone `keras` package), use `from keras import ...`. Avoid mixing `keras` and `tf.keras` imports in the same codebase. Consider migrating older Keras 2 code to `tf_keras` if maintaining compatibility is paramount for inactive projects.
affects: Keras 2.x and TensorFlow 2.x onwards; Keras 3.x
Upgrade
Version history
1.0.8latest on PyPI · released May 30, 2019
Audit
Dependencies
tensorflowrequiredKeras is the high-level API for TensorFlow, and Keras Applications models are now primarily accessed through `tf.keras.applications`.