Zero-Shot Learning Unseen Class Recognition
# Zero-Shot Learning: Unseen Class Recognition
## Introduction & Motivation
Zero-Shot: recognize unseen classes. Semantic attributes; auxiliary information. Transfer knowledge without examples. Applications: novel object recognition, rare classes.
Motivation: Generalize to never-before-seen classes.
Applications: Novel classes, scaling.
---
## Core Concepts & Theory
### Semantic Attributes
Class descriptions; transfer knowledge.
### Embedding Alignment
Map features to semantic space.
### Cross-Modal Transfer
Different modalities; shared embedding.
---
## Mathematical Formulation
Attribute prediction:
$$\hat{a} = f(x) \approx a_y$$
Semantic embedding:
$$ ext{sim}(f(x), s_y) = \max_y$$
---
## Summary & Key Takeaways
Zero-Shot Learning via semantic attributes enables recognition of unseen classes through knowledge transfer.
---
---
## Appendix: Practical Labs
### Lab 1: Attribute Prediction
import numpy as np
def predict_attributes(features, attribute_classifier):
"""Predict semantic attributes from visual features"""
attributes = features @ attribute_classifier
return attributes
# Test
np.random.seed(42)
features = np.random.randn(50, 256)
classifier = np.random.randn(256, 10)
attrs = predict_attributes(features, classifier)
assert attrs.shape == (50, 10), "Attribute shape"
print("✓ Attribute prediction working")
if __name__ == "__main__":
print("Lab 1: AttributePrediction - PASSED")### Lab 2: Semantic Similarity
import numpy as np
def semantic_similarity(predicted_attrs, class_attributes):
"""Compute similarity to class semantic vectors"""
similarities = predicted_attrs @ class_attributes.T
predicted_class = np.argmax(similarities, axis=1)
return predicted_class
# Test
np.random.seed(42)
pred_attrs = np.random.randn(50, 10)
class_attrs = np.random.randn(20, 10)
classes = semantic_similarity(pred_attrs, class_attrs)
assert classes.shape == (50,), "Class predictions"
print("✓ Semantic similarity working")
if __name__ == "__main__":
print("Lab 2: SemanticSimilarity - PASSED")### Lab 3: Generalized Zero-Shot
import numpy as np
def generalized_zero_shot_evaluation(predictions, true_labels, seen_classes, unseen_classes):
"""Evaluate on both seen and unseen classes"""
seen_acc = (predictions[true_labels < len(seen_classes)] == true_labels[true_labels < len(seen_classes)]).mean()
unseen_acc = (predictions[true_labels >= len(seen_classes)] == true_labels[true_labels >= len(seen_classes)]).mean()
harmonic_mean = 2 * seen_acc * unseen_acc / (seen_acc + unseen_acc + 1e-8)
return seen_acc, unseen_acc, harmonic_mean
# Test
np.random.seed(42)
preds = np.random.randint(0, 20, 100)
labels = np.random.randint(0, 20, 100)
seen_acc, unseen_acc, h_mean = generalized_zero_shot_evaluation(preds, labels, list(range(10)), list(range(10, 20)))
assert 0 <= h_mean <= 1, "H-mean valid"
print("✓ Generalized zero-shot working")
if __name__ == "__main__":
print("Lab 3: GeneralizedZeroShot - PASSED")### Lab 4: Transfer via Attributes
import numpy as np
def transfer_via_attributes(source_features, target_classes, attribute_matrix):
"""Transfer knowledge via shared attribute space"""
# Embed source in attribute space
source_attrs = source_features.mean(axis=0, keepdims=True)
# Find closest target classes in attribute space
similarities = source_attrs @ attribute_matrix.T
return similarities
# Test
np.random.seed(42)
src_feat = np.random.randn(50, 256)
tgt_classes = np.random.randn(10, 20)
attr_matrix = np.random.randn(20, 20)
sims = transfer_via_attributes(src_feat, tgt_classes, attr_matrix)
assert sims.shape == (1, 10), "Similarities shape"
print("✓ Attribute transfer working")
if __name__ == "__main__":
print("Lab 4: AttributeTransfer - PASSED")