欢迎访问宙启技术站
智能推送

使用Keras.applications.mobilenet模块在Python中进行迁移学习

发布时间:2023-12-27 19:19:23

迁移学习是一种常见的机器学习技术,它使用预训练模型的权重来解决新任务。在深度学习领域,Keras提供了许多预训练模型,包括MobileNet, VGG16, ResNet等。MobileNet是一种轻量级的卷积神经网络,特别适用于移动和嵌入式设备。

在本例中,我们将看到如何使用Keras中的MobileNet模块进行迁移学习,以解决一个新的分类任务。下面是一个使用Keras应用程序进行迁移学习的示例代码:

import tensorflow as tf
from tensorflow.keras.applications.mobilenet import MobileNet
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# 加载预训练的MobileNet模型,不包括顶层
base_model = MobileNet(weights='imagenet', include_top=False)

# 在顶层添加新的分类层
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(num_classes, activation='softmax')(x)

# 创建整个模型
model = Model(inputs=base_model.input, outputs=predictions)

# 冻结预训练模型的权重
for layer in base_model.layers:
    layer.trainable = False

# 编译模型
model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])

# 数据增强
train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

# 从目录中加载训练和测试数据
train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=(224, 224),
    batch_size=batch_size,
    class_mode='categorical')

test_generator = test_datagen.flow_from_directory(
    test_dir,
    target_size=(224, 224),
    batch_size=batch_size,
    class_mode='categorical')

# 训练模型
model.fit(
    train_generator,
    steps_per_epoch=train_generator.n // batch_size,
    epochs=epochs,
    validation_data=test_generator,
    validation_steps=test_generator.n // batch_size)

# 保存训练后的模型
model.save('my_model.h5')

以上代码的详细解释如下:

1. 首先,我们导入所需的模块和函数:tensorflow,MobileNet模块,各种层(如Dense,GlobalAveragePooling2D等),模型和优化器(如Adam)。我们还导入ImageDataGenerator类来进行数据增强。

2. 然后,我们加载预训练的MobileNet模型,通过设置weights='imagenet'来使用在ImageNet上预训练的权重。我们还通过include_top=False排除模型的顶层。

3. 接下来,在模型的顶层上添加新的分类层。我们使用GlobalAveragePooling2D层来对特征图进行平均池化,然后添加全连接层和最终的softmax激活函数层作为预测层。

4. 创建整个模型,使用base_model.input作为输入,predictions作为输出。

5. 接下来,我们冻结预训练模型的权重,通过将其layers的trainable属性设置为False实现。

6. 编译模型,指定优化器(Adam)、损失函数(交叉熵)和评估指标(准确率)。

7. 创建ImageDataGenerator对象来进行数据增强。我们可以通过设置不同的参数来修改增强方式。

8. 使用flow_from_directory函数从目录中加载训练和测试数据,指定目标图片大小、批处理大小和类别方式(categorical)。

9. 最后,我们使用fit函数训练模型,指定训练数据生成器、每个epoch的步数、epoch的数量、验证数据生成器和验证步数。训练完成后,我们可以使用save函数将训练后的模型保存到文件中。

这就是一个使用Keras.applications.mobilenet完成迁移学习的例子。您可以根据自己的特定任务和数据集进行修改和优化。