更复杂的体系结构能保证更好的模型吗?

更复杂的体系结构能保证更好的模型吗?

使用的数据集和数据预处理

[En]

Data sets and data preprocessing used

我们将使用Kaggle的狗与猫数据集。它是根据知识共享许可证授权的,这意味着你可以免费使用它:

更复杂的体系结构能保证更好的模型吗?

该数据集相当大——25000张图像均匀分布在不同的类中(12500张狗图像和12500张猫图像)。它应该足够大,以训练一个像样的图像分类器。

您可以根据上一篇文章创建适当的目录结构,并将其拆分为训练集、测试集和验证集:

[En]

You can create an appropriate directory structure according to the previous article and split it into training sets, test sets, and validation sets:

https://towardsdatascience.com/tensorflow-for-image-classification-top-3-prerequisites-for-deep-learning-projects-34c549c89e42

你还应该删除train/cat/666.jpg和train/dog/11702.jpg图像,这些已经损坏,你的模型将无法使用它们进行训练。

接下来,让我们看看如何使用TensorFlow加载图像。

如何使用TensorFlow加载图像数据

您今天将看到的模型将具有比以前文章中的模型更多的层。

[En]

The model you will see today will have more layers than the model in previous articles.

为了可读性,我们将从TensorFlow中导入单个类。如果你正在跟进,请确保有一个带有GPU的系统,或者至少使用Google Colab。

让我们把图书馆的重要性放在一边:

[En]

Let’s put aside the import of the library:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import warnings
warnings.filterwarnings('ignore')

import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.losses import categorical_crossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import BinaryAccuracy

tf.random.set_seed(42)
physical_devices = tf.config.list_physical_devices('GPU')

try:
    tf.config.experimental.set_memory_growth(physical_devices[0], True)
except:
    pass

这是很多,但模型将因此而显得格外干净。

[En]

This is a lot, but the model will look extra clean because of it.

我们现在将像往常一样加载图像数据——使用ImageDataGenerator类。

我们将把图像矩阵转换为0–1范围,使用用三个颜色通道,将所有图像调整为224×224。出于内存方面的考虑,我们将barch大小降低到32:

train_datagen = ImageDataGenerator(rescale=1/255.0)
valid_datagen = ImageDataGenerator(rescale=1/255.0)

train_data = train_datagen.flow_from_directory(
    directory='data/train/',
    target_size=(224, 224),
    class_mode='categorical',
    batch_size=32,
    shuffle=True,
    seed=42
)

valid_data = valid_datagen.flow_from_directory(
    directory='data/validation/',
    target_size=(224, 224),
    class_mode='categorical',
    batch_size=32,
    seed=42
)

以下是您应该看到的输出:

[En]

Here is the output you should see:

更复杂的体系结构能保证更好的模型吗?

让我们鼓捣第一个模型!

向TensorFlow模型中添加层会有什么不同吗?

从头开始编写卷积模型总是一项棘手的任务。由于卷积模型训练时间长,需要检查的参数太多,所以网格搜索寻找最优结构是不可行的。事实上,你更有可能使用迁移学习。这是我们在不久的将来要讨论的话题。

[En]

Writing convolution models from scratch is always a tricky task. Grid search for the optimal architecture is not feasible because the convolution model takes a long time to train and there are too many parameters to check. In fact, you are more likely to use transfer learning. This is the subject we will discuss in the near future.

今天,这一切都是关于理解为什么在模型体系结构中激进是不值得的。我们用一个简单的模型获得了75%的准确率,所以这是我们必须超越的基线:

[En]

Today, it’s all about understanding why being aggressive in model architecture is not worth it. We got 75% accuracy with a simple model, so this is the baseline we have to go beyond:

https://towardsdatascience.com/tensorflow-for-computer-vision-how-to-train-image-classifier-with-convolutional-neural-networks-77f2fd6ed152

模型1-两个卷积块

我们将宣布第一个模型在某种程度上类似于VGG体系结构——两个卷积层,后面是一个池层。滤波器设置如下,第一个块32个,第二个块64个。

至于损失和优化器,我们将坚持基本原则——分类交叉熵和Adam。数据集中的类是完全平衡的,这意味着我们只需跟踪准确率即可:

model_1 = tf.keras.Sequential([
    Conv2D(filters=32, kernel_size=(3, 3), input_shape=(224, 224, 3), activation='relu'),
    Conv2D(filters=32, kernel_size=(3, 3), activation='relu'),
    MaxPool2D(pool_size=(2, 2), padding='same'),

    Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    MaxPool2D(pool_size=(2, 2), padding='same'),

    Flatten(),
    Dense(units=128, activation='relu'),
    Dense(units=2, activation='softmax')
])

