@toc
参考论文:Xception: Deep Learning with Depthwise Separable Convolutions作者:Franc ̧ois Chollet(深度学习框架Keras的作者)
这篇是Inception架构系列的极致版本
1、Xception简介
Xception是对Inception-V3的另一种改进版本,这两种网络的参数量都差不多。
1.1 初始假设
Inception 模块背后的想法是通过明确地将其分解为一系列独立查看跨通道相关性和空间相关性的操作,从而使该过程更容易和更有效。更准确地说,典型的 Inception 模块首先通过一组 1x1 卷积查看跨通道相关性,将输入数据映射到小于原始输入空间的 3 或 4 个独立空间,然后将所有相关性映射到这些较小的 3D 空间中,通过常规的 3x3 或 5x5 卷积。
图 1. 经典Inception 模块(Inception V3)。
简化的Inception模块
考虑一个简化版本的 Inception 模块,它只使用一种大小的卷积(例如 3x3)并且不包括平均池化(图 2)。
这个 Inception 模块可以重新构造为一个大的 1x1 卷积,然后是空间卷积,该卷积将在输出通道的非重叠段上运行(图 3)。
每个3*3
卷积分别处理不同通道,通道互不重合。共享同一个
1*1
卷积产生的feature
Inception 模块的“极端”版本将首先使用 1x1 卷积来映射跨通道相关性,然后分别映射每个输出通道的空间相关性。如图 4 所示。我们注意到 Inception 模块的这种极端形式几乎与深度可分离卷积相同,这是一种已在神经网络中使用的操作
极限版本:每个3*3
卷积单独处理一个通道。
深度可分离卷积,在 TensorFlow 和 Keras 等深度学习框架中通常称为“可分离卷积”,由深度卷积组成,即在输入的每个通道上独立执行的空间卷积,然后是逐点卷积,即 1x1 卷积,将深度卷积输出的通道投影到新的通道空间上。不要将其与空间可分离卷积混淆,后者在图像处理社区中也通常称为“可分离卷积”。
1.2 Xception与Depthwise separable convolution的细微差别
- 顺序不同:通常实现的深度可分离卷积(例如在 TensorFlow 中)首先执行通道空间卷积,然后执行 1x1 卷积,而 Inception 先执行 1x1 卷积。
- 第一次操作后是否存在非线性:在 Inception 中,这两个操作后面都有一个 ReLU 非线性,然而深度可分离卷积通常在没有非线性的情况下实现。
1.3 Xception架构
图 5. Xception 架构:数据首先通过入口流,然后通过重复八次的中间流,最后通过出口流。请注意,所有 Convolution 和 SeparableConvolution 层之后都进行了批量归一化 [7](未包含在图中)。所有 SeparableConvolution 层都使用深度乘数 1(无深度扩展)。
1.4 Xception与Inception-V3的比较
1.4.1 分类性能
所有评估均使用单一裁剪的输入图像和单一模型运行。 ImageNet 结果报告在验证集而不是测试集上(即来自 ILSVRC 2012 验证集的非黑名单图像)。 JFT 结果是在 3000 万次迭代(训练一个月)后而不是在完全收敛后报告的。结果在表 1 和表 2 以及图 6、图 7、图 8 中提供。在 JFT 上,我们测试了不包含任何全连接层的网络版本和包含两个全连接层的版本逻辑回归层之前的每层 4096 个单元。
在 ImageNet 上,Xception 的结果略好于 Inception V3。在 JFT 上,Xception 在 FastEval14k MAP@100 指标上显示出 4.3% 的相对改进。我们还注意到 Xception 优于 He 等人报告的 ImageNet 结果。对于 ResNet-50、ResNet-101 和 ResNet-152
表 1. ImageNet 上的分类性能比较(单crop,单模型)。 VGG-16 和 ResNet-152 数字仅作为提醒。被基准测试的 Inception V3 版本不包括辅助头。
表 2. JFT(单crop,单模型)的分类性能比较。图 6. ImageNet 上的训练结果
Xception收敛稍微快一点。图 7. JFT 上的训练配置文件,没有全连接层
图 8. JFT 上的训练配置文件,具有全连接层
1.4.2 尺寸和速度
在 ImageNet 上报告参数计数(1000 个类,无全连接层),在 ImageNet 上报告每秒训练步数(梯度更新),其中 60 个 K80 GPU 运行同步梯度下降。两种架构的大小大致相同(在 3.5% 以内),并且 Xception 稍微慢一些。我们预计深度卷积操作级别的工程优化可以在不久的将来使 Xception 比 Inception V3 更快。两种架构具有几乎相同数量的参数这一事实表明,在 ImageNet 和 JFT 上看到的改进并非来自增加的容量,而是来自更有效地使用模型参数。
1.5 残差连接的影响
图 9. 有和没有残差连接的训练情况。
结果如图 9 所示。在速度和最终分类性能方面,残差连接显然对于帮助收敛至关重要。然而,我们会注意到,使用与残差模型相同的优化配置对非残差模型进行基准测试可能是不公平的,并且更好的优化配置可能会产生更具竞争力的结果。
1.6 pointwise convolutions后中间激活的效果
图 10. 在可分离卷积层的depthwise和pointwise操作之间具有不同激活的训练配置文件。
图 10 在 ImageNet 上报告了结果,表明没有任何非线性会导致更快的收敛和更好的最终性能
1.7 Future directions
我们之前注意到在常规卷积和深度可分离卷积之间存在离散谱,由用于执行空间卷积的独立通道空间段的数量参数化。 Inception 模块是这个范围内的一个点。我们在实证评估中表明,Inception 模块的极端公式化深度可分离卷积可能比常规的 Inception 模块具有优势。然而,没有理由相信深度可分离卷积是最优的。可能是频谱上的中间点,位于常规 Inception 模块和深度可分离卷积之间,具有更多优势。这个问题留待以后研究。
1.8 Conclusion
我们展示了卷积和深度可分离卷积如何位于离散谱的两个极端,而 Inception 模块是介于两者之间的中间点。这一观察导致我们提议在神经计算机视觉架构中用深度可分离卷积替换 Inception 模块。我们基于这个想法提出了一种新颖的架构,名为 Xception,它的参数计数与 Inception V3 相似。与 Inception V3 相比,Xception 在 ImageNet 数据集上的分类性能略有提升,而在 JFT 数据集上则有较大的提升。我们预计深度可分离卷积将成为未来卷积神经网络架构设计的基石,因为它们提供与 Inception 模块相似的属性,但与常规卷积层一样易于使用。
2、Xception架构代码复现
2.1 数据集介绍
还是使用keras中的预训练模型来对自定义的数据集进行图像分类,共17个类别,文件夹的名称就是图片的标签。
数据集是放在远程的GPU服务器上的,这个模型太大,我本地机器跑不动。
2.2 代码复现
导入依赖
import tensorflow as tf
from tensorflow.keras.applications import Xception
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPool2D, Flatten, Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt
from tensorflow.keras.callbacks import LearningRateScheduler
超参数
# 类别数
num_classes = 17
# 批次大小
batch_size = 32
# 周期数
epochs = 100
# 图片大小
image_size = 224
数据增强
# 训练集数据进行数据增强
train_datagen = ImageDataGenerator(
rotation_range=20, # 随机旋转度数
width_shift_range=0.1, # 随机水平平移
height_shift_range=0.1, # 随机竖直平移
rescale=1 / 255, # 数据归一化
shear_range=10, # 随机错切变换
zoom_range=0.1, # 随机放大
horizontal_flip=True, # 水平翻转
brightness_range=(0.7, 1.3), # 亮度变化
fill_mode='nearest', # 填充方式
)
# 测试集数据只需要归一化就可以
test_datagen = ImageDataGenerator(
rescale=1 / 255, # 数据归一化
)
数据生成器
# 训练集数据生成器,可以在训练时自动产生数据进行训练
# 从'data/train'获得训练集数据
# 获得数据后会把图片resize为image_size×image_size的大小
# generator每次会产生batch_size个数据
train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(image_size, image_size),
batch_size=batch_size,
)
# 测试集数据生成器
test_generator = test_datagen.flow_from_directory(
'data/test',
target_size=(image_size, image_size),
batch_size=batch_size,
)
训练集1088张图片,验证机272张图片
Xception模型结构:去掉最后的全连接层
xception=Xception(
weights='imagenet',
include_top=False, # 不包含最后的全连接层
input_shape=(image_size, image_size, 3)
)
xception.summary()
Model: "xception"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 224, 224, 3) 0
__________________________________________________________________________________________________
block1_conv1 (Conv2D) (None, 111, 111, 32) 864 input_1[0][0]
__________________________________________________________________________________________________
block1_conv1_bn (BatchNormaliza (None, 111, 111, 32) 128 block1_conv1[0][0]
__________________________________________________________________________________________________
block1_conv1_act (Activation) (None, 111, 111, 32) 0 block1_conv1_bn[0][0]
__________________________________________________________________________________________________
block1_conv2 (Conv2D) (None, 109, 109, 64) 18432 block1_conv1_act[0][0]
__________________________________________________________________________________________________
block1_conv2_bn (BatchNormaliza (None, 109, 109, 64) 256 block1_conv2[0][0]
__________________________________________________________________________________________________
block1_conv2_act (Activation) (None, 109, 109, 64) 0 block1_conv2_bn[0][0]
__________________________________________________________________________________________________
block2_sepconv1 (SeparableConv2 (None, 109, 109, 128 8768 block1_conv2_act[0][0]
__________________________________________________________________________________________________
block2_sepconv1_bn (BatchNormal (None, 109, 109, 128 512 block2_sepconv1[0][0]
__________________________________________________________________________________________________
block2_sepconv2_act (Activation (None, 109, 109, 128 0 block2_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block2_sepconv2 (SeparableConv2 (None, 109, 109, 128 17536 block2_sepconv2_act[0][0]
__________________________________________________________________________________________________
block2_sepconv2_bn (BatchNormal (None, 109, 109, 128 512 block2_sepconv2[0][0]
__________________________________________________________________________________________________
conv2d (Conv2D) (None, 55, 55, 128) 8192 block1_conv2_act[0][0]
__________________________________________________________________________________________________
block2_pool (MaxPooling2D) (None, 55, 55, 128) 0 block2_sepconv2_bn[0][0]
__________________________________________________________________________________________________
batch_normalization (BatchNorma (None, 55, 55, 128) 512 conv2d[0][0]
__________________________________________________________________________________________________
add (Add) (None, 55, 55, 128) 0 block2_pool[0][0]
batch_normalization[0][0]
__________________________________________________________________________________________________
block3_sepconv1_act (Activation (None, 55, 55, 128) 0 add[0][0]
__________________________________________________________________________________________________
block3_sepconv1 (SeparableConv2 (None, 55, 55, 256) 33920 block3_sepconv1_act[0][0]
__________________________________________________________________________________________________
block3_sepconv1_bn (BatchNormal (None, 55, 55, 256) 1024 block3_sepconv1[0][0]
__________________________________________________________________________________________________
block3_sepconv2_act (Activation (None, 55, 55, 256) 0 block3_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block3_sepconv2 (SeparableConv2 (None, 55, 55, 256) 67840 block3_sepconv2_act[0][0]
__________________________________________________________________________________________________
block3_sepconv2_bn (BatchNormal (None, 55, 55, 256) 1024 block3_sepconv2[0][0]
__________________________________________________________________________________________________
conv2d_1 (Conv2D) (None, 28, 28, 256) 32768 add[0][0]
__________________________________________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0 block3_sepconv2_bn[0][0]
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, 28, 28, 256) 1024 conv2d_1[0][0]
__________________________________________________________________________________________________
add_1 (Add) (None, 28, 28, 256) 0 block3_pool[0][0]
batch_normalization_1[0][0]
__________________________________________________________________________________________________
block4_sepconv1_act (Activation (None, 28, 28, 256) 0 add_1[0][0]
__________________________________________________________________________________________________
block4_sepconv1 (SeparableConv2 (None, 28, 28, 728) 188672 block4_sepconv1_act[0][0]
__________________________________________________________________________________________________
block4_sepconv1_bn (BatchNormal (None, 28, 28, 728) 2912 block4_sepconv1[0][0]
__________________________________________________________________________________________________
block4_sepconv2_act (Activation (None, 28, 28, 728) 0 block4_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block4_sepconv2 (SeparableConv2 (None, 28, 28, 728) 536536 block4_sepconv2_act[0][0]
__________________________________________________________________________________________________
block4_sepconv2_bn (BatchNormal (None, 28, 28, 728) 2912 block4_sepconv2[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D) (None, 14, 14, 728) 186368 add_1[0][0]
__________________________________________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 728) 0 block4_sepconv2_bn[0][0]
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, 14, 14, 728) 2912 conv2d_2[0][0]
__________________________________________________________________________________________________
add_2 (Add) (None, 14, 14, 728) 0 block4_pool[0][0]
batch_normalization_2[0][0]
__________________________________________________________________________________________________
block5_sepconv1_act (Activation (None, 14, 14, 728) 0 add_2[0][0]
__________________________________________________________________________________________________
block5_sepconv1 (SeparableConv2 (None, 14, 14, 728) 536536 block5_sepconv1_act[0][0]
__________________________________________________________________________________________________
block5_sepconv1_bn (BatchNormal (None, 14, 14, 728) 2912 block5_sepconv1[0][0]
__________________________________________________________________________________________________
block5_sepconv2_act (Activation (None, 14, 14, 728) 0 block5_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block5_sepconv2 (SeparableConv2 (None, 14, 14, 728) 536536 block5_sepconv2_act[0][0]
__________________________________________________________________________________________________
block5_sepconv2_bn (BatchNormal (None, 14, 14, 728) 2912 block5_sepconv2[0][0]
__________________________________________________________________________________________________
block5_sepconv3_act (Activation (None, 14, 14, 728) 0 block5_sepconv2_bn[0][0]
__________________________________________________________________________________________________
block5_sepconv3 (SeparableConv2 (None, 14, 14, 728) 536536 block5_sepconv3_act[0][0]
__________________________________________________________________________________________________
block5_sepconv3_bn (BatchNormal (None, 14, 14, 728) 2912 block5_sepconv3[0][0]
__________________________________________________________________________________________________
add_3 (Add) (None, 14, 14, 728) 0 block5_sepconv3_bn[0][0]
add_2[0][0]
__________________________________________________________________________________________________
block6_sepconv1_act (Activation (None, 14, 14, 728) 0 add_3[0][0]
__________________________________________________________________________________________________
block6_sepconv1 (SeparableConv2 (None, 14, 14, 728) 536536 block6_sepconv1_act[0][0]
__________________________________________________________________________________________________
block6_sepconv1_bn (BatchNormal (None, 14, 14, 728) 2912 block6_sepconv1[0][0]
__________________________________________________________________________________________________
block6_sepconv2_act (Activation (None, 14, 14, 728) 0 block6_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block6_sepconv2 (SeparableConv2 (None, 14, 14, 728) 536536 block6_sepconv2_act[0][0]
__________________________________________________________________________________________________
block6_sepconv2_bn (BatchNormal (None, 14, 14, 728) 2912 block6_sepconv2[0][0]
__________________________________________________________________________________________________
block6_sepconv3_act (Activation (None, 14, 14, 728) 0 block6_sepconv2_bn[0][0]
__________________________________________________________________________________________________
block6_sepconv3 (SeparableConv2 (None, 14, 14, 728) 536536 block6_sepconv3_act[0][0]
__________________________________________________________________________________________________
block6_sepconv3_bn (BatchNormal (None, 14, 14, 728) 2912 block6_sepconv3[0][0]
__________________________________________________________________________________________________
add_4 (Add) (None, 14, 14, 728) 0 block6_sepconv3_bn[0][0]
add_3[0][0]
__________________________________________________________________________________________________
block7_sepconv1_act (Activation (None, 14, 14, 728) 0 add_4[0][0]
__________________________________________________________________________________________________
block7_sepconv1 (SeparableConv2 (None, 14, 14, 728) 536536 block7_sepconv1_act[0][0]
__________________________________________________________________________________________________
block7_sepconv1_bn (BatchNormal (None, 14, 14, 728) 2912 block7_sepconv1[0][0]
__________________________________________________________________________________________________
block7_sepconv2_act (Activation (None, 14, 14, 728) 0 block7_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block7_sepconv2 (SeparableConv2 (None, 14, 14, 728) 536536 block7_sepconv2_act[0][0]
__________________________________________________________________________________________________
block7_sepconv2_bn (BatchNormal (None, 14, 14, 728) 2912 block7_sepconv2[0][0]
__________________________________________________________________________________________________
block7_sepconv3_act (Activation (None, 14, 14, 728) 0 block7_sepconv2_bn[0][0]
__________________________________________________________________________________________________
block7_sepconv3 (SeparableConv2 (None, 14, 14, 728) 536536 block7_sepconv3_act[0][0]
__________________________________________________________________________________________________
block7_sepconv3_bn (BatchNormal (None, 14, 14, 728) 2912 block7_sepconv3[0][0]
__________________________________________________________________________________________________
add_5 (Add) (None, 14, 14, 728) 0 block7_sepconv3_bn[0][0]
add_4[0][0]
__________________________________________________________________________________________________
block8_sepconv1_act (Activation (None, 14, 14, 728) 0 add_5[0][0]
__________________________________________________________________________________________________
block8_sepconv1 (SeparableConv2 (None, 14, 14, 728) 536536 block8_sepconv1_act[0][0]
__________________________________________________________________________________________________
block8_sepconv1_bn (BatchNormal (None, 14, 14, 728) 2912 block8_sepconv1[0][0]
__________________________________________________________________________________________________
block8_sepconv2_act (Activation (None, 14, 14, 728) 0 block8_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block8_sepconv2 (SeparableConv2 (None, 14, 14, 728) 536536 block8_sepconv2_act[0][0]
__________________________________________________________________________________________________
block8_sepconv2_bn (BatchNormal (None, 14, 14, 728) 2912 block8_sepconv2[0][0]
__________________________________________________________________________________________________
block8_sepconv3_act (Activation (None, 14, 14, 728) 0 block8_sepconv2_bn[0][0]
__________________________________________________________________________________________________
block8_sepconv3 (SeparableConv2 (None, 14, 14, 728) 536536 block8_sepconv3_act[0][0]
__________________________________________________________________________________________________
block8_sepconv3_bn (BatchNormal (None, 14, 14, 728) 2912 block8_sepconv3[0][0]
__________________________________________________________________________________________________
add_6 (Add) (None, 14, 14, 728) 0 block8_sepconv3_bn[0][0]
add_5[0][0]
__________________________________________________________________________________________________
block9_sepconv1_act (Activation (None, 14, 14, 728) 0 add_6[0][0]
__________________________________________________________________________________________________
block9_sepconv1 (SeparableConv2 (None, 14, 14, 728) 536536 block9_sepconv1_act[0][0]
__________________________________________________________________________________________________
block9_sepconv1_bn (BatchNormal (None, 14, 14, 728) 2912 block9_sepconv1[0][0]
__________________________________________________________________________________________________
block9_sepconv2_act (Activation (None, 14, 14, 728) 0 block9_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block9_sepconv2 (SeparableConv2 (None, 14, 14, 728) 536536 block9_sepconv2_act[0][0]
__________________________________________________________________________________________________
block9_sepconv2_bn (BatchNormal (None, 14, 14, 728) 2912 block9_sepconv2[0][0]
__________________________________________________________________________________________________
block9_sepconv3_act (Activation (None, 14, 14, 728) 0 block9_sepconv2_bn[0][0]
__________________________________________________________________________________________________
block9_sepconv3 (SeparableConv2 (None, 14, 14, 728) 536536 block9_sepconv3_act[0][0]
__________________________________________________________________________________________________
block9_sepconv3_bn (BatchNormal (None, 14, 14, 728) 2912 block9_sepconv3[0][0]
__________________________________________________________________________________________________
add_7 (Add) (None, 14, 14, 728) 0 block9_sepconv3_bn[0][0]
add_6[0][0]
__________________________________________________________________________________________________
block10_sepconv1_act (Activatio (None, 14, 14, 728) 0 add_7[0][0]
__________________________________________________________________________________________________
block10_sepconv1 (SeparableConv (None, 14, 14, 728) 536536 block10_sepconv1_act[0][0]
__________________________________________________________________________________________________
block10_sepconv1_bn (BatchNorma (None, 14, 14, 728) 2912 block10_sepconv1[0][0]
__________________________________________________________________________________________________
block10_sepconv2_act (Activatio (None, 14, 14, 728) 0 block10_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block10_sepconv2 (SeparableConv (None, 14, 14, 728) 536536 block10_sepconv2_act[0][0]
__________________________________________________________________________________________________
block10_sepconv2_bn (BatchNorma (None, 14, 14, 728) 2912 block10_sepconv2[0][0]
__________________________________________________________________________________________________
block10_sepconv3_act (Activatio (None, 14, 14, 728) 0 block10_sepconv2_bn[0][0]
__________________________________________________________________________________________________
block10_sepconv3 (SeparableConv (None, 14, 14, 728) 536536 block10_sepconv3_act[0][0]
__________________________________________________________________________________________________
block10_sepconv3_bn (BatchNorma (None, 14, 14, 728) 2912 block10_sepconv3[0][0]
__________________________________________________________________________________________________
add_8 (Add) (None, 14, 14, 728) 0 block10_sepconv3_bn[0][0]
add_7[0][0]
__________________________________________________________________________________________________
block11_sepconv1_act (Activatio (None, 14, 14, 728) 0 add_8[0][0]
__________________________________________________________________________________________________
block11_sepconv1 (SeparableConv (None, 14, 14, 728) 536536 block11_sepconv1_act[0][0]
__________________________________________________________________________________________________
block11_sepconv1_bn (BatchNorma (None, 14, 14, 728) 2912 block11_sepconv1[0][0]
__________________________________________________________________________________________________
block11_sepconv2_act (Activatio (None, 14, 14, 728) 0 block11_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block11_sepconv2 (SeparableConv (None, 14, 14, 728) 536536 block11_sepconv2_act[0][0]
__________________________________________________________________________________________________
block11_sepconv2_bn (BatchNorma (None, 14, 14, 728) 2912 block11_sepconv2[0][0]
__________________________________________________________________________________________________
block11_sepconv3_act (Activatio (None, 14, 14, 728) 0 block11_sepconv2_bn[0][0]
__________________________________________________________________________________________________
block11_sepconv3 (SeparableConv (None, 14, 14, 728) 536536 block11_sepconv3_act[0][0]
__________________________________________________________________________________________________
block11_sepconv3_bn (BatchNorma (None, 14, 14, 728) 2912 block11_sepconv3[0][0]
__________________________________________________________________________________________________
add_9 (Add) (None, 14, 14, 728) 0 block11_sepconv3_bn[0][0]
add_8[0][0]
__________________________________________________________________________________________________
block12_sepconv1_act (Activatio (None, 14, 14, 728) 0 add_9[0][0]
__________________________________________________________________________________________________
block12_sepconv1 (SeparableConv (None, 14, 14, 728) 536536 block12_sepconv1_act[0][0]
__________________________________________________________________________________________________
block12_sepconv1_bn (BatchNorma (None, 14, 14, 728) 2912 block12_sepconv1[0][0]
__________________________________________________________________________________________________
block12_sepconv2_act (Activatio (None, 14, 14, 728) 0 block12_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block12_sepconv2 (SeparableConv (None, 14, 14, 728) 536536 block12_sepconv2_act[0][0]
__________________________________________________________________________________________________
block12_sepconv2_bn (BatchNorma (None, 14, 14, 728) 2912 block12_sepconv2[0][0]
__________________________________________________________________________________________________
block12_sepconv3_act (Activatio (None, 14, 14, 728) 0 block12_sepconv2_bn[0][0]
__________________________________________________________________________________________________
block12_sepconv3 (SeparableConv (None, 14, 14, 728) 536536 block12_sepconv3_act[0][0]
__________________________________________________________________________________________________
block12_sepconv3_bn (BatchNorma (None, 14, 14, 728) 2912 block12_sepconv3[0][0]
__________________________________________________________________________________________________
add_10 (Add) (None, 14, 14, 728) 0 block12_sepconv3_bn[0][0]
add_9[0][0]
__________________________________________________________________________________________________
block13_sepconv1_act (Activatio (None, 14, 14, 728) 0 add_10[0][0]
__________________________________________________________________________________________________
block13_sepconv1 (SeparableConv (None, 14, 14, 728) 536536 block13_sepconv1_act[0][0]
__________________________________________________________________________________________________
block13_sepconv1_bn (BatchNorma (None, 14, 14, 728) 2912 block13_sepconv1[0][0]
__________________________________________________________________________________________________
block13_sepconv2_act (Activatio (None, 14, 14, 728) 0 block13_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block13_sepconv2 (SeparableConv (None, 14, 14, 1024) 752024 block13_sepconv2_act[0][0]
__________________________________________________________________________________________________
block13_sepconv2_bn (BatchNorma (None, 14, 14, 1024) 4096 block13_sepconv2[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D) (None, 7, 7, 1024) 745472 add_10[0][0]
__________________________________________________________________________________________________
block13_pool (MaxPooling2D) (None, 7, 7, 1024) 0 block13_sepconv2_bn[0][0]
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, 7, 7, 1024) 4096 conv2d_3[0][0]
__________________________________________________________________________________________________
add_11 (Add) (None, 7, 7, 1024) 0 block13_pool[0][0]
batch_normalization_3[0][0]
__________________________________________________________________________________________________
block14_sepconv1 (SeparableConv (None, 7, 7, 1536) 1582080 add_11[0][0]
__________________________________________________________________________________________________
block14_sepconv1_bn (BatchNorma (None, 7, 7, 1536) 6144 block14_sepconv1[0][0]
__________________________________________________________________________________________________
block14_sepconv1_act (Activatio (None, 7, 7, 1536) 0 block14_sepconv1_bn[0][0]
__________________________________________________________________________________________________
block14_sepconv2 (SeparableConv (None, 7, 7, 2048) 3159552 block14_sepconv1_act[0][0]
__________________________________________________________________________________________________
block14_sepconv2_bn (BatchNorma (None, 7, 7, 2048) 8192 block14_sepconv2[0][0]
__________________________________________________________________________________________________
block14_sepconv2_act (Activatio (None, 7, 7, 2048) 0 block14_sepconv2_bn[0][0]
==================================================================================================
Total params: 20,861,480
Trainable params: 20,806,952
Non-trainable params: 54,528
model=Sequential([
xception,
Flatten(),
Dense(num_classes,activation='softmax')
])
模型摘要
model.summary()
# 学习率调节函数,逐渐减小学习率
def adjust_learning_rate(epoch):
# 前30周期
if epoch<=30:
lr = 1e-4
# 前30到70周期
elif 30 < epoch <= 70:
lr = 1e-5
# 70到100周期
else:
lr = 1e-6
return lr
# 定义优化器
adam = Adam(lr=1e-4)
# 定义学习率衰减策略
callbacks = []
callbacks.append(LearningRateScheduler(adjust_learning_rate))
编译和训练
# 定义优化器,loss function,训练过程中计算准确率
model.compile(optimizer=adam,loss='categorical_crossentropy',metrics=['accuracy'])
# Tensorflow2.1版本(包括2.1)之后可以直接使用fit训练模型
history = model.fit(x=train_generator,epochs=epochs,validation_data=test_generator,callbacks=callbacks)
Epoch 1/100
2/34 [>.............................] - ETA: 9s - loss: 2.8702 - accuracy: 0.0938WARNING:tensorflow:Callbacks method `on_train_batch_end` is slow compared to the batch time (batch time: 0.1483s vs `on_train_batch_end` time: 0.4162s). Check your callbacks.
34/34 [==============================] - 22s 642ms/step - loss: 1.2750 - accuracy: 0.6241 - val_loss: 0.6603 - val_accuracy: 0.7831
Epoch 2/100
34/34 [==============================] - 21s 615ms/step - loss: 0.1918 - accuracy: 0.9458 - val_loss: 0.4002 - val_accuracy: 0.8713
Epoch 3/100
34/34 [==============================] - 21s 628ms/step - loss: 0.0848 - accuracy: 0.9761 - val_loss: 0.2029 - val_accuracy: 0.9522
Epoch 4/100
34/34 [==============================] - 22s 633ms/step - loss: 0.0730 - accuracy: 0.9779 - val_loss: 0.1151 - val_accuracy: 0.9816
Epoch 5/100
34/34 [==============================] - 22s 633ms/step - loss: 0.0250 - accuracy: 0.9963 - val_loss: 0.1372 - val_accuracy: 0.9816
Epoch 6/100
34/34 [==============================] - 22s 640ms/step - loss: 0.0092 - accuracy: 0.9991 - val_loss: 0.1238 - val_accuracy: 0.9816
Epoch 7/100
34/34 [==============================] - 22s 636ms/step - loss: 0.0173 - accuracy: 0.9945 - val_loss: 0.1286 - val_accuracy: 0.9632
Epoch 8/100
34/34 [==============================] - 22s 642ms/step - loss: 0.0114 - accuracy: 0.9972 - val_loss: 0.1030 - val_accuracy: 0.9816
Epoch 9/100
34/34 [==============================] - 22s 644ms/step - loss: 0.0086 - accuracy: 0.9972 - val_loss: 0.1208 - val_accuracy: 0.9743
Epoch 10/100
34/34 [==============================] - 22s 648ms/step - loss: 0.0067 - accuracy: 0.9982 - val_loss: 0.1116 - val_accuracy: 0.9779
Epoch 11/100
34/34 [==============================] - 22s 638ms/step - loss: 0.0044 - accuracy: 1.0000 - val_loss: 0.0921 - val_accuracy: 0.9779
Epoch 12/100
34/34 [==============================] - 22s 645ms/step - loss: 0.0057 - accuracy: 0.9982 - val_loss: 0.1118 - val_accuracy: 0.9743
Epoch 13/100
34/34 [==============================] - 22s 648ms/step - loss: 0.0054 - accuracy: 0.9972 - val_loss: 0.1116 - val_accuracy: 0.9779
Epoch 14/100
34/34 [==============================] - 22s 638ms/step - loss: 0.0033 - accuracy: 1.0000 - val_loss: 0.1332 - val_accuracy: 0.9743
Epoch 15/100
34/34 [==============================] - 22s 640ms/step - loss: 0.0015 - accuracy: 1.0000 - val_loss: 0.1473 - val_accuracy: 0.9706
Epoch 16/100
34/34 [==============================] - 22s 638ms/step - loss: 0.0015 - accuracy: 1.0000 - val_loss: 0.1481 - val_accuracy: 0.9669
Epoch 17/100
34/34 [==============================] - 22s 638ms/step - loss: 0.0058 - accuracy: 0.9982 - val_loss: 0.1170 - val_accuracy: 0.9779
Epoch 18/100
34/34 [==============================] - 22s 639ms/step - loss: 0.0299 - accuracy: 0.9917 - val_loss: 0.1581 - val_accuracy: 0.9706
Epoch 19/100
34/34 [==============================] - 21s 632ms/step - loss: 0.0273 - accuracy: 0.9936 - val_loss: 0.1736 - val_accuracy: 0.9669
Epoch 20/100
34/34 [==============================] - 22s 650ms/step - loss: 0.0107 - accuracy: 0.9982 - val_loss: 0.1256 - val_accuracy: 0.9743
Epoch 21/100
34/34 [==============================] - 22s 649ms/step - loss: 0.0048 - accuracy: 0.9982 - val_loss: 0.1396 - val_accuracy: 0.9706
Epoch 22/100
34/34 [==============================] - 22s 639ms/step - loss: 0.0060 - accuracy: 0.9991 - val_loss: 0.1023 - val_accuracy: 0.9779
Epoch 23/100
34/34 [==============================] - 22s 641ms/step - loss: 0.0049 - accuracy: 0.9982 - val_loss: 0.0968 - val_accuracy: 0.9816
Epoch 24/100
34/34 [==============================] - 22s 638ms/step - loss: 0.0034 - accuracy: 0.9991 - val_loss: 0.1411 - val_accuracy: 0.9669
Epoch 25/100
34/34 [==============================] - 22s 640ms/step - loss: 0.0085 - accuracy: 0.9982 - val_loss: 0.3162 - val_accuracy: 0.9449
Epoch 26/100
34/34 [==============================] - 22s 639ms/step - loss: 0.0158 - accuracy: 0.9936 - val_loss: 0.1003 - val_accuracy: 0.9743
Epoch 27/100
34/34 [==============================] - 22s 635ms/step - loss: 0.0201 - accuracy: 0.9972 - val_loss: 0.1178 - val_accuracy: 0.9706
Epoch 28/100
34/34 [==============================] - 22s 635ms/step - loss: 0.0180 - accuracy: 0.9963 - val_loss: 0.0871 - val_accuracy: 0.9816
Epoch 29/100
34/34 [==============================] - 22s 635ms/step - loss: 0.0169 - accuracy: 0.9972 - val_loss: 0.0729 - val_accuracy: 0.9890
Epoch 30/100
34/34 [==============================] - 22s 637ms/step - loss: 0.0139 - accuracy: 0.9972 - val_loss: 0.1288 - val_accuracy: 0.9779
Epoch 31/100
34/34 [==============================] - 22s 647ms/step - loss: 0.0079 - accuracy: 0.9963 - val_loss: 0.1144 - val_accuracy: 0.9706
Epoch 32/100
34/34 [==============================] - 22s 639ms/step - loss: 0.0089 - accuracy: 0.9991 - val_loss: 0.0900 - val_accuracy: 0.9816
Epoch 33/100
34/34 [==============================] - 22s 638ms/step - loss: 0.0032 - accuracy: 1.0000 - val_loss: 0.0846 - val_accuracy: 0.9853
Epoch 34/100
34/34 [==============================] - 22s 637ms/step - loss: 0.0050 - accuracy: 0.9972 - val_loss: 0.0813 - val_accuracy: 0.9853
Epoch 35/100
34/34 [==============================] - 22s 636ms/step - loss: 0.0024 - accuracy: 0.9991 - val_loss: 0.0806 - val_accuracy: 0.9853
Epoch 36/100
34/34 [==============================] - 22s 640ms/step - loss: 0.0105 - accuracy: 0.9982 - val_loss: 0.0777 - val_accuracy: 0.9853
Epoch 37/100
34/34 [==============================] - 22s 637ms/step - loss: 0.0011 - accuracy: 1.0000 - val_loss: 0.0769 - val_accuracy: 0.9890
Epoch 38/100
34/34 [==============================] - 22s 635ms/step - loss: 0.0018 - accuracy: 0.9991 - val_loss: 0.0747 - val_accuracy: 0.9890
Epoch 39/100
34/34 [==============================] - 22s 635ms/step - loss: 9.8177e-04 - accuracy: 1.0000 - val_loss: 0.0740 - val_accuracy: 0.9890
Epoch 40/100
34/34 [==============================] - 22s 640ms/step - loss: 5.4439e-04 - accuracy: 1.0000 - val_loss: 0.0747 - val_accuracy: 0.9890
Epoch 41/100
34/34 [==============================] - 22s 635ms/step - loss: 0.0028 - accuracy: 0.9991 - val_loss: 0.0764 - val_accuracy: 0.9890
Epoch 42/100
34/34 [==============================] - 22s 644ms/step - loss: 0.0025 - accuracy: 0.9991 - val_loss: 0.0756 - val_accuracy: 0.9890
Epoch 43/100
34/34 [==============================] - 22s 643ms/step - loss: 0.0014 - accuracy: 0.9991 - val_loss: 0.0771 - val_accuracy: 0.9890
Epoch 44/100
34/34 [==============================] - 22s 641ms/step - loss: 4.9184e-04 - accuracy: 1.0000 - val_loss: 0.0776 - val_accuracy: 0.9890
Epoch 45/100
34/34 [==============================] - 22s 646ms/step - loss: 0.0167 - accuracy: 0.9972 - val_loss: 0.0755 - val_accuracy: 0.9853
Epoch 46/100
34/34 [==============================] - 22s 647ms/step - loss: 4.7047e-04 - accuracy: 1.0000 - val_loss: 0.0750 - val_accuracy: 0.9853
Epoch 47/100
34/34 [==============================] - 22s 644ms/step - loss: 4.7222e-04 - accuracy: 1.0000 - val_loss: 0.0750 - val_accuracy: 0.9853
Epoch 48/100
34/34 [==============================] - 22s 642ms/step - loss: 3.7026e-04 - accuracy: 1.0000 - val_loss: 0.0754 - val_accuracy: 0.9853
Epoch 49/100
34/34 [==============================] - 22s 635ms/step - loss: 0.0018 - accuracy: 0.9991 - val_loss: 0.0730 - val_accuracy: 0.9890
Epoch 50/100
34/34 [==============================] - 22s 639ms/step - loss: 4.7187e-04 - accuracy: 1.0000 - val_loss: 0.0720 - val_accuracy: 0.9890
Epoch 51/100
34/34 [==============================] - 22s 643ms/step - loss: 7.1332e-04 - accuracy: 1.0000 - val_loss: 0.0727 - val_accuracy: 0.9890
Epoch 52/100
34/34 [==============================] - 22s 642ms/step - loss: 0.0014 - accuracy: 1.0000 - val_loss: 0.0737 - val_accuracy: 0.9890
Epoch 53/100
34/34 [==============================] - 22s 635ms/step - loss: 9.9908e-04 - accuracy: 1.0000 - val_loss: 0.0739 - val_accuracy: 0.9890
Epoch 54/100
34/34 [==============================] - 22s 635ms/step - loss: 0.0019 - accuracy: 0.9991 - val_loss: 0.0697 - val_accuracy: 0.9890
Epoch 55/100
34/34 [==============================] - 22s 652ms/step - loss: 4.7767e-04 - accuracy: 1.0000 - val_loss: 0.0693 - val_accuracy: 0.9890
Epoch 56/100
34/34 [==============================] - 22s 637ms/step - loss: 2.8078e-04 - accuracy: 1.0000 - val_loss: 0.0699 - val_accuracy: 0.9890
Epoch 57/100
34/34 [==============================] - 22s 641ms/step - loss: 4.7606e-04 - accuracy: 1.0000 - val_loss: 0.0699 - val_accuracy: 0.9890
Epoch 58/100
34/34 [==============================] - 22s 655ms/step - loss: 3.5135e-04 - accuracy: 1.0000 - val_loss: 0.0690 - val_accuracy: 0.9890
Epoch 59/100
34/34 [==============================] - 22s 641ms/step - loss: 0.0011 - accuracy: 1.0000 - val_loss: 0.0691 - val_accuracy: 0.9890
Epoch 60/100
34/34 [==============================] - 22s 635ms/step - loss: 0.0035 - accuracy: 0.9991 - val_loss: 0.0706 - val_accuracy: 0.9890
Epoch 61/100
34/34 [==============================] - 22s 634ms/step - loss: 7.1807e-04 - accuracy: 1.0000 - val_loss: 0.0705 - val_accuracy: 0.9890
Epoch 62/100
34/34 [==============================] - 22s 638ms/step - loss: 0.0031 - accuracy: 0.9991 - val_loss: 0.0712 - val_accuracy: 0.9890
Epoch 63/100
34/34 [==============================] - 22s 640ms/step - loss: 2.8924e-04 - accuracy: 1.0000 - val_loss: 0.0721 - val_accuracy: 0.9890
Epoch 64/100
34/34 [==============================] - 22s 641ms/step - loss: 5.8139e-04 - accuracy: 1.0000 - val_loss: 0.0728 - val_accuracy: 0.9890
Epoch 65/100
34/34 [==============================] - 22s 636ms/step - loss: 5.1540e-04 - accuracy: 1.0000 - val_loss: 0.0740 - val_accuracy: 0.9890
Epoch 66/100
34/34 [==============================] - 22s 637ms/step - loss: 2.2913e-04 - accuracy: 1.0000 - val_loss: 0.0746 - val_accuracy: 0.9890
Epoch 67/100
34/34 [==============================] - 22s 645ms/step - loss: 1.8229e-04 - accuracy: 1.0000 - val_loss: 0.0742 - val_accuracy: 0.9853
Epoch 68/100
34/34 [==============================] - 22s 633ms/step - loss: 2.7320e-04 - accuracy: 1.0000 - val_loss: 0.0745 - val_accuracy: 0.9853
Epoch 69/100
34/34 [==============================] - 22s 635ms/step - loss: 3.1285e-04 - accuracy: 1.0000 - val_loss: 0.0734 - val_accuracy: 0.9853
Epoch 70/100
34/34 [==============================] - 22s 639ms/step - loss: 4.5094e-04 - accuracy: 1.0000 - val_loss: 0.0730 - val_accuracy: 0.9890
Epoch 71/100
34/34 [==============================] - 22s 643ms/step - loss: 1.2878e-04 - accuracy: 1.0000 - val_loss: 0.0734 - val_accuracy: 0.9890
Epoch 72/100
34/34 [==============================] - 22s 637ms/step - loss: 6.7639e-04 - accuracy: 1.0000 - val_loss: 0.0736 - val_accuracy: 0.9890
Epoch 73/100
34/34 [==============================] - 22s 647ms/step - loss: 2.2660e-04 - accuracy: 1.0000 - val_loss: 0.0736 - val_accuracy: 0.9890
Epoch 74/100
34/34 [==============================] - 22s 639ms/step - loss: 2.2052e-04 - accuracy: 1.0000 - val_loss: 0.0740 - val_accuracy: 0.9890
Epoch 75/100
34/34 [==============================] - 22s 638ms/step - loss: 6.0579e-04 - accuracy: 1.0000 - val_loss: 0.0742 - val_accuracy: 0.9890
Epoch 76/100
34/34 [==============================] - 22s 637ms/step - loss: 1.6572e-04 - accuracy: 1.0000 - val_loss: 0.0743 - val_accuracy: 0.9890
Epoch 77/100
34/34 [==============================] - 22s 647ms/step - loss: 3.1470e-04 - accuracy: 1.0000 - val_loss: 0.0741 - val_accuracy: 0.9890
Epoch 78/100
34/34 [==============================] - 22s 640ms/step - loss: 0.0012 - accuracy: 0.9991 - val_loss: 0.0746 - val_accuracy: 0.9890
Epoch 79/100
34/34 [==============================] - 22s 641ms/step - loss: 0.0013 - accuracy: 1.0000 - val_loss: 0.0749 - val_accuracy: 0.9853
Epoch 80/100
34/34 [==============================] - 22s 642ms/step - loss: 4.3973e-04 - accuracy: 1.0000 - val_loss: 0.0748 - val_accuracy: 0.9890
Epoch 81/100
34/34 [==============================] - 22s 638ms/step - loss: 5.4159e-04 - accuracy: 1.0000 - val_loss: 0.0749 - val_accuracy: 0.9890
Epoch 82/100
34/34 [==============================] - 22s 641ms/step - loss: 2.7500e-04 - accuracy: 1.0000 - val_loss: 0.0756 - val_accuracy: 0.9853
Epoch 83/100
34/34 [==============================] - 22s 639ms/step - loss: 1.3783e-04 - accuracy: 1.0000 - val_loss: 0.0752 - val_accuracy: 0.9853
Epoch 84/100
34/34 [==============================] - 22s 640ms/step - loss: 3.9947e-04 - accuracy: 1.0000 - val_loss: 0.0755 - val_accuracy: 0.9890
Epoch 85/100
34/34 [==============================] - 22s 646ms/step - loss: 4.1835e-04 - accuracy: 1.0000 - val_loss: 0.0756 - val_accuracy: 0.9890
Epoch 86/100
34/34 [==============================] - 22s 645ms/step - loss: 3.7472e-04 - accuracy: 1.0000 - val_loss: 0.0750 - val_accuracy: 0.9890
Epoch 87/100
34/34 [==============================] - 22s 645ms/step - loss: 7.5369e-04 - accuracy: 1.0000 - val_loss: 0.0752 - val_accuracy: 0.9890
Epoch 88/100
34/34 [==============================] - 22s 637ms/step - loss: 2.6409e-04 - accuracy: 1.0000 - val_loss: 0.0749 - val_accuracy: 0.9890
Epoch 89/100
34/34 [==============================] - 22s 643ms/step - loss: 7.0757e-04 - accuracy: 1.0000 - val_loss: 0.0745 - val_accuracy: 0.9890
Epoch 90/100
34/34 [==============================] - 22s 633ms/step - loss: 2.5368e-04 - accuracy: 1.0000 - val_loss: 0.0751 - val_accuracy: 0.9890
Epoch 91/100
34/34 [==============================] - 22s 633ms/step - loss: 4.6264e-04 - accuracy: 1.0000 - val_loss: 0.0752 - val_accuracy: 0.9890
Epoch 92/100
34/34 [==============================] - 22s 636ms/step - loss: 1.3873e-04 - accuracy: 1.0000 - val_loss: 0.0749 - val_accuracy: 0.9890
Epoch 93/100
34/34 [==============================] - 22s 643ms/step - loss: 1.5283e-04 - accuracy: 1.0000 - val_loss: 0.0748 - val_accuracy: 0.9890
Epoch 94/100
34/34 [==============================] - 22s 644ms/step - loss: 1.9203e-04 - accuracy: 1.0000 - val_loss: 0.0752 - val_accuracy: 0.9890
Epoch 95/100
34/34 [==============================] - 22s 644ms/step - loss: 0.0014 - accuracy: 0.9991 - val_loss: 0.0749 - val_accuracy: 0.9890
Epoch 96/100
34/34 [==============================] - 22s 639ms/step - loss: 0.0013 - accuracy: 0.9991 - val_loss: 0.0743 - val_accuracy: 0.9890
Epoch 97/100
34/34 [==============================] - 22s 640ms/step - loss: 4.8190e-04 - accuracy: 1.0000 - val_loss: 0.0740 - val_accuracy: 0.9890
Epoch 98/100
34/34 [==============================] - 22s 645ms/step - loss: 1.4175e-04 - accuracy: 1.0000 - val_loss: 0.0738 - val_accuracy: 0.9890
Epoch 99/100
34/34 [==============================] - 22s 654ms/step - loss: 6.7065e-04 - accuracy: 1.0000 - val_loss: 0.0735 - val_accuracy: 0.9890
Epoch 100/100
34/34 [==============================] - 22s 642ms/step - loss: 2.3137e-04 - accuracy: 1.0000 - val_loss: 0.0736 - val_accuracy: 0.9890
这个模型的表现怎么比ResNet-50还要好,第一个epoch验证集准确率就出奇的高,可能因为这个本来就是针对ImageNet数据集调参的吧。模型表现的太好了,可能是我们的数据集太小了。
可视化acc和loss
# 显示训练集和验证集的acc和loss曲线
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.subplot(1, 2, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()
保存
#模型保存
model.save('model/Xception.h5')
重新加载
#模型载入
from tensorflow.keras.models import load_model
model1=load_model('model/Xception.h5')