Face Recognition System - update.

1. Project

The project face recognition system. Item basic idea is as follows: For the input image, first, face detection by MTCNN network, the acquired face image, then insightface for facial feature extraction, data comparison in the database, identify the face. Item structure as shown:

Face Recognition System configuration FIG 1

First need to train mtcnn network, identify the face detection. MTCNN training network in the " algorithm to understand and implement MTCNN has been achieved" in this model can be used directly trained. For insightface, the need for insightface training.

This project uses environment: ubuntu16.04 + RTX2070S.

2. Project implementation process

2.1 Data Processing

2.1.1 Data processing training

Used in the project CASIA-Webface data, data download the following files:

Which, train.idx and train.rec data is used for training, and the rest of the bin files are test data sets.

First, create a folder utils, common components are stored inside the project uses, which in config.py global configuration file, the code is as follows:

 1 import os
 2 
 3 
 4 # prepare_data parameters
 5 mxdata_dir = "./data"
 6 tfrecord_dir = "./data/CASIA.tfrecords"
 7 lfw_dir = './data/lfw'
 8 lfw_save_dir = './data/lfw_face'
 9 eval_dir = './data/lfw_face.db'
10 eval_datasets_self = ['./data/lfw_face.db']
11 eval_datasets = ["./data/lfw.bin", "./data/agedb_30.bin", "./data/calfw.bin",
12                  "./data/cfp_ff.bin", "./data/cfp_fp.bin", "./data/cplfw.bin", './data/lfw_face.db']
13 
14 
15 # model parameters
16 model_params = {"backbone_type": "resnet_v2_m_50",
17                 "out_type": "E",
18                 "bn_decay": 0.9,
19                 "weight_decay": 0.0005,
20                 "keep_prob": 0.4,
21                 "embd_size": 512}
22 
23 
24 # training parameters
25 s = 64.0
26 m = 0.5
27 class_num = 85742
28 lr_steps = [40000, 60000, 80000]
29 lr_values = [0.004, 0.002, 0.0012, 0.0004]
30 momentum = 0.9
31 addrt = "./data/CASIA.tfrecords"
32 model_patht = "./model/Arcface_model"
33 img_size = 112
34 batch_size = 128
35 addr = "../data/CASIA.tfrecords"
36 model_name = "Arcface"
37 train_step = 1000001
38 model_path = "../model/Arcface_model"
39 gpu_num = 2
40 model_save_gap = 30000
41 
42 
43 # evaluation parameters
44 eval_dropout_flag = False
45 eval_bn_flag = False
46 
47 
48 # face database parameters
49 custom_dir = '../data/custom'
50 arc_model_name = 'Arcface-330000'
51 arc_model_path = './model/Arcface_model/Arcface-330000'
52 
53 base_dir = './model/MTCNN_model'
54 mtcnn_model_path = [os.path.join(base_dir, "Pnet_model/Pnet_model.ckpt-20000"),
55                     os.path.join(base_dir, "Rnet_model/Rnet_model.ckpt-40000"),
56                     os.path.join(base_dir, "Onet_model/Onet_model.ckpt-40000")]
57 embds_save_dir = "../data/face_db"
View Code

 

Because the data is stored mxnet format, so you first need to convert the data into a format TFrecord create gen_tfrecord_mxdata.py file in the root directory of the project, the code is as follows:

 1 import tensorflow as tf
 2 import mxnet as mx
 3 import os
 4 import io
 5 import numpy as np
 6 import cv2
 7 import time
 8 from scipy import misc
 9 import argparse
10 from utils import config
11 
12 
13 def arg_parse():
14 
15     parser = argparse.ArgumentParser()
16     parser.add_argument("--read_dir", default=config.mxdata_dir, type=str, help='directory to read data')
17     parser.add_argument("--save_dir", default=config.tfrecord_dir, type=str, help='path to save TFRecord file')
18 
19     return parser.parse_args()
20 
21 
22 def main():
23     
24     with tf.python_io.TFRecordWriter(save_dir) as writer:
25         idx_path = os.path.join(read_dir, 'train.idx')
26         bin_path = os.path.join(read_dir, 'train.rec')
27         imgrec = mx.recordio.MXIndexedRecordIO(idx_path, bin_path, 'r')
28         s = imgrec.read_idx(0)
29         header, _ = mx.recordio.unpack(s)
30         imgidx = list(range(1, int(header.label[0])))
31         labels = []
32         for i in imgidx:
33             img_info = imgrec.read_idx(i)
34             header, img = mx.recordio.unpack(img_info)
35             label = int(header.label)
36             labels.append(label)
37             img = io.BytesIO(img)
38             img = misc.imread(img).astype(np.uint8)
39             img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
40             img_raw = img.tobytes()
41             
42             example = tf.train.Example(features=tf.train.Features(feature={
43                 "img": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])),
44                 "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
45                 }))
46             
47             writer.write(example.SerializeToString())             
48             
49             if i % 10000 == 0:
50                 print('%d pics processed' % i, "time: ", time.time()-begin)
51 
52 
53 if __name__ == "__main__":
54 
55     parser = arg_parse()
56 
57     save_dir = parser.save_dir
58     read_dir = parser.read_dir
59 
60     begin = time.time()
61     
62     main()
View Code

 

By the above code, the training data is converted into data format mxnet TFRecord format.

 

ArcFace loss analysis and code see insightface introduction,

Guess you like

Origin www.cnblogs.com/xjlearningAI/p/12459464.html