Introduction
Pre-trained models help us use powerful AI models that are already trained on large image sets. This saves time and effort when solving new image tasks.
from tensorflow.keras.applications import VGG16, ResNet50, MobileNet # Load a pre-trained model model = VGG16(weights='imagenet', include_top=True) # Use the model to predict on new images predictions = model.predict(image_batch)
from tensorflow.keras.applications import VGG16 model = VGG16(weights='imagenet')
from tensorflow.keras.applications import ResNet50 model = ResNet50(weights='imagenet', include_top=False)
from tensorflow.keras.applications import MobileNet model = MobileNet(weights='imagenet')
import numpy as np from tensorflow.keras.applications import VGG16 from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions from tensorflow.keras.preprocessing import image from tensorflow.keras.utils import get_file # Load VGG16 pre-trained model model = VGG16(weights='imagenet') # Load and prepare an example image (elephant.jpg from Keras utils) img_path = get_file('elephant.jpg', origin='https://storage.googleapis.com/download.tensorflow.org/example_images/elephant.jpg') img = image.load_img(img_path, target_size=(224, 224)) # Convert image to array x = image.img_to_array(img) # Add batch dimension x = np.expand_dims(x, axis=0) # Preprocess input for VGG16 x = preprocess_input(x) # Predict preds = model.predict(x) # Decode predictions to class names print('Predicted:', decode_predictions(preds, top=3)[0])
import tensorflow as tf model = tf.keras.applications.ResNet50(weights='imagenet') input_shape = (1, 224, 224, 3) sample_input = tf.random.uniform(input_shape) output = model(sample_input) print(output.shape)
model = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_shape=(28,28,3))
Pass the test and this pattern moves unknown