神经网络架构参考:2-2 卷积篇

news/2024/11/14 12:02:57/文章来源:https://www.cnblogs.com/apachecn/p/18545709

densenet

结构

层名称 类型 输入大小 (H x W x C) 输出大小 (H x W x C) 核尺寸 步长 参数数量
Initial Conv Conv2D 224 x 224 x 3 112 x 112 x 64 7 x 7 2 9,408
Max Pooling MaxPool2D 112 x 112 x 64 56 x 56 x 64 3 x 3 2 0
Dense Block 1 Composite 56 x 56 x 64 56 x 56 x 256 - - -
Bottleneck 1.1 Conv2D 56 x 56 x 64 56 x 56 x 128 1 x 1 1
Conv 1.1 Conv2D 56 x 56 x 128 56 x 56 x 32 3 x 3 1
... ... ... ... ... ...
Bottleneck 1.6 Conv2D 56 x 56 x 256 56 x 56 x 128 1 x 1 1
Conv 1.6 Conv2D 56 x 56 x 128 56 x 56 x 32 3 x 3 1
Transition Layer 1 Composite 56 x 56 x 320 28 x 28 x 128 - - -
Conv Conv2D 56 x 56 x 320 56 x 56 x 128 1 x 1 1
Average Pooling AveragePool2D 56 x 56 x 128 28 x 28 x 128 2 x 2 2
Dense Block 2 Composite 28 x 28 x 128 28 x 28 x 512 - - -
Bottleneck 2.1 Conv2D 28 x 28 x 128 28 x 28 x 128 1 x 1 1
Conv 2.1 Conv2D 28 x 28 x 128 28 x 28 x 32 3 x 3 1
... ... ... ... ... ...
Bottleneck 2.6 Conv2D 28 x 28 x 512 28 x 28 x 128 1 x 1 1
Conv 2.6 Conv2D 28 x 28 x 128 28 x 28 x 32 3 x 3 1
Transition Layer 2 Composite 28 x 28 x 640 14 x 14 x 256 - - -
Conv Conv2D 28 x 28 x 640 28 x 28 x 256 1 x 1 1
Average Pooling AveragePool2D 28 x 28 x 256 14 x

下面是一个Dense Block的结构表格示例,这里以DenseNet-121中的第一个Dense Block为例,该Dense Block包含6个卷积层(每个卷积层由一个瓶颈层和一个3x3卷积层组成)。请注意,每个卷积层的输入大小是基于之前所有层的特征图合并后的结果。

层名称 类型 输入大小 (H x W x C) 输出大小 (H x W x C) 核尺寸 步长 参数数量
Bottleneck 1.1 Conv2D 56 x 56 x 64 56 x 56 x 128 1 x 1 1 832
Conv 1.1 Conv2D 56 x 56 x 128 56 x 56 x 32 3 x 3 1 3,072
Bottleneck 1.2 Conv2D 56 x 56 x 96 56 x 56 x 128 1 x 1 1 1,056
Conv 1.2 Conv2D 56 x 56 x 128 56 x 56 x 32 3 x 3 1 3,072
Bottleneck 1.3 Conv2D 56 x 56 x 128 56 x 56 x 128 1 x 1 1 1,056
Conv 1.3 Conv2D 56 x 56 x 128 56 x 56 x 32 3 x 3 1 3,072
Bottleneck 1.4 Conv2D 56 x 56 x 160 56 x 56 x 128 1 x 1 1 1,056
Conv 1.4 Conv2D 56 x 56 x 128 56 x 56 x 32 3 x 3 1 3,072
Bottleneck 1.5 Conv2D 56 x 56 x 192 56 x 56 x 128 1 x 1 1 1,056
Conv 1.5 Conv2D 56 x 56 x 128 56 x 56 x 32 3 x 3 1 3,072
Bottleneck 1.6 Conv2D 56 x 56 x 224 56 x 56 x 128 1 x 1 1 1,056
Conv 1.6 Conv2D 56 x 56 x 128 56 x 56 x 32 3 x 3 1 3,072

