Image Classification
Image classification is the task of assigning a label to an entire image. It is the most fundamental computer vision task and the foundation for more complex ones.
How Image Classification Works
Image classification takes an image as input and outputs a category.
Input: an image (grid of pixel values)
Output: a class label (cat, dog, car, airplane, etc.) with a confidence score
Examples:
- Medical imaging: chest X-ray is classified as normal or showing pneumonia
- Food apps: take a photo of your meal and the app identifies the dish
- Quality control: factory camera classifies products as defective or not defective
- Wildlife tracking: camera traps classify which animal appeared
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
Classification Output
# What image classification output looks like
# A classifier outputs probabilities for each class
# These probabilities always add up to 1.0
def classify_image(image_description):
"""
Simulated classifier output.
Real classifier would process pixel data through CNN layers.
"""
# Example outputs for different image descriptions
class_probabilities = {
"photo of golden retriever": {
"cat": 0.02, "dog": 0.93, "bird": 0.01, "fish": 0.04
},
"photo of tabby cat": {
"cat": 0.89, "dog": 0.05, "bird": 0.03, "fish": 0.03
},
"photo of parrot": {
"cat": 0.04, "dog": 0.02, "bird": 0.91, "fish": 0.03
},
}
return class_probabilities.get(image_description, {})
# Test the classifier
test_images = [
"photo of golden retriever",
"photo of tabby cat",
"photo of parrot",
]
for image in test_images:
probs = classify_image(image)
if probs:
predicted = max(probs, key=probs.get)
confidence = probs[predicted]
print(f"Image: '{image}'")
print(f" Predicted: {predicted} ({confidence*100:.0f}% confidence)")
print(f" All probabilities: {', '.join(f'{k}: {v:.2f}' for k, v in probs.items())}")
print()Tip
Tip
In practice, you rarely train an image classifier from scratch. Instead, you start with a pre-trained model (like ResNet or EfficientNet) that was already trained on millions of images. You then fine-tune it on your specific images. This saves weeks of training time.