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

44 阅读20分钟

densenet

结构

层名称类型输入大小 (H x W x C)输出大小 (H x W x C)核尺寸步长参数数量
Initial ConvConv2D224 x 224 x 3112 x 112 x 647 x 729,408
Max PoolingMaxPool2D112 x 112 x 6456 x 56 x 643 x 320
Dense Block 1Composite56 x 56 x 6456 x 56 x 256---
Bottleneck 1.1Conv2D56 x 56 x 6456 x 56 x 1281 x 11832
Conv 1.1Conv2D56 x 56 x 12856 x 56 x 323 x 313,072
.....................
Bottleneck 1.6Conv2D56 x 56 x 25656 x 56 x 1281 x 11832
Conv 1.6Conv2D56 x 56 x 12856 x 56 x 323 x 313,072
Transition Layer 1Composite56 x 56 x 32028 x 28 x 128---
ConvConv2D56 x 56 x 32056 x 56 x 1281 x 114,096
Average PoolingAveragePool2D56 x 56 x 12828 x 28 x 1282 x 220
Dense Block 2Composite28 x 28 x 12828 x 28 x 512---
Bottleneck 2.1Conv2D28 x 28 x 12828 x 28 x 1281 x 111,664
Conv 2.1Conv2D28 x 28 x 12828 x 28 x 323 x 319,216
.....................
Bottleneck 2.6Conv2D28 x 28 x 51228 x 28 x 1281 x 111,664
Conv 2.6Conv2D28 x 28 x 12828 x 28 x 323 x 319,216
Transition Layer 2Composite28 x 28 x 64014 x 14 x 256---
ConvConv2D28 x 28 x 64028 x 28 x 2561 x 1116,384
Average PoolingAveragePool2D28 x 28 x 25614 x

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

层名称类型输入大小 (H x W x C)输出大小 (H x W x C)核尺寸步长参数数量
Bottleneck 1.1Conv2D56 x 56 x 6456 x 56 x 1281 x 11832
Conv 1.1Conv2D56 x 56 x 12856 x 56 x 323 x 313,072
Bottleneck 1.2Conv2D56 x 56 x 9656 x 56 x 1281 x 111,056
Conv 1.2Conv2D56 x 56 x 12856 x 56 x 323 x 313,072
Bottleneck 1.3Conv2D56 x 56 x 12856 x 56 x 1281 x 111,056
Conv 1.3Conv2D56 x 56 x 12856 x 56 x 323 x 313,072
Bottleneck 1.4Conv2D56 x 56 x 16056 x 56 x 1281 x 111,056
Conv 1.4Conv2D56 x 56 x 12856 x 56 x 323 x 313,072
Bottleneck 1.5Conv2D56 x 56 x 19256 x 56 x 1281 x 111,056
Conv 1.5Conv2D56 x 56 x 12856 x 56 x 323 x 313,072
Bottleneck 1.6Conv2D56 x 56 x 22456 x 56 x 1281 x 111,056
Conv 1.6Conv2D56 x 56 x 12856 x 56 x 323 x 313,072

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

层名称类型输入大小 (H x W x C)输出大小 (H x W x C)核尺寸步长参数数量
Conv (Transition)Conv2D56 x 56 x 25656 x 56 x 1281 x 1133,024
Avg PoolingAveragePooling2D56 x 56 x 12828 x 28 x 1282 x 220

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_rate
        self.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_features
        for i, num_layers in enumerate(block_config):
            # 添加一个Dense Block
            self.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和ReLU
        self.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_0Conv2d224x224x3112x112x323x32864
DepthwiseConv2d_1DepthwiseConv2d112x112x32112x112x323x31288
Conv2d_2Conv2d112x112x32112x112x641x112048
DepthwiseConv2d_3DepthwiseConv2d112x112x6456x56x643x32576
Conv2d_4Conv2d56x56x6456x56x1281x118192
.....................
DepthwiseConv2d_12DepthwiseConv2d14x14x51214x14x5123x314608
Conv2d_13Conv2d14x14x51214x14x10241x11524288
DepthwiseConv2d_14DepthwiseConv2d14x14x10247x7x10243x329216
Conv2d_15Conv2d7x7x10247x7x10241x111048576
AvgPool2d_16AvgPool2d7x7x10241x1x10247x710
Flatten_17Flatten1x1x10241024--0
Linear_18Linear10241000--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 = x
        return 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
