Weakly Supervised Learning
Module: Computer Vision | Difficulty: Advanced
Class Activation Mapping (CAM)
where is the class-specific weight and is the feature map.
Erasing-Based Approach
- Generate CAM
- Erase most discriminative region
- Re-train to find other regions
Bounding Box Supervision
Multiple Instance Learning (MIL)
import torch
import torch.nn as nn
class CAMGenerator(nn.Module):
def __init__(self, backbone, num_classes):
super().__init__()
self.backbone = backbone
self_GAP = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Linear(backbone.out_channels, num_classes)
def forward(self, x):
features = self.backbone(x) # (B, C, H, W)
pooled = self.GAP(features).flatten(1)
weights = self.fc(pooled) # (B, num_classes)
# Generate CAM for each class
cam = torch.einsum('bc,bchw->bhw', weights, features)
return cam, weights
Key Takeaways
- CAM visualizes discriminative regions learned by CNNs
- Weak supervision reduces annotation cost significantly
- Erasing-based methods discover additional object regions