Unet代码解读(一)网络架构

本文介绍了一种使用VGG16作为特征提取器,并将其与UNet架构相结合的方法来完成图像分割任务。通过预训练的VGG16网络提取图像特征,再利用UNet进行上采样恢复图像细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章代码来自博主Bubbliiiing,特此感谢。

一.网络总览

在这里插入图片描述

二. VGG16主干提取网络

文章采用VGG16作为主干提取网络,只会用到两种类型的层,分别是卷积层和最大池化层。
代码对部分层进行了改动,详情可见下方网络结构,可参考下图作为理解
在这里插入图片描述
完整代码如下:

1.vggnet

import torch
import torch.nn as nn
from torchvision.models.utils import load_state_dict_from_url

class VGG(nn.Module):
    def __init__(self, features, num_classes=1000):
        super(VGG, self).__init__()
        self.features = features
        self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
        self.classifier = nn.Sequential(
            nn.Linear(512 * 7 * 7, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, num_classes),
        )
        self._initialize_weights()

    def forward(self, x):
        x = self.features(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x

    def _initialize_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                if m.bias is not 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值