-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
95 lines (75 loc) · 2.78 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
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
import torch
from torch import nn
import torch.nn.functional as F
class ResBlock(nn.Module):
def __init__(self, in_channels: int, out_channels: int, stride=1):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(
in_channels=in_channels, out_channels=out_channels,
kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(out_channels),
)
self.downsample_layer = None
self.do_downsample = False
if in_channels != out_channels or stride != 1:
self.do_downsample = True
self.downsample_layer = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, stride, 1, bias=False),
nn.BatchNorm2d(out_channels),
)
# initialize weights
self.apply(self.init_weights)
def forward(self, x):
identity = x
out = self.net(x)
if self.do_downsample:
identity = self.downsample_layer(x)
return F.relu(out + identity, inplace=True)
@staticmethod
def init_weights(m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
class ResNet(nn.Module):
def __init__(self, num_classes: int):
super().__init__()
self.net = nn.Sequential(
ResBlock(in_channels=1, out_channels=16),
ResBlock(in_channels=16, out_channels=16),
ResBlock(in_channels=16, out_channels=16, stride=2),
)
self.linear_input_size = 3136
self.linear = nn.Linear(self.linear_input_size, num_classes)
# initialize weights
self.apply(self.init_weights)
def forward(self, x):
x = self.net(x)
x = x.view(-1, self.linear_input_size)
return self.linear(x)
@staticmethod
def init_weights(m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
class LiuDSH(nn.Module):
def __init__(self, code_size: int):
super().__init__()
resnet = ResNet(num_classes=10)
resnet.linear = nn.Linear(
in_features=resnet.linear_input_size, out_features=code_size)
self.net = resnet
# initialize weights
self.apply(self.init_weights)
def forward(self, x):
return self.net(x)
@staticmethod
def init_weights(m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
if __name__ == '__main__':
dummy_tensor = torch.randn((10, 1, 28, 28))
dsh = LiuDSH(code_size=11)
print(dsh)
print(dsh(dummy_tensor).size())