-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
144 lines (123 loc) · 6.11 KB
/
models.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import timm
import torch
from torch import nn
class ChoppedWideResNet(nn.Module):
def __init__(self, channels: int, layer_to_extract_from: str, layer_index: int = -1) -> None:
super().__init__()
self._backbone = timm.create_model("wide_resnet101_2", pretrained=True)
self._output = None
self._channels = channels
def forward_hook(m, inputs: torch.Tensor, outputs: torch.Tensor) -> None:
self._output = outputs
raise self.LayerReachedException
for layer in self._backbone.named_children():
if layer[0] == layer_to_extract_from:
layer[1][layer_index].register_forward_hook(forward_hook)
def forward(self, input: torch.Tensor) -> torch.Tensor:
with torch.no_grad():
try:
self._backbone.forward(input)
except self.LayerReachedException:
pass
return self._output[:, : self._channels, :, :]
class LayerReachedException(Exception):
pass
class PatchDescriptionNetwork(nn.Module):
def __init__(self, channels: int) -> None:
super().__init__()
self.channels = channels
self.conv1 = nn.Conv2d(in_channels=3, out_channels=128, kernel_size=4, stride=1, padding=3)
self.avgpool_1 = nn.AvgPool2d(kernel_size=2, stride=2, padding=1)
self.conv2 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=4, stride=1, padding=3)
self.avgpool2 = nn.AvgPool2d(kernel_size=2, stride=2, padding=1)
self.conv3 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1)
self.conv4 = nn.Conv2d(in_channels=256, out_channels=self.channels, kernel_size=4, stride=1, padding=0)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.conv1(x)
x = nn.functional.relu(x, inplace=True)
x = self.avgpool_1(x)
x = self.conv2(x)
x = nn.functional.relu(x, inplace=True)
x = self.avgpool2(x)
x = self.conv3(x)
x = nn.functional.relu(x, inplace=True)
x = self.conv4(x)
return x
class NormalizedPatchDescriptionNetwork(nn.Module):
def __init__(self, pdn: PatchDescriptionNetwork) -> None:
super().__init__()
self.pdn = pdn
self.normalization = nn.BatchNorm2d(num_features=256, affine=False) # self.pdn.channels
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.pdn(x)
x = self.normalization(x)
return x
class AutoEncoder(nn.Module):
def __init__(self, channels: int, output_size: tuple[int, int] = (64, 64)) -> None:
super().__init__()
self._output_size = output_size
# encoding
self.enc_conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=4, stride=2, padding=1)
self.enc_conv2 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size=4, stride=2, padding=1)
self.enc_conv3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=4, stride=2, padding=1)
self.enc_conv4 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=2, padding=1)
self.enc_conv5 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=2, padding=1)
self.enc_conv6 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=8, stride=1, padding=0)
# decoding
self.dec_conv1 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1, padding=2)
self.dec_dropout1 = nn.Dropout(p=0.2)
self.dec_conv2 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1, padding=2)
self.dec_dropout2 = nn.Dropout(p=0.2)
self.dec_conv3 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1, padding=2)
self.dec_dropout3 = nn.Dropout(p=0.2)
self.dec_conv4 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1, padding=2)
self.dec_dropout4 = nn.Dropout(p=0.2)
self.dec_conv5 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1, padding=2)
self.dec_dropout5 = nn.Dropout(p=0.2)
self.dec_conv6 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=4, stride=1, padding=2)
self.dec_dropout6 = nn.Dropout(p=0.2)
self.dec_conv7 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1)
self.dec_conv8 = nn.Conv2d(in_channels=64, out_channels=channels, kernel_size=3, stride=1, padding=1)
def forward(self, x) -> torch.Tensor:
# encoding
x = self.enc_conv1(x)
x = nn.functional.relu(x, inplace=True)
x = self.enc_conv2(x)
x = nn.functional.relu(x, inplace=True)
x = self.enc_conv3(x)
x = nn.functional.relu(x, inplace=True)
x = self.enc_conv4(x)
x = nn.functional.relu(x, inplace=True)
x = self.enc_conv5(x)
x = nn.functional.relu(x, inplace=True)
x = self.enc_conv6(x)
# decoding
x = nn.functional.interpolate(x, size=3, mode="bilinear")
x = self.dec_conv1(x)
x = nn.functional.relu(x, inplace=True)
x = self.dec_dropout1(x)
x = nn.functional.interpolate(x, size=8, mode="bilinear")
x = self.dec_conv2(x)
x = nn.functional.relu(x, inplace=True)
x = self.dec_dropout2(x)
x = nn.functional.interpolate(x, size=15, mode="bilinear")
x = self.dec_conv3(x)
x = nn.functional.relu(x, inplace=True)
x = self.dec_dropout3(x)
x = nn.functional.interpolate(x, size=32, mode="bilinear")
x = self.dec_conv4(x)
x = nn.functional.relu(x, inplace=True)
x = self.dec_dropout4(x)
x = nn.functional.interpolate(x, size=63, mode="bilinear")
x = self.dec_conv5(x)
x = nn.functional.relu(x, inplace=True)
x = self.dec_dropout5(x)
x = nn.functional.interpolate(x, size=127, mode="bilinear")
x = self.dec_conv6(x)
x = nn.functional.relu(x, inplace=True)
x = self.dec_dropout6(x)
x = nn.functional.interpolate(x, size=self._output_size, mode="bilinear")
x = self.dec_conv7(x)
x = nn.functional.relu(x, inplace=True)
x = self.dec_conv8(x)
return x