-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
47 lines (34 loc) · 1.38 KB
/
model.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
import torch
from torch import nn
from copy import deepcopy
class MyNet(nn.Module):
def __init__(self):
super(MyNet, self).__init__()
alexnet = torch.hub.load('pytorch/vision:v0.6.0', 'alexnet', pretrained=True)
# Convolutional layers from AlexNet
self.conv1 = deepcopy(alexnet.features)
self.conv2 = deepcopy(alexnet.features)
self.conv1.requires_grad = False
self.conv2.requires_grad = False
self.pyramid = [
nn.AdaptiveMaxPool2d((1, 1)),
nn.AdaptiveMaxPool2d((2, 2)),
nn.AdaptiveMaxPool2d((3, 3)),
nn.AdaptiveMaxPool2d((6, 6)),
# nn.AdaptiveMaxPool2d((13, 13))
]
self.fcQ = nn.Linear(25600, 3)
self.fcT = nn.Linear(25600, 3)
self.beta = 1
def forward(self, first, second):
out1 = self.conv1(first)
out2 = self.conv2(second)
spp1 = torch.cat([ torch.flatten(pool(out1), start_dim=1) for pool in self.pyramid ], dim=1)
spp2 = torch.cat([ torch.flatten(pool(out2), start_dim=1) for pool in self.pyramid ], dim=1)
spp = torch.cat((spp1, spp2), dim=1)
t = self.fcT(spp)
q = self.fcQ(spp)
return t, q
def loss(self, t, q, t_true, q_true):
out_loss = torch.norm(t - t_true, dim=1) + self.beta * torch.norm(q - q_true, dim=1)
return out_loss.mean()