Conv1Conv2D224x224x3112x112x647x729472
BatchNorm1BatchNorm112x112x64112x112x64--256
ReLU1ReLU112x112x64112x112x64--0
MaxPool1MaxPooling112x112x6456x56x643x320
Conv2Conv2D56x56x6456x56x1283x3173856
BatchNorm2BatchNorm56x56x12856x56x128--512
ReLU2ReLU56x56x12856x56x128--0
SpatialAttn1SpatialAttn56x56x12856x56x128--8192
Conv3Conv2D56x56x12828x28x2563x32295168
BatchNorm3BatchNorm28x28x25628x28x256--1024
ReLU3ReLU28x28x25628x28x256--0
SpatialAttn2SpatialAttn28x28x25628x28x256--32768
Conv4Conv2D28x28x25614x14x5123x321180160
BatchNorm4BatchNorm14x14x51214x14x512--2048
ReLU4ReLU14x14x51214x14x512--0
SpatialAttn3SpatialAttn14x14x51214x14x512--131072
AvgPoolAvgPooling14x14x5127x7x5127x710
FlattenFlatten7x7x51225088--0
FC1Dense250884096--102764544
ReLU5ReLU40964096--0
DropoutDropout40964096--0
FC2Dense40961000--4097000
SoftmaxSoftmax10001000--0

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

层名称类型输入大小(HWC)输出大小(HWC)核尺寸步长参数数量
Input-HxWxC---0
Conv1Conv2DHxWxCHxWx11x11C
SigmoidSigmoidHxWx1HxWx1--0
MultiplyElement-wise MulHxWxCHxWxC--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
Conv1Conv2D128x128x364x64x323x32x2896
ReLU1ReLU64x64x3264x64x32--0
Conv2Conv2D64x64x3232x32x643x32x218496
ReLU2ReLU32x32x6432x32x64--0
Conv3Conv2D32x32x6416x16x1283x32x273856
ReLU3ReLU16x16x12816x16x128--0
FlattenFlatten16x16x12832768--0
FC4Dense32768512--16781312
FC_meanDense51210--5130
FC_log_varDense51210--5130
SamplingSampling1010--0
FC5Dense10512--5220
FC6Dense51232768--16781312
ReshapeReshape3276816x16x128--0
Deconv1Conv2DTranspose16x16x12832x32x643x32x273792
ReLU4ReLU32x32x6432x32x64--0
Deconv2Conv2DTranspose32x32x6464x64x323x32x218432
ReLU5ReLU64x64x3264x64x32--0
Deconv3Conv2DTranspose64x64x32128x128x33x32x2864
SigmoidSigmoid128x128x3128x128x3--0

结构2(池化+上采样)

层名称类型输入大小(HWC)输出大小(HWC)核尺寸步长参数数量
Input-128x128x3---0
Conv1Conv2D128x128x3128x128x323x31x1896
ReLU1ReLU128x128x32128x128x32--0
Pool1MaxPooling2D128x128x3264x64x322x22x20
Conv2Conv2D64x64x3264x64x643x31x118496
ReLU2ReLU64x64x6464x64x64--0
Pool2MaxPooling2D64x64x6432x32x642x22x20
Conv3Conv2D32x32x6432x32x1283x31x173856
ReLU3ReLU32x32x12832x32x128--0
Pool3MaxPooling2D32x32x12816x16x1282x22x20
FlattenFlatten16x16x12832768--0
FC4Dense32768512--16781312
FC_meanDense51210--5130
FC_log_varDense51210--5130
SamplingSampling1010--0
FC5Dense10512--5220
FC6Dense51232768--16781312
ReshapeReshape3276816x16x128--0
Deconv1Conv2DTranspose16x16x12832x32x643x31x173792
Upsample1UpSampling2D32x32x6464x64x642x22x20
Deconv2Conv2DTranspose64x64x6464x64x323x31x118432
Upsample2UpSampling2D64x64x32128x128x322x22x20
Deconv3Conv2DTranspose128x128x32128x128x33x31x1864
SigmoidSigmoid128x128x3128x128x3--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),  # 输出: 128x128x32
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2),  # 输出: 64x64x32
            nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),  # 输出: 64x64x64
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2),  # 输出: 32x32x64
            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),  # 输出: 32x32x128
            nn.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),  # 输出: 16x16x64
            nn.ReLU(),
            nn.UpSampling2d(scale_factor=2),  # 输出: 32x32x64
            nn.ConvTranspose2d(64, 32, kernel_size=3, stride=1, padding=1),  # 输出: 32x32x32
            nn.ReLU(),
            nn.UpSampling2d(scale_factor=2),  # 输出: 64x64x32
            nn.ConvTranspose2d(32, 3, kernel_size=3, stride=1, padding=1),  # 输出: 64x64x3
            nn.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*std
    def 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_1Conv2D572x572x1568x568x643x311728