下面是一个Transition Layer的结构表格示例,这里以DenseNet-121中的一个Transition Layer为例:

层名称 类型 输入大小 (H x W x C) 输出大小 (H x W x C) 核尺寸 步长 参数数量
Conv (Transition) Conv2D 56 x 56 x 256 56 x 56 x 128 1 x 1 1 33,024
Avg Pooling AveragePooling2D 56 x 56 x 128 28 x 28 x 128 2 x 2 2 0

pytorch 源码

import torch
import torch.nn as nn
import torch.nn.functional as F
# 定义Dense Block中的单个Dense Layer
class DenseLayer(nn.Module):def __init__(self, in_channels, growth_rate):super(DenseLayer, self).__init__()inter_channels = 4 * growth_rateself.bn1 = nn.BatchNorm2d(in_channels)self.relu = nn.ReLU(inplace=True)self.conv1 = nn.Conv2d(in_channels, inter_channels, kernel_size=1, bias=False)self.bn2 = nn.BatchNorm2d(inter_channels)self.conv2 = nn.Conv2d(inter_channels, growth_rate, kernel_size=3, padding=1, bias=False)def forward(self, x):out = self.bn1(x)out = self.relu(out)out = self.conv1(out)out = self.bn2(out)out = self.relu(out)out = self.conv2(out)out = torch.cat([x, out], 1)return out
# 定义Dense Block
class DenseBlock(nn.Module):def __init__(self, in_channels, growth_rate, num_layers):super(DenseBlock, self).__init__()layers = []for i in range(num_layers):layers.append(DenseLayer(in_channels + i * growth_rate, growth_rate))self.layers = nn.Sequential(*layers)def forward(self, x):return self.layers(x)
# 定义Transition Layer
class TransitionLayer(nn.Module):def __init__(self, in_channels, out_channels):super(TransitionLayer, self).__init__()self.bn = nn.BatchNorm2d(in_channels)self.relu = nn.ReLU(inplace=True)self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False)self.pool = nn.AvgPool2d(kernel_size=2, stride=2)def forward(self, x):out = self.bn(x)out = self.relu(out)out = self.conv(out)out = self.pool(out)return out
# 定义DenseNet
class DenseNet(nn.Module):def __init__(self, growth_rate=32, block_config=(6, 12, 24, 16), num_init_features=64, bn_size=4, drop_rate=0, num_classes=1000):super(DenseNet, self).__init__()# 初始卷积层self.features = nn.Sequential(nn.Conv2d(3, num_init_features, kernel_size=7, stride=2, padding=3, bias=False),nn.BatchNorm2d(num_init_features),nn.ReLU(inplace=True),nn.MaxPool2d(kernel_size=3, stride=2, padding=1))# 每个Dense Block之前的通道数num_features = num_init_featuresfor i, num_layers in enumerate(block_config):# 添加一个Dense Blockself.features.add_module('denseblock%d' % (i + 1),DenseBlock(num_features, growth_rate, num_layers))# 更新通道数num_features += num_layers * growth_rate# 在Dense Block之间添加Transition Layer,除了最后一个if i != len(block_config) - 1:self.features.add_module('transition%d' % (i + 1),TransitionLayer(num_features, num_features // 2))num_features = num_features // 2# 最终的BatchNorm和ReLUself.features.add_module('bn', nn.BatchNorm2d(num_features))self.features.add_module('relu', nn.ReLU(inplace=True))# 全局平均池化层和分类器self.classifier = nn.Linear(num_features, num_classes)# 初始化权重for m in self.modules():if isinstance(m, nn.Conv2d):nn.init.kaiming_normal_(m.weight)elif isinstance(m, nn.BatchNorm2d):nn.init.constant_(m.weight, 1)nn.init.constant_(m.bias, 0)elif isinstance(m, nn.Linear):nn.init.constant_(m.bias, 0)def forward(self, x):features = self.features(x)out = F.adaptive_avg_pool2d(features, (1, 1))out = torch.flatten(out, 1)out = self.classifier(out)return out
# 创建DenseNet-121模型
densenet121 = DenseNet(growth_rate=32, block_config=(6, 12, 24, 16))
# 打印模型结构
print(densenet121)
# 假设输入张量是3x224x224
input_tensor = torch.randn(1, 3, 224, 224)
# 前向传播
output = densenet121(input_tensor)
print(output.shape)  # 应该输出torch.Size([1, 1000]),表示batch size为1,类别数为1000

