0 前言
?? 優(yōu)質(zhì)競賽項目系列,今天要分享的是
基于深度學(xué)習(xí)機器視覺的人臉性別年齡識別系統(tǒng)
該項目較為新穎,適合作為競賽課題方向,學(xué)長非常推薦!
?? 更多資料, 項目分享:
https://gitee.com/dancheng-senior/postgraduate文章來源地址http://www.zghlxwxcb.cn/news/detail-834711.html
1 課題描述
隨著大數(shù)據(jù)與人工智能逐漸走入人們的生活,計算機視覺應(yīng)用越發(fā)廣泛。如醫(yī)療影像識別、無人駕駛車載視覺、通用物體識別、自然場景下的文本識別等,根據(jù)不同的應(yīng)用場景,人臉研究方向可以分為人臉檢測、身份識別、性別識別、年齡預(yù)測、種族識別、表情識別等。近年來,人臉身份識別技術(shù)發(fā)展迅猛,在生活應(yīng)用中取得了較好的效果,也逐漸趨于成熟,而年齡識別與性別預(yù)測,仍然是生物特征識別研究領(lǐng)域中一項具有挑戰(zhàn)性的課題。
課題意義
相比人臉性別屬性而言,人臉年齡屬性的研究更富有挑戰(zhàn)性。主要有兩點原因,首先每個人的年齡會隨著身體健康狀況、皮膚保養(yǎng)情況而表現(xiàn)得有所不同,即便是在同一年,表現(xiàn)年齡會隨著個人狀態(tài)的不同而改變,人類識別尚且具有較高難度。其次,可用的人臉年齡估計數(shù)據(jù)集比較少,不同年齡的數(shù)據(jù)標(biāo)簽收集不易,現(xiàn)有大多數(shù)的年齡數(shù)據(jù)集都是在不同的復(fù)雜環(huán)境下的照片、人臉圖片存在光照變化較復(fù)雜、部分遮擋、圖像模糊、姿態(tài)旋轉(zhuǎn)角度較大等一系列問題,對人臉模型的魯棒性產(chǎn)生了較大的影響。
2 實現(xiàn)效果
這里廢話不多說,先放上大家最關(guān)心的實現(xiàn)效果:
輸入圖片:
識別結(jié)果:
或者實時檢測
3 算法實現(xiàn)原理
3.1 數(shù)據(jù)集
學(xué)長收集的數(shù)據(jù)集:
該人臉數(shù)據(jù)庫的圖片來源于互聯(lián)網(wǎng)的爬取,而非研究機構(gòu)整理,一共含有13000多張人臉圖像,在這個數(shù)據(jù)集中大約有1860張圖片是成對出現(xiàn)的,即同一個人的2張不同照片,有助于人臉識別算法的研究,圖像標(biāo)簽中標(biāo)有人的身份信息,人臉坐標(biāo),關(guān)鍵點信息,可用于人臉檢測和人臉識別的研究,此數(shù)據(jù)集是對人臉?biāo)惴ㄐЧ炞C的權(quán)威數(shù)據(jù)集.
該數(shù)據(jù)集包含的人臉范圍比較全面,歐亞人種都有。
3.2 深度學(xué)習(xí)識別算法
卷積神經(jīng)網(wǎng)絡(luò)是常見的深度學(xué)習(xí)架構(gòu),而在CNN出現(xiàn)之前,圖像需要處理的數(shù)據(jù)量過大,導(dǎo)致成本很高,效率很低,圖像在數(shù)字化的過程中很難保留原有的特征,導(dǎo)致圖像處理的準(zhǔn)確率不高。CNN的出現(xiàn)使得提取特征的能力變得更強,為更多優(yōu)秀網(wǎng)絡(luò)的研究提供了有力的支撐。CNN的核心思想是利用神經(jīng)網(wǎng)絡(luò)模擬人腦視覺神經(jīng)系統(tǒng),構(gòu)造多個神經(jīng)元并建立彼此之間的聯(lián)系。不同的神經(jīng)元進行分工,淺層神經(jīng)元處理低緯度圖像特征,深層神經(jīng)元處理圖像高級特征、語義信息等,CNN的網(wǎng)絡(luò)結(jié)構(gòu)主要由卷積層、BN層、激活層、池化層、全連接層、損失函數(shù)層構(gòu)成,多個層協(xié)同工作實現(xiàn)了特征提取的功能,并通過特有的網(wǎng)絡(luò)結(jié)構(gòu)降低參數(shù)的數(shù)量級,防止過擬合,最終得到輸出結(jié)果.
CNN傳承了多層感知機的思想,并受到了生物神經(jīng)科學(xué)的啟發(fā),通過卷積的運算模擬人類視覺皮層的“感受野”。不同于傳統(tǒng)的前饋神經(jīng)網(wǎng)絡(luò),卷積運算對圖像的區(qū)域值進行加權(quán)求和,最終以神經(jīng)元的形式進行輸出。前饋神經(jīng)網(wǎng)絡(luò)對每一個輸入的信號進行加權(quán)求和:
- (a)圖是前饋神經(jīng)網(wǎng)絡(luò)的連接方式
- (b)圖是CNN的連接方式。
cnn框架如下:
3.3 特征提取主干網(wǎng)絡(luò)
在深度學(xué)習(xí)算法研究中,通用主干特征提取網(wǎng)絡(luò)結(jié)合特定任務(wù)網(wǎng)絡(luò)已經(jīng)成為一種標(biāo)準(zhǔn)的設(shè)計模式。特征提取對于分類、識別、分割等任務(wù)都是至關(guān)重要的部分。下面介紹本文研究中用到的主干神經(jīng)網(wǎng)絡(luò)。
ResNet網(wǎng)絡(luò)
ResNet是ILSVRC-2015的圖像分類任務(wù)冠軍,也是CVPR2016的最佳論文,目前應(yīng)用十分廣泛,ResNet的重要性在于將網(wǎng)絡(luò)的訓(xùn)練深度延伸到了數(shù)百層,而且取得了非常好的效果。在ResNet出現(xiàn)之前,網(wǎng)絡(luò)結(jié)構(gòu)一般在20層左右,對于一般情況,網(wǎng)絡(luò)結(jié)構(gòu)越深,模型效果就會越好,但是研究人員發(fā)現(xiàn)加深網(wǎng)絡(luò)反而會使結(jié)果變差。
人臉特征提取我這里選用ResNet,網(wǎng)絡(luò)結(jié)構(gòu)如下:
3.4 總體實現(xiàn)流程
4 具體實現(xiàn)
4.1 預(yù)訓(xùn)練數(shù)據(jù)格式
4.2 部分實現(xiàn)代碼
訓(xùn)練部分代碼:
?
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from six.moves import xrange
from datetime import datetime
import time
import os
import numpy as np
import tensorflow as tf
from data import distorted_inputs
from model import select_model
import json
import re
LAMBDA = 0.01
MOM = 0.9
tf.app.flags.DEFINE_string('pre_checkpoint_path', '',
"""If specified, restore this pretrained model """
"""before beginning any training.""")
tf.app.flags.DEFINE_string('train_dir', '/home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/test_fold_is_0',
'Training directory')
tf.app.flags.DEFINE_boolean('log_device_placement', False,
"""Whether to log device placement.""")
tf.app.flags.DEFINE_integer('num_preprocess_threads', 4,
'Number of preprocessing threads')
tf.app.flags.DEFINE_string('optim', 'Momentum',
'Optimizer')
tf.app.flags.DEFINE_integer('image_size', 227,
'Image size')
tf.app.flags.DEFINE_float('eta', 0.01,
'Learning rate')
tf.app.flags.DEFINE_float('pdrop', 0.,
'Dropout probability')
tf.app.flags.DEFINE_integer('max_steps', 40000,
'Number of iterations')
tf.app.flags.DEFINE_integer('steps_per_decay', 10000,
'Number of steps before learning rate decay')
tf.app.flags.DEFINE_float('eta_decay_rate', 0.1,
'Learning rate decay')
tf.app.flags.DEFINE_integer('epochs', -1,
'Number of epochs')
tf.app.flags.DEFINE_integer('batch_size', 128,
'Batch size')
tf.app.flags.DEFINE_string('checkpoint', 'checkpoint',
'Checkpoint name')
tf.app.flags.DEFINE_string('model_type', 'default',
'Type of convnet')
tf.app.flags.DEFINE_string('pre_model',
'',#'./inception_v3.ckpt',
'checkpoint file')
FLAGS = tf.app.flags.FLAGS
# Every 5k steps cut learning rate in half
def exponential_staircase_decay(at_step=10000, decay_rate=0.1):
print('decay [%f] every [%d] steps' % (decay_rate, at_step))
def _decay(lr, global_step):
return tf.train.exponential_decay(lr, global_step,
at_step, decay_rate, staircase=True)
return _decay
def optimizer(optim, eta, loss_fn, at_step, decay_rate):
global_step = tf.Variable(0, trainable=False)
optz = optim
if optim == 'Adadelta':
optz = lambda lr: tf.train.AdadeltaOptimizer(lr, 0.95, 1e-6)
lr_decay_fn = None
elif optim == 'Momentum':
optz = lambda lr: tf.train.MomentumOptimizer(lr, MOM)
lr_decay_fn = exponential_staircase_decay(at_step, decay_rate)
return tf.contrib.layers.optimize_loss(loss_fn, global_step, eta, optz, clip_gradients=4., learning_rate_decay_fn=lr_decay_fn)
def loss(logits, labels):
labels = tf.cast(labels, tf.int32)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits, labels=labels, name='cross_entropy_per_example')
cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
tf.add_to_collection('losses', cross_entropy_mean)
losses = tf.get_collection('losses')
regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
total_loss = cross_entropy_mean + LAMBDA * sum(regularization_losses)
tf.summary.scalar('tl (raw)', total_loss)
#total_loss = tf.add_n(losses + regularization_losses, name='total_loss')
loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg')
loss_averages_op = loss_averages.apply(losses + [total_loss])
for l in losses + [total_loss]:
tf.summary.scalar(l.op.name + ' (raw)', l)
tf.summary.scalar(l.op.name, loss_averages.average(l))
with tf.control_dependencies([loss_averages_op]):
total_loss = tf.identity(total_loss)
return total_loss
def main(argv=None):
with tf.Graph().as_default():
model_fn = select_model(FLAGS.model_type)
# Open the metadata file and figure out nlabels, and size of epoch
input_file = os.path.join(FLAGS.train_dir, 'md.json')
print(input_file)
with open(input_file, 'r') as f:
md = json.load(f)
images, labels, _ = distorted_inputs(FLAGS.train_dir, FLAGS.batch_size, FLAGS.image_size, FLAGS.num_preprocess_threads)
logits = model_fn(md['nlabels'], images, 1-FLAGS.pdrop, True)
total_loss = loss(logits, labels)
train_op = optimizer(FLAGS.optim, FLAGS.eta, total_loss, FLAGS.steps_per_decay, FLAGS.eta_decay_rate)
saver = tf.train.Saver(tf.global_variables())
summary_op = tf.summary.merge_all()
sess = tf.Session(config=tf.ConfigProto(
log_device_placement=FLAGS.log_device_placement))
tf.global_variables_initializer().run(session=sess)
# This is total hackland, it only works to fine-tune iv3
if FLAGS.pre_model:
inception_variables = tf.get_collection(
tf.GraphKeys.VARIABLES, scope="InceptionV3")
restorer = tf.train.Saver(inception_variables)
restorer.restore(sess, FLAGS.pre_model)
if FLAGS.pre_checkpoint_path:
if tf.gfile.Exists(FLAGS.pre_checkpoint_path) is True:
print('Trying to restore checkpoint from %s' % FLAGS.pre_checkpoint_path)
restorer = tf.train.Saver()
tf.train.latest_checkpoint(FLAGS.pre_checkpoint_path)
print('%s: Pre-trained model restored from %s' %
(datetime.now(), FLAGS.pre_checkpoint_path))
run_dir = '%s/run-%d' % (FLAGS.train_dir, os.getpid())
checkpoint_path = '%s/%s' % (run_dir, FLAGS.checkpoint)
if tf.gfile.Exists(run_dir) is False:
print('Creating %s' % run_dir)
tf.gfile.MakeDirs(run_dir)
tf.train.write_graph(sess.graph_def, run_dir, 'model.pb', as_text=True)
tf.train.start_queue_runners(sess=sess)
summary_writer = tf.summary.FileWriter(run_dir, sess.graph)
steps_per_train_epoch = int(md['train_counts'] / FLAGS.batch_size)
num_steps = FLAGS.max_steps if FLAGS.epochs < 1 else FLAGS.epochs * steps_per_train_epoch
print('Requested number of steps [%d]' % num_steps)
for step in xrange(num_steps):
start_time = time.time()
_, loss_value = sess.run([train_op, total_loss])
duration = time.time() - start_time
assert not np.isnan(loss_value), 'Model diverged with loss = NaN'
if step % 10 == 0:
num_examples_per_step = FLAGS.batch_size
examples_per_sec = num_examples_per_step / duration
sec_per_batch = float(duration)
format_str = ('%s: step %d, loss = %.3f (%.1f examples/sec; %.3f ' 'sec/batch)')
print(format_str % (datetime.now(), step, loss_value,
examples_per_sec, sec_per_batch))
# Loss only actually evaluated every 100 steps?
if step % 100 == 0:
summary_str = sess.run(summary_op)
summary_writer.add_summary(summary_str, step)
if step % 1000 == 0 or (step + 1) == num_steps:
saver.save(sess, checkpoint_path, global_step=step)
if __name__ == '__main__':
tf.app.run()
5 最后
?? 更多資料, 項目分享:文章來源:http://www.zghlxwxcb.cn/news/detail-834711.html
https://gitee.com/dancheng-senior/postgraduate
到了這里,關(guān)于互聯(lián)網(wǎng)加競賽 基于設(shè)深度學(xué)習(xí)的人臉性別年齡識別系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!