model_1.compile(
    loss=categorical_crossentropy,
    optimizer=Adam(),
    metrics=[BinaryAccuracy(name='accuracy')]
)
model_1_history = model_1.fit(
    train_data,
    validation_data=valid_data,
    epochs=10
)

以下是经过10个epoch后的训练结果:

更复杂的体系结构能保证更好的模型吗?

看起来我们的表现并没有超过基线,因为验证的准确率仍然在75%左右。如果我们再加一块卷积块会怎么样?

[En]

It seems that our performance has not exceeded the baseline, because the accuracy of verification is still around 75%. What happens if we add another convolution block?

模型2-三个卷积块

我们将保持模型体系结构不变,唯一的区别是增加了一个包含128个过滤器的卷积块:

[En]

We will keep the model architecture the same, with the only difference being the addition of a convolution block containing 128 filters:

model_2 = Sequential([
    Conv2D(filters=32, kernel_size=(3, 3), input_shape=(224, 224, 3), activation='relu'),
    Conv2D(filters=32, kernel_size=(3, 3), activation='relu'),
    MaxPool2D(pool_size=(2, 2), padding='same'),

    Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    MaxPool2D(pool_size=(2, 2), padding='same'),

    Conv2D(filters=128, kernel_size=(3, 3), activation='relu'),
    Conv2D(filters=128, kernel_size=(3, 3), activation='relu'),
    MaxPool2D(pool_size=(2, 2), padding='same'),

    Flatten(),
    Dense(units=128, activation='relu'),
    Dense(units=2, activation='softmax')
])

model_2.compile(
    loss=categorical_crossentropy,
    optimizer=Adam(),
    metrics=[BinaryAccuracy(name='accuracy')]
)
model_2_history = model_2.fit(
    train_data,
    validation_data=valid_data,
    epochs=10
)

日志如下:

更复杂的体系结构能保证更好的模型吗?

效果变差了。虽然你可以随意调整batch大小和学习率,但效果可能仍然不行。第一个架构在我们的数据集上工作得更好,所以让我们试着继续调整一下。

模型3-带Dropout的卷积块

第三个模型的架构与第一个模型相同,唯一的区别是增加了一个全连接层和一个Dropout层。让我们看看这是否会有所不同:

model_3 = tf.keras.Sequential([
    Conv2D(filters=32, kernel_size=(3, 3), input_shape=(224, 224, 3), activation='relu'),
    Conv2D(filters=32, kernel_size=(3, 3), activation='relu'),
    MaxPool2D(pool_size=(2, 2), padding='same'),

    Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    MaxPool2D(pool_size=(2, 2), padding='same'),

    Flatten(),
    Dense(units=512, activation='relu'),
    Dropout(rate=0.3),
    Dense(units=128),
    Dense(units=2, activation='softmax')
])

model_3.compile(
    loss=categorical_crossentropy,
    optimizer=Adam(),
    metrics=[BinaryAccuracy(name='accuracy')]
)

model_3_history = model_3.fit(
    train_data,
    validation_data=valid_data,
    epochs=10
)

以下是训练日志:

更复杂的体系结构能保证更好的模型吗?

太可怕了,现在还不到70%!上一篇文章中的简单架构非常好。反而是数据质量问题限制了模型的预测能力。

结论

这证明,更复杂的模型体系结构不一定会产生性能更好的模型。也许你可以找到一种更适合猫狗数据集的架构,但它可能是徒劳的。

[En]

This proves that more complex model architectures do not necessarily produce better-performing models. Maybe you can find an architecture that is more suitable for cat and dog data sets, but it may be futile.

你应该将重点转移到提高数据集质量上。当然,有20K个训练图像,但我们仍然可以增加多样性。这就是数据增强的用武之地。

感谢阅读!

☆ END ☆

如果看到这里,说明你喜欢这篇文章,请转发、点赞。微信搜索「uncle_pn」,欢迎添加小编微信「 woshicver」,每日朋友圈更新一篇高质量博文。

↓ 扫描二维码添加小编↓

更复杂的体系结构能保证更好的模型吗?

Original: https://blog.csdn.net/woshicver/article/details/124240185
Author: woshicver
Title: 更复杂的体系结构能保证更好的模型吗?

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/514192/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球