mobilenet

结构

层名称 类型 输入大小(HWC) 输出大小(HWC) 核尺寸 步长 参数数量
Conv2d_0 Conv2d 224x224x3 112x112x32 3x3 2 864
DepthwiseConv2d_1 DepthwiseConv2d 112x112x32 112x112x32 3x3 1 288
Conv2d_2 Conv2d 112x112x32 112x112x64 1x1 1 2048
DepthwiseConv2d_3 DepthwiseConv2d 112x112x64 56x56x64 3x3 2 576
Conv2d_4 Conv2d 56x56x64 56x56x128 1x1 1 8192
... ... ... ... ... ... ...
DepthwiseConv2d_12 DepthwiseConv2d 14x14x512 14x14x512 3x3 1 4608
Conv2d_13 Conv2d 14x14x512 14x14x1024 1x1 1 524288
DepthwiseConv2d_14 DepthwiseConv2d 14x14x1024 7x7x1024 3x3 2 9216
Conv2d_15 Conv2d 7x7x1024 7x7x1024 1x1 1 1048576
AvgPool2d_16 AvgPool2d 7x7x1024 1x1x1024 7x7 1 0
Flatten_17 Flatten 1x1x1024 1024 - - 0
Linear_18 Linear 1024 1000 - - 1025000

pytorch 源码

import torch
import torch.nn as nn
import torch.nn.functional as F
class MobileNetV1(nn.Module):def __init__(self, num_classes=1000):super(MobileNetV1, self).__init__()self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=2, padding=1, bias=False)self.bn1 = nn.BatchNorm2d(32)self.layers = self._make_layers(in_channels=32)self.conv2 = nn.Conv2d(1024, 1024, kernel_size=1, stride=1, bias=False)self.bn2 = nn.BatchNorm2d(1024)self.avgpool = nn.AdaptiveAvgPool2d((1, 1))self.fc = nn.Linear(1024, num_classes)def _make_layers(self, in_channels):layers = []# 定义每一层的配置cfg = [(32, 1),(64, 2),(128, 2),(256, 2),(512, 6),(1024, 2),]for x, stride in cfg:# 深度可分离卷积layers.append(nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=stride, padding=1, groups=in_channels, bias=False))layers.append(nn.BatchNorm2d(in_channels))layers.append(nn.ReLU6(inplace=True))# 点卷积(1x1卷积)layers.append(nn.Conv2d(in_channels, x, kernel_size=1, stride=1, padding=0, bias=False))layers.append(nn.BatchNorm2d(x))layers.append(nn.ReLU6(inplace=True))in_channels = xreturn nn.Sequential(*layers)def forward(self, x):x = F.relu6(self.bn1(self.conv1(x)))x = self.layers(x)x = F.relu6(self.bn2(self.conv2(x)))x = self.avgpool(x)x = x.view(x.size(0), -1)x = self.fc(x)return x
# 创建模型实例
model = MobileNetV1(num_classes=1000)
print(model)

空间注意力网络

结构

