资源简介

TensorFlow实现人脸识别(3)--------对人脸样本进行训练,保存人脸识别模型 具体解释参考http://blog.csdn.net/yunge812/article/details/79447179

资源截图

代码片段和文件信息

#-*- coding:UTF-8 -*-
import random
import os
import numpy as np
from sklearn.cross_validation  import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense Dropout Activation Flatten
from keras.layers import Convolution2D MaxPooling2D
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.models import load_model
from keras import backend as K
import cv2


from imgProcess import load_dataset resize_imageIMAGE_SIZE #调用两个函数和一个宏定义

class Dataset:
    def __init__(selfpath_name):
        self.train_img    = None
        self.train_labels = None
        self.valid_img    = None
        self.valid_labels = None
        self.test_img     = None
        self.test_labels  = None
        self.path_name    = path_name
        self.input_shape  = None
    
    def loadAllData(selfpath_name):
        positive_data_imagespositive_data_labels=load_dataset(path_name‘traindata‘)
        negative_data_imagesnegative_data_labels=load_dataset(path_name‘testdata‘)
        images =np.concatenate((positive_data_images negative_data_images) axis=0) #数组拼接
        labels=np.concatenate((positive_data_labels negative_data_labels) axis=0)
        return imageslabels

    
     # 加载数据集并按照交叉验证的原则划分数据集并进行相关预处理工作
    def load(selfimg_rows=IMAGE_SIZEimg_cols=IMAGE_SIZEimg_channels=3nb_classes=2):
        imageslabels = self.loadAllData(self.path_name) #images为四维数组,尺寸为(图片数量总(包括test+train)*IMAGE_SIZE*IMAGE_SIZE*3)
        #随机划分训练集和验证集
        train_imagesvalid_imagestrain_labelsvalid_labels = train_test_split(images labels test_size = 0.3random_state = random.randint(0100))
        _            test_images_            test_labels = train_test_split(images labels test_size = 0.3random_state = random.randint(0100))
        
        # 当前的维度顺序如果为‘th‘,则输入图片数据时的顺序为:channelsrowscols,否则:rowscolschannels
        # 这部分代码就是根据keras库要求的维度顺序重组训练数据集
        if K.image_dim_ordering() == ‘th‘:  #theano的格式
            train_images     = train_images.reshape(train_images.shape[0] img_channels img_rows img_cols)
            valid_images     = valid_images.reshape(valid_images.shape[0] img_channels img_rows img_cols)
            test_images      = test_images.reshape( test_images.shape[0]  img_channels img_rows img_cols)
            self.input_shape = (img_channels img_rows img_cols)
        else:  # tensorflow格式
            train_images     = train_images.reshape(train_images.shape[0] img_rows img_cols img_channels)
            valid_images     = valid_images.reshape(valid_images.shape[0] img_rows img_cols img_channels)
            test_images      = test_images.reshape( test_images.shape[0]  img_rows img_cols img_channels)
            self.input_shape = (img_rows img_cols img_channels)
        
         # 输出训练集、验证集、测试集的数量
        print(train_images.shape[0] ‘train samples‘

评论

共有 条评论