File size: 5,882 Bytes
3d7a505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
# Add more imports if required

# Sample Transformation function
# YOUR CODE HERE for changing the Transformation values.
trnscm = transforms.Compose([transforms.Resize((100,100)), transforms.ToTensor()])

##Example Network
class Siamese(torch.nn.Module):
    def __init__(self):
        super(Siamese, self).__init__()
        #YOUR CODE HERE
        
    def forward(self, x):
        pass   # remove 'pass' once you have written your code
        #YOUR CODE HERE
        
##########################################################################################################
## Sample classification network (Specify if you are using a pytorch classifier during the training)    ##
## classifier = nn.Sequential(nn.Linear(64, 64), nn.BatchNorm1d(64), nn.ReLU(), nn.Linear...)           ##
##########################################################################################################

# YOUR CODE HERE for pytorch classifier

# Definition of classes as dictionary
classes = ['person1','person2','person3','person4','person5','person6','person7']
def get_similarity(img1, img2):
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    
    det_img1 = detected_face(img1)
    det_img2 = detected_face(img2)
    if(det_img1 == 0 or det_img2 == 0):
        det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
        det_img2 = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY))
    face1 = trnscm(det_img1).unsqueeze(0)
    face2 = trnscm(det_img2).unsqueeze(0)
    ##########################################################################################
    ##Example for loading a model using weight state dictionary:                            ##
    ## feature_net = light_cnn() #Example Network                                           ##
    ## model = torch.load(current_path + '/siamese_model.t7', map_location=device)          ##
    ## feature_net.load_state_dict(model['net_dict'])                                       ##
    ##                                                                                      ##
    ##current_path + '/<network_definition>' is path of the saved model if present in       ##
    ##the same path as this file, we recommend to put in the same directory                 ##
    ##########################################################################################
    ##########################################################################################
    
    # YOUR CODE HERE, load the model
    
    # YOUR CODE HERE, return similarity measure using your model
    # 1. Initialize and Load Siamese Network
    try:
        # Assuming your Siamese Network class is named 'SiameseNetwork'
        siamese_net = SiameseNetwork().to(device)
        siamese_net.load_state_dict(torch.load(SIAMESE_MODEL_PATH, map_location=device))
        siamese_net.eval()
    except Exception as e:
        print(f"Error loading Siamese Model: {e}")
        return -1 # Return error code

    # 2. Get Features (Embeddings)
    with torch.no_grad():
        # Get the feature vector from one tower/forward_once method
        # Ensure your SiameseNetwork class has a forward_once or get_embedding method
        embed1 = siamese_net.forward_once(face1).cpu().numpy()
        embed2 = siamese_net.forward_once(face2).cpu().numpy()
        
    # 3. Calculate Similarity Measure
    # The Euclidean distance is the fundamental metric used by the Triplet/Contrastive loss.
    # We return the NEGATIVE Euclidean distance or COSINE similarity, as *higher* value usually means *more* similar.
    
    # Option A: Euclidean Distance (Lower is better) -> return NEGATIVE distance for API expectation
    # distance = euclidean_distances(embed1, embed2)[0][0]
    # similarity = -distance 
    
    # Option B: Cosine Similarity (Higher is better) -> Recommended
    similarity = cosine_similarity(embed1, embed2)[0][0]
    
    return float(similarity)
    
def get_face_class(img1):
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    
    det_img1 = detected_face(img1)
    if(det_img1 == 0):
        det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
    ##YOUR CODE HERE, return face class here
    ##Hint: you need a classifier finetuned for your classes, it takes o/p of siamese as i/p to it
    ##Better Hint: Siamese experiment is covered in one of the labs
    face1_tensor = trnscm(det_img1).unsqueeze(0).to(device)

    # 1. Load Siamese Network (Feature Extractor)
    try:
        siamese_net = SiameseNetwork().to(device)
        siamese_net.load_state_dict(torch.load(SIAMESE_MODEL_PATH, map_location=device))
        siamese_net.eval()
    except Exception as e:
        return f"Error loading Siamese Model get_face_class: {e}"

    # 2. Extract Embedding
    with torch.no_grad():
        embedding_np = siamese_net.forward_once(face1_tensor).cpu().numpy()
    
    # 3. Load Sklearn Scaler and Classifier (Joblib)
    try:
        knn_classifier = joblib.load(KNN_CLASSIFIER_PATH)
        scaler = joblib.load(SCALER_PATH)
    except Exception as e:
        return f"Error loading Sklearn models: {e}"
    
    # 4. Preprocess Embedding and Predict
    # The embedding must be reshaped to (1, N_features) for the scaler
    embedding_scaled = scaler.transform(embedding_np.reshape(1, -1))
    
    # Perform prediction (returns a NumPy array with the predicted label index)
    predicted_label_index = knn_classifier.predict(embedding_scaled)[0]
    
    # 5. Map index to Class Name
    if predicted_label_index < len(CLASS_NAMES):
        predicted_class_name = CLASS_NAMES[predicted_label_index]
    else:
        predicted_class_name = "UNKNOWN_CLASS"

    return predicted_class_name