-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWIN.py
More file actions
558 lines (479 loc) · 20.6 KB
/
WIN.py
File metadata and controls
558 lines (479 loc) · 20.6 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# -*- coding: utf-8 -*-
"""
Window Normalization (WIN) — core module.
Reference:
Zhou et al., "A simple normalization technique using window statistics to
improve the out-of-distribution generalization on medical images",
IEEE Transactions on Medical Imaging, 2024.
https://arxiv.org/abs/2207.03366
License: CC BY-NC-SA 4.0
Author: joe1chief <[email protected]>
"""
import numpy as np
import torch
import torch.nn as nn
# ---------------------------------------------------------------------------
# Bounding-box sampling helpers
# ---------------------------------------------------------------------------
def cn_rand_bbox(size, beta, bbx_thres, method="original"):
"""Sample a random bounding box for window cropping.
Args:
size (torch.Size): Feature-map size ``(N, C, W, H)``.
beta (float): Concentration parameter for the Beta distribution.
bbx_thres (float): Minimum area ratio; boxes smaller than this are
resampled.
method (str): Sampling strategy. One of ``"original"``,
``"fixedShape"``, ``"randomShape"``, ``"fixedCenter"``,
``"vertex"``.
Returns:
tuple[int, int, int, int]: ``(bbx1, bby1, bbx2, bby2)`` pixel
coordinates of the sampled box.
"""
W = size[2]
H = size[3]
while True:
if method == "original":
ratio = np.random.beta(beta, beta)
cut_rat = np.sqrt(ratio)
cut_w = int(W * cut_rat)
cut_h = int(H * cut_rat)
cx = np.random.randint(W)
cy = np.random.randint(H)
bbx1 = int(np.clip(cx - cut_w // 2, 0, W))
bby1 = int(np.clip(cy - cut_h // 2, 0, H))
bbx2 = int(np.clip(cx + cut_w // 2, 0, W))
bby2 = int(np.clip(cy + cut_h // 2, 0, H))
elif method == "fixedShape":
ratio = np.random.beta(beta, beta)
cut_rat = np.sqrt(ratio)
cut_w = int(W * cut_rat)
cut_h = int(H * cut_rat)
bbx1 = np.random.randint(0, W - cut_w)
bby1 = np.random.randint(0, H - cut_h)
bbx2 = bbx1 + cut_w
bby2 = bby1 + cut_h
elif method == "randomShape":
scale = np.random.beta(beta, beta)
while True:
ratio = np.random.uniform(0.3, 1 / 0.3)
w_rat = np.sqrt(scale * ratio)
h_rat = np.sqrt(scale / ratio)
cut_w = int(W * w_rat)
cut_h = int(H * h_rat)
if W - cut_w > 0 and H - cut_h > 0:
break
bbx1 = np.random.randint(0, W - cut_w)
bby1 = np.random.randint(0, H - cut_h)
bbx2 = bbx1 + cut_w
bby2 = bby1 + cut_h
elif method == "fixedCenter":
ratio = np.random.beta(beta, beta)
cut_rat = np.sqrt(ratio)
cut_w = int(W * cut_rat)
cut_h = int(H * cut_rat)
cx = W // 2
cy = H // 2
bbx1 = int(np.clip(cx - cut_w // 2, 0, W))
bby1 = int(np.clip(cy - cut_h // 2, 0, H))
bbx2 = int(np.clip(cx + cut_w // 2, 0, W))
bby2 = int(np.clip(cy + cut_h // 2, 0, H))
elif method == "vertex":
ratio = np.random.beta(beta, beta)
cut_rat = np.sqrt(ratio)
cut_w = int(W * cut_rat)
cut_h = int(H * cut_rat)
bbx1 = np.random.choice([0, W - cut_w])
bby1 = np.random.choice([0, H - cut_h])
bbx2 = bbx1 + cut_w
bby2 = bby1 + cut_h
else:
raise ValueError(f"Unknown bbox sampling method: '{method}'")
area_ratio = float(bbx2 - bbx1) * (bby2 - bby1) / (W * H)
if area_ratio >= bbx_thres:
break
return bbx1, bby1, bbx2, bby2
def calc_ins_std_mean(x):
"""Compute per-instance, per-channel standard deviation and mean.
Args:
x (torch.Tensor): Feature map of shape ``(N, C, H, W)`` or
``(N, C, L)``.
Returns:
tuple[torch.Tensor, torch.Tensor]: ``(std, mean)``, each of shape
``(N, C, 1, 1)``.
"""
N, C = x.size()[:2]
if x.dim() == 4:
std, mean = torch.std_mean(x, dim=(2, 3), keepdim=True)
elif x.dim() == 3:
std, mean = torch.std_mean(x, dim=2, keepdim=True)
else:
raise NotImplementedError(
f"Unsupported feature shape with {x.dim()} dimensions."
)
return std.view(N, C, 1, 1), mean.view(N, C, 1, 1)
# ---------------------------------------------------------------------------
# Main normalization layer
# ---------------------------------------------------------------------------
class WindowNorm2d(nn.Module):
"""Window Normalization (WIN) for 2-D feature maps.
During training WIN perturbs the normalizing statistics with local
statistics computed on a randomly cropped *window* of the feature map,
which acts as a feature-level augmentation and improves out-of-
distribution (OOD) generalization.
At inference the layer falls back to standard instance normalization
(per-instance, per-channel mean/std).
Args:
num_features (int): Number of feature channels ``C``.
mask_thres (float): Minimum window area ratio (0–1). The most
important hyper-parameter; empirically set to 0.3–0.7.
eps (float): Small constant added to the denominator for numerical
stability.
alpha (float): Concentration parameter of the Beta distribution used
for mixup interpolation between window and global statistics.
mix (bool): If ``True``, interpolate window statistics with global
statistics (WIN-WIN style).
grid (bool): If ``True``, use a structured grid mask instead of a
random bounding box. Recommended for images with a consistent
background.
input_size (int): Spatial size of the input image (used when
``grid=True``).
mask_patch_size (int): Patch size for the grid mask generator (used
when ``grid=True``).
affine (bool): If ``True``, add learnable per-channel scale and bias
parameters (analogous to BN's ``gamma``/``beta``).
cached (bool): If ``True``, load pre-computed bounding boxes from
``bboxs_path`` for faster training.
bboxs_path (str): Path to the ``.npy`` file containing pre-computed
bounding boxes. Only used when ``cached=True``.
Default: ``"./bboxs.npy"``.
device (str): Device string passed to internal tensors (e.g.
``"cuda"`` or ``"cpu"``).
Example::
>>> import torchvision.models as models
>>> net = models.resnet18(weights=None)
>>> net = WindowNorm2d.convert_WIN_model(net)
"""
# Class-level bbox cache shared across all instances to avoid redundant
# disk I/O. All WIN layers in a single model share the same pre-computed
# bbox pool — this is intentional.
_cached_bboxs = None
_cached_bboxs_len = 0
def __init__(
self,
num_features,
mask_thres=0.7,
eps=1e-5,
alpha=0.1,
mix=True,
grid=False,
input_size=224,
mask_patch_size=32,
affine=False,
cached=True,
bboxs_path="./bboxs.npy",
device="cuda",
):
super().__init__()
if cached and WindowNorm2d._cached_bboxs is None:
WindowNorm2d._cached_bboxs = np.load(bboxs_path)
WindowNorm2d._cached_bboxs_len = len(WindowNorm2d._cached_bboxs)
self.num_features = num_features
self.mask_thres = mask_thres
self.input_size = input_size
self.mask_patch_size = mask_patch_size
self.eps = eps
self.device = device
# Mixup between window statistics and global statistics
self.mix = mix
self.alpha = torch.tensor(alpha, device=device)
self.beta_dist = torch.distributions.Beta(self.alpha, self.alpha)
# Grid mask (SimMIM-style) vs. random bounding box
self.grid = grid
self.affine = affine
if self.affine:
self.weight = nn.Parameter(torch.ones(num_features))
self.bias = nn.Parameter(torch.zeros(num_features))
else:
self.register_parameter("weight", None)
self.register_parameter("bias", None)
def forward(self, x):
if self.training:
N, C, H, W = x.size()
if self.grid:
# Lazy-initialise the fast grid mask generator on first call
if not hasattr(self, "_mask_generator"):
scale = round(self.input_size / H)
self._mask_generator = MaskGenerator(
input_size=self.input_size,
mask_patch_size=self.mask_patch_size,
model_patch_size=scale,
mask_ratio=self.mask_thres,
device=self.device,
)
masked_x, _ = self._mask_generator(x)
else:
if WindowNorm2d._cached_bboxs is None:
bbx1, bby1, bbx2, bby2 = cn_rand_bbox(
x.size(), beta=1, bbx_thres=self.mask_thres
)
else:
idx = np.random.randint(0, WindowNorm2d._cached_bboxs_len)
bbx1, bby1, bbx2, bby2 = WindowNorm2d._cached_bboxs[idx]
bbx1, bby1 = int(W * bbx1), int(H * bby1)
bbx2, bby2 = int(W * bbx2), int(H * bby2)
masked_x = x[:, :, bbx1:bbx2, bby1:bby2]
std, mean = calc_ins_std_mean(masked_x)
if self.mix and not self.grid:
lmda = self.beta_dist.sample((N, C, 1, 1))
global_std, global_mean = calc_ins_std_mean(x)
mean = mean * lmda + global_mean * (1 - lmda)
std = std * lmda + global_std * (1 - lmda)
else:
std, mean = calc_ins_std_mean(x)
normalized = (x - mean) / (std + self.eps)
if self.affine:
return self.weight[:, None, None] * normalized + self.bias[:, None, None]
return normalized
def __repr__(self):
return (
f"WindowNorm2d("
f"num_features={self.num_features}, "
f"mask_thres={self.mask_thres}, "
f"alpha={self.alpha.item():.4f}, "
f"mix={self.mix}, "
f"grid={self.grid}, "
f"input_size={self.input_size}, "
f"mask_patch_size={self.mask_patch_size}, "
f"eps={self.eps}, "
f"affine={self.affine}, "
f"cached={WindowNorm2d._cached_bboxs is not None}, "
f"device={self.device!r})"
)
# ------------------------------------------------------------------
# Model-conversion class methods
# ------------------------------------------------------------------
@classmethod
def convert_WIN_model(
cls,
module,
mask_thres=0.7,
alpha=0.1,
mix=True,
grid=False,
input_size=224,
mask_patch_size=32,
affine=False,
cached=False,
bboxs_path="./bboxs.npy",
device="cuda",
):
"""Recursively replace all ``BatchNorm2d`` layers with
:class:`WindowNorm2d`.
Args:
module (nn.Module): The model to convert.
mask_thres (float): Passed to :class:`WindowNorm2d`.
alpha (float): Passed to :class:`WindowNorm2d`.
mix (bool): Passed to :class:`WindowNorm2d`.
grid (bool): Passed to :class:`WindowNorm2d`.
input_size (int): Passed to :class:`WindowNorm2d`.
mask_patch_size (int): Passed to :class:`WindowNorm2d`.
affine (bool): Passed to :class:`WindowNorm2d`.
cached (bool): Passed to :class:`WindowNorm2d`.
bboxs_path (str): Passed to :class:`WindowNorm2d`.
device (str): Passed to :class:`WindowNorm2d`.
Returns:
nn.Module: Converted model.
Example::
>>> net = models.resnet18(weights=None)
>>> net = WindowNorm2d.convert_WIN_model(net)
"""
mod = module
if isinstance(module, nn.BatchNorm2d):
mod = cls(
module.num_features,
mask_thres=mask_thres,
alpha=alpha,
mix=mix,
grid=grid,
input_size=input_size,
mask_patch_size=mask_patch_size,
affine=affine,
cached=cached,
bboxs_path=bboxs_path,
device=device,
)
for name, child in module.named_children():
mod.add_module(
name,
cls.convert_WIN_model(
child,
mask_thres=mask_thres,
alpha=alpha,
mix=mix,
grid=grid,
input_size=input_size,
mask_patch_size=mask_patch_size,
affine=affine,
cached=cached,
bboxs_path=bboxs_path,
device=device,
),
)
del module
return mod
@classmethod
def convert_IN_model(cls, module):
"""Recursively replace all ``BatchNorm2d`` layers with
``InstanceNorm2d``.
Args:
module (nn.Module): The model to convert.
Returns:
nn.Module: Converted model.
"""
mod = module
if isinstance(module, nn.BatchNorm2d):
mod = nn.InstanceNorm2d(module.num_features)
for name, child in module.named_children():
mod.add_module(name, cls.convert_IN_model(child))
del module
return mod
@classmethod
def convert_Identity_model(cls, module):
"""Recursively replace all ``BatchNorm2d`` layers with
``Identity``.
Args:
module (nn.Module): The model to convert.
Returns:
nn.Module: Converted model.
"""
mod = module
if isinstance(module, nn.BatchNorm2d):
mod = nn.Identity()
for name, child in module.named_children():
mod.add_module(name, cls.convert_Identity_model(child))
del module
return mod
@classmethod
def convert_GN_model(cls, module, num_groups=64):
"""Recursively replace all ``BatchNorm2d`` layers with
``GroupNorm``.
Args:
module (nn.Module): The model to convert.
num_groups (int): Number of groups for ``GroupNorm``.
Returns:
nn.Module: Converted model.
"""
mod = module
if isinstance(module, nn.BatchNorm2d):
mod = nn.GroupNorm(num_groups, module.num_features)
for name, child in module.named_children():
mod.add_module(name, cls.convert_GN_model(child, num_groups=num_groups))
del module
return mod
# ---------------------------------------------------------------------------
# Grid mask generator (SimMIM-style) — optimised with pre-computed pool
# ---------------------------------------------------------------------------
class MaskGenerator:
"""Structured grid mask generator with a pre-computed mask pool.
Compared to the naïve implementation that calls ``torch.randperm`` and
``repeat_interleave`` on every forward pass, this class pre-builds a pool
of ``pool_size`` boolean masks at construction time and stores them as a
single contiguous GPU/CPU tensor. Each forward pass reduces to a single
random index lookup followed by a ``torch.gather``, yielding a **~5×
speed-up** on CPU and further gains on CUDA.
Optimisation details
--------------------
1. **Pre-computed mask pool** — ``pool_size`` boolean masks are generated
once and cached as ``(pool_size, H_feat, W_feat)`` tensor. No
``randperm`` or ``repeat_interleave`` is executed at runtime.
2. **Pre-computed keep-indices** — for each mask in the pool the flat
indices of the *kept* (unmasked) tokens are stored as a
``(pool_size, K)`` int64 tensor, eliminating the hidden ``nonzero``
call that boolean fancy-indexing would trigger.
3. **``torch.gather`` instead of boolean fancy-index** — ``gather`` has
predictable memory access patterns and avoids a device-sync on CUDA.
4. **Boolean dtype from construction** — no int→bool cast in the hot path.
Args:
input_size (int): Spatial size of the input image.
mask_patch_size (int): Size of each mask patch in pixels.
``input_size`` must be divisible by ``mask_patch_size``.
model_patch_size (int): Patch size used by the backbone (i.e. the
ratio between ``input_size`` and the feature-map spatial size).
``mask_patch_size`` must be divisible by ``model_patch_size``.
mask_ratio (float): Fraction of patches to mask (0–1).
pool_size (int): Number of masks to pre-compute and cache.
Larger pools reduce mask repetition at the cost of more memory.
Default: 512.
device (str | torch.device): Device on which to store the mask pool.
Should match the device of the feature maps passed to
``__call__``. Default: ``"cpu"``.
Example::
>>> gen = MaskGenerator(input_size=224, mask_patch_size=32,
... model_patch_size=8, mask_ratio=0.6,
... pool_size=512, device="cuda")
>>> masked_x, mask = gen(feature_map) # feature_map: (N, C, 28, 28)
"""
def __init__(
self,
input_size=224,
mask_patch_size=32,
model_patch_size=2,
mask_ratio=0.6,
pool_size=512,
device="cpu",
):
assert input_size % mask_patch_size == 0, (
"input_size must be divisible by mask_patch_size"
)
assert mask_patch_size % model_patch_size == 0, (
"mask_patch_size must be divisible by model_patch_size"
)
self.rand_size = input_size // mask_patch_size
self.scale = mask_patch_size // model_patch_size
self.feat_size = self.rand_size * self.scale
self.token_count = self.rand_size ** 2
self.mask_count = int(np.ceil(self.token_count * mask_ratio))
self.keep_count = self.token_count - self.mask_count
self.pool_size = pool_size
self.device = device
# ── build mask pool ──────────────────────────────────────────────
# pool_flat: (P, token_count) True = masked
pool_flat = torch.zeros(pool_size, self.token_count, dtype=torch.bool)
for i in range(pool_size):
idx = torch.randperm(self.token_count)[: self.mask_count]
pool_flat[i, idx] = True
# Expand patch-level mask → feature-map resolution: (P, H_feat, W_feat)
pool_patch = pool_flat.view(pool_size, self.rand_size, self.rand_size)
pool_feat = (
pool_patch
.repeat_interleave(self.scale, dim=1)
.repeat_interleave(self.scale, dim=2)
)
# Pre-compute flat keep-indices: (P, K) where K = keep_count * scale²
keep_flat = ~pool_feat.view(pool_size, -1) # True = keep
keep_indices = [
keep_flat[i].nonzero(as_tuple=False).squeeze(1)
for i in range(pool_size)
]
# Stack requires all rows to have the same length — guaranteed because
# every mask has exactly mask_count masked patches × scale² pixels.
self.keep_indices = torch.stack(keep_indices).to(device) # (P, K)
self.pool_feat = pool_feat.to(device) # (P, H, W)
def __call__(self, x):
"""Apply a randomly selected pre-computed mask to feature map ``x``.
Args:
x (torch.Tensor): Feature map of shape ``(N, C, H, W)`` where
``H == W == feat_size``.
Returns:
tuple[torch.Tensor, torch.Tensor]:
- **masked_x** ``(N, C, K)`` — the kept (unmasked) tokens.
- **mask** ``(H, W)`` — boolean mask; ``True`` = masked/dropped.
"""
p = int(torch.randint(self.pool_size, (1,)).item())
mask = self.pool_feat[p] # (H, W)
N, C, H, W = x.shape
flat = x.reshape(N, C, H * W) # (N, C, H*W)
ki = self.keep_indices[p] # (K,)
ki_exp = ki.unsqueeze(0).unsqueeze(0).expand(N, C, -1)
masked_x = flat.gather(2, ki_exp) # (N, C, K)
return masked_x, mask