-
Notifications
You must be signed in to change notification settings - Fork 23
/
model.py
50 lines (40 loc) · 1.66 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
48
49
50
########################################
#### Licensed under the MIT license ####
########################################
import torch
import torch.nn as nn
import torch.nn.functional as F
from numpy import prod
import capsules as caps
class CapsuleNetwork(nn.Module):
def __init__(self, img_shape, channels, primary_dim, num_classes, out_dim, num_routing, device: torch.device, kernel_size=9):
super(CapsuleNetwork, self).__init__()
self.img_shape = img_shape
self.num_classes = num_classes
self.device = device
self.conv1 = nn.Conv2d(img_shape[0], channels, kernel_size, stride=1, bias=True)
self.relu = nn.ReLU(inplace=True)
self.primary = caps.PrimaryCapsules(channels, channels, primary_dim, kernel_size)
primary_caps = int(channels / primary_dim * ( img_shape[1] - 2*(kernel_size-1) ) * ( img_shape[2] - 2*(kernel_size-1) ) / 4)
self.digits = caps.RoutingCapsules(primary_dim, primary_caps, num_classes, out_dim, num_routing, device=self.device)
self.decoder = nn.Sequential(
nn.Linear(out_dim * num_classes, 512),
nn.ReLU(inplace=True),
nn.Linear(512, 1024),
nn.ReLU(inplace=True),
nn.Linear(1024, int(prod(img_shape)) ),
nn.Sigmoid()
)
def forward(self, x):
out = self.conv1(x)
out = self.relu(out)
out = self.primary(out)
out = self.digits(out)
preds = torch.norm(out, dim=-1)
# Reconstruct the *predicted* image
_, max_length_idx = preds.max(dim=1)
y = torch.eye(self.num_classes).to(self.device)
y = y.index_select(dim=0, index=max_length_idx).unsqueeze(2)
reconstructions = self.decoder( (out*y).view(out.size(0), -1) )
reconstructions = reconstructions.view(-1, *self.img_shape)
return preds, reconstructions