def forward(self, x):
b, c, h, w = x.shape
qkv = self.qkv2(self.qkv1(x))
q, k, v = qkv.chunk(3, dim=1)
q = rearrange(q, "b (head c) h w -> b head c (h w)", head=self.num_heads)
k = rearrange(k, "b (head c) h w -> b head c (h w)", head=self.num_heads)
v = rearrange(v, "b (head c) h w -> b head c (h w)", head=self.num_heads)
q = torch.nn.functional.normalize(q, dim=-1)
k = torch.nn.functional.normalize(k, dim=-1)
attn = (q @ k.transpose(-2, -1)) * self.scale
attn = attn.softmax(dim=-1)
out = attn @ v
out = rearrange(
out, "b head c (h w) -> b (head c) h w", head=self.num_heads, h=h, w=w
)
out = self.proj(out)
return out
def forward(self, feature_I_B, feature_V_B):
x = torch.cat((feature_I_B, feature_V_B), dim=1) # [B, 2*DIM, H, W]
x = x + self.attn(self.norm1(x))
x = x + self.mlp(self.norm2(x))
x = self.reduce_dim(x)
return x
self.qkv = nn.Conv2d(dim, dim * 3, kernel_size=1, bias=bias)
self.qkv_dwconv = nn.Conv2d(
dim * 3,
dim * 3,
kernel_size=3,
stride=1,
padding=1,
groups=dim * 3,
bias=bias,
)
self.project_out = nn.Conv2d(dim, dim, kernel_size=1, bias=bias)
def forward(self, x):
b, c, h, w = x.shape
qkv = self.qkv_dwconv(self.qkv(x))
q, k, v = qkv.chunk(3, dim=1)
q = rearrange(q, "b (head c) h w -> b head c (h w)", head=self.num_heads)
k = rearrange(k, "b (head c) h w -> b head c (h w)", head=self.num_heads)
v = rearrange(v, "b (head c) h w -> b head c (h w)", head=self.num_heads)
q = torch.nn.functional.normalize(q, dim=-1)
k = torch.nn.functional.normalize(k, dim=-1)
attn = (q @ k.transpose(-2, -1)) * self.temperature
attn = attn.softmax(dim=-1)
out = attn @ v
out = rearrange(
out, "b head c (h w) -> b (head c) h w", head=self.num_heads, h=h, w=w
)
out = self.project_out(out)
return out
if name == "main":
height = 128
width = 128
window_size = 8
modelE = Restormer_Encoder().cuda()
modelD = Restormer_Decoder().cuda(),整个网络架构
The code you provided defines a neural network architecture composed of several components, including attention mechanisms, multi-layer perceptrons (MLPs), and a combination of encoder and decoder modules. Let's break it down:
Main Components
AttentionBase:
This class implements a basic attention mechanism with multi-head attention.
It splits input features into queries, keys, and values, applies attention, and projects the output back to the original dimension.
It's equipped with a scaling parameter and normalizes the query and key vectors.
Mlp:
Implements a basic MLP with a depth-wise convolution for richer feature extraction.
Used after normalization layers in the transformers for feed-forward operations.
BaseFeatureExtractor:
A layer consisting of alternating attention and MLP blocks.
It applies LayerNorm before the attention and MLP blocks.
Extracts base features from the input.
AttFuseLayer:
Similar to BaseFeatureExtractor but processes concatenated features from two sources.
Uses attention and MLP blocks and reduces the feature dimensions back to the desired output dimension.
DetailNode and DetailFeatureExtractor:
DetailNode applies a specific transformation to input features using inverted residual blocks.
DetailFeatureExtractor stacks multiple DetailNodes to process and enhance detail features.
LayerNorm Variants:
Offers two types of layer normalization: with bias and bias-free.
LayerNorm class chooses between the two based on a configuration.
TransformerBlock:
Implements a standard Transformer block with attention and feed-forward layers interleaved with normalization.
Utilized within both encoder and decoder architectures.
OverlapPatchEmbed:
Provides a convolutional layer for patch embedding, which improves local feature extraction.
Network Architecture
Restormer_Encoder:
This is a transformer-based encoder that embeds input images, applies transformer blocks, and extracts both base and detail features.
It uses BaseFeatureExtractor and DetailFeatureExtractor for this purpose.
Restormer_Decoder:
Complements the encoder by recombining the base and detail features, reducing dimensions, processing through transformer blocks, and projecting the result back to the image domain.
Uses concatenation and channel reduction followed by transformer processing and final image reconstruction.
Main Execution
The script concludes with a test harness for both encoder and decoder modules. The models (Restormer_Encoder and Restormer_Decoder) are instantiated and moved to a CUDA device for execution on compatible hardware.
Overall Purpose
This neural network architecture likely serves a vision task, such as image denoising, super-resolution, or another form of image restoration. It splits feature extraction into base and detail components, leveraging self-attention for effective feature processing and refines this in the decoder to ideally produce a high-quality output image.
The usage of transformers and MLPs along with residual connections hints at its design for tasks needing high capacity and attention-based processing, a hallmark of contemporary deep learning approaches in vision.