-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.py
93 lines (78 loc) · 2.63 KB
/
net.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision.transforms import transforms
from pIL import Image
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.cnn1 = nn.Sequential(
nn.ReflectionPad2d(1),
nn.Conv2d(1,4, kernel_size=3),
nn.ReLU(inplace=True),
nn.BatchNorm2d(8),
nn.Dropout2d(p=.2),
nn.ReflectionPad2d(1),
nn.Conv2d(4,8, kernel_size=3),
nn.ReLU(inplace=True),
nn.BatchNorm2d(8),
nn.Dropout2d(p=.2),
nn.ReflectionPad2d(1),
nn.conv2d(8,8, kernel_size=3),
nn.ReLU(inplace=True),
nn.BatchNorm2d(8),
nn.Dropout2d(p=.2)
)
self.fc1 = nn.Sequential(
nn.Linear(8*100*100, 500), # 8 is O/p of Conv, 100*100 are dimensions of image
nn.ReLU(inplace=True),
nn.BatchNorm2d(8),
nn.Dropout2d(p=.2)
)
def forward_once(self,x):
output = self.cnn1(x)
output = output.view(output.size()[0], -1)
output = self.fc1(output)
return output
def forward(self, input1, input2):
output1 = self.forward_once(input1)
output2 = self.forward_once(input2)
return output1, output2
'''
class Loss(nn.Moudle):
def __init__(self, margin=2.0):
super(Loss, self).__init__()
self.margin = margin
def forward(self, output1, output2, label):
euclidean_distance = F.pairwise_distance(output1, output2)
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
'''
cuda_avail = torch.cuda.is_available()
if cuda_avail:
net = Network().cuda()
else:
net = Network()
def open_image(image_path):
print ("Opening Image")
image = Image.Open(image_path)
transformation = transforms.Compose([
transforms.Resize(400),
transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)),
transforms.ToTensor()
])
image_tensor = transformation(image).float()
image_tensor = image_tensor.unsqueeze(0)
if cuda:
image_tensor.cuda()
input = Variable(image_tensor)
return input
image_path1 = "folder1/image1.jpg"
image_path2 = "folder2/image2.jpg"
input1 = open_image(image_path1)
input2 = open_image(image_path2)
output1, output2 = net(input1,input2)
# Output1 and Output2 contains features from input1 and input2