层名称 类型 输入大小(HWC) 输出大小(HWC) 核尺寸 步长 参数数量
Input - 224x224x3 - - - 0
Conv1 Conv2D 224x224x3 112x112x64 7x7 2 9472
BatchNorm1 BatchNorm 112x112x64 112x112x64 - - 256
ReLU1 ReLU 112x112x64 112x112x64 - - 0
MaxPool1 MaxPooling 112x112x64 56x56x64 3x3 2 0
Conv2 Conv2D 56x56x64 56x56x128 3x3 1 73856
BatchNorm2 BatchNorm 56x56x128 56x56x128 - - 512
ReLU2 ReLU 56x56x128 56x56x128 - - 0
SpatialAttn1 SpatialAttn 56x56x128 56x56x128 - - 8192
Conv3 Conv2D 56x56x128 28x28x256 3x3 2 295168
BatchNorm3 BatchNorm 28x28x256 28x28x256 - - 1024
ReLU3 ReLU 28x28x256 28x28x256 - - 0
SpatialAttn2 SpatialAttn 28x28x256 28x28x256 - - 32768
Conv4 Conv2D 28x28x256 14x14x512 3x3 2 1180160
BatchNorm4 BatchNorm 14x14x512 14x14x512 - - 2048
ReLU4 ReLU 14x14x512 14x14x512 - - 0
SpatialAttn3 SpatialAttn 14x14x512 14x14x512 - - 131072
AvgPool AvgPooling 14x14x512 7x7x512 7x7 1 0
Flatten Flatten 7x7x512 25088 - - 0
FC1 Dense 25088 4096 - - 102764544
ReLU5 ReLU 4096 4096 - - 0
Dropout Dropout 4096 4096 - - 0
FC2 Dense 4096 1000 - - 4097000
Softmax Softmax 1000 1000 - - 0

以下是一个简化的空间注意力模块的结构表格。请注意,这个表格是一个示例,实际的网络结构可能会有所不同。

层名称 类型 输入大小(HWC) 输出大小(HWC) 核尺寸 步长 参数数量
Input - HxWxC - - - 0
Conv1 Conv2D HxWxC HxWx1 1x1 1 C
Sigmoid Sigmoid HxWx1 HxWx1 - - 0
Multiply Element-wise Mul HxWxC HxWxC - - 0

pytorch 源码

import torch
import torch.nn as nn
import torch.nn.functional as F
class SpatialAttentionModule(nn.Module):def __init__(self, kernel_size=7):super(SpatialAttentionModule, self).__init__()assert kernel_size % 2 == 1, "Kernel size must be odd"self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=kernel_size//2, bias=False)self.sigmoid = nn.Sigmoid()def forward(self, x):# 原始特征图avg_out = torch.mean(x, dim=1, keepdim=True)max_out, _ = torch.max(x, dim=1, keepdim=True)x = torch.cat([avg_out, max_out], dim=1)x = self.conv1(x)return self.sigmoid(x) * x
class SpatialAttentionNetwork(nn.Module):def __init__(self):super(SpatialAttentionNetwork, self).__init__()self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)self.bn1 = nn.BatchNorm2d(64)self.relu = nn.ReLU(inplace=True)self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)self.spatial_attention = SpatialAttentionModule(kernel_size=7)self.layer1 = self._make_layer(64, 64, 3)self.layer2 = self._make_layer(64, 128, 4, stride=2)self.layer3 = self._make_layer(128, 256, 6, stride=2)self.layer4 = self._make_layer(256, 512, 3, stride=2)self.avgpool = nn.AdaptiveAvgPool2d((1, 1))self.fc = nn.Linear(512, 1000)def _make_layer(self, in_channels, out_channels, blocks, stride=1):layers = []layers.append(nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False))layers.append(nn.BatchNorm2d(out_channels))layers.append(nn.ReLU(inplace=True))for i in range(1, blocks):layers.append(nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False))layers.append(nn.BatchNorm2d(out_channels))layers.append(nn.ReLU(inplace=True))return nn.Sequential(*layers)def forward(self, x):x = self.conv1(x)x = self.bn1(x)x = self.relu(x)x = self.maxpool(x)x = self.spatial_attention(x)x = self.layer1(x)x = self.layer2(x)x = self.layer3(x)x = self.layer4(x)x = self.avgpool(x)x = torch.flatten(x, 1)x = self.fc(x)return x
# 实例化网络
san = SpatialAttentionNetwork()
# 打印网络结构
print(san)

卷积变分自编码器

结构1(转置卷积)

