• 卷积神经网络——inception网络及python实现


    1、简介

           Inception 网络是卷积神经网络发展史上一个重要的里程碑。在 Inception 网络出现之前,大部分流行卷积神经网络都是把卷积层堆叠得越来越多,使网络越来越深,来对特征进行多次处理,以此希望能够得到更好的性能。Inception网络对输入图像进行并行采集特征,并将所有输出结果拼接为一个非常深的特征图,由于并行提取特征时卷积核大小不一样,这也就在一定程度上丰富了特征,使特征多样化。

    2、inception网络结构

              

     

           类似上述这种并行采集信号特征的方式,输入一张图片进来,会有1*1、3*3、5*5等不同大小的卷积核进行特征提取。这样,网络中每一层都能学习到“稀疏”(3x3、5x5)或“不稀疏”(1x1)的特征,既增加了网络的宽度,也增加了网络对尺度的适应性。使用1x1的卷积核实现降维操作,以此来减小网络的参数量。

    网络结构CBAPD如下:

           可以看出卷积都是使用全零填充,而激活函数都是 relu,基本都是CPA的结构,没有池化层和丢失层。

    3、python实现

    采用的数据集还是fashion数据集,可以自己去深刻了解。

    主要结构:

    1. class ConvBNRelu(Model):
    2. def __init__(self, ch, kernelsz=3, strides=1, padding='same'):
    3. super(ConvBNRelu, self).__init__()
    4. self.model = tf.keras.models.Sequential([
    5. Conv2D(ch, kernelsz, strides=strides, padding=padding),
    6. BatchNormalization(),
    7. Activation('relu')
    8. ])
    9. def call(self, x):
    10. x = self.model(x, training=False)
    11. return x
    12. class InceptionBlk(Model):
    13. def __init__(self, ch, strides=1):
    14. super(InceptionBlk, self).__init__()
    15. self.ch = ch
    16. self.strides = strides
    17. self.c1 = ConvBNRelu(ch, kernelsz=1, strides=strides)
    18. self.c2_1 = ConvBNRelu(ch, kernelsz=1, strides=strides)
    19. self.c2_2 = ConvBNRelu(ch, kernelsz=3, strides=1)
    20. self.c3_1 = ConvBNRelu(ch, kernelsz=1, strides=strides)
    21. self.c3_2 = ConvBNRelu(ch, kernelsz=5, strides=1)
    22. self.p4_1 = MaxPool2D(3, strides=1, padding='same')
    23. self.c4_2 = ConvBNRelu(ch, kernelsz=1, strides=strides)
    24. def call(self, x):
    25. x1 = self.c1(x)
    26. x2_1 = self.c2_1(x)
    27. x2_2 = self.c2_2(x2_1)
    28. x3_1 = self.c3_1(x)
    29. x3_2 = self.c3_2(x3_1)
    30. x4_1 = self.p4_1(x)
    31. x4_2 = self.c4_2(x4_1)
    32. # concat along axis=channel
    33. x = tf.concat([x1, x2_2, x3_2, x4_2], axis=3)
    34. return x
    35. class Inception(Model):
    36. def __init__(self, num_blocks, num_classes, init_ch=16, **kwargs):
    37. super(Inception, self).__init__(**kwargs)
    38. self.in_channels = init_ch
    39. self.out_channels = init_ch
    40. self.num_blocks = num_blocks
    41. self.init_ch = init_ch
    42. self.c1 = ConvBNRelu(init_ch)
    43. self.blocks = tf.keras.models.Sequential()
    44. for block_id in range(num_blocks):
    45. for layer_id in range(2):
    46. if layer_id == 0:
    47. block = InceptionBlk(self.out_channels, strides=2)
    48. else:
    49. block = InceptionBlk(self.out_channels, strides=1)
    50. self.blocks.add(block)
    51. # enlarger out_channels per block
    52. self.out_channels *= 2
    53. self.p1 = GlobalAveragePooling2D()
    54. self.f1 = Dense(num_classes, activation='softmax')
    55. def call(self, x):
    56. x = self.c1(x)
    57. x = self.blocks(x)
    58. x = self.p1(x)
    59. y = self.f1(x)
    60. return y
    61. model = Inception(num_blocks=2, num_classes=10)

    添加库和读取数据集 :

    1. import tensorflow as tf
    2. import os
    3. import numpy as np
    4. from matplotlib import pyplot as plt
    5. from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Dropout, Flatten, Dense, \
    6. GlobalAveragePooling2D
    7. from tensorflow.keras import Model
    8. np.set_printoptions(threshold=np.inf)
    9. fashion = tf.keras.datasets.fashion_mnist
    10. (x_train, y_train), (x_test, y_test) = fashion.load_data()
    11. x_train, x_test = x_train / 255.0, x_test / 255.0
    12. x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
    13. x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

    迭代求解并绘图:

    1. model.compile(optimizer='adam',
    2. loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
    3. metrics=['sparse_categorical_accuracy'])
    4. cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
    5. save_weights_only=True,
    6. save_best_only=True)
    7. history = model.fit(x_train, y_train, batch_size=128, epochs=20, validation_data=(x_test, y_test), validation_freq=1,
    8. callbacks=[cp_callback])
    9. model.summary()
    10. acc = history.history['sparse_categorical_accuracy']
    11. val_acc = history.history['val_sparse_categorical_accuracy']
    12. loss = history.history['loss']
    13. val_loss = history.history['val_loss']
    14. plt.subplot(1, 2, 1)
    15. plt.plot(acc, label='Training Accuracy')
    16. plt.plot(val_acc, label='Validation Accuracy')
    17. plt.title('Training and Validation Accuracy')
    18. plt.legend()
    19. plt.subplot(1, 2, 2)
    20. plt.plot(loss, label='Training Loss')
    21. plt.plot(val_loss, label='Validation Loss')
    22. plt.title('Training and Validation Loss')
    23. plt.legend()
    24. plt.show()

    结果如下图:

     

  • 相关阅读:
    代码随想录算法训练营 动态规划part15
    使用 L293D 电机驱动器 IC 和 Arduino 控制直流电机
    Python Web3.0应用开发【2022】
    后端非法字符检验的注解
    Golang 数组和切片
    【软件测试】软件测试的基础概念
    JavaWeb的基本概念
    MySql数据库优化查询工具
    SQL SELECT DISTINCT(选择不同) 语法
    【Try to Hack】vulhub靶场搭建
  • 原文地址:https://blog.csdn.net/abc1234abcdefg/article/details/125504377