Xception模型详解

简介

Xception的名称源自于"Extreme Inception",它是在Inception架构的基础上进行了扩展和改进。Inception架构是Google团队提出的一种经典的卷积神经网络架构,用于解决深度卷积神经网络中的计算和参数增长问题。

与Inception不同,Xception的主要创新在于使用了深度可分离卷积(Depthwise Separable Convolution)来替代传统的卷积操作。深度可分离卷积将卷积操作分解为两个步骤:深度卷积和逐点卷积。

深度卷积是一种在每个输入通道上分别应用卷积核的操作,它可以有效地减少计算量和参数数量。逐点卷积是一种使用1x1卷积核进行通道间的线性组合的操作,用于增加模型的表示能力。通过使用深度可分离卷积,Xception网络能够更加有效地学习特征表示,并在相同计算复杂度下获得更好的性能。

Xception 网络结构

一个标准的Inception模块(Inception V3)

0b0008e1ef754accaa4a8d8177ae566b.png

简化后的Inception模块

e9a6b169b6854e6eacf505a4ddfc6266.png

简化后的Inception的等价结构

344cef824d5e42b5b5c98018b2ce9c8f.png

采用深度可分离卷积的思想,使 3×3 卷积的数量与 1×1卷积输出通道的数量相等

542503ee83654a3dbae5b633c3123175.png

Xception模型,一共可以分为3个flow,分别是Entry flow、Middle flow、Exit flow。

dab82928e7e1473fac784949da4dfe0f.png

在这里 Entry 与 Exit 都具有相同的部分,Middle 与这二者有所不同。

Xception模型的pytorch复现

(1)深度可分离卷积

class SeparableConv2d(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=0, dilation=1, bias=False):
        super(SeparableConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channels, in_channels, kernel_size, stride, padding,
                               dilation, groups=in_channels, bias=bias)
        self.pointwise = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0,
                                   dilation=1, groups=1, bias=False)

    def forward(self, x):
        x = self.conv(x)
        x = self.pointwise(x)
        return x

(2)构建三个flow结构

class EntryFlow(nn.Module):
    def __init__(self):
        super(EntryFlow, self).__init__()

        self.headconv = nn.Sequential(
            nn.Conv2d(3, 32, 3, 2, bias=False),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace=True),

            nn.Conv2d(32, 64, 3, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
        )

        self.residual_block1 = nn.Sequential(
            SeparableConv2d(64, 128, 3, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            SeparableConv2d(128, 128, 3, padding=1),
            nn.BatchNorm2d(128),
            nn.MaxPool2d(3, stride=2, padding=1),
        )

        self.residual_block2 = nn.Sequential(
            nn.ReLU(inplace=True),
            SeparableConv2d(128, 256, 3, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            SeparableConv2d(256, 256, 3, padding=1),
            nn.BatchNorm2d(256),
            nn.MaxPool2d(3, stride=2, padding=1)
        )

        self.residual_block3 = nn.Sequential(
            nn.ReLU(inplace=True),
            SeparableConv2d(256, 728, 3, padding=1),
            nn.BatchNorm2d(728),
            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728),
            nn.MaxPool2d(3, stride=2, padding=1)
        )

    def shortcut(self, inp, oup):
        return nn.Sequential(
            nn.Conv2d(inp, oup, 1, 2, bias=False),
            nn.BatchNorm2d(oup)
            )

    def forward(self, x):
        x = self.headconv(x)
        residual = self.residual_block1(x)
        shortcut_block1 = self.shortcut(64, 128)
        x = residual + shortcut_block1(x)
        residual = self.residual_block2(x)
        shortcut_block2 = self.shortcut(128, 256)
        x = residual + shortcut_block2(x)
        residual = self.residual_block3(x)
        shortcut_block3 = self.shortcut(256, 728)
        x = residual + shortcut_block3(x)

        return x

class MiddleFlow(nn.Module):
    def __init__(self):
        super(MiddleFlow, self).__init__()

        self.shortcut = nn.Sequential()
        self.conv1 = nn.Sequential(
            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728),

            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728),

            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728)
        )

    def forward(self, x):
        residual = self.conv1(x)
        input = self.shortcut(x)
        return input + residual

