Spaces:
Running
on
Zero
Running
on
Zero
File size: 21,693 Bytes
c295391 |
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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
from typing import Tuple
import torch
import torch
import torch.nn as nn
import torch.nn.functional as F
from ..utils.model_utils import *
from ..utils import transformer
from ..utils.ind2sub import *
from ..utils.decompose_tensors import *
from ..utils.utils import *
from einops import rearrange
from ..aggregator import Aggregator
import pywt
def create_wavelet_filter(wave, in_size, out_size, type=torch.float):
w = pywt.Wavelet(wave)
dec_hi = torch.tensor(w.dec_hi[::-1], dtype=type)
dec_lo = torch.tensor(w.dec_lo[::-1], dtype=type)
dec_filters = torch.stack([dec_lo.unsqueeze(0) * dec_lo.unsqueeze(1),
dec_lo.unsqueeze(0) * dec_hi.unsqueeze(1),
dec_hi.unsqueeze(0) * dec_lo.unsqueeze(1),
dec_hi.unsqueeze(0) * dec_hi.unsqueeze(1)], dim=0)
dec_filters = dec_filters[:, None].repeat(in_size, 1, 1, 1)
rec_hi = torch.tensor(w.rec_hi[::-1], dtype=type).flip(dims=[0])
rec_lo = torch.tensor(w.rec_lo[::-1], dtype=type).flip(dims=[0])
rec_filters = torch.stack([rec_lo.unsqueeze(0) * rec_lo.unsqueeze(1),
rec_lo.unsqueeze(0) * rec_hi.unsqueeze(1),
rec_hi.unsqueeze(0) * rec_lo.unsqueeze(1),
rec_hi.unsqueeze(0) * rec_hi.unsqueeze(1)], dim=0)
rec_filters = rec_filters[:, None].repeat(out_size, 1, 1, 1)
return dec_filters, rec_filters
def wavelet_transform(x, filters):
b, c, h, w = x.shape
pad = (filters.shape[2] // 2 - 1, filters.shape[3] // 2 - 1)
x = F.conv2d(x, filters, stride=2, groups=c, padding=pad)
x = x.reshape(b, c, 4, h // 2, w // 2)
return x
def inverse_wavelet_transform(x, filters):
b, c, _, h_half, w_half = x.shape
pad = (filters.shape[2] // 2 - 1, filters.shape[3] // 2 - 1)
x = x.reshape(b, c * 4, h_half, w_half)
x = F.conv_transpose2d(x, filters, stride=2, groups=c, padding=pad)
return x
class ResidualConvUnit(nn.Module):
"""Residual convolution module."""
def __init__(self, features, activation, bn, groups=1):
"""Init.
Args:
features (int): number of features
"""
super().__init__()
self.bn = bn
self.groups = groups
self.conv1 = nn.Conv2d(features, features, kernel_size=3, stride=1, padding=1, bias=True, groups=self.groups)
self.conv2 = nn.Conv2d(features, features, kernel_size=3, stride=1, padding=1, bias=True, groups=self.groups)
self.norm1 = None
self.norm2 = None
self.activation = activation
self.skip_add = nn.quantized.FloatFunctional()
def forward(self, x):
"""Forward pass.
Args:
x (tensor): input
Returns:
tensor: output
"""
out = self.activation(x)
out = self.conv1(out)
if self.norm1 is not None:
out = self.norm1(out)
out = self.activation(out)
out = self.conv2(out)
if self.norm2 is not None:
out = self.norm2(out)
return self.skip_add.add(out, x)
class FeatureFusionBlock(nn.Module):
"""Feature fusion block."""
def __init__(
self,
features,
activation,
deconv=False,
bn=False,
expand=False,
align_corners=True,
size=None,
has_residual=True,
groups=1,
):
"""Init.
Args:
features (int): number of features
"""
super(FeatureFusionBlock, self).__init__()
self.deconv = deconv
self.align_corners = align_corners
self.groups = groups
self.expand = expand
out_features = features
if self.expand == True:
out_features = features // 2
self.out_conv = nn.Conv2d(
features, out_features, kernel_size=1, stride=1, padding=0, bias=True, groups=self.groups
)
if has_residual:
self.resConfUnit1 = ResidualConvUnit(features, activation, bn, groups=self.groups)
self.has_residual = has_residual
self.resConfUnit2 = ResidualConvUnit(features, activation, bn, groups=self.groups)
self.skip_add = nn.quantized.FloatFunctional()
self.size = size
def forward(self, *xs, size=None):
"""Forward pass.
Returns:
tensor: output
"""
output = xs[0]
if self.has_residual:
res = self.resConfUnit1(xs[1])
output = self.skip_add.add(output, res)
output = self.resConfUnit2(output)
if (size is None) and (self.size is None):
modifier = {"scale_factor": 2}
elif size is None:
modifier = {"size": self.size}
else:
modifier = {"size": size}
output = custom_interpolate(output.float(), **modifier, mode="bilinear", align_corners=self.align_corners).to(torch.bfloat16)
output = self.out_conv(output)
return output
def custom_interpolate(
x: torch.Tensor,
size: Tuple[int, int] = None,
scale_factor: float = None,
mode: str = "bilinear",
align_corners: bool = True,
) -> torch.Tensor:
"""
Custom interpolate to avoid INT_MAX issues in nn.functional.interpolate.
"""
if size is None:
size = (int(x.shape[-2] * scale_factor), int(x.shape[-1] * scale_factor))
INT_MAX = 1610612736
input_elements = size[0] * size[1] * x.shape[0] * x.shape[1]
if input_elements > INT_MAX:
chunks = torch.chunk(x, chunks=(input_elements // INT_MAX) + 1, dim=0)
interpolated_chunks = [
nn.functional.interpolate(chunk, size=size, mode=mode, align_corners=align_corners) for chunk in chunks
]
x = torch.cat(interpolated_chunks, dim=0)
return x.contiguous()
else:
return nn.functional.interpolate(x, size=size, mode=mode, align_corners=align_corners)
def _make_scratch(in_shape, out_shape: int, groups: int = 1, expand: bool = False) -> nn.Module:
"""
"""
scratch = nn.Module()
activation_function = nn.LeakyReLU
out_shape1 = out_shape
out_shape2 = out_shape
out_shape3 = out_shape
if len(in_shape) >= 4:
out_shape4 = out_shape
if expand:
out_shape1 = out_shape
out_shape2 = out_shape * 2
out_shape3 = out_shape * 4
if len(in_shape) >= 4:
out_shape4 = out_shape * 8
scratch.layer1_rn = nn.Sequential(
nn.Conv2d(
in_shape[0], out_shape1, kernel_size=3, stride=1, padding=1, bias=False, groups=groups
),
activation_function()
)
scratch.layer2_rn = nn.Sequential(
nn.Conv2d(
in_shape[1], out_shape2, kernel_size=3, stride=1, padding=1, bias=False, groups=groups
),
activation_function()
)
scratch.layer3_rn = nn.Sequential(
nn.Conv2d(
in_shape[2], out_shape3, kernel_size=3, stride=1, padding=1, bias=False, groups=groups
),
activation_function()
)
if len(in_shape) >= 4:
scratch.layer4_rn = nn.Sequential(
nn.Conv2d(
in_shape[3], out_shape4, kernel_size=3, stride=1, padding=1, bias=False, groups=groups
),
activation_function()
)
return scratch
def _make_fusion_block(features: int, size: int = None, has_residual: bool = True, groups: int = 1) -> nn.Module:
return FeatureFusionBlock(
features,
nn.LeakyReLU(inplace=False),
deconv=False,
bn=False,
expand=False,
align_corners=True,
size=size,
has_residual=has_residual,
groups=groups,
)
class ImageFeatureExtractor(nn.Module):
def __init__(self, depth=4,img_size=256, patch_size=8, embed_dim=384):
super(ImageFeatureExtractor, self).__init__()
self.aggregator = Aggregator(img_size, patch_size, embed_dim,depth=depth,patch_embed="dinov2_vits14_reg")
def forward(self, x, nImgArray):
feat_list, normal_patch_start_idx = self.aggregator(x)
return torch.stack(feat_list,dim=0).permute(1,2,0,3,4).flatten(0,1),normal_patch_start_idx
class ImageFeatureFusion(nn.Module):
def __init__(self,
in_channels,
use_efficient_attention=False,
out_channels = [256, 512, 1024, 1024],
features = 256,
):
super(ImageFeatureFusion, self).__init__()
_, self.iwt_filter = create_wavelet_filter('db1', 384, 384, torch.bfloat16)
self.pixel_shuffle = nn.PixelShuffle(2)
self.norm = nn.LayerNorm(in_channels)
self.projects = nn.ModuleList(
[
nn.Sequential(
nn.Conv2d(
in_channels=in_channels // 4,
out_channels=oc,
kernel_size=1,
stride=1,
padding=0,
bias=True
),
nn.LeakyReLU()
)
for oc in out_channels
]
)
self.resize_layers = nn.ModuleList(
[
nn.Sequential(
nn.ConvTranspose2d(
in_channels=out_channels[0], out_channels=out_channels[0], kernel_size=2, stride=2, padding=0
),
nn.LeakyReLU(),
nn.ConvTranspose2d(
in_channels=out_channels[0], out_channels=out_channels[0], kernel_size=2, stride=2, padding=0
),
nn.LeakyReLU()
),
nn.Sequential(
nn.ConvTranspose2d(
in_channels=out_channels[1], out_channels=out_channels[1], kernel_size=2, stride=2, padding=0
),
nn.LeakyReLU()
),
nn.Sequential(
nn.Conv2d(
in_channels=out_channels[2], out_channels=out_channels[2], kernel_size=1, stride=1, padding=0
),
nn.LeakyReLU()
),
nn.Sequential(
nn.Conv2d(
in_channels=out_channels[3], out_channels=out_channels[3], kernel_size=2, stride=2, padding=0
),
nn.LeakyReLU()
)
]
)
self.scratch = _make_scratch(
out_channels,
features,
expand=False,
)
self.scratch.stem_transpose = None
self.scratch.refinenet1 = _make_fusion_block(features)
self.scratch.refinenet2 = _make_fusion_block(features)
self.scratch.refinenet3 = _make_fusion_block(features)
self.scratch.refinenet4 = _make_fusion_block(features, has_residual=False)
head_features_1 = features
self.scratch.output_conv1 = nn.Conv2d(
head_features_1, head_features_1 , kernel_size=3, stride=2, padding=1
)
def _apply_pos_embed(self, x: torch.Tensor, W: int, H: int, ratio: float = 0.1) -> torch.Tensor:
"""
Apply positional embedding to tensor x.
"""
patch_w = x.shape[-1]
patch_h = x.shape[-2]
pos_embed = create_uv_grid(patch_w, patch_h, aspect_ratio=W / H, dtype=x.dtype, device=x.device)
pos_embed = position_grid_to_embed(pos_embed, x.shape[1])
pos_embed = pos_embed * ratio
pos_embed = pos_embed.permute(2, 0, 1)[None].expand(x.shape[0], -1, -1, -1)
return x + pos_embed
def scratch_forward(self, features) -> torch.Tensor:
"""
Forward pass through the fusion blocks.
Args:
features (List[Tensor]): List of feature maps from different layers.
Returns:
Tensor: Fused feature map.
"""
layer_1, layer_2, layer_3, layer_4 = features
layer_1_rn = self.scratch.layer1_rn(layer_1)
layer_2_rn = self.scratch.layer2_rn(layer_2)
layer_3_rn = self.scratch.layer3_rn(layer_3)
layer_4_rn = self.scratch.layer4_rn(layer_4)
out = self.scratch.refinenet4(layer_4_rn, size=layer_3_rn.shape[2:])
del layer_4_rn, layer_4
out = self.scratch.refinenet3(out, layer_3_rn, size=layer_2_rn.shape[2:])
del layer_3_rn, layer_3
out = self.scratch.refinenet2(out, layer_2_rn, size=layer_1_rn.shape[2:])
del layer_2_rn, layer_2
out = self.scratch.refinenet1(out, layer_1_rn)
del layer_1_rn, layer_1
out = self.scratch.output_conv1(out)
return out
def forward(self,
glc: torch.Tensor,
nImgArray: torch.Tensor,
chunk_size: int = 6
) -> torch.Tensor:
B = glc.shape[0]
# 如果不需要分块(总批次大小小于或等于块大小),则直接调用核心实现
if chunk_size is None or chunk_size >= B:
return self._forward_impl(glc, nImgArray)
# 否则,进行分块处理
all_outputs = []
# 以 chunk_size 为步长进行循环
for start_idx in range(0, B, chunk_size):
# 计算当前块的结束索引
end_idx = min(start_idx + chunk_size, B)
# 从大的输入张量中切出当前要处理的小块
glc_chunk = glc[start_idx:end_idx]
# 注意:如果 nImgArray 也与批次相关,也需要进行切片
# nImgArray_chunk = nImgArray[start_idx:end_idx]
# 调用核心实现函数来处理这个小块
chunk_output = self._forward_impl(glc_chunk, nImgArray)
all_outputs.append(chunk_output)
# 将所有小块的处理结果,沿着批次维度(dim=0)重新拼接起来
final_output = torch.cat(all_outputs, dim=0)
return final_output
def _forward_impl(self, glc: torch.Tensor, nImgArray: torch.Tensor) -> torch.Tensor:
"""
这是核心实现方法,处理一个数据块(chunk)。
这里的代码就是您提供的原始 forward 方法的主体。
"""
self.iwt_filter = self.iwt_filter.to(glc.device)
B, layer_num, N, C = glc.shape # 这里的 B 现在是 chunk_size
out = []
for layer in range(layer_num):
x = glc[:, layer, :, :] # [B, N, C]
x = self.norm(x)
x = x.permute(0, 2, 1).reshape((x.shape[0], x.shape[-1], int(N**0.5), int(N**0.5))) # [B,C,sqrt(N),sqrt(N)]
x = self.pixel_shuffle(x) # [B, C, H, W] -> [B, C/4, H*2, W*2]
x = self.projects[layer](x)
x = self._apply_pos_embed(x, 256, 256).to(torch.bfloat16)
x = self.resize_layers[layer](x)
out.append(x)
out = self.scratch_forward(out)
out = self._apply_pos_embed(out, 256, 256).to(torch.bfloat16) # [B, 256, 64, 64]
return out
class ScaleInvariantSpatialLightImageEncoder(nn.Module):
def __init__(self, input_nc, depth=4, use_efficient_attention=False):
super(ScaleInvariantSpatialLightImageEncoder, self).__init__()
out_channels = (96, 192, 384, 768)
self.backbone = ImageFeatureExtractor(depth=depth)
self.fusion = ImageFeatureFusion(in_channels=1536, use_efficient_attention=use_efficient_attention)
self.feat_dim = 256
self.wt_filter, _ = create_wavelet_filter('db1', 3, 3, torch.bfloat16)
_, self.iwt_filter = create_wavelet_filter('db1', self.feat_dim, self.feat_dim, torch.bfloat16)
def forward(self, x, nImgArray, canonical_resolution):
N, C, H, W = x.shape
B = N//nImgArray[0]
mosaic_scale = H // canonical_resolution
K = mosaic_scale * mosaic_scale
self.wt_filter = self.wt_filter.to(x.device)
self.iwt_filter = self.iwt_filter.to(x.device)
""" (1a) resizing x to (Hc, Wc)"""
x_resized = F.interpolate(x.float(), size= (canonical_resolution, canonical_resolution), mode='bilinear', align_corners=True).to(torch.bfloat16)
x_resized = x_resized.view(len(nImgArray), int(nImgArray[0]), C, x_resized.shape[2], x_resized.shape[3])
""" (1b) decomposing x into K x K of (Hc, Wc) non-overlapped blocks (stride)"""
x_wt = wavelet_transform(x, self.wt_filter).permute(0, 2, 1, 3, 4)
x_wt = x_wt.reshape(B,nImgArray[0],K,3,canonical_resolution,canonical_resolution).flatten(1,2).flatten(0,1)
x_wt = x_wt.view(len(nImgArray), K * int(nImgArray[0]), C, x_wt.shape[2], x_wt.shape[3])
""" (2a) feature extraction """
aggregated_tokens_list, patch_start_idx = self.backbone(x_resized,nImgArray)
light_tokens_resized = aggregated_tokens_list[:,:,:patch_start_idx - 4,:]
light_tokens_resized = rearrange(light_tokens_resized,'(B f) layer num c -> B f layer num c',B = B)
x = self.fusion(aggregated_tokens_list[:,:,patch_start_idx:,:], nImgArray)
f_resized_grid = F.interpolate(x.reshape(N, self.feat_dim, canonical_resolution, canonical_resolution).float() , size= (H, W), mode='bilinear', align_corners=True).to(torch.bfloat16)
""" (2b) feature extraction (grid) """
aggregated_tokens_list, patch_start_idx = self.backbone(x_wt,nImgArray)
light_tokens_wt = aggregated_tokens_list[:,:,:patch_start_idx - 4,:]
light_tokens_wt = rearrange(light_tokens_wt,'(B f k) layer num c -> B f k layer num c',B = B, f=nImgArray[0])
light_tokens = torch.cat((light_tokens_resized.unsqueeze(2), light_tokens_wt), dim=2)
x = self.fusion(aggregated_tokens_list[:,:,patch_start_idx:,:], nImgArray)
x = rearrange(x, '(f k) c h w -> f c k h w ',k=4)
x = inverse_wavelet_transform(x, self.iwt_filter)
""" (3) upsample """
glc = (f_resized_grid + x)
return glc,light_tokens
class GLC_Upsample(nn.Module):
def __init__(self, input_nc, num_enc_sab=1, dim_hidden=256, dim_feedforward=1024, use_efficient_attention=False):
super(GLC_Upsample, self).__init__()
self.comm = transformer.CommunicationBlock(input_nc, num_enc_sab = num_enc_sab, dim_hidden=dim_hidden, ln=True, dim_feedforward = dim_feedforward,use_efficient_attention=False)
def forward(self, x):
x = self.comm(x)
return x
class GLC_Aggregation(nn.Module):
def __init__(self, input_nc, num_agg_transformer=2, dim_aggout=384, dim_feedforward=1024, use_efficient_attention=False):
super(GLC_Aggregation, self).__init__()
self.aggregation = transformer.AggregationBlock(dim_input = input_nc, num_enc_sab = num_agg_transformer, num_outputs = 1, dim_hidden=dim_aggout, dim_feedforward = dim_feedforward, num_heads=8, ln=True, attention_dropout=0.1, use_efficient_attention=use_efficient_attention)
def forward(self, x):
x = self.aggregation(x)
return x
class Regressor(nn.Module):
def __init__(self, input_nc, num_enc_sab=1, use_efficient_attention=False, dim_feedforward=256, output='normal'):
super(Regressor, self).__init__()
self.comm = transformer.CommunicationBlock(input_nc, num_enc_sab = num_enc_sab, dim_hidden=input_nc, ln=True, dim_feedforward = dim_feedforward, use_efficient_attention=use_efficient_attention)
if output == 'normal':
self.prediction_normal = PredictionHead(input_nc, 3, confidence=True)
self.target = output
def forward(self, x, num_sample_set):
"""Standard forward
INPUT: img [Num_Pix, F]
OUTPUT: [Num_Pix, 3]"""
if x.shape[0] % num_sample_set == 0:
x_ = x.reshape(-1, num_sample_set, x.shape[1])
x_ = self.comm(x_)
x = x_.reshape(-1, x.shape[1])
else:
ids = list(range(x.shape[0]))
num_split = len(ids) // num_sample_set
x_1 = x[:(num_split)*num_sample_set, :].reshape(-1, num_sample_set, x.shape[1])
x_1 = self.comm(x_1).reshape(-1, x.shape[1])
x_2 = x[(num_split)*num_sample_set:,:].reshape(1, -1, x.shape[1])
x_2 = self.comm(x_2).reshape(-1, x.shape[1])
x = torch.cat([x_1, x_2], dim=0)
if self.target == 'normal':
x_n, conf = self.prediction_normal(x.reshape(x.shape[0]//num_sample_set, num_sample_set, -1))
x_ = []
return x_n, x_, x, conf
class PredictionHead(nn.Module):
def __init__(self, dim_input, dim_output, confidence=False):
super(PredictionHead, self).__init__()
modules_regression = []
modules_regression.append(nn.Linear(dim_input, dim_input//2))
modules_regression.append(nn.ReLU())
self.out_layer = nn.Linear(dim_input//2, dim_output)
if confidence:
self.confi_layer = nn.Linear(dim_input//2, 1)
self.regression = nn.Sequential(*modules_regression)
def forward(self, x):
h = self.regression(x)
ret = self.out_layer(h)
if hasattr(self, 'confi_layer'):
confidence = self.confi_layer(h)
else:
confidence = torch.zeros_like([ret.shape[0], 1])
return ret, torch.sigmoid(confidence)
|