博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在TensorFlow中通过深度学习构建年龄和性别的多任务预测器
阅读量:2521 次
发布时间:2019-05-11

本文共 7570 字,大约阅读时间需要 25 分钟。

by Cole Murray

通过科尔·默里(Cole Murray)

, you learned about how to combine a convolutional neural network and Long short-term memory (LTSM) to create captions given an image. In this tutorial, you’ll learn how to build and train a multi-task machine learning model to predict the age and gender of a subject in an image.

,您学习了如何结合卷积神经网络和长短期记忆(LTSM)来创建给定图像的字幕。 在本教程中,您将学习如何构建和训练多任务机器学习模型,以预测图像中对象的年龄和性别。

总览 (Overview)

  • Introduction to age and gender model

    年龄和性别模型介绍
  • Building a Multi-task Tensorflow Estimator

    构建多任务Tensorflow估算器
  • Training

    训练

先决条件 (Prerequisites)

  • basic understanding of convolutional neural networks (CNN)

    卷积神经网络(CNN)的基本理解
  • basic understanding of TensorFlow

    对TensorFlow的基本了解
  • GPU (optional)

    GPU(可选)

年龄和性别模型介绍 (Introduction to Age and Gender Model)

In 2015, researchers from Computer Vision Lab, D-ITET, published a paper and made public their consisting of 500K+ face images with age and gender labels.

2015年,D-ITET计算机视觉实验室的研究人员发表了一篇论文 ,并公开了其由500K +带有年龄和性别标签的面部图像组成。

DEX outlines an neural network architecture involving a pretrained imagenet vgg16 model that estimates the apparent age in face images. DEX placed first in — a competition that deals with recognizing people in an image — outperforming human reference.

DEX概述了一种神经网络架构,其中涉及一个预训练的imagenet vgg16模型,该模型可估计人脸图像中的表观年龄。 DEX在中第一,该竞赛旨在识别图像中的人物,其表现优于人类参考。

年龄作为分类问题 (Age as a classification problem)

A conventional way of tackling an age estimation problem with an image as input would be using a regression-based model with mean-squared error as the loss function. DEX models this problem as a classification task, using a softmax classifier with each age represented as a unique class ranging from 1 to 101 and cross-entropy as the loss function.

用图像作为输入来解决年龄估计问题的常规方法是使用均方误差作为损失函数的基于回归的模型。 DEX使用softmax分类器将该问题建模为分类任务,每个年龄代表一个唯一的类,范围从1到101,交叉熵作为损失函数。

多任务学习 (Multi-task learning)

Multi-task learning is a technique of training on multiple tasks through a shared architecture. Layers at the beginning of the network will learn a joint generalized representation, preventing overfitting to a specific task that may contain noise.

多任务学习是一种通过共享体系结构对多个任务进行训练的技术。 网络开始处的各层将学习联合的通用表示形式,从而防止过度拟合可能包含噪声的特定任务。

By training with a multi-task network, the network can be trained in parallel on both tasks. This reduces the infrastructure complexity to only one training pipeline. Additionally, the computation required for training is reduced as both tasks are trained simultaneously.

通过使用多任务网络进行训练,可以同时在两个任务上对网络进行训练。 这将基础架构的复杂性降低到仅一个培训管道。 此外,由于同时训练两个任务,因此减少了训练所需的计算量。

在TensorFlow中构建多任务网络 (Building a multi-task network in TensorFlow)

Below you’ll use TensorFlow’s estimator abstraction to create the model. The model will be trained from raw image input to predict the age and gender of the face image.

在下面,您将使用TensorFlow的估算器抽象来创建模型。 该模型将从原始图像输入中进行训练,以预测面部图像的年龄和性别。

项目结构 (Project Structure)

.├── Dockerfile├── age_gender_estimation_tutorial│   ├── cnn_estimator.py│   ├── cnn_model.py│   └── dataset.py├── bin│   ├── download-imdb.sh│   ├── predict.py│   ├── preprocess_imdb.py│   └── train.py├── requirements.txt

环境 (Environment)

For the environment, you’ll use to install dependencies. A GPU version is also provided for convenience.

对于环境,您将使用安装依赖项。 为了方便起见,还提供了GPU版本。

docker build -t colemurray/age-gender-estimation-tutorial -f Dockerfile .

数据 (Data)

To train this model, you’ll use the IMDB-WIKI dataset, consisting of 500K+ images. For simplicity, you’ll download the pre-cropped imdb images (7GB). Run the script below to download the data.

要训​​练该模型,您将使用IMDB-WIKI数据集,该数据集包含500K +图像。 为简单起见,您将下载预裁剪的imdb映像(7GB)。 运行以下脚本以下载数据。

chmod +x bin/download-imdb-crop.sh
./bin/download-imdb-crop.sh

Preprocessing

前处理

You’ll now process the dataset to clean out low-quality images and crop the input to a fixed image size. Additionally, you’ll format the data as a CSV to simplify reading into TensorFlow.

现在,您将处理数据集以清除低质量的图像并将输入裁剪为固定的图像大小。 此外,您将数据格式化为CSV格式,以简化读取TensorFlow的过程。

