
前言
本文主要介紹數(shù)據(jù)的導(dǎo)入以及模型的構(gòu)建與訓(xùn)練
依賴庫
- sklearn
- keras
- random
- cv2
- numpy
- os
都比較常用,不多介紹
正文
數(shù)據(jù)的讀取
主要功能:
輸入一個文件路徑,對其下的每個文件夾下的圖片讀取,并對每個文件夾給一個不同的Label
返回一個img的list,返回一個對應(yīng)label的list,返回一下有幾個文件夾(有幾種label)
代碼如下:
import os
import cv2
import numpy as np
def read_file(path):
img_list = []
label_list = []
dir_counter = 0
IMG_SIZE = 128
#對路徑下的所有子文件夾中的所有jpg文件進(jìn)行讀取并存入到一個list中
for child_dir in os.listdir(path):
child_path = os.path.join(path, child_dir)
for dir_image in os.listdir(child_path):
if dir_image.endswith('jpg'):
img = cv2.imread(os.path.join(child_path, dir_image))
resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
recolored_img = cv2.cvtColor(resized_img,cv2.COLOR_BGR2GRAY)
img_list.append(recolored_img)
label_list.append(dir_counter)
dir_counter += 1
# 返回的img_list轉(zhuǎn)成了 np.array的格式
img_list = np.array(img_list)
return img_list,label_list,dir_counter
#讀取訓(xùn)練數(shù)據(jù)集的文件夾,把他們的名字返回給一個list
def read_name_list(path):
name_list = []
for child_dir in os.listdir(path):
name_list.append(child_dir)
return name_list
接著我們構(gòu)建一個dataset類:
from read_data import read_file
from sklearn.model_selection import train_test_split
from keras.utils import np_utils
import random
#建立一個用于存儲和格式化讀取訓(xùn)練數(shù)據(jù)的類
class DataSet(object):
def __init__(self,path):
self.num_classes = None
self.X_train = None
self.X_test = None
self.Y_train = None
self.Y_test = None
self.img_size = 128
self.extract_data(path) #在這個類初始化的過程中讀取path下的訓(xùn)練數(shù)據(jù)
def extract_data(self,path):
#根據(jù)指定路徑讀取出圖片、標(biāo)簽和類別數(shù)
imgs,labels,counter = read_file(path)
#將數(shù)據(jù)集打亂隨機(jī)分組
X_train,X_test,y_train,y_test = train_test_split(imgs,labels,test_size=0.2,random_state=random.randint(0, 100))
#重新格式化和標(biāo)準(zhǔn)化
# 本案例是基于thano的,如果基于tensorflow的backend需要進(jìn)行修改
X_train = X_train.reshape(X_train.shape[0], self.img_size, self.img_size,1)/255.0
X_test = X_test.reshape(X_test.shape[0], self.img_size, self.img_size,1) / 255.0
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
#將labels轉(zhuǎn)成 binary class matrices
Y_train = np_utils.to_categorical(y_train, num_classes=counter)
Y_test = np_utils.to_categorical(y_test, num_classes=counter)
#將格式化后的數(shù)據(jù)賦值給類的屬性上
self.X_train = X_train
self.X_test = X_test
self.Y_train = Y_train
self.Y_test = Y_test
self.num_classes = counter
def check(self):
print('num of dim:', self.X_test.ndim)
print('shape:', self.X_test.shape)
print('size:', self.X_test.size)
print('num of dim:', self.X_train.ndim)
print('shape:', self.X_train.shape)
print('size:', self.X_train.size)
模型訓(xùn)練
讀入數(shù)據(jù)后我們便可以開始模型構(gòu)建以及訓(xùn)練了
from dataSet import DataSet
from keras.models import Sequential,load_model
from keras.layers import Dense,Activation,Convolution2D,MaxPooling2D,Flatten,Dropout
import numpy as np
#建立一個基于CNN的人臉識別模型
class Model(object):
FILE_PATH = "model\model.h5" #模型進(jìn)行存儲和讀取的地方
IMAGE_SIZE = 128 #模型接受的人臉圖片一定得是128*128的
def __init__(self):
self.model = None
#讀取實(shí)例化后的DataSet類作為進(jìn)行訓(xùn)練的數(shù)據(jù)源
def read_trainData(self,dataset):
self.dataset = dataset
#建立一個CNN模型,一層卷積、一層池化、一層卷積、一層池化、抹平之后進(jìn)行全鏈接、最后進(jìn)行分類
def build_model(self):
self.model = Sequential()
self.model.add(
Convolution2D(
filters=32,
kernel_size=(5, 5),
padding='same',
dim_ordering='th',
input_shape=self.dataset.X_train.shape[1:]
)
)
self.model.add(Activation('relu'))
self.model.add(
MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2),
padding='same'
)
)
self.model.add(Convolution2D(filters=64, kernel_size=(5, 5), padding='same'))
self.model.add(Activation('relu'))
self.model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
self.model.add(Flatten())
self.model.add(Dense(512))
self.model.add(Activation('relu'))
self.model.add(Dense(self.dataset.num_classes))
self.model.add(Activation('softmax'))
self.model.summary()
#進(jìn)行模型訓(xùn)練的函數(shù),具體的optimizer、loss可以進(jìn)行不同選擇
def train_model(self):
self.model.compile(
optimizer='adam', #有很多可選的optimizer,例如RMSprop,Adagrad,你也可以試試哪個好,我個人感覺差異不大
loss='categorical_crossentropy', #你可以選用squared_hinge作為loss看看哪個好
metrics=['accuracy'])
#epochs、batch_size為可調(diào)的參數(shù),epochs為訓(xùn)練多少輪、batch_size為每次訓(xùn)練多少個樣本
self.model.fit(self.dataset.X_train,self.dataset.Y_train,epochs=15,batch_size=32)
def evaluate_model(self):
print('\nTesting---------------')
loss, accuracy = self.model.evaluate(self.dataset.X_test, self.dataset.Y_test)
print('test loss;', loss)
print('test accuracy:', accuracy)
def save(self, file_path=FILE_PATH):
print('Model Saved.')
self.model.save(file_path)
def load(self, file_path=FILE_PATH):
print('Model Loaded.')
self.model = load_model(file_path)
#需要確保輸入的img得是灰化之后(channel =1 )且 大小為IMAGE_SIZE的人臉圖片
def predict(self,img):
img = img.reshape((1, self.IMAGE_SIZE, self.IMAGE_SIZE,1))
img = img.astype('float32')
img = img/255.0
result = self.model.predict_proba(img) #測算一下該img屬于某個label的概率
max_index = np.argmax(result) #找出概率最高的
return max_index,result[0][max_index] #第一個參數(shù)為概率最高的label的index,第二個參數(shù)為對應(yīng)概率
訓(xùn)練及評估可以如下方式
if __name__ == '__main__':
dataset = DataSet('face')
model = Model()
model.read_trainData(dataset)
model.build_model()
model.train_model()
model.evaluate_model()
model.save()
結(jié)語
這部分內(nèi)容較多,這里只貼出代碼肯定很難理解,因此基礎(chǔ)知識還請自行學(xué)習(xí),詳情還請查閱我的github:https://github.com/haoxinl/face_detect