-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.py
64 lines (52 loc) · 2.05 KB
/
Network.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
class Time_Network(nn.Module):
# network architecture
def __init__(self, in_channels, embedding_size, ff_inner):
super().__init__()
encoder = [
torch.nn.Conv2d(in_channels, 32, kernel_size=3, stride=1, padding=0),
nn.ReLU(),
torch.nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=0),
nn.ReLU(),
torch.nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=0),
nn.ReLU(),
torch.nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=0),
nn.ReLU(),
torch.nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=0),
nn.ReLU(),
torch.nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=0),
nn.ReLU(),
nn.Flatten(),
nn.Linear(1024,512),
nn.ReLU(),
nn.Linear(512, embedding_size),
nn.BatchNorm1d(embedding_size)
]
time = [
nn.Linear(embedding_size, ff_inner),
nn.ReLU(),
nn.Linear(ff_inner, ff_inner//2),
nn.ReLU(),
nn.Linear(ff_inner//2, 1, bias=False)
]
self.encoder = nn.Sequential(*encoder)
self.time_network = nn.Sequential(*time)
def forward(self, x1, x2):
# input shape x: batch_size, channel, x, y
# compare images x1[i] with x2[i] index i=0..N
x = self.encoder(torch.cat((x1, x2), dim=0))
x1_embeddings, x2_embeddings = torch.split(x, x1.shape[0], dim=0)
time = self.time(x1_embeddings, x2_embeddings)
return time
def embedding(self, x):
return self.encoder(x)
def time(self, x1, x2):
# input shape x: batch_size, embedding_size
# cat x1[i] and x2[i] to calc time
compare = x2-x1 #torch.cat((x1, x2), dim=1)
time = self.time_network(compare)
time = 1 - torch.exp( - time)
return time