class ExitFlow(nn.Module):
    def __init__(self):
        super(ExitFlow, self).__init__()

        self.residual_with_exit = nn.Sequential(
            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728),
            nn.ReLU(inplace=True),
            SeparableConv2d(728, 1024, 3, padding=1),
            nn.BatchNorm2d(1024),
            nn.MaxPool2d(3, stride=2, padding=1)
        )

        self.endconv = nn.Sequential(
            SeparableConv2d(1024, 1536, 3, 1, 1),
            nn.BatchNorm2d(1536),
            nn.ReLU(inplace=True),

            SeparableConv2d(1536, 2048, 3, 1, 1),
            nn.BatchNorm2d(2048),
            nn.ReLU(inplace=True),

            nn.AdaptiveAvgPool2d((1, 1)),
        )

    def shortcut(self, inp, oup):
        return nn.Sequential(
            nn.Conv2d(inp, oup, 1, 2, bias=False),
            nn.BatchNorm2d(oup)
        )

    def forward(self, x):
        residual = self.residual_with_exit(x)
        shortcut_block = self.shortcut(728, 1024)
        output = residual + shortcut_block(x)
        return self.endconv(output)

(3)构建网络(完整代码)

"""
Copyright (c) 2023, Auorui.
All rights reserved.

Xception: Deep Learning with Depthwise Separable Convolutions
    <https://arxiv.org/pdf/1610.02357.pdf>
"""
import torch
import torch.nn as nn

class SeparableConv2d(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=0, dilation=1, bias=False):
        super(SeparableConv2d, self).__init__()
        self.conv = nn.Conv2d(in_channels, in_channels, kernel_size, stride, padding,
                               dilation, groups=in_channels, bias=bias)
        self.pointwise = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0,
                                   dilation=1, groups=1, bias=False)

    def forward(self, x):
        x = self.conv(x)
        x = self.pointwise(x)
        return x

class EntryFlow(nn.Module):
    def __init__(self):
        super(EntryFlow, self).__init__()

        self.headconv = nn.Sequential(
            nn.Conv2d(3, 32, 3, 2, bias=False),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace=True),

            nn.Conv2d(32, 64, 3, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
        )

        self.residual_block1 = nn.Sequential(
            SeparableConv2d(64, 128, 3, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            SeparableConv2d(128, 128, 3, padding=1),
            nn.BatchNorm2d(128),
            nn.MaxPool2d(3, stride=2, padding=1),
        )

        self.residual_block2 = nn.Sequential(
            nn.ReLU(inplace=True),
            SeparableConv2d(128, 256, 3, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            SeparableConv2d(256, 256, 3, padding=1),
            nn.BatchNorm2d(256),
            nn.MaxPool2d(3, stride=2, padding=1)
        )

        self.residual_block3 = nn.Sequential(
            nn.ReLU(inplace=True),
            SeparableConv2d(256, 728, 3, padding=1),
            nn.BatchNorm2d(728),
            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728),
            nn.MaxPool2d(3, stride=2, padding=1)
        )

    def shortcut(self, inp, oup):
        return nn.Sequential(
            nn.Conv2d(inp, oup, 1, 2, bias=False),
            nn.BatchNorm2d(oup)
            )

    def forward(self, x):
        x = self.headconv(x)
        residual = self.residual_block1(x)
        shortcut_block1 = self.shortcut(64, 128)
        x = residual + shortcut_block1(x)
        residual = self.residual_block2(x)
        shortcut_block2 = self.shortcut(128, 256)
        x = residual + shortcut_block2(x)
        residual = self.residual_block3(x)
        shortcut_block3 = self.shortcut(256, 728)
        x = residual + shortcut_block3(x)

        return x

class MiddleFlow(nn.Module):
    def __init__(self):
        super(MiddleFlow, self).__init__()

        self.shortcut = nn.Sequential()
        self.conv1 = nn.Sequential(
            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728),

            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728),

            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728)
        )

    def forward(self, x):
        residual = self.conv1(x)
        input = self.shortcut(x)
        return input + residual

