企业静态网站模板,沈阳网站建设 龙兴科技,网站移动端推广,制作网站的网页逐行注释
import torch
import torch.nn as nn
import torch.nn.functional as F
import warnings# 忽略警告信息#xff0c;这通常用于开发过程中#xff0c;避免警告干扰输出结果
warnings.filterwarnings(ignore)# 定义一个函数#xff0c;用于对神经网络模块的权重进行…逐行注释
import torch
import torch.nn as nn
import torch.nn.functional as F
import warnings# 忽略警告信息这通常用于开发过程中避免警告干扰输出结果
warnings.filterwarnings(ignore)# 定义一个函数用于对神经网络模块的权重进行正态分布初始化
def normal_init(module, mean0, std1, bias0):# 检查模块是否有权重属性并且权重不为Noneif hasattr(module, weight) and module.weight is not None:# 使用正态分布初始化权重均值为mean标准差为stdnn.init.normal_(module.weight, mean, std)# 检查模块是否有偏置属性并且偏置不为Noneif hasattr(module, bias) and module.bias is not None:# 将偏置初始化为bias指定的值nn.init.constant_(module.bias, bias)# 定义一个函数用于将神经网络模块的权重初始化为一个常数值
def constant_init(module, val, bias0):# 检查模块是否有权重属性并且权重不为Noneif hasattr(module, weight) and module.weight is not None:# 将权重初始化为val指定的常数值nn.init.constant_(module.weight, val)# 检查模块是否有偏置属性并且偏置不为Noneif hasattr(module, bias) and module.bias is not None:# 将偏置初始化为bias指定的值nn.init.constant_(module.bias, bias)
功能解释
normal_init 函数用于对神经网络中的权重进行正态分布初始化。这通常用于初始化卷积层或线性层的权重以引入小的随机性帮助模型学习。函数接受三个参数mean均值默认为0std标准差默认为1bias偏置初始化值默认为0。constant_init 函数用于将权重初始化为一个固定的常数值。这在某些特定情况下可能有用例如当需要将权重设置为特定值以实现某种特定的行为时。函数接受两个参数val权重的常数值bias偏置初始化值默认为0。
class DySample_UP(nn.Module):# 构造函数初始化DySample_UP模块def __init__(self, in_channels, scale2, stylelp, groups4, dyscopeFalse):super(DySample_UP, self).__init__() # 调用基类的构造函数self.scale scale # 上采样的尺度因子默认为2self.style style # 上采样的风格可以是lp或plself.groups groups # 组数用于分组卷积# 确保上采样风格是有效的assert style in [lp, pl]# 如果风格是pl则输入通道数必须是scale的平方并且是scale的倍数if style pl:assert in_channels scale ** 2 and in_channels % scale ** 2 0# 输入通道数必须至少等于组数并且是组数的倍数assert in_channels groups and in_channels % groups 0# 根据风格设置输入和输出通道数if style pl:in_channels in_channels // scale ** 2 # 对于pl风格调整输入通道数out_channels 2 * groups # 输出通道数为组数的两倍else:out_channels 2 * groups * scale ** 2 # 对于lp风格输出通道数为组数乘以scale的平方# 定义一个卷积层用于生成偏移量self.offset nn.Conv2d(in_channels, out_channels, 1)normal_init(self.offset, std0.001) # 使用标准差为0.001的正态分布初始化偏移量卷积层# 如果启用了dyscope动态作用域则添加一个额外的卷积层if dyscope:self.scope nn.Conv2d(in_channels, out_channels, 1)constant_init(self.scope, val0.) # 使用常数0初始化作用域卷积层# 注册一个缓冲区init_pos用于存储初始化的偏移位置self.register_buffer(init_pos, self._init_pos())# 初始化偏移位置的方法def _init_pos(self):# 使用arange生成一个从-self.scale/2到self.scale/2的序列然后除以scale进行归一化h torch.arange((-self.scale 1) / 2, (self.scale - 1) / 2 1) / self.scale# 使用meshgrid生成网格然后stack和transpose组合成一个2D偏移量矩阵return torch.stack(torch.meshgrid([h, h])).transpose(1, 2).repeat(1, self.groups, 1).reshape(1, -1, 1, 1)
功能解释
DySample_UP 类是一个动态上采样模块可以根据输入特征图动态地调整上采样的偏移量。in_channels 参数指定了输入特征图的通道数。scale 参数指定了上采样的尺度因子默认为2表示输出特征图的尺寸是输入的两倍。style 参数定义了上采样的风格可以是 lp局部感知或 pl像素洗牌后局部感知。groups 参数用于分组卷积可以增强特征图内的特征整合。dyscope 参数是一个布尔值用于确定是否使用动态作用域来调整偏移量。self.offset 是一个卷积层用于生成上采样的偏移量。normal_init 函数用于初始化 self.offset 的权重。self.scope 是一个可选的卷积层仅在 dyscope 为 True 时使用用于进一步调整偏移量。_init_pos 方法生成了一个初始化的偏移位置矩阵这个矩阵定义了上采样过程中每个像素点的参考位置。
class DySample_UP(nn.Module):# ...# sample 方法是上采样过程中对输入特征图 x 进行采样的核心函数def sample(self, x, offset):# 获取offset的尺寸B是批次大小H和W分别是特征图的高度和宽度B, _, H, W offset.shape# 调整offset的视角使其适用于后续的采样过程offset offset.view(B, 2, -1, H, W)# 创建一个网格坐标表示特征图中每个像素的位置coords_h torch.arange(H) 0.5coords_w torch.arange(W) 0.5coords torch.stack(torch.meshgrid([coords_w, coords_h])).\transpose(1, 2).unsqueeze(1).unsqueeze(0).type(x.dtype).to(x.device)# 归一化网格坐标使其范围在[-1, 1]这是F.grid_sample所需的坐标范围normalizer torch.tensor([W, H], dtypex.dtype, devicex.device).view(1, 2, 1, 1, 1)coords 2 * (coords offset) / normalizer - 1# 使用pixel_shuffle调整coords的维度以匹配后续的采样操作coords F.pixel_shuffle(coords.view(B, -1, H, W), self.scale).view(B, 2, -1, self.scale * H, self.scale * W).permute(0, 2, 3, 4, 1).contiguous().flatten(0, 1)# 使用grid_sample根据调整后的coords对x进行采样return F.grid_sample(x.reshape(B * self.groups, -1, H, W), coords, modebilinear,align_cornersFalse, padding_modeborder).view(B, -1, self.scale * H, self.scale * W)# forward_lp是局部感知Local Perception风格的上采样方法def forward_lp(self, x):# 如果定义了scope则使用scope调整offsetif hasattr(self, scope):offset self.offset(x) * self.scope(x).sigmoid() * 0.5 self.init_poselse:# 否则直接使用offset并加上初始化偏移offset self.offset(x) * 0.25 self.init_pos# 调用sample方法进行上采样return self.sample(x, offset)# forward_pl是像素洗牌后局部感知Pixel Shuffle then Local Perception风格的上采样方法def forward_pl(self, x):# 首先使用pixel_shuffle对x进行像素洗牌x_ F.pixel_shuffle(x, self.scale)# 如果定义了scope则使用scope调整offsetif hasattr(self, scope):offset F.pixel_unshuffle(self.offset(x_) * self.scope(x_).sigmoid(), self.scale) * 0.5 self.init_poselse:# 否则直接使用offset并加上初始化偏移offset F.pixel_unshuffle(self.offset(x_), self.scale) * 0.25 self.init_pos# 调用sample方法进行上采样return self.sample(x, offset) 功能解释
sample 方法是 DySample_UP 类的核心它负责根据偏移量 offset 对输入特征图 x 进行采样。这个方法使用了 F.grid_sample 来实现上采样通过调整采样坐标来实现动态上采样。forward_lp 和 forward_pl 是两种不同的上采样风格。它们首先计算偏移量然后调用 sample 方法来实现上采样。在 forward_lp 中如果没有定义 scope则偏移量是通过对 self.offset(x) 的输出进行缩放和加上初始化偏移量 self.init_pos 来得到的。在 forward_pl 中首先对输入 x 使用 F.pixel_shuffle 进行像素洗牌然后计算偏移量再使用 F.pixel_unshuffle 对偏移量进行逆操作以匹配像素洗牌后的维度。这两种方法都使用了 sample 方法来进行实际的上采样操作其中 modebilinear 指定了双线性插值作为采样方法align_cornersFalse 和 padding_modeborder 分别指定了坐标的对齐方式和填充模式。
通过这种方式DySample_UP 类提供了一种灵活的动态上采样机制可以根据不同的任务需求选择不同的上采样风格。