65.9K
CodeProject 正在变化。 阅读更多。
Home

为 AI 时尚分类准备数据

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3投票s)

2021年3月16日

CPOL

3分钟阅读

viewsIcon

10513

在本文中,我们将向您展示如何使用迁移学习来微调 VGG19 模型,以对时尚服装类别进行分类。

引言

DeepFashion 等数据集的可用性为时尚产业开辟了新的可能性。 在本系列文章中,我们将展示一个 AI 驱动的 深度学习系统,该系统可以通过帮助我们更好地了解客户的需求来彻底改变时尚设计产业。

在这个项目中,我们将使用

我们假设您熟悉深度学习的概念,以及 Jupyter Notebooks 和 TensorFlow。 如果您是 Jupyter Notebooks 的新手,请从本教程开始。 欢迎您下载项目代码

前一篇文章中,我们讨论了要使用的数据子集,并提出了问题。 在本文中,我们将应用迁移学习到 VGG16 深度网络,以对来自 DeepFashion 数据集中的图像中的衣服进行分类。 我们将微调 VGG16 预训练模型,以适应将衣服分类为 15 个不同类别的任务。 该网络将在包含 9,935 张图像的子集上进行训练。

加载数据集

在这个项目中,我们将使用 TensorFlow 和 Keras 来微调 VGG16,因为 Keras 提供了易于使用的工具来加载数据、加载预训练模型和进行微调。 ImageDataGenerator 工具将帮助我们加载、规范化、调整大小和重新缩放数据。

首先,让我们导入我们需要的库

import os
import matplotlib.pyplot as plt
import matplotlib.image as img
import tensorflow.keras as keras
import numpy as np
import tensorflow as tf
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

现在我们有了基础知识,让我们导入 ImageDataGenerator,添加我们的数据目录,然后开始加载训练和验证数据。 验证数据仅占我们训练数据的 10%:我们只需要验证数据来微调训练期间 VGG16 的超参数。 我们在这部分代码中设置了批量大小。 您可以设置其值以适合您的机器功能和内存。

datasetdir = r'C:\Users\myuser\Desktop\\DeepFashion\Train'
os.chdir(datasetdir)
from tensorflow.keras.preprocessing.image import ImageDataGenerator
batch_size = 3

def DataLoad(shape, preprocessing): 
    '''Create the training and validation datasets for 
    a given image shape.
    '''
    imgdatagen = ImageDataGenerator(
        preprocessing_function = preprocessing,
        horizontal_flip = True, 
        validation_split = 0.1,
    )

    height, width = shape

    train_dataset = imgdatagen.flow_from_directory(
        os.getcwd(),
        target_size = (height, width), 
        classes = ['Blazer', 'Blouse', 'Cardigan', 'Dress', 'Jacket',
                 'Jeans', 'Jumpsuit', 'Romper', 'Shorts', 'Skirts', 'Sweater', 'Sweatpants', 'Tank', 'Tee', 'Top'],
        batch_size = batch_size,
        subset = 'training', 
    )

    val_dataset = imgdatagen.flow_from_directory(
        os.getcwd(),
        target_size = (height, width), 
        classes = ['Blazer', 'Blouse', 'Cardigan', 'Dress', 'Jacket',
                 'Jeans', 'Jumpsuit', 'Romper', 'Shorts', 'Skirts', 'Sweater', 'Sweatpants', 'Tank', 'Tee', 'Top'],
        batch_size = batch_size,
        subset = 'validation'
    )
    return train_dataset, val_dataset

现在设置了 DalaLoad 函数,让我们使用它来提取我们的训练和验证数据,并将图像大小调整为适合我们预训练模型的形状:224 x 224 x 3。

train_dataset, val_dataset = DataLoad((224,224), preprocessing=vgg16.preprocess_input)

现在,当顺序加载单个数据集的图像时,我们可以使用 ImageDataGeneratornext 函数作为迭代器。 使用 next,您将分别将训练图像和标签保存在 X_trainy_train 参数中。 您可以将相同的功能应用于验证和测试数据。

# Function for plots images with labels within jupyter notebook
X_train, y_train = next(train_dataset)

如您所见,我们有 7,656 张图像作为训练数据,属于 15 个不同的类别,以及 842 张验证图像。

从 Keras 加载预训练模型 (VGG16)

是时候从 Keras 加载 VGG16 网络并显示其基线了

vgg16 = keras.applications.vgg16
conv_model = vgg16.VGG16(weights='imagenet', include_top=False)
conv_model.summary()

接下来,我们加载该网络的 ImageNet 权重,以便我们可以在迁移学习期间使用它们。

conv_model = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=(224,224,3))

加载了网络基线及其相应的权重后,让我们开始重构 VGG16 以对 15 个不同的服装类别进行分类。 为此,我们将添加一个展平层、三个 100 个节点的密集层、一个代表 15 个服装类别的 15 层密集层,以及一个 Softmax 层来显示类(类别)概率。

# flatten the output of the convolutional part: 
x = keras.layers.Flatten()(conv_model.output)
# three hidden layers
x = keras.layers.Dense(100, activation='relu')(x)
x = keras.layers.Dense(100, activation='relu')(x)
x = keras.layers.Dense(100, activation='relu')(x)
# final softmax layer with 15 categories
predictions = keras.layers.Dense(15, activation='softmax')(x)

# creating the full model:
full_model = keras.models.Model(inputs=conv_model.input, outputs=predictions)
full_model.summary()

后续步骤

下一篇文章中,我们将向您展示如何训练 VGG19 以识别人们的穿着。 敬请关注!

© . All rights reserved.