Introduction
Step 1: Input Image
Step 2: Feature Extraction
Step 3: Classification
Step 4: Output
Example: Image Classification with a Pretrained Model
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
img_path = 'path/to/your/image.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.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])
Output (example):
Predicted: [('n02124075', 'Egyptian_cat', 0.6352016), ('n02123045', 'tabby', 0.36479843), ('n02123159', 'tiger_cat', 1.5579239e-08)]
Conclusion
Image classification is a popular application of deep learning that involves identifying the main object in an image. It uses convolutional layers to extract features and dense layers to classify the object. With pretrained models, you can easily classify images with just a few lines of code! 📸