层名称 类型 输入大小(HWC) 输出大小(HWC) 核尺寸 步长 参数数量
Input - 128x128x3 - - - 0
Conv1 Conv2D 128x128x3 64x64x32 3x3 2x2 896
ReLU1 ReLU 64x64x32 64x64x32 - - 0
Conv2 Conv2D 64x64x32 32x32x64 3x3 2x2 18496
ReLU2 ReLU 32x32x64 32x32x64 - - 0
Conv3 Conv2D 32x32x64 16x16x128 3x3 2x2 73856
ReLU3 ReLU 16x16x128 16x16x128 - - 0
Flatten Flatten 16x16x128 32768 - - 0
FC4 Dense 32768 512 - - 16781312
FC_mean Dense 512 10 - - 5130
FC_log_var Dense 512 10 - - 5130
Sampling Sampling 10 10 - - 0
FC5 Dense 10 512 - - 5220
FC6 Dense 512 32768 - - 16781312
Reshape Reshape 32768 16x16x128 - - 0
Deconv1 Conv2DTranspose 16x16x128 32x32x64 3x3 2x2 73792
ReLU4 ReLU 32x32x64 32x32x64 - - 0
Deconv2 Conv2DTranspose 32x32x64 64x64x32 3x3 2x2 18432
ReLU5 ReLU 64x64x32 64x64x32 - - 0
Deconv3 Conv2DTranspose 64x64x32 128x128x3 3x3 2x2 864
Sigmoid Sigmoid 128x128x3 128x128x3 - - 0

结构2(池化+上采样)

层名称 类型 输入大小(HWC) 输出大小(HWC) 核尺寸 步长 参数数量
Input - 128x128x3 - - - 0
Conv1 Conv2D 128x128x3 128x128x32 3x3 1x1 896
ReLU1 ReLU 128x128x32 128x128x32 - - 0
Pool1 MaxPooling2D 128x128x32 64x64x32 2x2 2x2 0
Conv2 Conv2D 64x64x32 64x64x64 3x3 1x1 18496
ReLU2 ReLU 64x64x64 64x64x64 - - 0
Pool2 MaxPooling2D 64x64x64 32x32x64 2x2 2x2 0
Conv3 Conv2D 32x32x64 32x32x128 3x3 1x1 73856
ReLU3 ReLU 32x32x128 32x32x128 - - 0
Pool3 MaxPooling2D 32x32x128 16x16x128 2x2 2x2 0
Flatten Flatten 16x16x128 32768 - - 0
FC4 Dense 32768 512 - - 16781312
FC_mean Dense 512 10 - - 5130
FC_log_var Dense 512 10 - - 5130
Sampling Sampling 10 10 - - 0
FC5 Dense 10 512 - - 5220
FC6 Dense 512 32768 - - 16781312
Reshape Reshape 32768 16x16x128 - - 0
Deconv1 Conv2DTranspose 16x16x128 32x32x64 3x3 1x1 73792
Upsample1 UpSampling2D 32x32x64 64x64x64 2x2 2x2 0
Deconv2 Conv2DTranspose 64x64x64 64x64x32 3x3 1x1 18432
Upsample2 UpSampling2D 64x64x32 128x128x32 2x2 2x2 0
Deconv3 Conv2DTranspose 128x128x32 128x128x3 3x3 1x1 864
Sigmoid Sigmoid 128x128x3 128x128x3 - - 0

源码

