-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconvnextv2.py
More file actions
443 lines (352 loc) · 14.8 KB
/
convnextv2.py
File metadata and controls
443 lines (352 loc) · 14.8 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
# Code from https://github.com/facebookresearch/ConvNeXt-V2/tree/main
import torch
import torch.nn as nn
import torch.nn.functional as F
from timm.models.layers import trunc_normal_, DropPath
"""""" """""" """""" """""" """""" """"""
"""" DEFINE ConvNext-FPN MODEL"""
"""""" """""" """""" """""" """""" """"""
class ConvNextFPN(nn.Module):
def __init__(self, num_classes=1, pretrained_weights=None):
super().__init__()
# Implement backbone architecture
self.convnext = convnextv2_tiny(num_classes=num_classes, pretrained_weights=pretrained_weights)
feature_channels = [96, 192, 384, 768]
# Define FPN Decoder
self.FPN = FPN(
encoder_channels=feature_channels,
encoder_depth=3,
pyramid_channels=256,
segmentation_channels=128,
dropout=0.0,
merge_policy="cat",
num_classes=num_classes,
interpolation=4,
)
def forward(self, x):
# Produce encoder output
features = self.convnext.forward_features(x)
print(len(features), features[0].shape, features[1].shape, features[2].shape, features[3].shape)
# Produce decoder output
seg = self.FPN(*features)
return seg
def forward_features_list(self, x):
# Produce encoder output
features = self.convnext.forward_features(x)
return features
""""""""""""""""""""""""
"""" HELPER FUNCTIONS """
""""""""""""""""""""""""
class LayerNorm(nn.Module):
""" LayerNorm that supports two data formats: channels_last (default) or channels_first.
The ordering of the dimensions in the inputs. channels_last corresponds to inputs with
shape (batch_size, height, width, channels) while channels_first corresponds to inputs
with shape (batch_size, channels, height, width).
"""
def __init__(self, normalized_shape, eps=1e-6, data_format="channels_last"):
super().__init__()
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.bias = nn.Parameter(torch.zeros(normalized_shape))
self.eps = eps
self.data_format = data_format
if self.data_format not in ["channels_last", "channels_first"]:
raise NotImplementedError
self.normalized_shape = (normalized_shape,)
def forward(self, x):
if self.data_format == "channels_last":
return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
elif self.data_format == "channels_first":
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
class GRN(nn.Module):
""" GRN (Global Response Normalization) layer
"""
def __init__(self, dim):
super().__init__()
self.gamma = nn.Parameter(torch.zeros(1, 1, 1, dim))
self.beta = nn.Parameter(torch.zeros(1, 1, 1, dim))
def forward(self, x):
Gx = torch.norm(x, p=2, dim=(1, 2), keepdim=True)
Nx = Gx / (Gx.mean(dim=-1, keepdim=True) + 1e-6)
return self.gamma * (x * Nx) + self.beta + x
class Block(nn.Module):
""" ConvNeXtV2 Block.
Args:
dim (int): Number of input channels.
drop_path (float): Stochastic depth rate. Default: 0.0
"""
def __init__(self, dim, drop_path=0.):
super().__init__()
self.dwconv = nn.Conv2d(dim, dim, kernel_size=7, padding=3, groups=dim) # depthwise conv
self.norm = LayerNorm(dim, eps=1e-6)
self.pwconv1 = nn.Linear(dim, 4 * dim) # pointwise/1x1 convs, implemented with linear layers
self.act = nn.GELU()
self.grn = GRN(4 * dim)
self.pwconv2 = nn.Linear(4 * dim, dim)
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
def forward(self, x):
input = x
x = self.dwconv(x)
x = x.permute(0, 2, 3, 1) # (N, C, H, W) -> (N, H, W, C)
x = self.norm(x)
x = self.pwconv1(x)
x = self.act(x)
x = self.grn(x)
x = self.pwconv2(x)
x = x.permute(0, 3, 1, 2) # (N, H, W, C) -> (N, C, H, W)
x = input + self.drop_path(x)
return x
""""""""""""""""""""""""
"""" MODEL DEFINITIONS """
""""""""""""""""""""""""
class ConvNeXtV2(nn.Module):
""" ConvNeXt V2
Args:
in_chans (int): Number of input image channels. Default: 3
num_classes (int): Number of classes for classification head. Default: 1000
depths (tuple(int)): Number of blocks at each stage. Default: [3, 3, 9, 3]
dims (int): Feature dimension at each stage. Default: [96, 192, 384, 768]
drop_path_rate (float): Stochastic depth rate. Default: 0.
head_init_scale (float): Init scaling value for classifier weights and biases. Default: 1.
"""
def __init__(self, in_chans=3, num_classes=1000,
depths=[3, 3, 9, 3], dims=[96, 192, 384, 768],
drop_path_rate=0., head_init_scale=1.,
pretrained_weights=None):
super().__init__()
self.depths = depths
self.downsample_layers = nn.ModuleList() # stem and 3 intermediate downsampling conv layers
stem = nn.Sequential(
nn.Conv2d(in_chans, dims[0], kernel_size=4, stride=4),
LayerNorm(dims[0], eps=1e-6, data_format="channels_first")
)
self.downsample_layers.append(stem)
for i in range(3):
downsample_layer = nn.Sequential(
LayerNorm(dims[i], eps=1e-6, data_format="channels_first"),
nn.Conv2d(dims[i], dims[i + 1], kernel_size=2, stride=2),
)
self.downsample_layers.append(downsample_layer)
self.stages = nn.ModuleList() # 4 feature resolution stages, each consisting of multiple residual blocks
dp_rates = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))]
cur = 0
for i in range(4):
stage = nn.Sequential(
*[Block(dim=dims[i], drop_path=dp_rates[cur + j]) for j in range(depths[i])]
)
self.stages.append(stage)
cur += depths[i]
self.norm = nn.LayerNorm(dims[-1], eps=1e-6) # final norm layer
self.head = nn.Linear(dims[-1], num_classes)
self.apply(self._init_weights)
self.head.weight.data.mul_(head_init_scale)
self.head.bias.data.mul_(head_init_scale)
if pretrained_weights:
self.load_pretrained_weights(pretrained_weights)
def _init_weights(self, m):
if isinstance(m, (nn.Conv2d, nn.Linear)):
trunc_normal_(m.weight, std=.02)
nn.init.constant_(m.bias, 0)
def forward_features(self, x):
features = []
for i in range(4):
x = self.downsample_layers[i](x)
x = self.stages[i](x)
features.append(x)
return features
def forward(self, x):
features = self.forward_features(x)
x = self.norm(features[-1].mean([-2, -1])) # global average pooling, (N, C, H, W) -> (N, C)
x = self.head(x)
return x
def load_pretrained_weights(self, url):
# Download the weights using torch.hub
pretrained_dict = torch.hub.load_state_dict_from_url(url)
# remove classification head weights
pretrained_dict = {k: v for k, v in pretrained_dict.items() if not k.startswith("head.")}
msg = self.load_state_dict(pretrained_dict, strict=False)
print('Weights loaded from url:', url)
print('Check if weights are loaded successfully: ', msg)
def convnextv2_tiny(**kwargs):
model = ConvNeXtV2(depths=[3, 3, 9, 3], dims=[96, 192, 384, 768], **kwargs)
return model
def convnextv2_base(**kwargs):
model = ConvNeXtV2(depths=[3, 3, 27, 3], dims=[128, 256, 512, 1024], **kwargs)
return model
"""""" """""" """""" """"""
"""" FPN DEFINITIONS """
"""""" """""" """""" """"""
# Code adapted from:
# https://github.com/qubvel/segmentation_models.pytorch/blob/master/segmentation_models_pytorch/decoders/fpn/decoder.py
# https://github.com/qubvel/segmentation_models.pytorch/blob/master/segmentation_models_pytorch/base/modules.py
# https://github.com/qubvel/segmentation_models.pytorch/blob/master/segmentation_models_pytorch/base/heads.py
class Conv3x3GNReLU(nn.Module):
def __init__(self, in_channels, out_channels, upsample=False):
super().__init__()
self.upsample = upsample
self.block = nn.Sequential(
nn.Conv2d(
in_channels,
out_channels,
(3, 3),
stride=1,
padding=1,
bias=False,
),
nn.GroupNorm(32, out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
x = self.block(x)
if self.upsample:
x = F.interpolate(x, scale_factor=2, mode="bilinear", align_corners=True)
return x
class FPNBlock(nn.Module):
def __init__(self, pyramid_channels, skip_channels):
super().__init__()
self.skip_conv = nn.Conv2d(skip_channels, pyramid_channels, kernel_size=1)
def forward(self, x, skip=None):
x = F.interpolate(x, scale_factor=2, mode="nearest")
skip = self.skip_conv(skip)
x = x + skip
return x
class SegmentationBlock(nn.Module):
def __init__(self, in_channels, out_channels, n_upsamples=0):
super().__init__()
blocks = [Conv3x3GNReLU(in_channels, out_channels, upsample=bool(n_upsamples))]
if n_upsamples > 1:
for _ in range(1, n_upsamples):
blocks.append(Conv3x3GNReLU(out_channels, out_channels, upsample=True))
self.block = nn.Sequential(*blocks)
def forward(self, x):
return self.block(x)
class MergeBlock(nn.Module):
def __init__(self, policy):
super().__init__()
if policy not in ["add", "cat"]:
raise ValueError("`merge_policy` must be one of: ['add', 'cat'], got {}".format(policy))
self.policy = policy
def forward(self, x):
if self.policy == "add":
return sum(x)
elif self.policy == "cat":
return torch.cat(x, dim=1)
else:
raise ValueError("`merge_policy` must be one of: ['add', 'cat'], got {}".format(self.policy))
class Activation(nn.Module):
def __init__(self, name, **params):
super().__init__()
if name is None or name == "identity":
self.activation = nn.Identity(**params)
elif name == "sigmoid":
self.activation = nn.Sigmoid()
elif name == "softmax2d":
self.activation = nn.Softmax(dim=1, **params)
elif name == "softmax":
self.activation = nn.Softmax(**params)
elif name == "logsoftmax":
self.activation = nn.LogSoftmax(**params)
elif name == "tanh":
self.activation = nn.Tanh()
else:
raise ValueError(
f"Activation should be callable/sigmoid/softmax/logsoftmax/tanh/"
f"argmax/argmax2d/clamp/None; got {name}"
)
def forward(self, x):
return self.activation(x)
class SegmentationHead(nn.Sequential):
def __init__(
self,
in_channels,
out_channels,
kernel_size=3,
activation=None,
upsampling=1,
):
conv2d = nn.Conv2d(
in_channels,
out_channels,
kernel_size=kernel_size,
padding=kernel_size // 2,
)
upsampling = nn.UpsamplingBilinear2d(scale_factor=upsampling) if upsampling > 1 else nn.Identity()
activation = Activation(activation)
super().__init__(conv2d, upsampling, activation)
class FPN(nn.Module):
def __init__(
self,
encoder_channels,
encoder_depth=5,
pyramid_channels=256,
segmentation_channels=128,
dropout=0.2,
merge_policy="add",
num_classes=1,
interpolation=4,
):
super().__init__()
self.out_channels = segmentation_channels if merge_policy == "add" else segmentation_channels * 4
if encoder_depth < 3:
raise ValueError("Encoder depth for FPN decoder cannot be less than 3, got {}.".format(encoder_depth))
encoder_channels = encoder_channels[::-1]
encoder_channels = encoder_channels[: encoder_depth + 1]
self.p5 = nn.Conv2d(encoder_channels[0], pyramid_channels, kernel_size=1)
self.p4 = FPNBlock(pyramid_channels, encoder_channels[1])
self.p3 = FPNBlock(pyramid_channels, encoder_channels[2])
self.p2 = FPNBlock(pyramid_channels, encoder_channels[3])
self.seg_blocks = nn.ModuleList(
[
SegmentationBlock(
pyramid_channels,
segmentation_channels,
n_upsamples=n_upsamples,
)
for n_upsamples in [3, 2, 1, 0]
]
)
self.merge = MergeBlock(merge_policy)
self.dropout = nn.Dropout2d(p=dropout, inplace=True)
self.segmentation_head = SegmentationHead(
in_channels=self.out_channels,
out_channels=num_classes,
activation=None,
kernel_size=3,
upsampling=interpolation,
)
def forward(self, *features):
c2, c3, c4, c5 = features[-4:]
p5 = self.p5(c5)
p4 = self.p4(p5, c4)
p3 = self.p3(p4, c3)
p2 = self.p2(p3, c2)
feature_pyramid = [seg_block(p) for seg_block, p in zip(self.seg_blocks, [p5, p4, p3, p2])]
x = self.merge(feature_pyramid)
x = self.dropout(x)
seg = self.segmentation_head(x)
return seg
urls = {"convnextv2_tiny_imagenet1k": 'https://dl.fbaipublicfiles.com/convnext/convnextv2/im1k/convnextv2_tiny_1k_224_ema.pt',
"SurgeNet-ConvNextv2": "https://huggingface.co/TimJaspersTue/SurgeNetModels/resolve/main/SurgeNet_ConvNextv2_checkpoint_epoch0050_teacher.pth?download=true",
}
if __name__ == '__main__':
# For CAFormer
model = convnextv2_tiny(num_classes=4, pretrained_weights=urls["convnextv2_tiny_imagenet1k"]).cuda()
#weights = "E:\SurgNet2M_models\weights\SurgNet2M\caformer0100.pth"
#weights = torch.load(weights)
dummy = torch.zeros([12, 3, 256, 256]).cuda()
out = model(dummy)
print(out.shape)
# For Full Segmentation model
#weights = "E:\SurgNet2M_models\weights\SurgNet2M\caformer0100.pth"
#weights = torch.load(weights)
model = ConvNextFPN(num_classes=4, pretrained_weights=urls["convnextv2_tiny_imagenet1k"]).cuda() # pretrained {ImageNet, SurgNet}
dummy = torch.zeros([12, 3, 256, 256]).cuda()
out = model(dummy)
print(out.shape)