๐️ Class Activation Mapping (CAM) – How AI “Sees” Images
Have you ever wondered how an AI knows where to look in an image?
That’s exactly what Class Activation Mapping (CAM) helps us understand. It reveals what parts of an image influenced the AI’s decision.
๐ Table of Contents
- What is CAM?
- Why CAM Matters
- How CAM Works
- Math Behind CAM (Simple)
- Grad-CAM Explained
- Code Example
- CLI Output
- Key Takeaways
- Related Articles
๐ What is CAM?
CAM creates a heatmap showing which parts of an image were important.
If an AI says “this is a cat,” CAM shows whether it looked at the ears, face, or something irrelevant.
๐ Why CAM Matters
- Healthcare → Ensure correct diagnosis focus
- Self-driving cars → Detect pedestrians
- Security → Analyze correct features
⚙️ How CAM Works
- Feature Extraction → Detect patterns
- Classification → Predict label
- Weighting → Highlight important areas
๐ Math Behind CAM (Easy Explanation)
1. Feature Maps
\[ f_k(x, y) \]
Each feature map captures patterns like edges or textures.
2. Weighted Sum
\[ M(x,y) = \sum_k w_k f_k(x,y) \]
What does this mean?
- \( f_k(x,y) \) = feature map
- \( w_k \) = importance weight
3. Final Heatmap
\[ Heatmap = ReLU(M(x,y)) \]
This keeps only positive influences.
๐ฅ Grad-CAM (Improved Version)
Grad-CAM uses gradients to compute importance:
\[ \alpha_k = \frac{1}{Z} \sum_i \sum_j \frac{\partial y}{\partial f_k(i,j)} \]
Then:
\[ M(x,y) = \sum_k \alpha_k f_k(x,y) \]
๐ป Code Example
import torch
import torchvision.models as models
model = models.resnet18(pretrained=True)
model.eval()
# Example input
input = torch.randn(1,3,224,224)
output = model(input)
print(output.shape)
๐ฅ️ CLI Output
Click to Expand
Output Shape: torch.Size([1, 1000])
๐ก Key Takeaways
- CAM shows where AI is looking
- Helps build trust in AI systems
- Grad-CAM works with modern networks
- Useful in critical applications
๐ฏ Final Thoughts
CAM helps us understand AI decisions visually.
Instead of guessing how AI works, we can now see it think.
No comments:
Post a Comment