import torch
import torch.nn as nn
import torch.nn.functional as F
class CVAE(nn.Module):def __init__(self):super(CVAE, self).__init__()# 编码器部分self.encoder = nn.Sequential(nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1),  # 输出: 128x128x32nn.ReLU(),nn.MaxPool2d(kernel_size=2, stride=2),  # 输出: 64x64x32nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),  # 输出: 64x64x64nn.ReLU(),nn.MaxPool2d(kernel_size=2, stride=2),  # 输出: 32x32x64nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),  # 输出: 32x32x128nn.ReLU(),nn.MaxPool2d(kernel_size=2, stride=2),  # 输出: 16x16x128)# 全连接层,用于获取均值和方差self.fc_mean = nn.Linear(16*16*128, 10)self.fc_log_var = nn.Linear(16*16*128, 10)# 解码器部分self.decoder = nn.Sequential(nn.Linear(10, 16*16*128),nn.ReLU(),nn.Unflatten(1, (128, 16, 16)),nn.ConvTranspose2d(128, 64, kernel_size=3, stride=1, padding=1),  # 输出: 16x16x64nn.ReLU(),nn.UpSampling2d(scale_factor=2),  # 输出: 32x32x64nn.ConvTranspose2d(64, 32, kernel_size=3, stride=1, padding=1),  # 输出: 32x32x32nn.ReLU(),nn.UpSampling2d(scale_factor=2),  # 输出: 64x64x32nn.ConvTranspose2d(32, 3, kernel_size=3, stride=1, padding=1),  # 输出: 64x64x3nn.Sigmoid(),nn.UpSampling2d(scale_factor=2),  # 输出: 128x128x3)def reparameterize(self, mu, logvar):std = torch.exp(0.5*logvar)eps = torch.randn_like(std)return mu + eps*stddef forward(self, x):# 编码encoded = self.encoder(x)encoded = encoded.view(encoded.size(0), -1)mu = self.fc_mean(encoded)logvar = self.fc_log_var(encoded)# 重参数化z = self.reparameterize(mu, logvar)# 解码decoded = self.decoder(z)return decoded, mu, logvar
# 实例化模型
cvae = CVAE()
# 打印模型结构
print(cvae)

UNET

结构

层名称 类型 输入大小 (HxWxC) 输出大小 (HxWxC) 核尺寸 步长 参数数量
Input - 572x572x1 - - - -
Conv2D_1 Conv2D 572x572x1 568x568x64 3x3 1 1728
BatchNorm_1 BatchNorm 568x568x64 568x568x64 - - 256
ReLU_1 ReLU 568x568x64 568x568x64 - - 0
MaxPool2D_1 MaxPool2D 568x568x64 284x284x64 2x2 2 0
Conv2D_2 Conv2D 284x284x64 280x280x128 3x3 1 18432
BatchNorm_2 BatchNorm 280x280x128 280x280x128 - - 512
ReLU_2 ReLU 280x280x128 280x280x128 - - 0
MaxPool2D_2 MaxPool2D 280x280x128 140x140x128 2x2 2 0
Conv2D_3 Conv2D 140x140x128 136x136x256 3x3 1 73728
BatchNorm_3 BatchNorm 136x136x256 136x136x256 - - 1024
ReLU_3 ReLU 136x136x256 136x136x256 - - 0
MaxPool2D_3 MaxPool2D 136x136x256 68x68x256 2x2 2 0
Conv2D_4 Conv2D 68x68x256 64x64x512 3x3 1 295040
BatchNorm_4 BatchNorm 64x64x512 64x64x512 - - 2048
ReLU_4 ReLU 64x64x512 64x64x512 - - 0
MaxPool2D_4 MaxPool2D 64x64x512 32x32x512 2x2 2 0
Conv2D_5 Conv2D 32x32x512 32x32x1024 3x3 1 1180160
BatchNorm_5 BatchNorm 32x32x1024 32x32x1024 - - 4096
ReLU_5 ReLU 32x32x1024 32x32x1024 - - 0
UpConv2D_1 ConvTranspose 32x32x1024 64x64x512 2x2 2 2099200
Concat_1 Concat 64x64x1536 64x64x1024 - - 0
Conv2D_6 Conv2D 64x64x1024 64x64x512 3x3 1 524800
BatchNorm_6 BatchNorm 64x64x512 64x64x512 - - 2048
ReLU_6 ReLU 64x64x512 64x64x512 - - 0
UpConv2D_2 ConvTranspose 64x64x512 128x128x256 2x2 2 1049600
Concat_2 Concat 128x128x512 128x128x512 - - 0
Conv2D_7 Conv2D 128x128x512 128x128x256 3x3 1 262400
BatchNorm_7 BatchNorm 128x128x256 128x128x256 - - 1024
ReLU_7 ReLU 128x128x256 128x128x256 - - 0
UpConv2D_3 ConvTranspose 128x128x256 256x256x128 2x2 2 524800
Concat_3 Concat 256x256x256 256x256x256 - - 0
Conv2D_8 Conv2D 256x256x256 256x256x128 3x3 1 131200
BatchNorm_8 BatchNorm 256x256x128 256x256x128 - - 512
ReLU_8 ReLU 256x256x128 256x256x128 - - 0
UpConv2D_4 ConvTranspose 256x256x128 512x512x64 2x2 2 262400
Concat_4 Concat 512x512x128 512x512x128 - - 0
Conv2D_9 Conv2D 512x512x128 512x512x64 3x3 1 64800
BatchNorm_9 BatchNorm 512x512x64 512x512x64 - - 256
ReLU_9 ReLU 512x512x64 512x512x64 - - 0
Conv2D_10 Conv2D 512x512x64 512x512x1 1x1 1 65
Sigmoid Sigmoid 512x512x1 512x512x1 - - 0

