-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathskip_hourglass_fission.py
232 lines (190 loc) · 7.05 KB
/
skip_hourglass_fission.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "Christian Heider Nielsen"
__doc__ = r"""
Created on 19-09-2021
"""
from typing import Dict, Iterable, Sequence, Tuple, Union
import numpy
import torch
from draugr.torch_utilities import to_tensor
from torch import nn
from torch.nn import init
from neodroidvision.multitask.fission.skip_hourglass.factory import (
fcn_decoder,
fcn_encoder,
)
from neodroidvision.multitask.fission.skip_hourglass.modes import MergeMode, UpscaleMode
__all__ = ["SkipHourglassFission"]
class SkipHourglassFission(nn.Module):
"""
Multi Headed Skip Fully Convolutional Network
Based on https://arxiv.org/abs/1505.04597
Contextual spatial information (from the decoding, expansive pathway) about an input tensor is merged
with information representing the localization of details (from the encoding, compressive pathway).
Modifications to the original paper:
(1) padding is used in 3x3 convolutions to prevent loss of border pixels
(2) merging outputs does not require cropping due to (1)
(3) residual connections can be used by specifying UNet(merge_mode='add')
(4) if non-parametric upsampling is used in the decoder
pathway (specified by upmode='upsample'), then an
additional 1x1 2d convolution occurs after upsampling
to reduce channel dimensionality by a factor of 2.
This channel halving happens with the convolution in
the tranpose convolution (specified by upmode='fractional')"""
def parse_arguments(self, up_mode: UpscaleMode, merge_mode: MergeMode):
"""
Args:
up_mode:
merge_mode:
"""
if up_mode in UpscaleMode:
self.up_mode = up_mode
else:
raise ValueError(
f'"{up_mode}" is not a valid mode for upsampling.'
f' Only "fractional" and "upsample" are allowed.'
)
if merge_mode in MergeMode:
self.merge_mode = merge_mode
else:
raise ValueError(
f'"{up_mode}" is not a valid mode for merging up and down paths.'
f' Only "concat" and "add" are allowed.'
)
if self.up_mode == UpscaleMode.Upsample and self.merge_mode == MergeMode.Add:
# NOTE: up_mode 'upsample' is incompatible with merge_mode 'add'
raise ValueError(
'up_mode "upsample" is incompatible with merge_mode "add"'
"can not use nearest neighbour to reduce depth channels (by half)."
)
def __init__(
self,
*,
input_channels: int,
output_heads: Union[Dict, Iterable],
encoding_depth: int = 5,
start_channels: int = 32,
up_mode: UpscaleMode = UpscaleMode.FractionalTranspose,
merge_mode: MergeMode = MergeMode.Add,
):
"""
:type input_channels: int
:type output_heads: Union[Dict, Iterable]
:type encoding_depth: int
:type start_channels: int
:type up_mode: str
:type merge_mode: str
:param input_channels: number of channels in the input tensor. E.g. 3 for RGB images.
:param output_heads:
:param encoding_depth: number of MaxPools in the U-Net.
:param start_channels: number of convolutional filters for the first convolution.
:param up_mode: string, type of upconvolution. Choices: 'fractional' for transpose convolution or
'upsample' for nearest neighbour upsampling.
:param merge_mode:"""
super().__init__()
self.parse_arguments(up_mode, merge_mode)
self.start_channels = start_channels
self.network_depth = encoding_depth
down_convolutions, encoding_channels = fcn_encoder(
input_channels, self.network_depth, self.start_channels
)
self.down_convolutions = nn.ModuleList(down_convolutions)
self.default_prefix = "fork"
self._dict_output = False
if isinstance(output_heads, Dict):
self.forks = output_heads
self._dict_output = True
else:
self.forks = {
f"{self.default_prefix}{i}": s for i, s in enumerate(output_heads)
}
for iden, channel_size in self.forks.items():
up_convolutions_ae, ae_prev_layer_channels = fcn_decoder(
encoding_channels, self.network_depth, self.up_mode, self.merge_mode
)
up_convolutions_ae.append(
nn.Conv2d(
ae_prev_layer_channels,
channel_size,
kernel_size=1,
groups=1,
stride=1,
)
)
setattr(self, iden, nn.ModuleList(up_convolutions_ae))
self.reset_params()
@staticmethod
def weight_init(m: nn.Module) -> None:
"""
Args:
m:
"""
if isinstance(m, nn.Conv2d):
init.xavier_normal_(m.weight)
init.constant_(m.bias, 0)
def reset_params(self) -> None:
"""description"""
for i, m in enumerate(self.modules()):
self.weight_init(m)
def forward(
self, x_enc: torch.Tensor
) -> Union[Tuple[torch.Tensor], Dict[str, torch.Tensor]]:
"""
Args:
x_enc:
Returns:
"""
encoder_skips = []
for i, module in enumerate(
self.down_convolutions
): # encoder pathway, keep outputs for merging
x_enc, before_pool = module(x_enc)
encoder_skips.append(before_pool)
out = {}
for key in self.forks.keys():
x_prev = x_enc
fork_i = getattr(self, key)
for j, module in enumerate(fork_i[:-1]):
before_pool_skip = encoder_skips[-(j + 2)]
x_prev = module(before_pool_skip, x_prev)
out[key] = fork_i[-1](x_prev)
if self._dict_output:
return out
return (*out.values(),)
def trim(self, idx: Sequence[Union[str, int]]) -> None:
"""
Args:
idx:
"""
if not isinstance(idx, Sequence):
idx = list(idx)
for a in idx:
if not isinstance(a, str):
a = f"{self.default_prefix}{a}"
delattr(self, a)
if __name__ == "__main__":
def _main():
from matplotlib import pyplot
channels = 3
model = SkipHourglassFission(
input_channels=channels,
output_heads=(channels, 1),
encoding_depth=2,
merge_mode=MergeMode.Concat,
)
x = to_tensor(
numpy.random.random((1, channels, 320, 320)), device="cpu"
).float()
out, out2, *_ = model(x)
loss = torch.sum(out)
loss.backward()
im = out.detach()
print(im.shape)
pyplot.imshow((torch.tanh(im[0].transpose(2, 0)) + 1) * 0.5)
pyplot.show()
im2 = out2.detach()
print(im2.shape)
pyplot.imshow((torch.tanh(im2[0][0, :, :]) + 1) * 0.5)
pyplot.show()
_main()