Yuvaaaaaaaraaaj commited on
Commit
363eadf
·
verified ·
1 Parent(s): 8f6e005

Upload BEN2.py

Browse files
Files changed (1) hide show
  1. tools/BEN2.py +1359 -0
tools/BEN2.py ADDED
@@ -0,0 +1,1359 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2025 Prama LLC
2
+ # SPDX-License-Identifier: MIT
3
+
4
+ import math
5
+ import os
6
+ import random
7
+ import subprocess
8
+ import tempfile
9
+ import time
10
+
11
+ import cv2
12
+ import numpy as np
13
+ import torch
14
+ import torch.nn as nn
15
+ import torch.nn.functional as F
16
+ import torch.utils.checkpoint as checkpoint
17
+ from einops import rearrange
18
+ from PIL import Image, ImageOps
19
+ from timm.models.layers import DropPath, to_2tuple, trunc_normal_
20
+ from torchvision import transforms
21
+
22
+
23
+ def set_random_seed(seed):
24
+ random.seed(seed)
25
+ np.random.seed(seed)
26
+ torch.manual_seed(seed)
27
+ torch.cuda.manual_seed(seed)
28
+ torch.cuda.manual_seed_all(seed)
29
+ torch.backends.cudnn.deterministic = True
30
+ torch.backends.cudnn.benchmark = False
31
+
32
+
33
+ # set_random_seed(9)
34
+
35
+ torch.set_float32_matmul_precision('highest')
36
+
37
+
38
+ class Mlp(nn.Module):
39
+ """ Multilayer perceptron."""
40
+
41
+ def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):
42
+ super().__init__()
43
+ out_features = out_features or in_features
44
+ hidden_features = hidden_features or in_features
45
+ self.fc1 = nn.Linear(in_features, hidden_features)
46
+ self.act = act_layer()
47
+ self.fc2 = nn.Linear(hidden_features, out_features)
48
+ self.drop = nn.Dropout(drop)
49
+
50
+ def forward(self, x):
51
+ x = self.fc1(x)
52
+ x = self.act(x)
53
+ x = self.drop(x)
54
+ x = self.fc2(x)
55
+ x = self.drop(x)
56
+ return x
57
+
58
+
59
+ def window_partition(x, window_size):
60
+ """
61
+ Args:
62
+ x: (B, H, W, C)
63
+ window_size (int): window size
64
+ Returns:
65
+ windows: (num_windows*B, window_size, window_size, C)
66
+ """
67
+ B, H, W, C = x.shape
68
+ x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)
69
+ windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C)
70
+ return windows
71
+
72
+
73
+ def window_reverse(windows, window_size, H, W):
74
+ """
75
+ Args:
76
+ windows: (num_windows*B, window_size, window_size, C)
77
+ window_size (int): Window size
78
+ H (int): Height of image
79
+ W (int): Width of image
80
+ Returns:
81
+ x: (B, H, W, C)
82
+ """
83
+ B = int(windows.shape[0] / (H * W / window_size / window_size))
84
+ x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1)
85
+ x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
86
+ return x
87
+
88
+
89
+ class WindowAttention(nn.Module):
90
+ """ Window based multi-head self attention (W-MSA) module with relative position bias.
91
+ It supports both of shifted and non-shifted window.
92
+ Args:
93
+ dim (int): Number of input channels.
94
+ window_size (tuple[int]): The height and width of the window.
95
+ num_heads (int): Number of attention heads.
96
+ qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
97
+ qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set
98
+ attn_drop (float, optional): Dropout ratio of attention weight. Default: 0.0
99
+ proj_drop (float, optional): Dropout ratio of output. Default: 0.0
100
+ """
101
+
102
+ def __init__(self, dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0., proj_drop=0.):
103
+
104
+ super().__init__()
105
+ self.dim = dim
106
+ self.window_size = window_size # Wh, Ww
107
+ self.num_heads = num_heads
108
+ head_dim = dim // num_heads
109
+ self.scale = qk_scale or head_dim ** -0.5
110
+
111
+ # define a parameter table of relative position bias
112
+ self.relative_position_bias_table = nn.Parameter(
113
+ torch.zeros((2 * window_size[0] - 1) * (2 * window_size[1] - 1), num_heads)) # 2*Wh-1 * 2*Ww-1, nH
114
+
115
+ # get pair-wise relative position index for each token inside the window
116
+ coords_h = torch.arange(self.window_size[0])
117
+ coords_w = torch.arange(self.window_size[1])
118
+ coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww
119
+ coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww
120
+ relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww
121
+ relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2
122
+ relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0
123
+ relative_coords[:, :, 1] += self.window_size[1] - 1
124
+ relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1
125
+ relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww
126
+ self.register_buffer("relative_position_index", relative_position_index)
127
+
128
+ self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
129
+ self.attn_drop = nn.Dropout(attn_drop)
130
+ self.proj = nn.Linear(dim, dim)
131
+ self.proj_drop = nn.Dropout(proj_drop)
132
+
133
+ trunc_normal_(self.relative_position_bias_table, std=.02)
134
+ self.softmax = nn.Softmax(dim=-1)
135
+
136
+ def forward(self, x, mask=None):
137
+ """ Forward function.
138
+ Args:
139
+ x: input features with shape of (num_windows*B, N, C)
140
+ mask: (0/-inf) mask with shape of (num_windows, Wh*Ww, Wh*Ww) or None
141
+ """
142
+ B_, N, C = x.shape
143
+ qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
144
+ q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple)
145
+
146
+ q = q * self.scale
147
+ attn = (q @ k.transpose(-2, -1))
148
+
149
+ relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view(
150
+ self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1) # Wh*Ww,Wh*Ww,nH
151
+ relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
152
+ attn = attn + relative_position_bias.unsqueeze(0)
153
+
154
+ if mask is not None:
155
+ nW = mask.shape[0]
156
+ attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0)
157
+ attn = attn.view(-1, self.num_heads, N, N)
158
+ attn = self.softmax(attn)
159
+ else:
160
+ attn = self.softmax(attn)
161
+
162
+ attn = self.attn_drop(attn)
163
+
164
+ x = (attn @ v).transpose(1, 2).reshape(B_, N, C)
165
+ x = self.proj(x)
166
+ x = self.proj_drop(x)
167
+ return x
168
+
169
+
170
+ class SwinTransformerBlock(nn.Module):
171
+ """ Swin Transformer Block.
172
+ Args:
173
+ dim (int): Number of input channels.
174
+ num_heads (int): Number of attention heads.
175
+ window_size (int): Window size.
176
+ shift_size (int): Shift size for SW-MSA.
177
+ mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
178
+ qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
179
+ qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set.
180
+ drop (float, optional): Dropout rate. Default: 0.0
181
+ attn_drop (float, optional): Attention dropout rate. Default: 0.0
182
+ drop_path (float, optional): Stochastic depth rate. Default: 0.0
183
+ act_layer (nn.Module, optional): Activation layer. Default: nn.GELU
184
+ norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
185
+ """
186
+
187
+ def __init__(self, dim, num_heads, window_size=7, shift_size=0,
188
+ mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0., drop_path=0.,
189
+ act_layer=nn.GELU, norm_layer=nn.LayerNorm):
190
+ super().__init__()
191
+ self.dim = dim
192
+ self.num_heads = num_heads
193
+ self.window_size = window_size
194
+ self.shift_size = shift_size
195
+ self.mlp_ratio = mlp_ratio
196
+ assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size"
197
+
198
+ self.norm1 = norm_layer(dim)
199
+ self.attn = WindowAttention(
200
+ dim, window_size=to_2tuple(self.window_size), num_heads=num_heads,
201
+ qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
202
+
203
+ self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
204
+ self.norm2 = norm_layer(dim)
205
+ mlp_hidden_dim = int(dim * mlp_ratio)
206
+ self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
207
+
208
+ self.H = None
209
+ self.W = None
210
+
211
+ def forward(self, x, mask_matrix):
212
+ """ Forward function.
213
+ Args:
214
+ x: Input feature, tensor size (B, H*W, C).
215
+ H, W: Spatial resolution of the input feature.
216
+ mask_matrix: Attention mask for cyclic shift.
217
+ """
218
+ B, L, C = x.shape
219
+ H, W = self.H, self.W
220
+ assert L == H * W, "input feature has wrong size"
221
+
222
+ shortcut = x
223
+ x = self.norm1(x)
224
+ x = x.view(B, H, W, C)
225
+
226
+ # pad feature maps to multiples of window size
227
+ pad_l = pad_t = 0
228
+ pad_r = (self.window_size - W % self.window_size) % self.window_size
229
+ pad_b = (self.window_size - H % self.window_size) % self.window_size
230
+ x = F.pad(x, (0, 0, pad_l, pad_r, pad_t, pad_b))
231
+ _, Hp, Wp, _ = x.shape
232
+
233
+ # cyclic shift
234
+ if self.shift_size > 0:
235
+ shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2))
236
+ attn_mask = mask_matrix
237
+ else:
238
+ shifted_x = x
239
+ attn_mask = None
240
+
241
+ # partition windows
242
+ x_windows = window_partition(shifted_x, self.window_size) # nW*B, window_size, window_size, C
243
+ x_windows = x_windows.view(-1, self.window_size * self.window_size, C) # nW*B, window_size*window_size, C
244
+
245
+ # W-MSA/SW-MSA
246
+ attn_windows = self.attn(x_windows, mask=attn_mask) # nW*B, window_size*window_size, C
247
+
248
+ # merge windows
249
+ attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C)
250
+ shifted_x = window_reverse(attn_windows, self.window_size, Hp, Wp) # B H' W' C
251
+
252
+ # reverse cyclic shift
253
+ if self.shift_size > 0:
254
+ x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2))
255
+ else:
256
+ x = shifted_x
257
+
258
+ if pad_r > 0 or pad_b > 0:
259
+ x = x[:, :H, :W, :].contiguous()
260
+
261
+ x = x.view(B, H * W, C)
262
+
263
+ # FFN
264
+ x = shortcut + self.drop_path(x)
265
+ x = x + self.drop_path(self.mlp(self.norm2(x)))
266
+
267
+ return x
268
+
269
+
270
+ class PatchMerging(nn.Module):
271
+ """ Patch Merging Layer
272
+ Args:
273
+ dim (int): Number of input channels.
274
+ norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
275
+ """
276
+
277
+ def __init__(self, dim, norm_layer=nn.LayerNorm):
278
+ super().__init__()
279
+ self.dim = dim
280
+ self.reduction = nn.Linear(4 * dim, 2 * dim, bias=False)
281
+ self.norm = norm_layer(4 * dim)
282
+
283
+ def forward(self, x, H, W):
284
+ """ Forward function.
285
+ Args:
286
+ x: Input feature, tensor size (B, H*W, C).
287
+ H, W: Spatial resolution of the input feature.
288
+ """
289
+ B, L, C = x.shape
290
+ assert L == H * W, "input feature has wrong size"
291
+
292
+ x = x.view(B, H, W, C)
293
+
294
+ # padding
295
+ pad_input = (H % 2 == 1) or (W % 2 == 1)
296
+ if pad_input:
297
+ x = F.pad(x, (0, 0, 0, W % 2, 0, H % 2))
298
+
299
+ x0 = x[:, 0::2, 0::2, :] # B H/2 W/2 C
300
+ x1 = x[:, 1::2, 0::2, :] # B H/2 W/2 C
301
+ x2 = x[:, 0::2, 1::2, :] # B H/2 W/2 C
302
+ x3 = x[:, 1::2, 1::2, :] # B H/2 W/2 C
303
+ x = torch.cat([x0, x1, x2, x3], -1) # B H/2 W/2 4*C
304
+ x = x.view(B, -1, 4 * C) # B H/2*W/2 4*C
305
+
306
+ x = self.norm(x)
307
+ x = self.reduction(x)
308
+
309
+ return x
310
+
311
+
312
+ class BasicLayer(nn.Module):
313
+ """ A basic Swin Transformer layer for one stage.
314
+ Args:
315
+ dim (int): Number of feature channels
316
+ depth (int): Depths of this stage.
317
+ num_heads (int): Number of attention head.
318
+ window_size (int): Local window size. Default: 7.
319
+ mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4.
320
+ qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
321
+ qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set.
322
+ drop (float, optional): Dropout rate. Default: 0.0
323
+ attn_drop (float, optional): Attention dropout rate. Default: 0.0
324
+ drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0
325
+ norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
326
+ downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None
327
+ use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False.
328
+ """
329
+
330
+ def __init__(self,
331
+ dim,
332
+ depth,
333
+ num_heads,
334
+ window_size=7,
335
+ mlp_ratio=4.,
336
+ qkv_bias=True,
337
+ qk_scale=None,
338
+ drop=0.,
339
+ attn_drop=0.,
340
+ drop_path=0.,
341
+ norm_layer=nn.LayerNorm,
342
+ downsample=None,
343
+ use_checkpoint=False):
344
+ super().__init__()
345
+ self.window_size = window_size
346
+ self.shift_size = window_size // 2
347
+ self.depth = depth
348
+ self.use_checkpoint = use_checkpoint
349
+
350
+ # build blocks
351
+ self.blocks = nn.ModuleList([
352
+ SwinTransformerBlock(
353
+ dim=dim,
354
+ num_heads=num_heads,
355
+ window_size=window_size,
356
+ shift_size=0 if (i % 2 == 0) else window_size // 2,
357
+ mlp_ratio=mlp_ratio,
358
+ qkv_bias=qkv_bias,
359
+ qk_scale=qk_scale,
360
+ drop=drop,
361
+ attn_drop=attn_drop,
362
+ drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path,
363
+ norm_layer=norm_layer)
364
+ for i in range(depth)])
365
+
366
+ # patch merging layer
367
+ if downsample is not None:
368
+ self.downsample = downsample(dim=dim, norm_layer=norm_layer)
369
+ else:
370
+ self.downsample = None
371
+
372
+ def forward(self, x, H, W):
373
+ """ Forward function.
374
+ Args:
375
+ x: Input feature, tensor size (B, H*W, C).
376
+ H, W: Spatial resolution of the input feature.
377
+ """
378
+
379
+ # calculate attention mask for SW-MSA
380
+ Hp = int(np.ceil(H / self.window_size)) * self.window_size
381
+ Wp = int(np.ceil(W / self.window_size)) * self.window_size
382
+ img_mask = torch.zeros((1, Hp, Wp, 1), device=x.device) # 1 Hp Wp 1
383
+ h_slices = (slice(0, -self.window_size),
384
+ slice(-self.window_size, -self.shift_size),
385
+ slice(-self.shift_size, None))
386
+ w_slices = (slice(0, -self.window_size),
387
+ slice(-self.window_size, -self.shift_size),
388
+ slice(-self.shift_size, None))
389
+ cnt = 0
390
+ for h in h_slices:
391
+ for w in w_slices:
392
+ img_mask[:, h, w, :] = cnt
393
+ cnt += 1
394
+
395
+ mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1
396
+ mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
397
+ attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
398
+ attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))
399
+
400
+ for blk in self.blocks:
401
+ blk.H, blk.W = H, W
402
+ if self.use_checkpoint:
403
+ x = checkpoint.checkpoint(blk, x, attn_mask)
404
+ else:
405
+ x = blk(x, attn_mask)
406
+ if self.downsample is not None:
407
+ x_down = self.downsample(x, H, W)
408
+ Wh, Ww = (H + 1) // 2, (W + 1) // 2
409
+ return x, H, W, x_down, Wh, Ww
410
+ else:
411
+ return x, H, W, x, H, W
412
+
413
+
414
+ class PatchEmbed(nn.Module):
415
+ """ Image to Patch Embedding
416
+ Args:
417
+ patch_size (int): Patch token size. Default: 4.
418
+ in_chans (int): Number of input image channels. Default: 3.
419
+ embed_dim (int): Number of linear projection output channels. Default: 96.
420
+ norm_layer (nn.Module, optional): Normalization layer. Default: None
421
+ """
422
+
423
+ def __init__(self, patch_size=4, in_chans=3, embed_dim=96, norm_layer=None):
424
+ super().__init__()
425
+ patch_size = to_2tuple(patch_size)
426
+ self.patch_size = patch_size
427
+
428
+ self.in_chans = in_chans
429
+ self.embed_dim = embed_dim
430
+
431
+ self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
432
+ if norm_layer is not None:
433
+ self.norm = norm_layer(embed_dim)
434
+ else:
435
+ self.norm = None
436
+
437
+ def forward(self, x):
438
+ """Forward function."""
439
+ # padding
440
+ _, _, H, W = x.size()
441
+ if W % self.patch_size[1] != 0:
442
+ x = F.pad(x, (0, self.patch_size[1] - W % self.patch_size[1]))
443
+ if H % self.patch_size[0] != 0:
444
+ x = F.pad(x, (0, 0, 0, self.patch_size[0] - H % self.patch_size[0]))
445
+
446
+ x = self.proj(x) # B C Wh Ww
447
+ if self.norm is not None:
448
+ Wh, Ww = x.size(2), x.size(3)
449
+ x = x.flatten(2).transpose(1, 2)
450
+ x = self.norm(x)
451
+ x = x.transpose(1, 2).view(-1, self.embed_dim, Wh, Ww)
452
+
453
+ return x
454
+
455
+
456
+ class SwinTransformer(nn.Module):
457
+ """ Swin Transformer backbone.
458
+ A PyTorch impl of : `Swin Transformer: Hierarchical Vision Transformer using Shifted Windows` -
459
+ https://arxiv.org/pdf/2103.14030
460
+ Args:
461
+ pretrain_img_size (int): Input image size for training the pretrained model,
462
+ used in absolute postion embedding. Default 224.
463
+ patch_size (int | tuple(int)): Patch size. Default: 4.
464
+ in_chans (int): Number of input image channels. Default: 3.
465
+ embed_dim (int): Number of linear projection output channels. Default: 96.
466
+ depths (tuple[int]): Depths of each Swin Transformer stage.
467
+ num_heads (tuple[int]): Number of attention head of each stage.
468
+ window_size (int): Window size. Default: 7.
469
+ mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4.
470
+ qkv_bias (bool): If True, add a learnable bias to query, key, value. Default: True
471
+ qk_scale (float): Override default qk scale of head_dim ** -0.5 if set.
472
+ drop_rate (float): Dropout rate.
473
+ attn_drop_rate (float): Attention dropout rate. Default: 0.
474
+ drop_path_rate (float): Stochastic depth rate. Default: 0.2.
475
+ norm_layer (nn.Module): Normalization layer. Default: nn.LayerNorm.
476
+ ape (bool): If True, add absolute position embedding to the patch embedding. Default: False.
477
+ patch_norm (bool): If True, add normalization after patch embedding. Default: True.
478
+ out_indices (Sequence[int]): Output from which stages.
479
+ frozen_stages (int): Stages to be frozen (stop grad and set eval mode).
480
+ -1 means not freezing any parameters.
481
+ use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False.
482
+ """
483
+
484
+ def __init__(self,
485
+ pretrain_img_size=224,
486
+ patch_size=4,
487
+ in_chans=3,
488
+ embed_dim=96,
489
+ depths=[2, 2, 6, 2],
490
+ num_heads=[3, 6, 12, 24],
491
+ window_size=7,
492
+ mlp_ratio=4.,
493
+ qkv_bias=True,
494
+ qk_scale=None,
495
+ drop_rate=0.,
496
+ attn_drop_rate=0.,
497
+ drop_path_rate=0.2,
498
+ norm_layer=nn.LayerNorm,
499
+ ape=False,
500
+ patch_norm=True,
501
+ out_indices=(0, 1, 2, 3),
502
+ frozen_stages=-1,
503
+ use_checkpoint=False):
504
+ super().__init__()
505
+
506
+ self.pretrain_img_size = pretrain_img_size
507
+ self.num_layers = len(depths)
508
+ self.embed_dim = embed_dim
509
+ self.ape = ape
510
+ self.patch_norm = patch_norm
511
+ self.out_indices = out_indices
512
+ self.frozen_stages = frozen_stages
513
+
514
+ # split image into non-overlapping patches
515
+ self.patch_embed = PatchEmbed(
516
+ patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim,
517
+ norm_layer=norm_layer if self.patch_norm else None)
518
+
519
+ # absolute position embedding
520
+ if self.ape:
521
+ pretrain_img_size = to_2tuple(pretrain_img_size)
522
+ patch_size = to_2tuple(patch_size)
523
+ patches_resolution = [pretrain_img_size[0] // patch_size[0], pretrain_img_size[1] // patch_size[1]]
524
+
525
+ self.absolute_pos_embed = nn.Parameter(
526
+ torch.zeros(1, embed_dim, patches_resolution[0], patches_resolution[1]))
527
+ trunc_normal_(self.absolute_pos_embed, std=.02)
528
+
529
+ self.pos_drop = nn.Dropout(p=drop_rate)
530
+
531
+ # stochastic depth
532
+ dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))] # stochastic depth decay rule
533
+
534
+ # build layers
535
+ self.layers = nn.ModuleList()
536
+ for i_layer in range(self.num_layers):
537
+ layer = BasicLayer(
538
+ dim=int(embed_dim * 2 ** i_layer),
539
+ depth=depths[i_layer],
540
+ num_heads=num_heads[i_layer],
541
+ window_size=window_size,
542
+ mlp_ratio=mlp_ratio,
543
+ qkv_bias=qkv_bias,
544
+ qk_scale=qk_scale,
545
+ drop=drop_rate,
546
+ attn_drop=attn_drop_rate,
547
+ drop_path=dpr[sum(depths[:i_layer]):sum(depths[:i_layer + 1])],
548
+ norm_layer=norm_layer,
549
+ downsample=PatchMerging if (i_layer < self.num_layers - 1) else None,
550
+ use_checkpoint=use_checkpoint)
551
+ self.layers.append(layer)
552
+
553
+ num_features = [int(embed_dim * 2 ** i) for i in range(self.num_layers)]
554
+ self.num_features = num_features
555
+
556
+ # add a norm layer for each output
557
+ for i_layer in out_indices:
558
+ layer = norm_layer(num_features[i_layer])
559
+ layer_name = f'norm{i_layer}'
560
+ self.add_module(layer_name, layer)
561
+
562
+ self._freeze_stages()
563
+
564
+ def _freeze_stages(self):
565
+ if self.frozen_stages >= 0:
566
+ self.patch_embed.eval()
567
+ for param in self.patch_embed.parameters():
568
+ param.requires_grad = False
569
+
570
+ if self.frozen_stages >= 1 and self.ape:
571
+ self.absolute_pos_embed.requires_grad = False
572
+
573
+ if self.frozen_stages >= 2:
574
+ self.pos_drop.eval()
575
+ for i in range(0, self.frozen_stages - 1):
576
+ m = self.layers[i]
577
+ m.eval()
578
+ for param in m.parameters():
579
+ param.requires_grad = False
580
+
581
+ def forward(self, x):
582
+
583
+ x = self.patch_embed(x)
584
+
585
+ Wh, Ww = x.size(2), x.size(3)
586
+ if self.ape:
587
+ # interpolate the position embedding to the corresponding size
588
+ absolute_pos_embed = F.interpolate(self.absolute_pos_embed, size=(Wh, Ww), mode='bicubic')
589
+ x = (x + absolute_pos_embed) # B Wh*Ww C
590
+
591
+ outs = [x.contiguous()]
592
+ x = x.flatten(2).transpose(1, 2)
593
+ x = self.pos_drop(x)
594
+
595
+ for i in range(self.num_layers):
596
+ layer = self.layers[i]
597
+ x_out, H, W, x, Wh, Ww = layer(x, Wh, Ww)
598
+
599
+ if i in self.out_indices:
600
+ norm_layer = getattr(self, f'norm{i}')
601
+ x_out = norm_layer(x_out)
602
+
603
+ out = x_out.view(-1, H, W, self.num_features[i]).permute(0, 3, 1, 2).contiguous()
604
+ outs.append(out)
605
+
606
+ return tuple(outs)
607
+
608
+
609
+ def get_activation_fn(activation):
610
+ """Return an activation function given a string"""
611
+ if activation == "gelu":
612
+ return F.gelu
613
+
614
+ raise RuntimeError(F"activation should be gelu, not {activation}.")
615
+
616
+
617
+ def make_cbr(in_dim, out_dim):
618
+ return nn.Sequential(nn.Conv2d(in_dim, out_dim, kernel_size=3, padding=1), nn.InstanceNorm2d(out_dim), nn.GELU())
619
+
620
+
621
+ def make_cbg(in_dim, out_dim):
622
+ return nn.Sequential(nn.Conv2d(in_dim, out_dim, kernel_size=3, padding=1), nn.InstanceNorm2d(out_dim), nn.GELU())
623
+
624
+
625
+ def rescale_to(x, scale_factor: float = 2, interpolation='nearest'):
626
+ return F.interpolate(x, scale_factor=scale_factor, mode=interpolation)
627
+
628
+
629
+ def resize_as(x, y, interpolation='bilinear'):
630
+ return F.interpolate(x, size=y.shape[-2:], mode=interpolation)
631
+
632
+
633
+ def image2patches(x):
634
+ """b c (hg h) (wg w) -> (hg wg b) c h w"""
635
+ x = rearrange(x, 'b c (hg h) (wg w) -> (hg wg b) c h w', hg=2, wg=2)
636
+ return x
637
+
638
+
639
+ def patches2image(x):
640
+ """(hg wg b) c h w -> b c (hg h) (wg w)"""
641
+ x = rearrange(x, '(hg wg b) c h w -> b c (hg h) (wg w)', hg=2, wg=2)
642
+ return x
643
+
644
+
645
+ class PositionEmbeddingSine:
646
+ def __init__(self, num_pos_feats=64, temperature=10000, normalize=False, scale=None):
647
+ super().__init__()
648
+ self.num_pos_feats = num_pos_feats
649
+ self.temperature = temperature
650
+ self.normalize = normalize
651
+ if scale is not None and normalize is False:
652
+ raise ValueError("normalize should be True if scale is passed")
653
+ if scale is None:
654
+ scale = 2 * math.pi
655
+ self.scale = scale
656
+ self.dim_t = torch.arange(0, self.num_pos_feats, dtype=torch.float32)
657
+
658
+ def __call__(self, b, h, w):
659
+ device = self.dim_t.device
660
+ mask = torch.zeros([b, h, w], dtype=torch.bool, device=device)
661
+ assert mask is not None
662
+ not_mask = ~mask
663
+ y_embed = not_mask.cumsum(dim=1, dtype=torch.float32)
664
+ x_embed = not_mask.cumsum(dim=2, dtype=torch.float32)
665
+ if self.normalize:
666
+ eps = 1e-6
667
+ y_embed = (y_embed - 0.5) / (y_embed[:, -1:, :] + eps) * self.scale
668
+ x_embed = (x_embed - 0.5) / (x_embed[:, :, -1:] + eps) * self.scale
669
+
670
+ dim_t = self.temperature ** (2 * (self.dim_t.to(device) // 2) / self.num_pos_feats)
671
+ pos_x = x_embed[:, :, :, None] / dim_t
672
+ pos_y = y_embed[:, :, :, None] / dim_t
673
+
674
+ pos_x = torch.stack((pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()), dim=4).flatten(3)
675
+ pos_y = torch.stack((pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()), dim=4).flatten(3)
676
+
677
+ return torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2)
678
+
679
+
680
+ class PositionEmbeddingSine:
681
+ def __init__(self, num_pos_feats=64, temperature=10000, normalize=False, scale=None):
682
+ super().__init__()
683
+ self.num_pos_feats = num_pos_feats
684
+ self.temperature = temperature
685
+ self.normalize = normalize
686
+ if scale is not None and normalize is False:
687
+ raise ValueError("normalize should be True if scale is passed")
688
+ if scale is None:
689
+ scale = 2 * math.pi
690
+ self.scale = scale
691
+ self.dim_t = torch.arange(0, self.num_pos_feats, dtype=torch.float32)
692
+
693
+ def __call__(self, b, h, w):
694
+ device = self.dim_t.device
695
+ mask = torch.zeros([b, h, w], dtype=torch.bool, device=device)
696
+ assert mask is not None
697
+ not_mask = ~mask
698
+ y_embed = not_mask.cumsum(dim=1, dtype=torch.float32)
699
+ x_embed = not_mask.cumsum(dim=2, dtype=torch.float32)
700
+ if self.normalize:
701
+ eps = 1e-6
702
+ y_embed = (y_embed - 0.5) / (y_embed[:, -1:, :] + eps) * self.scale
703
+ x_embed = (x_embed - 0.5) / (x_embed[:, :, -1:] + eps) * self.scale
704
+
705
+ dim_t = self.temperature ** (2 * (self.dim_t.to(device) // 2) / self.num_pos_feats)
706
+ pos_x = x_embed[:, :, :, None] / dim_t
707
+ pos_y = y_embed[:, :, :, None] / dim_t
708
+
709
+ pos_x = torch.stack((pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()), dim=4).flatten(3)
710
+ pos_y = torch.stack((pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()), dim=4).flatten(3)
711
+
712
+ return torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2)
713
+
714
+
715
+ class MCLM(nn.Module):
716
+ def __init__(self, d_model, num_heads, pool_ratios=[1, 4, 8]):
717
+ super(MCLM, self).__init__()
718
+ self.attention = nn.ModuleList([
719
+ nn.MultiheadAttention(d_model, num_heads, dropout=0.1),
720
+ nn.MultiheadAttention(d_model, num_heads, dropout=0.1),
721
+ nn.MultiheadAttention(d_model, num_heads, dropout=0.1),
722
+ nn.MultiheadAttention(d_model, num_heads, dropout=0.1),
723
+ nn.MultiheadAttention(d_model, num_heads, dropout=0.1)
724
+ ])
725
+
726
+ self.linear1 = nn.Linear(d_model, d_model * 2)
727
+ self.linear2 = nn.Linear(d_model * 2, d_model)
728
+ self.linear3 = nn.Linear(d_model, d_model * 2)
729
+ self.linear4 = nn.Linear(d_model * 2, d_model)
730
+ self.norm1 = nn.LayerNorm(d_model)
731
+ self.norm2 = nn.LayerNorm(d_model)
732
+ self.dropout = nn.Dropout(0.1)
733
+ self.dropout1 = nn.Dropout(0.1)
734
+ self.dropout2 = nn.Dropout(0.1)
735
+ self.activation = get_activation_fn('gelu')
736
+ self.pool_ratios = pool_ratios
737
+ self.p_poses = []
738
+ self.g_pos = None
739
+ self.positional_encoding = PositionEmbeddingSine(num_pos_feats=d_model // 2, normalize=True)
740
+
741
+ def forward(self, l, g):
742
+ """
743
+ l: 4,c,h,w
744
+ g: 1,c,h,w
745
+ """
746
+ self.p_poses = []
747
+ self.g_pos = None
748
+ b, c, h, w = l.size()
749
+ # 4,c,h,w -> 1,c,2h,2w
750
+ concated_locs = rearrange(l, '(hg wg b) c h w -> b c (hg h) (wg w)', hg=2, wg=2)
751
+
752
+ pools = []
753
+ for pool_ratio in self.pool_ratios:
754
+ # b,c,h,w
755
+ tgt_hw = (round(h / pool_ratio), round(w / pool_ratio))
756
+ pool = F.adaptive_avg_pool2d(concated_locs, tgt_hw)
757
+ pools.append(rearrange(pool, 'b c h w -> (h w) b c'))
758
+ if self.g_pos is None:
759
+ pos_emb = self.positional_encoding(pool.shape[0], pool.shape[2], pool.shape[3])
760
+ pos_emb = rearrange(pos_emb, 'b c h w -> (h w) b c')
761
+ self.p_poses.append(pos_emb)
762
+ pools = torch.cat(pools, 0)
763
+ if self.g_pos is None:
764
+ self.p_poses = torch.cat(self.p_poses, dim=0)
765
+ pos_emb = self.positional_encoding(g.shape[0], g.shape[2], g.shape[3])
766
+ self.g_pos = rearrange(pos_emb, 'b c h w -> (h w) b c')
767
+
768
+ device = pools.device
769
+ self.p_poses = self.p_poses.to(device)
770
+ self.g_pos = self.g_pos.to(device)
771
+
772
+ # attention between glb (q) & multisensory concated-locs (k,v)
773
+ g_hw_b_c = rearrange(g, 'b c h w -> (h w) b c')
774
+
775
+ g_hw_b_c = g_hw_b_c + self.dropout1(self.attention[0](g_hw_b_c + self.g_pos, pools + self.p_poses, pools)[0])
776
+ g_hw_b_c = self.norm1(g_hw_b_c)
777
+ g_hw_b_c = g_hw_b_c + self.dropout2(self.linear2(self.dropout(self.activation(self.linear1(g_hw_b_c)).clone())))
778
+ g_hw_b_c = self.norm2(g_hw_b_c)
779
+
780
+ # attention between origin locs (q) & freashed glb (k,v)
781
+ l_hw_b_c = rearrange(l, "b c h w -> (h w) b c")
782
+ _g_hw_b_c = rearrange(g_hw_b_c, '(h w) b c -> h w b c', h=h, w=w)
783
+ _g_hw_b_c = rearrange(_g_hw_b_c, "(ng h) (nw w) b c -> (h w) (ng nw b) c", ng=2, nw=2)
784
+ outputs_re = []
785
+ for i, (_l, _g) in enumerate(zip(l_hw_b_c.chunk(4, dim=1), _g_hw_b_c.chunk(4, dim=1))):
786
+ outputs_re.append(self.attention[i + 1](_l, _g, _g)[0]) # (h w) 1 c
787
+ outputs_re = torch.cat(outputs_re, 1) # (h w) 4 c
788
+
789
+ l_hw_b_c = l_hw_b_c + self.dropout1(outputs_re)
790
+ l_hw_b_c = self.norm1(l_hw_b_c)
791
+ l_hw_b_c = l_hw_b_c + self.dropout2(self.linear4(self.dropout(self.activation(self.linear3(l_hw_b_c)).clone())))
792
+ l_hw_b_c = self.norm2(l_hw_b_c)
793
+
794
+ l = torch.cat((l_hw_b_c, g_hw_b_c), 1) # hw,b(5),c
795
+ return rearrange(l, "(h w) b c -> b c h w", h=h, w=w) ## (5,c,h*w)
796
+
797
+
798
+ class MCRM(nn.Module):
799
+ def __init__(self, d_model, num_heads, pool_ratios=[4, 8, 16], h=None):
800
+ super(MCRM, self).__init__()
801
+ self.attention = nn.ModuleList([
802
+ nn.MultiheadAttention(d_model, num_heads, dropout=0.1),
803
+ nn.MultiheadAttention(d_model, num_heads, dropout=0.1),
804
+ nn.MultiheadAttention(d_model, num_heads, dropout=0.1),
805
+ nn.MultiheadAttention(d_model, num_heads, dropout=0.1)
806
+ ])
807
+ self.linear3 = nn.Linear(d_model, d_model * 2)
808
+ self.linear4 = nn.Linear(d_model * 2, d_model)
809
+ self.norm1 = nn.LayerNorm(d_model)
810
+ self.norm2 = nn.LayerNorm(d_model)
811
+ self.dropout = nn.Dropout(0.1)
812
+ self.dropout1 = nn.Dropout(0.1)
813
+ self.dropout2 = nn.Dropout(0.1)
814
+ self.sigmoid = nn.Sigmoid()
815
+ self.activation = get_activation_fn('gelu')
816
+ self.sal_conv = nn.Conv2d(d_model, 1, 1)
817
+ self.pool_ratios = pool_ratios
818
+
819
+ def forward(self, x):
820
+ device = x.device
821
+ b, c, h, w = x.size()
822
+ loc, glb = x.split([4, 1], dim=0) # 4,c,h,w; 1,c,h,w
823
+
824
+ patched_glb = rearrange(glb, 'b c (hg h) (wg w) -> (hg wg b) c h w', hg=2, wg=2)
825
+
826
+ token_attention_map = self.sigmoid(self.sal_conv(glb))
827
+ token_attention_map = F.interpolate(token_attention_map, size=patches2image(loc).shape[-2:], mode='nearest')
828
+ loc = loc * rearrange(token_attention_map, 'b c (hg h) (wg w) -> (hg wg b) c h w', hg=2, wg=2)
829
+
830
+ pools = []
831
+ for pool_ratio in self.pool_ratios:
832
+ tgt_hw = (round(h / pool_ratio), round(w / pool_ratio))
833
+ pool = F.adaptive_avg_pool2d(patched_glb, tgt_hw)
834
+ pools.append(rearrange(pool, 'nl c h w -> nl c (h w)')) # nl(4),c,hw
835
+
836
+ pools = rearrange(torch.cat(pools, 2), "nl c nphw -> nl nphw 1 c")
837
+ loc_ = rearrange(loc, 'nl c h w -> nl (h w) 1 c')
838
+
839
+ outputs = []
840
+ for i, q in enumerate(loc_.unbind(dim=0)): # traverse all local patches
841
+ v = pools[i]
842
+ k = v
843
+ outputs.append(self.attention[i](q, k, v)[0])
844
+
845
+ outputs = torch.cat(outputs, 1)
846
+ src = loc.view(4, c, -1).permute(2, 0, 1) + self.dropout1(outputs)
847
+ src = self.norm1(src)
848
+ src = src + self.dropout2(self.linear4(self.dropout(self.activation(self.linear3(src)).clone())))
849
+ src = self.norm2(src)
850
+ src = src.permute(1, 2, 0).reshape(4, c, h, w) # freshed loc
851
+ glb = glb + F.interpolate(patches2image(src), size=glb.shape[-2:], mode='nearest') # freshed glb
852
+
853
+ return torch.cat((src, glb), 0), token_attention_map
854
+
855
+
856
+ class BEN_Base(nn.Module):
857
+ def __init__(self):
858
+ super().__init__()
859
+
860
+ self.backbone = SwinTransformer(embed_dim=128, depths=[2, 2, 18, 2], num_heads=[4, 8, 16, 32], window_size=12)
861
+ emb_dim = 128
862
+ self.sideout5 = nn.Sequential(nn.Conv2d(emb_dim, 1, kernel_size=3, padding=1))
863
+ self.sideout4 = nn.Sequential(nn.Conv2d(emb_dim, 1, kernel_size=3, padding=1))
864
+ self.sideout3 = nn.Sequential(nn.Conv2d(emb_dim, 1, kernel_size=3, padding=1))
865
+ self.sideout2 = nn.Sequential(nn.Conv2d(emb_dim, 1, kernel_size=3, padding=1))
866
+ self.sideout1 = nn.Sequential(nn.Conv2d(emb_dim, 1, kernel_size=3, padding=1))
867
+
868
+ self.output5 = make_cbr(1024, emb_dim)
869
+ self.output4 = make_cbr(512, emb_dim)
870
+ self.output3 = make_cbr(256, emb_dim)
871
+ self.output2 = make_cbr(128, emb_dim)
872
+ self.output1 = make_cbr(128, emb_dim)
873
+
874
+ self.multifieldcrossatt = MCLM(emb_dim, 1, [1, 4, 8])
875
+ self.conv1 = make_cbr(emb_dim, emb_dim)
876
+ self.conv2 = make_cbr(emb_dim, emb_dim)
877
+ self.conv3 = make_cbr(emb_dim, emb_dim)
878
+ self.conv4 = make_cbr(emb_dim, emb_dim)
879
+ self.dec_blk1 = MCRM(emb_dim, 1, [2, 4, 8])
880
+ self.dec_blk2 = MCRM(emb_dim, 1, [2, 4, 8])
881
+ self.dec_blk3 = MCRM(emb_dim, 1, [2, 4, 8])
882
+ self.dec_blk4 = MCRM(emb_dim, 1, [2, 4, 8])
883
+
884
+ self.insmask_head = nn.Sequential(
885
+ nn.Conv2d(emb_dim, 384, kernel_size=3, padding=1),
886
+ nn.InstanceNorm2d(384),
887
+ nn.GELU(),
888
+ nn.Conv2d(384, 384, kernel_size=3, padding=1),
889
+ nn.InstanceNorm2d(384),
890
+ nn.GELU(),
891
+ nn.Conv2d(384, emb_dim, kernel_size=3, padding=1)
892
+ )
893
+
894
+ self.shallow = nn.Sequential(nn.Conv2d(3, emb_dim, kernel_size=3, padding=1))
895
+ self.upsample1 = make_cbg(emb_dim, emb_dim)
896
+ self.upsample2 = make_cbg(emb_dim, emb_dim)
897
+ self.output = nn.Sequential(nn.Conv2d(emb_dim, 1, kernel_size=3, padding=1))
898
+
899
+ for m in self.modules():
900
+ if isinstance(m, nn.GELU) or isinstance(m, nn.Dropout):
901
+ m.inplace = True
902
+
903
+ @torch.inference_mode()
904
+ @torch.autocast(device_type="cuda", dtype=torch.float16)
905
+ def forward(self, x):
906
+ real_batch = x.size(0)
907
+
908
+ shallow_batch = self.shallow(x)
909
+ glb_batch = rescale_to(x, scale_factor=0.5, interpolation='bilinear')
910
+
911
+ final_input = None
912
+ for i in range(real_batch):
913
+ start = i * 4
914
+ end = (i + 1) * 4
915
+ loc_batch = image2patches(x[i, :, :, :].unsqueeze(dim=0))
916
+ input_ = torch.cat((loc_batch, glb_batch[i, :, :, :].unsqueeze(dim=0)), dim=0)
917
+
918
+ if final_input == None:
919
+ final_input = input_
920
+ else:
921
+ final_input = torch.cat((final_input, input_), dim=0)
922
+
923
+ features = self.backbone(final_input)
924
+ outputs = []
925
+
926
+ for i in range(real_batch):
927
+ start = i * 5
928
+ end = (i + 1) * 5
929
+
930
+ f4 = features[4][start:end, :, :, :] # shape: [5, C, H, W]
931
+ f3 = features[3][start:end, :, :, :]
932
+ f2 = features[2][start:end, :, :, :]
933
+ f1 = features[1][start:end, :, :, :]
934
+ f0 = features[0][start:end, :, :, :]
935
+ e5 = self.output5(f4)
936
+ e4 = self.output4(f3)
937
+ e3 = self.output3(f2)
938
+ e2 = self.output2(f1)
939
+ e1 = self.output1(f0)
940
+ loc_e5, glb_e5 = e5.split([4, 1], dim=0)
941
+ e5 = self.multifieldcrossatt(loc_e5, glb_e5) # (4,128,16,16)
942
+
943
+ e4, tokenattmap4 = self.dec_blk4(e4 + resize_as(e5, e4))
944
+ e4 = self.conv4(e4)
945
+ e3, tokenattmap3 = self.dec_blk3(e3 + resize_as(e4, e3))
946
+ e3 = self.conv3(e3)
947
+ e2, tokenattmap2 = self.dec_blk2(e2 + resize_as(e3, e2))
948
+ e2 = self.conv2(e2)
949
+ e1, tokenattmap1 = self.dec_blk1(e1 + resize_as(e2, e1))
950
+ e1 = self.conv1(e1)
951
+
952
+ loc_e1, glb_e1 = e1.split([4, 1], dim=0)
953
+
954
+ output1_cat = patches2image(loc_e1) # (1,128,256,256)
955
+
956
+ # add glb feat in
957
+ output1_cat = output1_cat + resize_as(glb_e1, output1_cat)
958
+ # merge
959
+ final_output = self.insmask_head(output1_cat) # (1,128,256,256)
960
+ # shallow feature merge
961
+ shallow = shallow_batch[i, :, :, :].unsqueeze(dim=0)
962
+ final_output = final_output + resize_as(shallow, final_output)
963
+ final_output = self.upsample1(rescale_to(final_output))
964
+ final_output = rescale_to(final_output + resize_as(shallow, final_output))
965
+ final_output = self.upsample2(final_output)
966
+ final_output = self.output(final_output)
967
+ mask = final_output.sigmoid()
968
+ outputs.append(mask)
969
+
970
+ return torch.cat(outputs, dim=0)
971
+
972
+ def loadcheckpoints(self, model_path):
973
+ model_dict = torch.load(model_path, map_location="cpu", weights_only=True)
974
+ self.load_state_dict(model_dict['model_state_dict'], strict=True)
975
+ del model_path
976
+
977
+ def inference(self, image, refine_foreground=False):
978
+
979
+ # set_random_seed(9)
980
+ # image = ImageOps.exif_transpose(image)
981
+ if isinstance(image, Image.Image):
982
+ image, h, w, original_image = rgb_loader_refiner(image)
983
+ if torch.cuda.is_available():
984
+
985
+ img_tensor = img_transform(image).unsqueeze(0).to(next(self.parameters()).device)
986
+ else:
987
+ img_tensor = img_transform32(image).unsqueeze(0).to(next(self.parameters()).device)
988
+
989
+ with torch.no_grad():
990
+ res = self.forward(img_tensor)
991
+
992
+ # Show Results
993
+ if refine_foreground == True:
994
+
995
+ pred_pil = transforms.ToPILImage()(res.squeeze())
996
+ image_masked = refine_foreground_process(original_image, pred_pil)
997
+
998
+ image_masked.putalpha(pred_pil.resize(original_image.size))
999
+ return image_masked
1000
+
1001
+ else:
1002
+ alpha = postprocess_image(res, im_size=[w, h])
1003
+ pred_pil = transforms.ToPILImage()(alpha)
1004
+ mask = pred_pil.resize(original_image.size)
1005
+ original_image.putalpha(mask)
1006
+ # mask = Image.fromarray(alpha)
1007
+
1008
+ # 将背景置为白色
1009
+ white_background = Image.new('RGB', original_image.size, (255, 255, 255))
1010
+ white_background.paste(original_image, mask=original_image.split()[3])
1011
+ original_image = white_background
1012
+
1013
+ return original_image
1014
+
1015
+
1016
+ else:
1017
+ foregrounds = []
1018
+ for batch in image:
1019
+ image, h, w, original_image = rgb_loader_refiner(batch)
1020
+ if torch.cuda.is_available():
1021
+
1022
+ img_tensor = img_transform(image).unsqueeze(0).to(next(self.parameters()).device)
1023
+ else:
1024
+ img_tensor = img_transform32(image).unsqueeze(0).to(next(self.parameters()).device)
1025
+
1026
+ with torch.no_grad():
1027
+ res = self.forward(img_tensor)
1028
+
1029
+ if refine_foreground == True:
1030
+
1031
+ pred_pil = transforms.ToPILImage()(res.squeeze())
1032
+ image_masked = refine_foreground_process(original_image, pred_pil)
1033
+
1034
+ image_masked.putalpha(pred_pil.resize(original_image.size))
1035
+
1036
+ foregrounds.append(image_masked)
1037
+ else:
1038
+ alpha = postprocess_image(res, im_size=[w, h])
1039
+ pred_pil = transforms.ToPILImage()(alpha)
1040
+ mask = pred_pil.resize(original_image.size)
1041
+ original_image.putalpha(mask)
1042
+ # mask = Image.fromarray(alpha)
1043
+ foregrounds.append(original_image)
1044
+
1045
+ return foregrounds
1046
+
1047
+ def segment_video(self, video_path, output_path="./", fps=0, refine_foreground=False, batch=1,
1048
+ print_frames_processed=True, webm=False, rgb_value=(0, 255, 0)):
1049
+
1050
+ """
1051
+ Segments the given video to extract the foreground (with alpha) from each frame
1052
+ and saves the result as either a WebM video (with alpha channel) or MP4 (with a
1053
+ color background).
1054
+
1055
+ Args:
1056
+ video_path (str):
1057
+ Path to the input video file.
1058
+
1059
+ output_path (str, optional):
1060
+ Directory (or full path) where the output video and/or files will be saved.
1061
+ Defaults to "./".
1062
+
1063
+ fps (int, optional):
1064
+ The frames per second (FPS) to use for the output video. If 0 (default), the
1065
+ original FPS of the input video is used. Otherwise, overrides it.
1066
+
1067
+ refine_foreground (bool, optional):
1068
+ Whether to run an additional “refine foreground” process on each frame.
1069
+ Defaults to False.
1070
+
1071
+ batch (int, optional):
1072
+ Number of frames to process at once (inference batch size). Large batch sizes
1073
+ may require more GPU memory. Defaults to 1.
1074
+
1075
+ print_frames_processed (bool, optional):
1076
+ If True (default), prints progress (how many frames have been processed) to
1077
+ the console.
1078
+
1079
+ webm (bool, optional):
1080
+ If True (default), exports a WebM video with alpha channel (VP9 / yuva420p).
1081
+ If False, exports an MP4 video composited over a solid color background.
1082
+
1083
+ rgb_value (tuple, optional):
1084
+ The RGB background color (e.g., green screen) used to composite frames when
1085
+ saving to MP4. Defaults to (0, 255, 0).
1086
+
1087
+ Returns:
1088
+ None. Writes the output video(s) to disk in the specified format.
1089
+ """
1090
+
1091
+ cap = cv2.VideoCapture(video_path)
1092
+ if not cap.isOpened():
1093
+ raise IOError(f"Cannot open video: {video_path}")
1094
+
1095
+ original_fps = cap.get(cv2.CAP_PROP_FPS)
1096
+ original_fps = 30 if original_fps == 0 else original_fps
1097
+ fps = original_fps if fps == 0 else fps
1098
+
1099
+ ret, first_frame = cap.read()
1100
+ if not ret:
1101
+ raise ValueError("No frames found in the video.")
1102
+ height, width = first_frame.shape[:2]
1103
+ cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
1104
+
1105
+ foregrounds = []
1106
+ frame_idx = 0
1107
+ processed_count = 0
1108
+ batch_frames = []
1109
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
1110
+
1111
+ while True:
1112
+ ret, frame = cap.read()
1113
+ if not ret:
1114
+ if batch_frames:
1115
+ batch_results = self.inference(batch_frames, refine_foreground)
1116
+ if isinstance(batch_results, Image.Image):
1117
+ foregrounds.append(batch_results)
1118
+ else:
1119
+ foregrounds.extend(batch_results)
1120
+ if print_frames_processed:
1121
+ print(f"Processed frames {frame_idx - len(batch_frames) + 1} to {frame_idx} of {total_frames}")
1122
+ break
1123
+
1124
+ # Process every frame instead of using intervals
1125
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
1126
+ pil_frame = Image.fromarray(frame_rgb)
1127
+ batch_frames.append(pil_frame)
1128
+
1129
+ if len(batch_frames) == batch:
1130
+ batch_results = self.inference(batch_frames, refine_foreground)
1131
+ if isinstance(batch_results, Image.Image):
1132
+ foregrounds.append(batch_results)
1133
+ else:
1134
+ foregrounds.extend(batch_results)
1135
+ if print_frames_processed:
1136
+ print(f"Processed frames {frame_idx - batch + 1} to {frame_idx} of {total_frames}")
1137
+ batch_frames = []
1138
+ processed_count += batch
1139
+
1140
+ frame_idx += 1
1141
+
1142
+ if webm:
1143
+ alpha_webm_path = os.path.join(output_path, "foreground.webm")
1144
+ pil_images_to_webm_alpha(foregrounds, alpha_webm_path, fps=original_fps)
1145
+
1146
+ else:
1147
+ cap.release()
1148
+ fg_output = os.path.join(output_path, 'foreground.mp4')
1149
+
1150
+ pil_images_to_mp4(foregrounds, fg_output, fps=original_fps, rgb_value=rgb_value)
1151
+ cv2.destroyAllWindows()
1152
+
1153
+ try:
1154
+ fg_audio_output = os.path.join(output_path, 'foreground_output_with_audio.mp4')
1155
+ add_audio_to_video(fg_output, video_path, fg_audio_output)
1156
+ except Exception as e:
1157
+ print("No audio found in the original video")
1158
+ print(e)
1159
+
1160
+
1161
+ def rgb_loader_refiner(original_image):
1162
+ h, w = original_image.size
1163
+
1164
+ image = original_image
1165
+ # Convert to RGB if necessary
1166
+ if image.mode != 'RGB':
1167
+ image = image.convert('RGB')
1168
+
1169
+ # Resize the image
1170
+ image = image.resize((1024, 1024), resample=Image.LANCZOS)
1171
+
1172
+ return image.convert('RGB'), h, w, original_image
1173
+
1174
+
1175
+ # Define the image transformation
1176
+ img_transform = transforms.Compose([
1177
+ transforms.ToTensor(),
1178
+ transforms.ConvertImageDtype(torch.float16),
1179
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
1180
+ ])
1181
+
1182
+ img_transform32 = transforms.Compose([
1183
+ transforms.ToTensor(),
1184
+ transforms.ConvertImageDtype(torch.float32),
1185
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
1186
+ ])
1187
+
1188
+
1189
+ def pil_images_to_mp4(images, output_path, fps=24, rgb_value=(0, 255, 0)):
1190
+ """
1191
+ Converts an array of PIL images to an MP4 video.
1192
+
1193
+ Args:
1194
+ images: List of PIL images
1195
+ output_path: Path to save the MP4 file
1196
+ fps: Frames per second (default: 24)
1197
+ rgb_value: Background RGB color tuple (default: green (0, 255, 0))
1198
+ """
1199
+ if not images:
1200
+ raise ValueError("No images provided to convert to MP4.")
1201
+
1202
+ width, height = images[0].size
1203
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
1204
+ video_writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
1205
+
1206
+ for image in images:
1207
+ # If image has alpha channel, composite onto the specified background color
1208
+ if image.mode == 'RGBA':
1209
+ # Create background image with specified RGB color
1210
+ background = Image.new('RGB', image.size, rgb_value)
1211
+ background = background.convert('RGBA')
1212
+ # Composite the image onto the background
1213
+ image = Image.alpha_composite(background, image)
1214
+ image = image.convert('RGB')
1215
+ else:
1216
+ # Ensure RGB format for non-alpha images
1217
+ image = image.convert('RGB')
1218
+
1219
+ # Convert to OpenCV format and write
1220
+ open_cv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
1221
+ video_writer.write(open_cv_image)
1222
+
1223
+ video_writer.release()
1224
+
1225
+
1226
+ def pil_images_to_webm_alpha(images, output_path, fps=30):
1227
+ """
1228
+ Converts a list of PIL RGBA images to a VP9 .webm video with alpha channel.
1229
+
1230
+ NOTE: Not all players will display alpha in WebM.
1231
+ Browsers like Chrome/Firefox typically do support VP9 alpha.
1232
+ """
1233
+ if not images:
1234
+ raise ValueError("No images provided for WebM with alpha.")
1235
+
1236
+ # Ensure output directory exists
1237
+ os.makedirs(os.path.dirname(output_path), exist_ok=True)
1238
+
1239
+ with tempfile.TemporaryDirectory() as tmpdir:
1240
+ # Save frames as PNG (with alpha)
1241
+ for idx, img in enumerate(images):
1242
+ if img.mode != "RGBA":
1243
+ img = img.convert("RGBA")
1244
+ out_path = os.path.join(tmpdir, f"{idx:06d}.png")
1245
+ img.save(out_path, "PNG")
1246
+
1247
+ # Construct ffmpeg command
1248
+ # -c:v libvpx-vp9 => VP9 encoder
1249
+ # -pix_fmt yuva420p => alpha-enabled pixel format
1250
+ # -auto-alt-ref 0 => helps preserve alpha frames (libvpx quirk)
1251
+ ffmpeg_cmd = [
1252
+ "ffmpeg", "-y",
1253
+ "-framerate", str(fps),
1254
+ "-i", os.path.join(tmpdir, "%06d.png"),
1255
+ "-c:v", "libvpx-vp9",
1256
+ "-pix_fmt", "yuva420p",
1257
+ "-auto-alt-ref", "0",
1258
+ output_path
1259
+ ]
1260
+
1261
+ subprocess.run(ffmpeg_cmd, check=True)
1262
+
1263
+ print(f"WebM with alpha saved to {output_path}")
1264
+
1265
+
1266
+ def add_audio_to_video(video_without_audio_path, original_video_path, output_path):
1267
+ """
1268
+ Check if the original video has an audio stream. If yes, add it. If not, skip.
1269
+ """
1270
+ # 1) Probe original video for audio streams
1271
+ probe_command = [
1272
+ 'ffprobe', '-v', 'error',
1273
+ '-select_streams', 'a:0',
1274
+ '-show_entries', 'stream=index',
1275
+ '-of', 'csv=p=0',
1276
+ original_video_path
1277
+ ]
1278
+ result = subprocess.run(probe_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
1279
+
1280
+ # result.stdout is empty if no audio stream found
1281
+ if not result.stdout.strip():
1282
+ print("No audio track found in original video, skipping audio addition.")
1283
+ return
1284
+
1285
+ print("Audio track detected; proceeding to mux audio.")
1286
+ # 2) If audio found, run ffmpeg to add it
1287
+ command = [
1288
+ 'ffmpeg', '-y',
1289
+ '-i', video_without_audio_path,
1290
+ '-i', original_video_path,
1291
+ '-c', 'copy',
1292
+ '-map', '0:v:0',
1293
+ '-map', '1:a:0', # we know there's an audio track now
1294
+ output_path
1295
+ ]
1296
+ subprocess.run(command, check=True)
1297
+ print(f"Audio added successfully => {output_path}")
1298
+
1299
+
1300
+ ### Thanks to the source: https://huggingface.co/ZhengPeng7/BiRefNet/blob/main/handler.py
1301
+ def refine_foreground_process(image, mask, r=90):
1302
+ if mask.size != image.size:
1303
+ mask = mask.resize(image.size)
1304
+ image = np.array(image) / 255.0
1305
+ mask = np.array(mask) / 255.0
1306
+ estimated_foreground = FB_blur_fusion_foreground_estimator_2(image, mask, r=r)
1307
+ image_masked = Image.fromarray((estimated_foreground * 255.0).astype(np.uint8))
1308
+ return image_masked
1309
+
1310
+
1311
+ def FB_blur_fusion_foreground_estimator_2(image, alpha, r=90):
1312
+ # Thanks to the source: https://github.com/Photoroom/fast-foreground-estimation
1313
+ alpha = alpha[:, :, None]
1314
+ F, blur_B = FB_blur_fusion_foreground_estimator(image, image, image, alpha, r)
1315
+ return FB_blur_fusion_foreground_estimator(image, F, blur_B, alpha, r=6)[0]
1316
+
1317
+
1318
+ def FB_blur_fusion_foreground_estimator(image, F, B, alpha, r=90):
1319
+ if isinstance(image, Image.Image):
1320
+ image = np.array(image) / 255.0
1321
+ blurred_alpha = cv2.blur(alpha, (r, r))[:, :, None]
1322
+
1323
+ blurred_FA = cv2.blur(F * alpha, (r, r))
1324
+ blurred_F = blurred_FA / (blurred_alpha + 1e-5)
1325
+
1326
+ blurred_B1A = cv2.blur(B * (1 - alpha), (r, r))
1327
+ blurred_B = blurred_B1A / ((1 - blurred_alpha) + 1e-5)
1328
+ F = blurred_F + alpha * \
1329
+ (image - alpha * blurred_F - (1 - alpha) * blurred_B)
1330
+ F = np.clip(F, 0, 1)
1331
+ return F, blurred_B
1332
+
1333
+
1334
+ def postprocess_image(result: torch.Tensor, im_size: list) -> np.ndarray:
1335
+ result = torch.squeeze(F.interpolate(result, size=im_size, mode='bilinear'), 0)
1336
+ ma = torch.max(result)
1337
+ mi = torch.min(result)
1338
+ result = (result - mi) / (ma - mi)
1339
+ im_array = (result * 255).permute(1, 2, 0).cpu().data.numpy().astype(np.uint8)
1340
+ im_array = np.squeeze(im_array)
1341
+ return im_array
1342
+
1343
+
1344
+ def rgb_loader_refiner(original_image):
1345
+ h, w = original_image.size
1346
+ # # Apply EXIF orientation
1347
+
1348
+ image = ImageOps.exif_transpose(original_image)
1349
+
1350
+ if original_image.mode != 'RGB':
1351
+ original_image = original_image.convert('RGB')
1352
+
1353
+ image = original_image
1354
+ # Convert to RGB if necessary
1355
+
1356
+ # Resize the image
1357
+ image = image.resize((1024, 1024), resample=Image.LANCZOS)
1358
+
1359
+ return image, h, w, original_image