-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathnet_desc.py
229 lines (188 loc) · 7.45 KB
/
net_desc.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
import sys
from collections import OrderedDict
from typing import List
import numpy as np
import torch
import torch.nn as nn
from torchvision.models.resnet import Bottleneck as ResNetBottleneck
from torchvision.models.resnet import ResNet
sys.path.append("../../tiatoolbox")
from tiatoolbox.models.abc import ModelABC
from tiatoolbox.models.architecture.hovernet import HoVerNet as TIAHoVerNet
from tiatoolbox.models.architecture.utils import UpSample2x
class ResNetExt(ResNet):
def _forward_impl(self, x, freeze):
# See note [TorchScript super()]
if self.training:
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
with torch.set_grad_enabled(not freeze):
x1 = x = self.layer1(x)
x2 = x = self.layer2(x)
x3 = x = self.layer3(x)
x4 = x = self.layer4(x)
else:
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x1 = x = self.layer1(x)
x2 = x = self.layer2(x)
x3 = x = self.layer3(x)
x4 = x = self.layer4(x)
return x1, x2, x3, x4
def forward(self, x: torch.Tensor, freeze: bool = False) -> torch.Tensor:
return self._forward_impl(x, freeze)
@staticmethod
def resnet50(num_input_channels, pretrained=None):
model = ResNetExt(ResNetBottleneck, [3, 4, 6, 3])
model.conv1 = nn.Conv2d(
num_input_channels, 64, 7, stride=1, padding=3)
if pretrained is not None:
pretrained = torch.load(pretrained)
(
missing_keys, unexpected_keys
) = model.load_state_dict(pretrained, strict=False)
return model
class DenseBlock(nn.Module):
"""Dense Block as defined in:
Huang, Gao, Zhuang Liu, Laurens Van Der Maaten, and Kilian Q. Weinberger.
"Densely connected convolutional networks." In Proceedings of the IEEE conference
on computer vision and pattern recognition, pp. 4700-4708. 2017.
Only performs `valid` convolution.
"""
def __init__(self, in_ch, unit_ksize, unit_ch, unit_count, split=1):
super().__init__()
assert len(unit_ksize) == len(unit_ch), "Unbalance Unit Info"
self.nr_unit = unit_count
self.in_ch = in_ch
self.unit_ch = unit_ch
# ! For inference only so init values for batchnorm may not match tensorflow
unit_in_ch = in_ch
pad_vals = [v // 2 for v in unit_ksize]
self.units = nn.ModuleList()
for idx in range(unit_count):
self.units.append(
nn.Sequential(
nn.BatchNorm2d(unit_in_ch, eps=1e-5),
nn.ReLU(inplace=True),
nn.Conv2d(
unit_in_ch, unit_ch[0], unit_ksize[0],
stride=1, padding=pad_vals[0], bias=False,
),
nn.BatchNorm2d(unit_ch[0], eps=1e-5),
nn.ReLU(inplace=True),
nn.Conv2d(
unit_ch[0], unit_ch[1], unit_ksize[1],
stride=1, padding=pad_vals[1], bias=False,
groups=split,
),
)
)
unit_in_ch += unit_ch[1]
self.blk_bna = nn.Sequential(
nn.BatchNorm2d(unit_in_ch, eps=1e-5),
nn.ReLU(inplace=True)
)
def out_ch(self):
return self.in_ch + self.nr_unit * self.unit_ch[-1]
def forward(self, prev_feat):
for idx in range(self.nr_unit):
new_feat = self.units[idx](prev_feat)
prev_feat = torch.cat([prev_feat, new_feat], dim=1)
prev_feat = self.blk_bna(prev_feat)
return prev_feat
class HoVerNetConic(ModelABC):
"""Initialise HoVer-Net."""
def __init__(
self,
num_types=None,
freeze=False,
pretrained_backbone=None,
):
super().__init__()
self.freeze = freeze
self.num_types = num_types
self.output_ch = 3 if num_types is None else 4
self.backbone = ResNetExt.resnet50(
3, pretrained=pretrained_backbone)
self.conv_bot = nn.Conv2d(
2048, 1024, 1, stride=1, padding=0, bias=False)
def create_decoder_branch(out_ch=2, ksize=5):
pad = ksize // 2
module_list = [
nn.Conv2d(1024, 256, ksize, stride=1, padding=pad, bias=False),
DenseBlock(256, [1, ksize], [128, 32], 8, split=4),
nn.Conv2d(512, 512, 1, stride=1, padding=0, bias=False),
]
u3 = nn.Sequential(*module_list)
module_list = [
nn.Conv2d(512, 128, ksize, stride=1, padding=pad, bias=False),
DenseBlock(128, [1, ksize], [128, 32], 4, split=4),
nn.Conv2d(256, 256, 1, stride=1, padding=0, bias=False),
]
u2 = nn.Sequential(*module_list)
module_list = [
nn.Conv2d(256, 64, ksize, stride=1, padding=pad, bias=False),
]
u1 = nn.Sequential(*module_list)
module_list = [
nn.BatchNorm2d(64, eps=1e-5),
nn.ReLU(inplace=True),
nn.Conv2d(64, out_ch, 1, stride=1, padding=0, bias=True),
]
u0 = nn.Sequential(*module_list)
decoder = nn.Sequential(
OrderedDict([("u3", u3), ("u2", u2), ("u1", u1), ("u0", u0)])
)
return decoder
ksize = 3
if num_types is None:
self.decoder = nn.ModuleDict(
OrderedDict(
[
("np", create_decoder_branch(ksize=ksize, out_ch=2)),
("hv", create_decoder_branch(ksize=ksize, out_ch=2)),
]
)
)
else:
self.decoder = nn.ModuleDict(
OrderedDict(
[
("tp", create_decoder_branch(ksize=ksize, out_ch=num_types)),
("np", create_decoder_branch(ksize=ksize, out_ch=2)),
("hv", create_decoder_branch(ksize=ksize, out_ch=2)),
]
)
)
self.upsample2x = UpSample2x()
def forward(self, imgs):
imgs = imgs / 255.0 # to 0-1 range to match XY
d0, d1, d2, d3 = self.backbone(imgs, self.freeze)
d3 = self.conv_bot(d3)
d = [d0, d1, d2, d3]
out_dict = OrderedDict()
for branch_name, branch_desc in self.decoder.items():
u3 = self.upsample2x(d[-1]) + d[-2]
u3 = branch_desc[0](u3)
u2 = self.upsample2x(u3) + d[-3]
u2 = branch_desc[1](u2)
u1 = self.upsample2x(u2) + d[-4]
u1 = branch_desc[2](u1)
u0 = branch_desc[3](u1)
out_dict[branch_name] = u0
return out_dict
@staticmethod
def _proc_np_hv(np_map: np.ndarray, hv_map: np.ndarray, fx: float = 1):
return TIAHoVerNet._proc_np_hv(np_map, hv_map, fx)
@staticmethod
def _get_instance_info(pred_inst, pred_type=None):
return TIAHoVerNet._get_instance_info(pred_inst, pred_type)
@staticmethod
# skipcq: PYL-W0221
def postproc(raw_maps: List[np.ndarray]):
return TIAHoVerNet.postproc(raw_maps)
@staticmethod
def infer_batch(model, batch_data, on_gpu):
return TIAHoVerNet.infer_batch(model, batch_data, on_gpu)