源码

import torch
import torch.nn as nn
import torch.nn.functional as F
class UNet(nn.Module):def __init__(self, in_channels=1, out_channels=1):super(UNet, self).__init__()# Encoder pathself.conv1 = self.conv_block(in_channels, 64)self.conv2 = self.conv_block(64, 128)self.conv3 = self.conv_block(128, 256)self.conv4 = self.conv_block(256, 512)self.conv5 = self.conv_block(512, 1024)# Decoder pathself.upconv4 = self.up_conv_block(1024, 512)self.upconv3 = self.up_conv_block(512, 256)self.upconv2 = self.up_conv_block(256, 128)self.upconv1 = self.up_conv_block(128, 64)# Outputself.out = nn.Conv2d(64, out_channels, kernel_size=1)def conv_block(self, in_channels, out_channels):block = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),nn.BatchNorm2d(out_channels),nn.ReLU(inplace=True),nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),nn.BatchNorm2d(out_channels),nn.ReLU(inplace=True))return blockdef up_conv_block(self, in_channels, out_channels):block = nn.Sequential(nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2),nn.ReLU(inplace=True))return blockdef forward(self, x):# Encoder pathenc1 = self.conv1(x)enc2 = self.conv2(F.max_pool2d(enc1, 2))enc3 = self.conv3(F.max_pool2d(enc2, 2))enc4 = self.conv4(F.max_pool2d(enc3, 2))enc5 = self.conv5(F.max_pool2d(enc4, 2))# Decoder pathdec4 = self.upconv4(enc5)dec4 = torch.cat((enc4, dec4), dim=1)dec3 = self.upconv3(dec4)dec3 = torch.cat((enc3, dec3), dim=1)dec2 = self.upconv2(dec3)dec2 = torch.cat((enc2, dec2), dim=1)dec1 = self.upconv1(dec2)dec1 = torch.cat((enc1, dec1), dim=1)# Outputout = self.out(dec1)return out
# Example usage:
# unet = UNet()
# input_tensor = torch.randn(1, 1, 572, 572)
# output = unet(input_tensor)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/833360.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据库字段设置非空, phalcon创建数据验证不通过

在使用phalcon的insert和update功能时,因为数据库所有的字段设置的都是NOT NULL,而phalcon的model在插入或更新之前会自动判断字段是否需要必填,因此导致有空字段时无法存入。 开始遇到这问题时,想到两种解决方法: 一、改数据库字段,把NOT NULL改为可以为空。但该数据库还…

2024年11月中国数据库排行榜:OB连续四月居榜首,腾讯云TDSQL升第九

11月墨天轮排行榜解读新鲜出炉!榜单前五稳中求进,OceanBase连续四月居榜首,第六至十位竞争加剧,此外亦有部分产品焕发活力,一起来看更多排名情况与解读!11月墨天轮社区的中国数据库流行度排行榜已更新,本期共有226个数据库产品参与,相较于年初的292个数据库,产品阵容已…