BatchNorm_1BatchNorm568x568x64568x568x64--256
ReLU_1ReLU568x568x64568x568x64--0
MaxPool2D_1MaxPool2D568x568x64284x284x642x220
Conv2D_2Conv2D284x284x64280x280x1283x3118432
BatchNorm_2BatchNorm280x280x128280x280x128--512
ReLU_2ReLU280x280x128280x280x128--0
MaxPool2D_2MaxPool2D280x280x128140x140x1282x220
Conv2D_3Conv2D140x140x128136x136x2563x3173728
BatchNorm_3BatchNorm136x136x256136x136x256--1024
ReLU_3ReLU136x136x256136x136x256--0
MaxPool2D_3MaxPool2D136x136x25668x68x2562x220
Conv2D_4Conv2D68x68x25664x64x5123x31295040
BatchNorm_4BatchNorm64x64x51264x64x512--2048
ReLU_4ReLU64x64x51264x64x512--0
MaxPool2D_4MaxPool2D64x64x51232x32x5122x220
Conv2D_5Conv2D32x32x51232x32x10243x311180160
BatchNorm_5BatchNorm32x32x102432x32x1024--4096
ReLU_5ReLU32x32x102432x32x1024--0
UpConv2D_1ConvTranspose32x32x102464x64x5122x222099200
Concat_1Concat64x64x153664x64x1024--0
Conv2D_6Conv2D64x64x102464x64x5123x31524800
BatchNorm_6BatchNorm64x64x51264x64x512--2048
ReLU_6ReLU64x64x51264x64x512--0
UpConv2D_2ConvTranspose64x64x512128x128x2562x221049600
Concat_2Concat128x128x512128x128x512--0
Conv2D_7Conv2D128x128x512128x128x2563x31262400
BatchNorm_7BatchNorm128x128x256128x128x256--1024
ReLU_7ReLU128x128x256128x128x256--0
UpConv2D_3ConvTranspose128x128x256256x256x1282x22524800
Concat_3Concat256x256x256256x256x256--0
Conv2D_8Conv2D256x256x256256x256x1283x31131200
BatchNorm_8BatchNorm256x256x128256x256x128--512
ReLU_8ReLU256x256x128256x256x128--0
UpConv2D_4ConvTranspose256x256x128512x512x642x22262400
Concat_4Concat512x512x128512x512x128--0
Conv2D_9Conv2D512x512x128512x512x643x3164800
BatchNorm_9BatchNorm512x512x64512x512x64--256
ReLU_9ReLU512x512x64512x512x64--0
Conv2D_10Conv2D512x512x64512x512x11x1165
SigmoidSigmoid512x512x1512x512x1--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 path
        self.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 path
        self.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)
        
        # Output
        self.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 block
    
    def 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 block
    
    def forward(self, x):
        # Encoder path
        enc1 = 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 path
        dec4 = 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)
        
        # Output
        out = self.out(dec1)
        return out
# Example usage:
# unet = UNet()
# input_tensor = torch.randn(1, 1, 572, 572)
# output = unet(input_tensor)