class ExitFlow(nn.Module):
    def __init__(self):
        super(ExitFlow, self).__init__()

        self.residual_with_exit = nn.Sequential(
            nn.ReLU(inplace=True),
            SeparableConv2d(728, 728, 3, padding=1),
            nn.BatchNorm2d(728),
            nn.ReLU(inplace=True),
            SeparableConv2d(728, 1024, 3, padding=1),
            nn.BatchNorm2d(1024),
            nn.MaxPool2d(3, stride=2, padding=1)
        )

        self.endconv = nn.Sequential(
            SeparableConv2d(1024, 1536, 3, 1, 1),
            nn.BatchNorm2d(1536),
            nn.ReLU(inplace=True),

            SeparableConv2d(1536, 2048, 3, 1, 1),
            nn.BatchNorm2d(2048),
            nn.ReLU(inplace=True),

            nn.AdaptiveAvgPool2d((1, 1)),
        )

    def shortcut(self, inp, oup):
        return nn.Sequential(
            nn.Conv2d(inp, oup, 1, 2, bias=False),
            nn.BatchNorm2d(oup)
        )

    def forward(self, x):
        residual = self.residual_with_exit(x)
        shortcut_block = self.shortcut(728, 1024)
        output = residual + shortcut_block(x)
        return self.endconv(output)


class Xception(nn.Module):
    def __init__(self, num_classes=1000):
        super().__init__()
        self.num_classes = num_classes

        self.entry_flow = EntryFlow()
        self.middle_flow = MiddleFlow()
        self.exit_flow = ExitFlow()
        self.fc = nn.Linear(2048, num_classes)

    def forward(self, x):
        x = self.entry_flow(x)
        for i in range(8):
            x = self.middle_flow(x)
        x = self.exit_flow(x)
        x = x.view(x.size(0), -1)
        out = self.fc(x)

        return out

if __name__=='__main__':
    import torchsummary
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    input = torch.ones(2, 3, 224, 224).to(device)
    net = Xception(num_classes=4)
    net = net.to(device)
    out = net(input)
    print(out)
    print(out.shape)
    torchsummary.summary(net, input_size=(3, 224, 224))
    # Xception Total params: 19,838,076

参考文章

【精读AI论文】Xception ------(Xception: Deep Learning with Depthwise Separable Convolutions)_xception论文-CSDN博客

[ 轻量级网络 ] 经典网络模型4——Xception 详解与复现-CSDN博客

神经网络学习小记录22——Xception模型的复现详解_xception timm-CSDN博客

【卷积神经网络系列】十七、Xception_xception模块-CSDN博客

 

  • 26
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
1.项目代码功能经验证ok,确保稳定可靠运行。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通。 2.主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈! 【资源说明】 基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip 基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip 基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip 基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip 基于DeeplabV3+Xception+Unet实现遥感图像的语义分割python源码+项目说明(毕业设计).zip
训练Xception模型需要以下步骤: 1. 准备数据集:准备训练集和验证集,确保数据集的标签正确,并且尽可能地覆盖各种情况。 2. 定义模型:使用Keras框架定义Xception模型。可以利用预训练的权重来加速训练过程。 3. 编译模型:指定损失函数、优化器和评价指标。 4. 训练模型:使用训练集进行训练,并对验证集进行评估,以避免过拟合。 5. 调整模型:根据验证集的结果进行模型调整,如增加层数、调整学习率等。 6. 保存模型:保存训练好的模型,以便日后使用。 下面是一个简单的Xception模型训练代码示例: ```python from keras.applications.xception import Xception from keras.preprocessing import image from keras.models import Model from keras.layers import Dense, GlobalAveragePooling2D from keras import backend as K # create the base pre-trained model base_model = Xception(weights='imagenet', include_top=False) # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) # add a fully-connected layer x = Dense(1024, activation='relu')(x) # add a logistic layer for the number of classes predictions = Dense(num_classes, activation='softmax')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) # compile the model model.compile(optimizer='rmsprop', loss='categorical_crossentropy') # train the model on the new data for a few epochs model.fit_generator(...) ``` 注意:在训练Xception模型时需要注意内存和计算资源的消耗,可以使用GPU加速训练过程。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏天是冰红茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值