您当前的位置: 首页 >  分类

寒冰屋

暂无认证

  • 2浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

(二)为AI时尚分类准备数据

寒冰屋 发布时间:2021-07-12 23:10:21 ,浏览量:2

目录

介绍

加载数据集

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

下一步

  • 下载源 - 120.7 MB
介绍

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

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

  • Jupyter Notebook作为IDE
  • 库:
    • TensorFlow 2.0
    • NumPy
    • MatplotLib
  • DeepFashion数据集的自定义子集——相对较小以减少计算和内存开销

我们假设您熟悉深度学习的概念,以及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)

我们现在可以在顺序加载单个数据集的图像时将ImageDataGenerator的next函数用作迭代器。使用next,您将分别将训练图像和标签保存在X_train和y_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以认识人们穿的什么衣服。敬请关注!

https://www.codeproject.com/Articles/5297322/Preparing-Data-for-AI-Fashion-Classification

关注
打赏
1665926880
查看更多评论
立即登录/注册

微信扫码登录

0.2541s