These images will be trained in this section, as described in the previous chapters, and we can get a good sample of the training samples. The main use is Keras.
I. Building a DataSet class
1.1 Init Complete Initialization work
def __init__ (self,path_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
1.2 Loadalldata (Self,path_name):
Loading positive and negative sample data and stitching data images and labels together
def loadalldata (self,path_name):
positive_data_images,positive_data_labels=load_dataset (Path_name, ' Traindata ')
negative_data_images,negative_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 images,labels
1.3 Loading data sets
def load (self,img_rows=image_size,img_cols=image_size, img_channels=3,nb_classes=2): Images,labels = self.
Loadalldata (self.path_name) #images为四维数组, size (total picture (including Test+train) *image_size*image_size*3) #随机划分训练集和验证集 Train_images,valid_images,train_labels,valid_labels = train_test_split (images, labels,random_state = Random.randint (0,100) _, test_images,_, Test_labels = train_test_split (images, labels,test_size = 0.3,ran Dom_state = Random.randint (0,100)) # If the current dimension order is ' th ', the order in which the picture data is entered is: Channels,rows,cols, # otherwise: Rows,cols,chann Els # This part of the code is to reorganize the training data set according to the order of the dimensions required by the Keras Library if k.image_dim_ordering () = = ' th ': #theano的格式 train_image
s = 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_row S, img_cols) Self.input_shape = (Img_channels, img_rows, Img_cols) Else: # TensorFlow format t Rain_images = Train_images.reshape (train_images.shape[0], img_rows, Img_cols, Img_cha Nnels) valid_images = Valid_images.reshape (valid_images.shape[0], Img_row
S, 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) # Output training set, validation set, number of test sets print (Train_images.shape[0], ' train samples ') print (valid_images.shape[0], ' valid Samp Les ') print (test_images.shape[0], ' test samples ') #根据类别数量nb_classes将类别标签进行one the-hot encoding to quantify it, #在这里我
There are only two categories, and after conversion the label data becomes two-dimensional #将 1*638 into 2*638 train_labels = np_utils.to_categorical (train_labels,nb_classes) valid_labels = n
P_utils.to_categorical (valid_labels,nb_classes) test_labels = np_utils.to_categorical (test_labels,nb_classes) #图像像素转为浮点 train_images = Train_images.astype (' float32 ') valid_images = Valid_images.astype (' float32 ') ) Test_images = Test_images.astype (' float32 ') #归一化图像 train_images/= 255 valid_images/=
255 test_images/= 255 self.train_images = train_images Self.valid_images = valid_images
Self.test_images = test_images Self.train_labels = train_labels Self.valid_labels = valid_labels Self.test_labels = Test_labels
second, build model
2.1 Validating the Model
def evaluate (Self,dataset):
score = self.model.evaluate (dataset.test_images, dataset.test_labels,verbose=1)
print ('%s:%.2f%% '% (self.model.metrics_names[1], score[1] * 100))
2.2 Saving the Model
def save_model (Self,file_path = model_path):
self.model.save (File_path)
2.3 Loading the Model
def load_model (Self,file_path = model_path):
Self.model = Load_model (File_path)
2.4 Building a model
def build_model (self,dataset,nb_classes=2): Self.model = sequential () #2维卷积层 the first convolution layer contains 32 convolution cores, each of which is a 3x3 size, #border_mode值为 "Same" means that we use the reserved boundary feature to slide the window, # "valid" specifies to drop the boundary pixel #input_shape的值为 (64,64,3) self.m
Odel.add (convolution2d (32,3,3,border_mode= ' same ', Input_shape = Dataset.input_shape))
#激活函数 Self.model.add (Activation (' Relu ')) #2维卷积层 Self.model.add (convolution2d (32,3,3)) #激活函数 Self.model.add (Activation (' Relu ')) #池化层 reduce the input feature map, simplifying the network computational complexity 2*2 the maximum value Self.model.add (Maxpoo Ling2d (pool_size= (2,2))) #dropout层 Self.model.add (Dropout (0.25)) #2维卷积层 Self.model.add (Con volution2d (64,3,3,border_mode= ' same ', Input_shape = dataset.input_shape)) #激活函数 Self.mod El.add (Activation (' Relu ')) #2维卷积层 Self.model.add (convolution2d (64,3,3)) #激活函数 Self.mode L.add (Activation (' Relu ')) #池化层 Self.model.add (Maxpooling2d (pool_size= (2,2)) #dropout层 Self.model.add (Dropout (0.25 ) #flatten层 the full-connection layer requires the input data to be one-dimensional, #必须把输入数据 "flattening" into one dimension before entering the full-connected layer, Self.model.add (Flatten ()) #全连接 Layer Self.model.add (dense ()) #激活函数 Self.model.add (Activation (' Relu ')) #dropout层 SE Lf.model.add (Dropout (0.5)) #全连接层 Self.model.add (Dense (nb_classes)) #分类层 This value is actually the first J neurons in the output of all neurons accounted for
Percentage. Self.model.add (Activation (' Softmax ')) #输出模型概况 self.model.summary ()
2.5 Model Training train
Def train (self,dataset,batch_size = $, nb_epoch=2,data_augmentation = True): # trained with the Sgd+momentum optimizer, first generating an optimizer object SGD = SGD (lr=0.01, #学习率 decay = 1e-6, #每次更新后学习效率的衰减值 momentum=0.9, #指定动量值 Let the optimizer to some extent retain the previous optimization direction, while using the current sample to fine-tune the final optimization direction, so that can increase stability, improve learning speed, and to a certain extent, to avoid falling into the local optimal trap nesterov=true #是否采用nesterov动量方 METHOD) # Complete the actual model configuration work compile after the model can start training self.model.compile (loss= ' categorical_crossentropy
', #损失函数 optimizer=sgd, #优化器 metrics=[' accuracy '] # do not use data promotion, so-called Ascension is from the training data we provide using rotation, flipping, adding noise and other methods to create new training data, consciously improve the training data size, increase the model training amount if not data_augmentat
Ion:self.model.fit (#即可开始模型训练 dataset.train_images, Dataset.train_labels, batch_size = batch_size, #每次迭代训练样本的数量 Nb_epoch = Nb_epoch, #训练轮次 use trainingSet the entire sample training once for a training round validation_data = (dataset.valid_images,dataset.valid_labels), Shuffle = True #是否随机打乱数据集) Else: #定义数据生成器用于数据提升, which returns a generator object dat Agen,datagen every call to generate a set of data (sequentially generated), saving memory, is actually python data generator DataGen = Imagedatagenerator (Featurewis E_center=false, # Whether the input data is de-centered (mean 0), Samplewise_center=false, # Whether to make the input data of each sample mean value is 0 feature Wise_std_normalization=false, # Whether the data is normalized (the input data is divided by the standard deviation of the data set) Samplewise_std_normalization=false, # Whether to divide each sample data by Its own standard deviation zca_whitening=false, # Whether to apply Zca whitening rotation_range=20 to the input data, # The angle at which the picture randomly rotates when the data is lifted (the range is 0~18 0) width_shift_range=0.2, # The amplitude of the horizontal offset of the image when the data is lifted (in units of image width, floating point between 0~1) height_shift_range=0.2,
# ditto, just here is the vertical horizontal_flip=true, # Whether to make a random horizontal flip Vertical_flip=false # whether random vertical flipping ) # MeterCalculates the number of the entire set of training samples for use in eigenvalue normalization, Zca whitening, etc. datagen.fit (dataset.train_images) # Start the training model with the generator Self.mod
El.fit_generator (datagen.flow (dataset.train_images,dataset.train_labels,batch_size = batch_size),
Samples_per_epoch = Dataset.train_images.shape[0], Nb_epoch = Nb_epoch,
Validation_data = (dataset.valid_images,dataset.valid_labels) )
2.6 Calculating the probability of a prediction
def face_predict (self,image):
if k.image_dim_ordering () = = ' th ' and image.shape! = (1, 3, Image_size, image_size):
image = resize_image (image) # dimensions must be consistent with the training set should be image_size x image_size
image = Image.reshape ((1, 3, image_size, image_size) # Unlike model training, this time just predicts for 1 images
elif k.image_dim_ordering () = = ' tf ' and image.shape! = (1, image_size, Image_size, 3): Image
= resize_image (image)
image = Image.reshape ((1, Image_size, Image_size, 3))
image = Image.astype (' float32 ')
image /=255
# gives the probability that the input belongs to each category, we are a two value category, then the function gives the probability that the input image belongs to 0 and 1
#result = Self.model.predict_proba (image)
#print (' Result: ', result)
#预测
result = self.model.predict_classes ( Image)
return result[0]
third, the main operating procedures
3.1 Getting the current path
Path_name = OS.GETCWD () #获得当前路径
3.2 Getting data sets
DataSet = DataSet (Path_name) #获取数据集
3.3 Loading data sets
Dataset.load ()
3.4 Building a model
Model = Model ()
Model.build_model (DataSet)
3.5 Training Model Save model
Model.train (DataSet)
Model.save_model (File_path = './face.model.h5 ')
By following the steps above, you can save the trained model and call it directly face.model.h5 subsequent tests.
SOURCE Download: http://download.csdn.net/download/yunge812/10270013
Reference post: http://blog.csdn.net/wearge/article/category/7092833
http://blog.csdn.net/xvshu/article/details/78863430