八、Inception V1的网络结构代码实现

2021/12/5 6:18:22

本文主要是介绍八、Inception V1的网络结构代码实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • 前文
  • 数据生成器+数据部分展示
  • Inception V1
  • Inception V1模型编译与拟合
  • GitHub下载地址:

前文

  • 一、Windows系统下安装Tensorflow2.x(2.6)
  • 二、深度学习-读取数据
  • 三、Tensorflow图像处理预算
  • 四、线性回归模型的tensorflow实现
  • 五、深度学习-逻辑回归模型
  • 六、AlexNet实现中文字体识别——隶书和行楷
  • 七、VGG16实现鸟类数据库分类
  • 七、VGG16+BN(Batch Normalization)实现鸟类数据库分类
  • 七、BatchNormalization使用技巧
  • 七、Data Augmentation技巧

数据生成器+数据部分展示

# 读取数据
from keras.preprocessing.image import ImageDataGenerator

IMSIZE = 224
train_generator = ImageDataGenerator(rescale=1. / 255).flow_from_directory('../../data/data_inception/train',
                                                                           target_size=(IMSIZE, IMSIZE),
                                                                           batch_size=100,
                                                                           class_mode='categorical'
                                                                           )

validation_generator = ImageDataGenerator(rescale=1. / 255).flow_from_directory('../../data/data_inception/test',
                                                                                target_size=(IMSIZE, IMSIZE),
                                                                                batch_size=100,
                                                                                class_mode='categorical')

在这里插入图片描述

# 展示数据
from matplotlib import pyplot as plt

plt.figure()
fig, ax = plt.subplots(2, 5)
fig.set_figheight(7)
fig.set_figwidth(15)
ax = ax.flatten()
X, Y = next(train_generator)
for i in range(10): ax[i].imshow(X[i, :, :,: ])

在这里插入图片描述

Inception V1

#相比于之前,这里需要导入concatenate函数
from keras.layers import Conv2D, BatchNormalization, MaxPooling2D
from keras.layers import Flatten, Dropout, Dense, Input, concatenate
from keras import Model

input_layer = Input([IMSIZE, IMSIZE, 3])
x = input_layer
x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', activation='relu')(x)
x = BatchNormalization(axis=3)(x)
x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)

x = Conv2D(192, (3, 3), strides=(1, 1), padding='same', activation='relu')(x)
x = BatchNormalization(axis=3)(x)  #para=4*192=768
x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)
x

在这里插入图片描述

for i in range(9):
    brach1x1 = Conv2D(64, (1, 1), strides=(1, 1), padding='same', activation='relu')(x)
    brach1x1 = BatchNormalization(axis=3)(brach1x1)
    brach3x3 = Conv2D(96, (1, 1), strides=(1, 1), padding='same', activation='relu')(x)
    brach3x3 = BatchNormalization(axis=3)(brach3x3)

    brach3x3 = Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu')(brach3x3)
    brach3x3 = BatchNormalization(axis=3)(brach3x3)

    brach5x5 = Conv2D(16, (1, 1), strides=(1, 1), padding='same', activation='relu')(x)
    brach5x5 = BatchNormalization(axis=3)(brach5x5)

    brach5x5 = Conv2D(32, (3, 3), strides=(1, 1), padding='same', activation='relu')(brach5x5)
    brach5x5 = BatchNormalization(axis=3)(brach5x5)

    branchpool = MaxPooling2D(pool_size=(3, 3), strides=(1, 1), padding='same')(x)
    branchpool = Conv2D(32, (1, 1), strides=(1, 1), padding='same', activation='relu')(branchpool)
    branchpool = BatchNormalization(axis=3)(branchpool)
    x = concatenate([brach1x1, brach3x3, brach5x5, branchpool], axis=3)
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)

x = Dropout(0.4)(x)
x = Flatten()(x)
x = Dense(17, activation='softmax')(x)
output_layer = x
model = Model(input_layer, output_layer)
model.summary()

在这里插入图片描述

Inception V1模型编译与拟合

#运行
from keras.optimizers import Adam

model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy'])
model.fit_generator(train_generator, epochs=20, validation_data=validation_generator)

在这里插入图片描述

GitHub下载地址:

Tensorflow1.15深度学习



这篇关于八、Inception V1的网络结构代码实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程