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 provided code represents a neural network architecture built using PyTorch. This architecture is a variant of the Transformer-based network, specifically designed for tasks that may involve image processing or multi-scale feature extraction, although the specific task isn't explicitly mentioned. The network is an integration of several significant components, including attention mechanisms, feedforward networks, patch embeddings, and residual blocks. Here's an analysis of the overall architecture and its components:
Key Components:
Attention Mechanism:
Utilizes the AttentionBase and Attention classes to implement multi-head attention operations. The attention mechanism is crucial for capturing long-range dependencies and relationships between image patches or features.
Layer Normalization:
Implements both bias-free and biased Layer Norm (BiasFree_LayerNorm and WithBias_LayerNorm) to normalize feature maps, which is critical for stable training.
FeedForward Network:
Includes classes Mlp and FeedForward, which use convolutional operations to implement MLP-like transformations, allowing the model to mix and transform features between layers.
Transformer Blocks:
The network utilizes a TransformerBlock that combines attention and feedforward layers with normalization to process and refine feature maps progressively.
Feature Extraction:
The BaseFeatureExtractor and DetailFeatureExtractor extract base and detail features using transformer blocks and INN (Invertible Neural Network) inspired operations, respectively, facilitating multiscale feature representation.
Inverted Residuals:
The InvertedResidualBlock uses depthwise separable convolutions, improving the efficiency of feature extraction by reducing the number of parameters.
Patch Embedding:
The OverlapPatchEmbed layer embeds input images into overlapping patches and projects them into a higher-dimensional feature space, enabling better input representation for the transformer architecture.
Attention Fusion:
The AttFuseLayer combines features from different branches or modalities, possibly integrating information from multiple sources or levels within the network.
Encoder and Decoder:
Restormer_Encoder captures multi-scale features using an encoder module with patch embeddings and transformer blocks.
Restormer_Decoder reconstructs the output by merging base and detail features and applying a series of transformations to generate the final output.
Architecture Usage:
This network architecture is likely designed for tasks such as image restoration, super-resolution, or image generation, given its focus on multi-scale features, attention mechanisms, and non-linear transformations.
It integrates concepts from both CNNs and transformers, potentially benefiting from the strengths of both paradigms.
The encoder-decoder structure, typical in image-to-image translation tasks, suggests that the network transforms input images into a processed version through feature extraction and reconstruction phases.
Overall Workflow:
Input Image: Starts with an image input which is fed into the Restormer_Encoder.
Feature Extraction: Extracts base and detail features through various feature extractor modules.
Feature Fusion and Representation: Merges and processes the features through layers of attention and normalization.
Output Image: Uses the Restormer_Decoder to produce an enhanced or modified output image, likely in tasks involving image-to-image transformations.
In conclusion, this network architecture leverages advanced techniques in computer vision, particularly focusing on achieving balance between efficient, deep representations and the need for capturing intricate image details, which are crucial for high-quality generative or restorative image tasks.