sid-reddy-krishna commited on
Commit
c1bc8a5
·
1 Parent(s): c05576e

similarity

Browse files
app/Hackathon_setup/face_recognition.py CHANGED
@@ -73,6 +73,15 @@ def get_similarity(img1, img2):
73
  ##########################################################################################
74
 
75
  # YOUR CODE HERE, load the model
 
 
 
 
 
 
 
 
 
76
 
77
  # YOUR CODE HERE, return similarity measure using your model
78
 
 
73
  ##########################################################################################
74
 
75
  # YOUR CODE HERE, load the model
76
+ feature_net = SiameseNetwork()
77
+ model = torch.load(current_path + '/siamese_model.t7',map_location=torch.device('cpu'))
78
+ feature_net.load_state_dict(model['net_dict'])
79
+
80
+ # YOUR CODE HERE, return similarity measure using your model
81
+ # Extract features of images inputs_1 and inputs_2
82
+ features_1,features_2 = feature_net(face1,face2)
83
+
84
+ dissimilarity = torch.nn.functional.cosine_similarity(features_1, features_2).item()
85
 
86
  # YOUR CODE HERE, return similarity measure using your model
87
 
app/Hackathon_setup/face_recognition_model.py CHANGED
@@ -13,13 +13,46 @@ trnscm = transforms.Compose([transforms.Resize((100,100)), transforms.ToTensor()
13
  ##Example Network
14
  class Siamese(torch.nn.Module):
15
  def __init__(self):
16
- super(Siamese, self).__init__()
17
- #YOUR CODE HERE
18
-
19
- def forward(self, x):
20
- pass # remove 'pass' once you have written your code
21
- #YOUR CODE HERE
22
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  ##########################################################################################################
24
  ## Sample classification network (Specify if you are using a pytorch classifier during the training) ##
25
  ## classifier = nn.Sequential(nn.Linear(64, 64), nn.BatchNorm1d(64), nn.ReLU(), nn.Linear...) ##
@@ -28,4 +61,4 @@ class Siamese(torch.nn.Module):
28
  # YOUR CODE HERE for pytorch classifier
29
 
30
  # Definition of classes as dictionary
31
- classes = ['person1','person2','person3','person4','person5','person6','person7']
 
13
  ##Example Network
14
  class Siamese(torch.nn.Module):
15
  def __init__(self):
16
+ super(SiameseNetwork, self).__init__()
17
+ self.cnn1 = nn.Sequential(
18
+ nn.ReflectionPad2d(1), #Pads the input tensor using the reflection of the input boundary, it similar to the padding.
19
+ nn.Conv2d(1, 4, kernel_size=3),
20
+ nn.ReLU(inplace=True),
21
+ nn.BatchNorm2d(4),
22
+
23
+ nn.ReflectionPad2d(1),
24
+ nn.Conv2d(4, 8, kernel_size=3),
25
+ nn.ReLU(inplace=True),
26
+ nn.BatchNorm2d(8),
27
+
28
+
29
+ nn.ReflectionPad2d(1),
30
+ nn.Conv2d(8, 8, kernel_size=3),
31
+ nn.ReLU(inplace=True),
32
+ nn.BatchNorm2d(8),
33
+ )
34
+
35
+ self.fc1 = nn.Sequential(
36
+ nn.Linear(8*100*100, 500),
37
+ nn.ReLU(inplace=True),
38
+
39
+ nn.Linear(500, 500),
40
+ nn.ReLU(inplace=True),
41
+
42
+ nn.Linear(500, 5))
43
+
44
+ # forward_once is for one image. This can be used while classifying the face images
45
+ def forward_once(self, x):
46
+ output = self.cnn1(x)
47
+ output = output.view(output.size()[0], -1)
48
+ output = self.fc1(output)
49
+ return output
50
+
51
+ def forward(self, input1, input2):
52
+ output1 = self.forward_once(input1)
53
+ output2 = self.forward_once(input2)
54
+ return output1, output2
55
+
56
  ##########################################################################################################
57
  ## Sample classification network (Specify if you are using a pytorch classifier during the training) ##
58
  ## classifier = nn.Sequential(nn.Linear(64, 64), nn.BatchNorm1d(64), nn.ReLU(), nn.Linear...) ##
 
61
  # YOUR CODE HERE for pytorch classifier
62
 
63
  # Definition of classes as dictionary
64
+ classes = ['person1','person2','person3','person4','person5','person6','person7']