keras/构建卷积神经网络人脸识别

olivettifaces是纽约大学的一个比较小的人脸库,由40个人的400张图片构成,即每个人的人脸图片为10张。每张图片的灰度级为8位,每个像素的灰度大小位于0-255之间,每张图片大小为64×64。图片大小是1190*942,一共有20*20张人脸,故每张人脸大小是(1190/20)*(942/20)即57*47=2679本文所用的训练数据就是这张图片,400个样本,40个类别。

input_faces.py读取图片数据集

#coding:utf-8
"""
read_data_sets
"""
from PIL import Image
import numpy as np

def read_data_sets(dataset_path):
    img = Image.open(dataset_path)
    #np.asarray()将图片转为灰度
    img_ndarray = np.asarray(img,dtype='float64')/256
    #共有400张图片,每张图片大小57*47=2679
    faces = np.empty((400,2679))
    #将图片存入faces中
    for row in range(20):
        for column in range(20):
            #第一行的第一张脸存入face0,每张脸的大小为57*47
            faces[row*20+column] = np.ndarray.flatten(img_ndarray[row*57:(row+1)*57,column*47:(column+1)*47])

    #将标签存入label中,400个label标签,40类
    label = np.empty(400)
    for i in range(40):
        label[i*10:i*10+10] = i
        label = label.astype(np.int)
    #设置train320,test80
    train_data = np.empty((320,2679))
    train_label = np.empty(320)
    test_data = np.empty((80,2679))
    test_label = np.empty(80)

    #将数据集存入data,label中
    for i in range(40):
        #前8张存入train,第9-10张存入test,依此顺序存400张图片
        train_data[i*8:i*8+8] = faces[i*10:i*10+8]
        train_label[i*8:i*8+8] = label[i*10:i*10+8]
        test_data[i*2:i*2+2] = faces[i*10+8:i*10+10]
        test_label[i*2:i*2+2] = label[i*10+8:i*10+10]

    data = [(train_data,train_label),(test_data,test_label)]
    return data

#(train_data, train_label), (test_data, test_label) = read_data_sets('olivettifaces.gif')

构建卷积神经网络识别人脸图片:

#coding:utf-8
"""
python 2.7
keras 2.04
"""
import numpy as np
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Activation,Convolution2D,MaxPooling2D,Flatten
from keras.optimizers import SGD
from sklearn.metrics import confusion_matrix,classification_report
import input_faces
import datetime

start_time = datetime.datetime.now()
#设置随机种子
np.random.seed(1000)
(train_data,train_labels),(test_data,test_labels) = input_faces.read_data_sets('olivettifaces.gif')
#reshape
train_data = train_data.reshape(train_data.shape[0],1,57,47)
train_labels = np_utils.to_categorical(train_labels,nb_classes=40)
test_data = test_data.reshape(test_data.shape[0],1,57,47)
test_labels = np_utils.to_categorical(test_labels,nb_classes=40)

#构建模型
model = Sequential()
#(None,1,57,47) ---> (None,5,57,47)
model.add(Convolution2D(nb_filter=5,nb_row=3,nb_col=3,border_mode='same',input_shape=(1,57,47)))
#(None,5,57,47) --->(None,5,28,23)
model.add(MaxPooling2D(pool_size=(2,2),border_mode='same'))
#(None,5,28,23) --->(None,10,28,23)
model.add(Convolution2D(nb_filter=10,nb_row=3,nb_col=3,border_mode='same'))
#(None,10,28,23)---(None,10,14,11)
model.add(MaxPooling2D(pool_size=(2,2),border_mode='same'))
model.add(Flatten())
model.add(Dense(1000))
model.add(Activation('relu'))
model.add(Dense(40))
model.add(Activation('softmax'))
model.compile(optimizer=SGD(lr=0.01,decay=1e-6,momentum=0.9),loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(train_data,train_labels,nb_epoch=10,batch_size=40,shuffle=True,verbose=1)
test_score,test_accuracy = model.evaluate(test_data,test_labels)
predictions = model.predict_clases(test_data,batch_size=10)
#混淆矩阵
print(confusion_matrix(test_labels,predictions))
#report
print(classification_report(test_labels,np.array(predictions)))
#模型训练了多久
end_time = datetime.datetime.now()
total_time = (end_time - start_time).seconds
print('total time is:',total_time)

检测准确度达到0.97

keras/构建卷积神经网络人脸识别_第1张图片

数据集:
链接:http://pan.baidu.com/s/1eRSGIR4 密码:zznx

keras使用陷阱

model.fit()中有两个参数,shuffle和validation_split,validation_split将从训练集中抽取部分作为验证集,shuffle用于将数据打乱。如果训练的数据不是打乱的,当采用validation_split时可能会选取到只是正样本或负样本,再经过打乱后在测试集上表现会很差

你可能感兴趣的:(深度学习机器学习)