File size: 6,740 Bytes
1cd0545
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import models

from src import config


class FaceEmbeddingNet(nn.Module):
    """
    Backbone trích xuất đặc trưng khuôn mặt sử dụng ResNet50 làm nền tảng,
    chiếu lên vector embedding 512 chiều, chuẩn hóa BN và L2.
    """

    def __init__(self, embedding_size: int = 512, pretrained: bool = False) -> None:
        super().__init__()

        # Sử dụng weights của ImageNet nếu pretrained=True
        if pretrained:
            weights = models.ResNet50_Weights.IMAGENET1K_V2
        else:
            weights = None

        backbone = models.resnet50(weights=weights)

        # Loại bỏ lớp Fully Connected (FC) cuối cùng của ResNet50
        self.feature_extractor = nn.Sequential(*list(backbone.children())[:-1])

        # Ánh xạ từ đặc trưng 2048 chiều của ResNet50 về 512 chiều (embedding)
        self.embedding_layer = nn.Linear(2048, embedding_size)

        # Batch Normalization giúp embedding ổn định hơn
        self.bn = nn.BatchNorm1d(embedding_size)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # Trích xuất đặc trưng
        features = self.feature_extractor(x)
        features = torch.flatten(features, 1)

        # Dự chiếu và chuẩn hóa BN
        embeddings = self.embedding_layer(features)
        embeddings = self.bn(embeddings)

        # L2 Normalization để tính toán Cosine Similarity trực tiếp bằng dot product
        embeddings = F.normalize(embeddings, p=2, dim=1)

        return embeddings


class FaceRecognitionModel:
    """
    Wrapper tải checkpoint mô hình và thực hiện trích xuất embedding cho hệ thống nhận diện.
    """

    def __init__(self, checkpoint_path: str = None, device: str = None) -> None:
        self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
        self.model = FaceEmbeddingNet(embedding_size=512, pretrained=False)

        # Xác định checkpoint để load
        if checkpoint_path is None:
            # Các đường dẫn checkpoint có thể có trong hệ thống, sắp xếp theo thứ tự ưu tiên
            possible_paths = [
                # 1. Đường dẫn config.FINAL_CHECKPOINT_PATH (arcface_vggface2_warmup_lite.pth)
                getattr(config, "FINAL_CHECKPOINT_PATH", None),
                # 2. Checkpoint lite mặc định
                os.path.join(config.CHECKPOINT_DIR, "arcface_vggface2_warmup_lite.pth"),
                # 3. Checkpoint full mặc định
                os.path.join(config.CHECKPOINT_DIR, "arcface_vggface2_warmup.pth"),
                # 4. Checkpoint fine-tuned RMFRD lite
                os.path.join(config.CHECKPOINT_DIR, "arcface_rmfrd_finetuned_lite.pth"),
                # 5. Checkpoint fine-tuned RMFRD full
                os.path.join(config.CHECKPOINT_DIR, "arcface_rmfrd_finetuned.pth"),
            ]
            for path in possible_paths:
                if path and os.path.exists(path):
                    checkpoint_path = path
                    break

        if checkpoint_path and os.path.exists(checkpoint_path):
            print(f"Loading FaceRecognitionModel checkpoint from: {checkpoint_path}")
            # Đọc file checkpoint
            checkpoint = torch.load(checkpoint_path, map_location=self.device, weights_only=False)

            # Phân tách cấu trúc checkpoint
            if isinstance(checkpoint, dict):
                if "model_state_dict" in checkpoint:
                    state_dict = checkpoint["model_state_dict"]
                else:
                    state_dict = checkpoint
            else:
                state_dict = checkpoint

            # Load vào mô hình (chỉ nạp các trọng số của backbone)
            # Dùng strict=False để bỏ qua trọng số của ArcFace Head (nếu có trong checkpoint)
            missing_keys, unexpected_keys = self.model.load_state_dict(state_dict, strict=False)
            if missing_keys:
                print(f"[WARNING] Missing keys when loading checkpoint: {missing_keys}")
            if unexpected_keys:
                # Trọng số unexpected thường là của arcface_head, điều này là bình thường
                non_head_unexpected = [k for k in unexpected_keys if "weight" not in k]
                if non_head_unexpected:
                    print(f"[WARNING] Unexpected keys when loading checkpoint: {non_head_unexpected}")
        else:
            print("[WARNING] No face recognition checkpoint found. Model weights are uninitialized!")

        self.model.to(self.device)
        self.model.eval()

    def get_embedding(self, image: torch.Tensor) -> torch.Tensor:
        """
        Trích xuất vector đặc trưng (embedding) từ ảnh khuôn mặt đầu vào.
        
        Args:
            image: Tensor ảnh [B, 3, 112, 112] hoặc [3, 112, 112].
            
        Returns:
            Tensor embedding 512 chiều nằm trên CPU.
        """
        # Đảm bảo có chiều Batch
        if image.dim() == 3:
            image = image.unsqueeze(0)

        image = image.to(self.device)

        with torch.no_grad():
            embedding = self.model(image)

        return embedding.cpu()


class MobileNetV2FaceEmbeddingNet(nn.Module):
    """
    Backbone trích xuất đặc trưng khuôn mặt sử dụng MobileNetV2 làm nền tảng,
    chiếu lên vector embedding 512 chiều, chuẩn hóa BN và L2.
    """

    def __init__(self, embedding_size: int = 512, pretrained: bool = False) -> None:
        super().__init__()

        if pretrained:
            weights = models.MobileNet_V2_Weights.DEFAULT
        else:
            weights = None

        backbone = models.mobilenet_v2(weights=weights)

        # Loại bỏ classifier của MobileNetV2
        self.feature_extractor = backbone.features

        # Global average pooling
        self.pool = nn.AdaptiveAvgPool2d(1)

        # Ánh xạ từ đặc trưng 1280 chiều của MobileNetV2 về 512 chiều (embedding)
        self.embedding_layer = nn.Linear(1280, embedding_size)

        # Batch Normalization giúp embedding ổn định hơn
        self.bn = nn.BatchNorm1d(embedding_size)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        features = self.feature_extractor(x)
        features = self.pool(features)
        features = torch.flatten(features, 1)

        embeddings = self.embedding_layer(features)
        embeddings = self.bn(embeddings)

        # L2 Normalization
        embeddings = F.normalize(embeddings, p=2, dim=1)

        return embeddings