经典卷积模型回顾15—Googlenet实现图像分类(Tensorflow2.0,猫狗分类)

GoogLeNet是Google在2014年提出的一个深度学习模型,也是当时ImageNet图像分类挑战赛(ILSVRC14)的获胜者,比起先前的模型,GoogLeNet在模型深度和模型参数上都有很大的优化,同时也提高了模型的准确率。GoogLeNet拥有22层网络,其中有9个Inception模块。Inception模块采用1x1、3x3和5x5的卷积核来分别捕捉不同尺度的特征,同时使用池化操作,最后将不同尺度的特征图在深度维度上进行拼接。这样可以保证网络既有足够深度来捕捉复杂特征,又避免了参数过多的问题,同时还能保证计算效率。此外,GoogLeNet还引入了辅助分类器(Auxiliary Classifier)来帮助网络更加快速地收敛。辅助分类器是对中间层的输出进行分类,这些分类器的误差也参与整个网络的反向传播,从而更加有效地更新网络参数,使得网络更加容易收敛。

我们将使用“Dogs vs Cats”数据集进行训练和评估。

首先,我们需要导入必要的库:

``` python

import tensorflow as tf

import matplotlib.pyplot as plt

import numpy as np

import os

import random

from tensorflow.keras.preprocessing.image import ImageDataGenerator

```

接下来,我们将定义一些超参数和数据集路径:

``` python

# 超参数

IMG_HEIGHT = 224

IMG_WIDTH = 224

BATCH_SIZE = 32

EPOCHS = 50

NUM_CLASSES = 2

# 数据集路径

train_dir = './dogs-vs-cats/train'

test_dir = './dogs-vs-cats/test1'

```

然后,我们将创建一个数据增强器,并将其应用于训练数据。 这可以帮助我们避免过度拟合,并提高模型的泛化能力。

``` python

# 数据增强器

train_image_generator = ImageDataGenerator(

    rescale=1./255,

    rotation_range=30,

    width_shift_range=0.2,

    height_shift_range=0.2,

    shear_range=0.2,

    zoom_range=0.2,

    horizontal_flip=True,

    fill_mode='nearest'

)

# 训练数据生成器

train_data_gen = train_image_generator.flow_from_directory(

    train_dir,

    target_size=(IMG_HEIGHT, IMG_WIDTH),

    batch_size=BATCH_SIZE,

    class_mode='categorical'

)

```

接下来,我们将创建一个用于测试数据的数据生成器。

``` python

# 测试数据生成器

test_image_generator = ImageDataGenerator(rescale=1./255)

test_data_gen = test_image_generator.flow_from_directory(

    test_dir,

    target_size=(IMG_HEIGHT, IMG_WIDTH),

    batch_size=BATCH_SIZE,

    class_mode='categorical'

)

```

现在,我们将定义一个GoogleNet模型。我们将使用预训练的模型,并在模型的顶部添加一些全连接层和Dropout层。

``` python

# 定义模型

base_model = tf.keras.applications.InceptionV3(include_top=False, weights='imagenet', input_shape=(IMG_HEIGHT, IMG_WIDTH, 3))

x = base_model.output

x = tf.keras.layers.GlobalAveragePooling2D()(x)

x = tf.keras.layers.Dense(512, activation='relu')(x)

x = tf.keras.layers.Dropout(0.5)(x)

predictions = tf.keras.layers.Dense(NUM_CLASSES, activation='softmax')(x)

model = tf.keras.models.Model(inputs=base_model.input, outputs=predictions)

for layer in base_model.layers:

    layer.trainable = False

```

现在,我们将编译模型并开始训练。

``` python

# 编译模型

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# 训练模型

history = model.fit(

    train_data_gen,

    epochs=EPOCHS,

    validation_data=test_data_gen

)

```

最后,我们将绘制模型的训练和验证损失和准确率。

``` python

# 绘制损失和准确率

acc = history.history['accuracy']

val_acc = history.history['val_accuracy']

loss = history.history['loss']

val_loss = history.history['val_loss']

epochs_range = range(EPOCHS)

plt.figure(figsize=(8, 8))

plt.subplot(2, 1, 1)

plt.plot(epochs_range, acc, label='Training Accuracy')

plt.plot(epochs_range, val_acc, label='Validation Accuracy')

plt.legend(loc='lower right')

plt.title('Training and Validation Accuracy')

plt.subplot(2, 1, 2)

plt.plot(epochs_range, loss, label='Training Loss')

plt.plot(epochs_range, val_loss, label='Validation Loss')

plt.legend(loc='upper right')

plt.title('Training and Validation Loss')

plt.show()

```

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

share_data

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

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

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

打赏作者

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

抵扣说明:

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

余额充值