Face recognition
এই পাঠে যা শিখবেন
- Face recognition pipeline 4 step
- FaceNet triplet loss
- ArcFace angular margin loss
- Ethics ও privacy concern
১ · Face recognition vs detection
- Face detection: "এই ছবিতে কোথায় মুখ?" — bounding box.
- Face recognition: "এই মুখ কে?" — identity.
- Verification: "এই দু'মুখ কি একই person?" — 1:1 match.
- Identification: database-এ "এই মুখ কে?" — 1:N search.
Recognition = "embedding learning". Each face → 128-512D vector. Same person → close, different → far. Cosine similarity-এ comparison.
২ · Pipeline 4 step
- Detection: face crop locate.
- Alignment: 5-68 landmark detect → rotate/scale to canonical pose.
- Embedding: CNN/ViT → vector.
- Matching: compare embedding-এ cosine similarity → threshold.
৩ · Detection
- Viola-Jones (২০০১): Haar cascade — classical, fast.
- MTCNN (২০১৬): three-stage CNN. Face + landmark.
- RetinaFace (২০১৯): single-stage, robust.
- SCRFD (২০২১): efficient mobile.
- YOLOv8-face: latest realtime.
৪ · Alignment
- 5-point: 2 eyes, nose, 2 mouth corner.
- Affine transform → canonical (e.g., eyes horizontal, fixed scale).
- Eliminates pose variation.
- Recognition accuracy boost ~5-10%.
৫ · FaceNet (২০১৫, Google)
Schroff et al. — embedding learning revolution.
Triplet loss:
$$\mathcal{L} = \max\big(0, \|a - p\|^2 - \|a - n\|^2 + \alpha\big)$$
- $a$: anchor — base image.
- $p$: positive — same person, different photo.
- $n$: negative — different person.
- $\alpha$: margin (typical 0.2).
- Push: anchor-positive close, anchor-negative far.
৬ · ArcFace (২০১৯)
Deng et al. — additive angular margin. Current SOTA.
Idea: classification head with angular margin between class.
$$\mathcal{L} = -\log \frac{e^{s \cos(\theta_{y_i} + m)}}{e^{s \cos(\theta_{y_i} + m)} + \sum_{j \ne y_i} e^{s \cos\theta_j}}$$
- $\theta$: angle between feature and class center.
- $m$: angular margin (e.g., 0.5).
- $s$: scale (e.g., 64).
- Inter-class margin enforce.
৭ · Recognition matching
- Each face → embedding (e.g., 512-D).
- L2 normalize.
- Compare: cosine similarity = $\cos\theta = a \cdot b$ (for unit vector).
- Threshold: > 0.6 same person, < 0.4 different (typical).
৮ · Practical use
# pip install deepface
from deepface import DeepFace
# Verification
result = DeepFace.verify(
img1_path="person1.jpg",
img2_path="person2.jpg",
model_name="ArcFace",
)
print("Same person:", result["verified"])
print("Distance:", result["distance"])
# Identification
result = DeepFace.find(
img_path="query.jpg",
db_path="people_db/",
model_name="ArcFace",
)
print("Match:", result)
# Face attribute (age, gender, race, emotion)
analysis = DeepFace.analyze(
img_path="photo.jpg",
actions=['age', 'gender', 'emotion'],
)
print(analysis)
৯ · Bias ও fairness
- Buolamwini & Gebru (২০১৮): commercial face recognition dark-skin face accuracy 35% lower.
- NIST FRVT: federal benchmark — bias documented.
- Causes: training data demographic skew.
- Mitigation: diverse dataset, fairness-aware loss, demographic-balanced eval.
১০ · Privacy ও regulation
- EU GDPR: biometric data special protection.
- EU AI Act: public space face recognition restricted.
- US: San Francisco, Boston ban.
- Bangladesh: Data Protection Bill emerging.
- Best practice: consent, opt-in, secure storage.
১১ · Use cases — pros ও cons
Beneficial:
- Phone unlock — Apple Face ID.
- Photo organization — Google Photos.
- Missing person search.
- Building access (consensual).
- Bank KYC verification.
Concerning:
- Mass surveillance.
- Stalking enabling.
- Wrongful arrest (false positive).
- Authoritarian misuse.
১২ · Bangladesh deployment
- NID verification — bank, telecom.
- Employee attendance (consent issue).
- Election verification (debate ongoing).
- Smartphone unlock.
- Bangladesh-specific dataset rare — bias risk.
ভাবনার প্রশ্ন
প্র ০১ Triplet loss-এ "hard negative mining" — কী ও কেন critical?
FaceNet training-এর key trick.
Naive triplet:
- Random anchor + positive + negative.
- Most triplet "easy" — anchor-negative already far.
- Loss zero, no learning signal.
Hard negative:
- Negative similar to anchor (high cosine similarity).
- Triplet loss > 0 — gradient flows.
- Model learns to distinguish.
Mining strategies:
- Hardest: closest negative. Effective but unstable.
- Semi-hard: anchor-negative > anchor-positive but within margin. Stable.
- Online mining: within batch, dynamic.
Empirical:
- Naive: 60% LFW.
- Hard mining: 99.6% LFW.
- Massive difference.
Modern alternatives:
- ArcFace — classification-based, no mining.
- Sphere face — angular margin.
- Contrastive — InfoNCE.
মূল উপলব্ধি: Sample selection ML training-এ underrated factor. Right hard examples >> more easy examples.
প্র ০২ ArcFace "angular margin" — geometry-এ কী ঘটছে? Cosine vs euclidean?
Embedding space geometry — face recognition-এর deep aspect.
Hyperspherical embedding:
- L2 normalize → unit vector.
- Embedding lives on hypersphere.
- Distance = angular distance.
- Cosine similarity = $\cos\theta$।
Why hyperspherical:
- Magnitude irrelevant — only direction matter.
- Image variation (lighting, etc.) magnitude-affecting, direction less.
- Stable across condition.
Angular margin:
- Each class — center on hypersphere.
- Add margin $m$ to angle: $\cos(\theta + m)$।
- Decision boundary "tight".
- Inter-class separation enforced.
Effect:
- Same person — narrow cone around center.
- Different person — angular gap > m.
- Verification — single threshold across.
Vs Euclidean:
- Euclidean — magnitude affects.
- Cosine — only angle.
- Image lighting/pose change — Euclidean fluctuate, cosine stable.
Modern face recognition:
- SphereFace — multiplicative margin.
- ArcFace — additive (cleaner).
- CosFace — variant.
- MagFace — magnitude-aware.
মূল উপলব্ধি: Embedding space geometry matters. Right metric (cosine vs Euclidean) — significant accuracy gain.
প্র ০৩ Bangladesh-এ face recognition deploy — privacy law, consent, bias. Engineer-এর checklist?
Real Bangladesh ethical engineering question.
Legal landscape:
- Bangladesh Data Protection Bill — pending.
- ICT Act 2006 — broad provisions.
- NID rule — biometric data government regulated.
- EU GDPR equivalent emerging.
Consent framework:
- Explicit, informed, specific.
- Withdraw possible.
- Children — parental consent.
- Document consent log.
Bias mitigation:
- Bangladesh-specific dataset — diverse skin tone.
- Per-demographic accuracy test.
- Public benchmark report.
- External audit.
Security:
- Embedding (not raw image) store — irreversible if salted.
- Encryption at rest + transit.
- Access log — audit trail.
- Breach response plan.
Engineer checklist:
- Use case ethical justify? (security vs convenience).
- Consent workflow design.
- Bias evaluation per demographic group.
- False positive/negative impact analysis.
- Data retention policy.
- Right to deletion implementation.
- Third-party audit.
- Public transparency report.
Use cases ranked:
- ✅ Phone unlock (personal, opt-in).
- ✅ Photo album organize.
- ⚠ Office attendance (consent + alternative offered).
- ⚠ Bank KYC (regulated, documented).
- ❌ Public CCTV mass surveillance.
- ❌ Behavioral advertising.
- ❌ Without consent identification.
Bangladesh examples:
- BD Election Commission — biometric verification.
- bKash/Nagad — KYC.
- Garment factory attendance — controversial.
Reading recommendation:
- UNESCO AI ethics framework.
- Microsoft AI principles.
- Anthropic Constitutional AI.
- Bangladesh CSA Cyber Security recommendation.
মূল উপলব্ধি: Engineer = ethical decision-maker. "Compliant" minimal bar; "ethical" higher. Bangladesh-এ AI ecosystem responsibility shape করার সুযোগ।
প্র ০৪ Face recognition adversarial attack — adversarial sticker, makeup. কী technique, defense কী?
Face recognition security — active research.
Attack types:
- Evasion: mask/glasses fool detection.
- Impersonation: appear as another person.
- Adversarial patch: printed sticker disrupt.
- Adversarial makeup: careful pattern.
- Eye glasses: Sharif et al. 2016 — fool ResNet.
Adversarial example math:
- Find perturbation $\delta$: $\| \delta \|_\infty < \epsilon$।
- $\arg\max \mathcal{L}(f(x + \delta), y_t)$ where $y_t$ = target identity.
- FGSM, PGD optimization.
Physical attack:
- Cap, glass, sticker — robust to printing imperfection.
- Multi-angle, multi-lighting test.
- Demo: Wu et al., 2020 — IR-light attack.
Liveness detection:
- Photo attack — passive countermeasure.
- Active liveness — blink, smile.
- 3D depth (iPhone Face ID).
- Texture analysis.
Defense techniques:
- Adversarial training: include adversarial examples.
- Input preprocessing: JPEG compress, blur.
- Detection: classify as adversarial.
- Multi-modal: face + voice.
- Continuous authentication: not single-shot.
Privacy-preserving counterattack:
- Fawkes (UChicago, 2020) — protect own photo from training.
- Cloak — adversarial perturbation.
- Anti-FaceNet glasses.
Production reality:
- Commercial system — multi-layer defense.
- Apple Face ID — depth + IR + neural attestation.
- Azure Face — liveness + behavior.
- Banking — multi-factor essential.
মূল উপলব্ধি: Security ≠ accuracy. Adversarial robustness separate consideration. Bangladesh banking, government — defense critical.
অনুশীলন
-
DeepFace verify: Two photo same person কিনা — DeepFace.
Code section ৮-এ। ArcFace, FaceNet — multiple model try.
-
Embedding visualize: 5 person × 3 photo — embedding extract, t-SNE plot.
from sklearn.manifold import TSNE embeddings = [DeepFace.represent(img)[0]['embedding'] for img in images] tsne = TSNE(n_components=2).fit_transform(np.array(embeddings)) # Plot — same person should cluster -
ভাবুন: Bangladesh smart attendance — face recognition vs biometric — কোনটা?
Fingerprint — physical contact, hygiene concern (post-COVID). Face — contactless, fast. Privacy — both biometric. Local consent + opt-out essential.
আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ
- পাঠ ৩২ · Pose estimation পরবর্তী পাঠBody keypoint detection.
- পাঠ ৩০ · ControlNet ও DreamBooth আগের পাঠGeneration tools.
- সব AI Courses দেখুন ABCL TECHসব কোর্স।