cmu15545笔记-索引并发控制(Concurrent Indexes)

目录OverviewLock和Latch辨析设计目标大致分类Hash Table LatchesPage LatchesSlot LatchesB+Tree Latches并发问题Latch Crabbing/CoupingOptimistic Coupling(乐观锁)Leaf Node Scan Overview Lock和Latch辨析Lock:抽象的,逻辑的,整体统筹 Latch:具体的,原语性的,自我…

cmu15545笔记:索引并发控制(Concurrent Indexes)

目录OverviewLock和Latch辨析设计目标大致分类Hash Table LatchesPage LatchesSlot LatchesB+Tree Latches并发问题Latch Crabbing/CoupingOptimistic Coupling(乐观锁)Leaf Node Scan Overview Lock和Latch辨析Lock:抽象的,逻辑的,整体统筹 Latch:具体的,原语性的,自我…

cmu15545-索引并发控制(Concurrent Indexes)

目录OverviewLock和Latch辨析设计目标大致分类Hash Table LatchesPage LatchesSlot LatchesB+Tree Latches并发问题Latch Crabbing/CoupingOptimistic Coupling(乐观锁)Leaf Node Scan Overview Lock和Latch辨析Lock:抽象的,逻辑的,整体统筹 Latch:具体的,原语性的,自我…

向量动态量化

本文介绍向量检索服务DashVector动态量化功能和作用。 背景介绍 量化(Quantization)是向量检索技术中一种常用的优化方法,通过一定程度的精度(召回率)损失,来换取性能的大幅度提升,以及内存占用(索引文件大小)大幅度降低。 向量检索服务DashVector支持向量的动态量化,…

从零到一构建并打包 React + TypeScript + Less组件库教程(一、项目初始化搭建+代码规范集成)

本系列涉及的内容如下:组件库基础搭建,react + ts + less 项目规范,包括但不限于 prettier、eslint、stylelint、husky、lint-staged、commitlint pnpm monorepo + turborepo 集成 gulp + webpack 构建 esm、cjs 和 umd storybook 文档集成此系列不包含发布 npm 和构建 CI 流…

分区Partition

理解Partition 向量检索服务DashVector的Collection具有分区(Partition)的能力,同一个Collection下的Doc可通过不同的Partition进行物理和逻辑上的分区。各种Doc操作(如插入Doc、检索Doc等。若指定Partition,则该操作将限定在该指定的Paritition内进行。通过合理的Partiti…

PGsql 大写字段名,flink目前不支持读取

flink1.20 连接 postgres_flink postgresql-CSDN博客[FLINK-23324] Postgres of JDBC Connector enable case-sensitive. - ASF JIRA

成本减半 + 效率翻倍:这家企业用 11 天实现数据处理飞跃

伴随着云计算和数字化浪潮的推进,越来越多行业数据实时性的权重正在被加速提升,对企业而言,如果想要保证业务的持续发展,其底层数据库必须要能满足数据的实时性和高并发要求。 除此之外,在降本增效的大潮下,“高性价比”也成为企业选择一款数仓的重要因素。 那么,企业应…

Windows激活神器HEU KMS Activator v42.3.1

软件介绍 HEU KMS Activator,简洁高效的全能KMS/OEM激活工具,适用所有Windows, Office版本,无需联网即可一键激活,支持UEFI的KMS激活工具。KMS服务是微软对Windows, Office等产品的批量许可服务,利用KMS可以激活局域网内的产品。该工具利用KMS机制在系统搭建KMS服务器,从…

如何远程实时监控员工的电脑屏幕?60教会你,五个妙招轻松搞定!

https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9073269535369745774%22%7D&n_type=1&p_from=4 使用远程桌面协议(RDP)远程桌面协议(RDP)是一种允许用户远程连接到另一台计算机的技术。通过配置RDP,管理员可以实时访问员工的…