docker run -v $PWD:/opt/app \-e PYTHONPATH=$PYTHONPATH:/opt/app \-it colemurray/age-gender-estimation-tutorial \python3 /opt/app/bin/preprocess_imdb.py \--db-path /opt/app/data/imdb_crop/imdb.mat \--photo-dir /opt/app/data/imdb_crop \--output-dir /opt/app/var \--min-score 1.0 \--img-size 224

After approximately 20 minutes, you’ll have a processed dataset.

大约20分钟后,您将获得一个已处理的数据集。

Next, you’ll use TensorFlow’s data pipeline module to provide data to the estimator. Tf.data is an abstraction to read and manipulate a dataset in parallel, utilizing C++ threads for performance.

接下来,您将使用TensorFlow的数据管道模块将数据提供给估算器。 Tf.data是使用C ++线程来提高性能的并行读取和操作数据集的抽象。

Here, you’ll utilize TensorFlow’s CSV Reader to parse the data, preprocess the images, create batches, and shuffle.

在这里,您将利用TensorFlow的CSV阅读器来解析数据,预处理图像,创建批处理和随机播放。

模型 (Model)

Below, you’ll create a basic CNN model. The model consists of three convolutions and two fully connected layers, with a softmax classifier head for each task.

在下面,您将创建一个基本的CNN模型。 该模型由三个卷积和两个完全连接的层组成,每个任务都有一个softmax分类器头。

关节损失功能 (Joint loss function)

For the training operation, you’ll use the Adam Optimizer. For a loss function, you’ll average the cross-entropy error of each head, creating a shared loss function between the heads.

对于训练操作,您将使用Adam Optimizer。 对于损耗函数,您将平均每个磁头的交叉熵误差,从而在磁头之间创建共享的损耗函数。

TensorFlow估算器 (TensorFlow estimator)

TensorFlow estimators provide a simple abstraction for graph creation and runtime processing. TensorFlow has specified an interface model_fn, that can be used to create custom estimators.

TensorFlow估算器为图形创建和运行时处理提供了一个简单的抽象。 TensorFlow已指定接口model_fn ,可用于创建自定义估算器。

Below, you’ll take the network created above and create training, eval, and predict. These specifications will be used by TensorFlow’s estimator class to alter the behavior of the graph.

在下面,您将使用上面创建的网络,并进行训练,评估和预测。 TensorFlow的estimator类将使用这些规范来更改图形的行为。

培养 (Train)

Now that you’ve preprocessed the data and created the model architecture and data pipeline, you’ll begin training the model.

现在,您已经预处理了数据并创建了模型架构和数据管道,您将开始训练模型。

docker run -v $PWD:/opt/app \-e PYTHONPATH=$PYTHONPATH:/opt/app \-it colemurray/age-gender-estimation-tutorial:gpu \python3 /opt/app/bin/train.py \--img-dir /opt/app/var/crop \--train-csv /opt/app/var/train.csv \--val-csv /opt/app/var/val.csv \--model-dir /opt/app/var/cnn-model \--img-size 224 \--num-steps 200000

预测 (Predict)

Below, you’ll load your age and gender TensorFlow model. The model will be loaded from disk and predict on the provided image.

在下面,您将加载年龄和性别的TensorFlow模型。 该模型将从磁盘加载并在提供的映像上进行预测。

# Update the model path below with your modeldocker run -v $PWD:/opt/app \-e PYTHONPATH=$PYTHONPATH:/opt/app \-it colemurray/age-gender-estimation-tutorial \python3 /opt/app/bin/predict.py \--image-path /opt/app/var/crop/25/nm0000325_rm2755562752_1956-1-7_2002.jpg \--model-dir /opt/app/var/cnn-model-3/serving/

结论 (Conclusion)

In this tutorial, you learned how to build and train a multi-task network for predicting a subject’s age and image. By using a shared architecture, both targets can be trained and predicted simultaneously.

在本教程中,您学习了如何构建和训练多任务网络以预测对象的年龄和图像。 通过使用共享的体系结构,可以同时训练和预测两个目标。

Next Steps:

下一步:

  • Evaluate on Your Own Dataset

    评估自己的数据集
  • Try a different network architecture

    尝试不同的网络架构
  • Experiment with Different Hyperparameters

    实验不同的超参数

Questions/issues? Open an issue

有疑问/问题吗? 打开一个问题

Complete code .

完成代码。

呼吁采取行动 (Call to Action)

If you enjoyed this tutorial, follow and recommend!

如果您喜欢本教程,请遵循并推荐!

Interested in learning more about Deep Learning / Machine Learning? Check out my other tutorials:

有兴趣了解有关深度学习/机器学习的更多信息吗? 查看我的其他教程:

Other places you can find me:

您可以找到我的其他地方:

翻译自:

转载地址:http://ddewd.baihongyu.com/

你可能感兴趣的文章
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
正则表达式的搜索和替换
查看>>
个人项目:WC
查看>>
地鼠的困境SSL1333 最大匹配
查看>>
flume+elasticsearch+kibana遇到的坑
查看>>
【MM系列】在SAP里查看数据的方法
查看>>
C#——winform
查看>>
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
电梯调度程序的UI设计
查看>>
转自 zera php中extends和implements的区别
查看>>
Array.of使用实例
查看>>
【Luogu】P2498拯救小云公主(spfa)
查看>>
如何获取网站icon
查看>>
几种排序写法
查看>>
java 多线程的应用场景
查看>>