-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiamese.py
251 lines (188 loc) · 7.88 KB
/
siamese.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import numpy as np
import random
from PIL import Image
import PIL.ImageOps
import torchvision
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
import torchvision.utils
import torch
from torch.autograd import Variable
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
import time
# Creating some helper functions
def imshow(img, text=None):
npimg = img.numpy()
plt.axis("off")
if text:
plt.text(75, 8, text, style='italic',fontweight='bold',
bbox={'facecolor':'white', 'alpha':0.8, 'pad':10})
plt.show()
def show_plot(iteration,loss):
plt.plot(iteration,loss)
plt.show()
def scale(img, factor: float):
if isinstance(img, Image.Image):
assert 0.1 < factor < 100
width, height = img.size
new_size = (int(factor * width), int(factor * height))
return img.resize(new_size, Image.LANCZOS)
if isinstance(img, np.ndarray):
w, h, *_ = img.shape
target_size = (int(h*factor), int(w*factor))
return cv2.resize(img, target_size)
assert False
class SiameseNetworkDataset(Dataset):
def __init__(self,imageFolderDataset,transform=None):
self.imageFolderDataset = imageFolderDataset
self.transform = transform
def __getitem__(self,index):
img0_tuple = random.choice(self.imageFolderDataset.imgs)
#We need to approximately 50% of images to be in the same class
should_get_same_class = random.randint(0,1)
if should_get_same_class:
while True:
#Look untill the same class image is found
img1_tuple = random.choice(self.imageFolderDataset.imgs)
if img0_tuple[1] == img1_tuple[1]:
break
else:
while True:
#Look untill a different class image is found
img1_tuple = random.choice(self.imageFolderDataset.imgs)
if img0_tuple[1] != img1_tuple[1]:
break
img0 = Image.open(img0_tuple[0])
img1 = Image.open(img1_tuple[0])
img0 = PIL.ImageOps.expand(img0, border=int(random.uniform(0, 30)), fill=0)
img0 = img0.convert("L")
img1 = img1.convert("L")
if self.transform is not None:
img0 = self.transform(img0)
img1 = self.transform(img1)
return img0, img1, torch.from_numpy(np.array([int(img1_tuple[1] != img0_tuple[1])], dtype=np.float32))
def __len__(self):
return len(self.imageFolderDataset.imgs)
# Load the training dataset
folder_dataset = datasets.ImageFolder(root="./data/")
# Resize the images and transform to tensors
transformation = transforms.Compose([transforms.Resize((100,100)),
transforms.ToTensor()
])
# Initialize the network
siamese_dataset = SiameseNetworkDataset(imageFolderDataset=folder_dataset,
transform=transformation)
# Create a simple dataloader just for simple visualization
vis_dataloader = DataLoader(siamese_dataset,
shuffle=True,
num_workers=2,
batch_size=8)
# Extract one batch
example_batch = next(iter(vis_dataloader))
# Example batch is a list containing 2x8 images, indexes 0 and 1, an also the label
# If the label is 1, it means that it is not the same person, label is 0, same person in both images
concatenated = torch.cat((example_batch[0], example_batch[1]),0)
print(example_batch[2].numpy().reshape(-1))
#create the Siamese Neural Network
class SiameseNetwork(nn.Module):
def __init__(self):
super(SiameseNetwork, self).__init__()
# Setting up the Sequential of CNN Layers
self.cnn1 = nn.Sequential(
nn.Conv2d(1, 96, kernel_size=11,stride=4),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, stride=2),
nn.Conv2d(96, 256, kernel_size=5, stride=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, stride=2),
nn.Conv2d(256, 384, kernel_size=3,stride=1),
nn.ReLU(inplace=True)
)
# Setting up the Fully Connected Layers
self.fc1 = nn.Sequential(
nn.Linear(384, 1024),
nn.ReLU(inplace=True),
nn.Linear(1024, 256),
nn.ReLU(inplace=True),
nn.Linear(256,2)
)
def forward_once(self, x):
print(f"{time.time():.4f}", "in forward once")
# This function will be called for both images
# It's output is used to determine the similiarity
output = self.cnn1(x)
output = output.view(output.size()[0], -1)
output = self.fc1(output)
print(f"{time.time():.4f}", "done with forward once")
return output
def forward(self, input1, input2):
# In this function we pass in both images and obtain both vectors
# which are returned
output1 = self.forward_once(input1)
output2 = self.forward_once(input2)
return output1, output2
# Define the Contrastive Loss Function
class ContrastiveLoss(torch.nn.Module):
def __init__(self, margin=2.0):
super(ContrastiveLoss, self).__init__()
self.margin = margin
def forward(self, output1, output2, label):
# Calculate the euclidian distance and calculate the contrastive loss
euclidean_distance = F.pairwise_distance(output1, output2, keepdim = True)
loss_contrastive = torch.mean((1-label) * torch.pow(euclidean_distance, 2) +
(label) * torch.pow(torch.clamp(self.margin - euclidean_distance, min=0.0), 2))
return loss_contrastive
# Load the training dataset
train_dataloader = DataLoader(siamese_dataset,
shuffle=True,
num_workers=8,
batch_size=64)
net = SiameseNetwork()
criterion = ContrastiveLoss()
optimizer = optim.Adam(net.parameters(), lr = 0.0005 )
counter = []
loss_history = []
iteration_number= 0
# Iterate throught the epochs
for epoch in range(10):
# Iterate over batches
for i, (img0, img1, label) in enumerate(train_dataloader, 0):
# # Send the images and labels to CUDA
# img0, img1, label = img0.cuda(), img1.cuda(), label.cuda()
# Zero the gradients
optimizer.zero_grad()
# Pass in the two images into the network and obtain two outputs
output1, output2 = net(img0, img1)
# Pass the outputs of the networks and label into the loss function
loss_contrastive = criterion(output1, output2, label)
# Calculate the backpropagation
loss_contrastive.backward()
# Optimize
optimizer.step()
# Every 10 batches print out the loss
if i % 10 == 0 :
print(f"Epoch number {epoch}\n Current loss {loss_contrastive.item()}\n")
iteration_number += 10
counter.append(iteration_number)
loss_history.append(loss_contrastive.item())
# Locate the test dataset and load it into the SiameseNetworkDataset
folder_dataset_test = datasets.ImageFolder(root="./data")
siamese_dataset = SiameseNetworkDataset(imageFolderDataset=folder_dataset_test,
transform=transformation)
test_dataloader = DataLoader(siamese_dataset, num_workers=2, batch_size=1, shuffle=True)
# Grab one image that we are going to test
dataiter = iter(test_dataloader)
for i in range(100):
print(f"{time.time():.4f}", "start")
# Iterate over 10 images and test them with the first image (x0)
x0, x1, label2 = next(dataiter)
print(f"{time.time():.4f}", "gotten image")
output1, output2 = net(x0, x1)
print(f"{time.time():.4f}", "get outputss")
euclidean_distance = F.pairwise_distance(output1, output2)
print(f"{time.time():.4f}", "get dist")
break
print(label2, euclidean_distance)