def partial_train(self, imagefolder_path, tpsfile_path, trainfolder_path, n_max): """! Lance l'apprentissage du modèle avec les valeurs par défaut @param trainfolder : path+"train.xml" @param n_max : nombre max d'images à prendre pour l'apprentissage (0-1) """ print("split") file_sizes = utils.utils.split_train_test(imagefolder_path, n_max) print("read_tps") dict_tps = utils.utils.read_tps(tpsfile_path) print("generate xml") utils.utils.generate_dlib_xml(dict_tps, file_sizes['train'], folder=self.path_create_model + "train", out_file=self.path_create_model + "train.xml") utils.utils.generate_dlib_xml(dict_tps, file_sizes['test'], folder=self.path_create_model + "test", out_file=self.path_create_model + "test.xml") utils.utils.dlib_xml_to_tps(self.path_create_model + "train.xml") utils.utils.dlib_xml_to_tps(self.path_create_model + "test.xml") print("options") self.parameter_model([500, 3], 0.08, 1, 20, 700, 20, 200) print("train") dlib.train_shape_predictor(trainfolder_path, self.path_create_model + "predictor.dat", self.options)
def train1(): import os import cv2 import dlib import glob # 训练68个特征点 current_path = os.getcwd() faces_path = current_path + '/examples/faces' # 训练部分 # 参数设置 options = dlib.shape_predictor_training_options() options.oversampling_amount = 300 options.nu = 0.05 options.tree_depth = 2 options.be_verbose = True # 导入打好了标签的xml文件 training_xml_path = os.path.join(faces_path, "training_with_face_landmarks.xml") # 进行训练,训练好的模型将保存为predictor.dat dlib.train_shape_predictor(training_xml_path, "predictor.dat", options) # 打印在训练集中的准确率 print("\nTraining accuracy:{0}".format( dlib.test_shape_predictor(training_xml_path, "predictor.dat"))) # 导入测试集的xml文件 testing_xml_path = os.path.join(faces_path, "testing_with_face_landmarks.xml") # 打印在测试集中的准确率 print("\Testing accuracy:{0}".format( dlib.test_shape_predictor(testing_xml_path, "predictor.dat")))
def train_model(name: str, xml: str, **kwargs): '''requires: the model name, and the path of the xml annotations. It trains and saves a new model according to the specified training options and given annotations''' options = get_training_options(**kwargs) dlib.train_shape_predictor(xml, name, options)
def train_model(name, xml): ''' requires: the model name, and the path to the xml annotations. It trains and saves a new model according to the specified training options and given annotations example @ https://github.com/Luca96/dlib-minified-models/tree/master/face_landmarks: options = dlib.shape_predictor_training_options() options.tree_depth = 4 options.nu = 0.1 options.cascade_depth = 15 options.feature_pool_size = 800 # or even 1000 options.num_test_splits = 200 # 150-200 is enough ''' # get the training options options = dlib.shape_predictor_training_options() options.tree_depth = 4 options.nu = 0.1 options.cascade_depth = 15 options.feature_pool_size = 400 options.num_test_splits = 50 options.oversampling_amount = 5 # options.be_verbose = True # tells what is happening during the training options.num_threads = 4 # number of the threads used to train the model # finally, train the model dlib.train_shape_predictor(xml, name, options)
def train_shape_predictor(self): self.__print_training_message('shape predictor') opt = dlib.shape_predictor_training_options() opt.oversampling_amount = 300 opt.nu = 0.05 opt.tree_depth = 2 opt.num_threads = self.cpu_cores opt.be_verbose = True dlib.train_shape_predictor(self.xml, PREDICTOR_DAT, opt)
def trainModel(xml_path, predictor): check = rx.convertXMLPoints(xml_path) if check.shape[0] == 1: options.oversampling_amount = 1 else: options.oversampling_amount = int((600 / check.shape[0]) + 1) dlib.train_shape_predictor(xml_path, predictor_name, options) predictor = dlib.shape_predictor(predictor_name) return predictor
def train_shape_predictor(self): self.__print_training_message('shape predictor') opt = dlib.shape_predictor_training_options() # opt.oversampling_amount = 300 # opt.nu = 0.05 # opt.tree_depth = 2 opt.num_threads = self.cpu_cores opt.be_verbose = True print(dlib.DLIB_USE_CUDA) dlib.train_shape_predictor(self.xml, 'predictor.dat', opt) #PREDICTOR_DAT, opt)
def train_model(self, trainfolder_path): """! Lance l'apprentissage du modèle avec les valeurs par défaut @param trainfolder : path+"train.xml" """ # self.parameter_model([500,6],0.6,1,18,700,40,500) dlib.train_shape_predictor(trainfolder_path, self.path_create_model + "predictor.dat", self.options) return "Training error (average pixel deviation): {}".format( dlib.test_shape_predictor( trainfolder_path, self.path_create_model + "predictor.dat"))
def train_shape_predictor(self, output_name, input_xml): print('[INFO] trining shape predictor....') # get the training options options = dlib.shape_predictor_training_options() options.tree_depth = 4 options.nu = 0.1 options.cascade_depth = 15 options.feature_pool_size = 400 options.num_test_splits = 100 options.oversampling_amount = 10 options.be_verbose = True options.num_threads = 4 # start training the model dlib.train_shape_predictor(input_xml, output_name, options)
def train(self, images, gt_shapes, bounding_boxes, prefix='', verbose=False): r""" Train an algorithm. Parameters ---------- images : `list` of `menpo.image.Image` The `list` of training images. gt_shapes : `list` of `menpo.shape.PointCloud` The `list` of ground truth shapes that correspond to the images. bounding_boxes : `list` of `list` of `menpo.shape.PointDirectedGraph` The `list` of `list` of perturbed bounding boxes per image. prefix : `str`, optional Prefix str for verbose. verbose : `bool`, optional If ``True``, then information about the training is printed. """ if verbose and self.dlib_options.oversampling_amount > 1: n_menpofit_peturbations = len(bounding_boxes[0]) n_dlib_perturbations = self.dlib_options.oversampling_amount total_perturbations = (n_menpofit_peturbations * n_dlib_perturbations) print_dynamic('{}WARNING: Dlib oversampling is being used. ' '{} = {} * {} total perturbations will be generated ' 'by Dlib!\n'.format(prefix, total_perturbations, n_menpofit_peturbations, n_dlib_perturbations)) im_pixels = [image_to_dlib_pixels(im) for im in images] detections = [] for bboxes, im, gt_s in zip(bounding_boxes, images, gt_shapes): fo_dets = [bounding_box_pointcloud_to_dlib_fo_detection(bb, gt_s) for bb in bboxes] detections.append(fo_dets) if verbose: print_dynamic('{}Performing Dlib training - please see stdout ' 'for verbose output provided by Dlib!'.format(prefix)) # Perform DLIB training self.dlib_options.be_verbose = verbose self.dlib_model = dlib.train_shape_predictor( im_pixels, detections, self.dlib_options) for bboxes, pix, fo_dets in zip(bounding_boxes, im_pixels, detections): for bb, fo_det in zip(bboxes, fo_dets): # Perform prediction pred = dlib_full_object_detection_to_pointcloud( self.dlib_model(pix, fo_det.rect)) # Update bounding box in place bb._from_vector_inplace(pred.bounding_box().as_vector()) if verbose: print_dynamic('{}Training Dlib done.\n'.format(prefix)) return bounding_boxes
def main(): # generate xml file with open('helen-dataset.xml', 'w') as out_xml_file: xml = generate_xml() out_xml_file.write(xml) # set options options = dlib.shape_predictor_training_options() options.num_threads = multiprocessing.cpu_count() # cpu threads options.be_verbose = True train_xml_filename = './helen-dataset.xml' print("Start training") dlib.train_shape_predictor(train_xml_filename, "helen-dataset.dat", options) print("Finish")
def train_model(name, xml): """requires: the model name, and the path to the xml annotations. It trains and saves a new model according to the specified training options and given annotations""" # get the training options options = dlib.shape_predictor_training_options() options.tree_depth = 4 options.nu = 0.1 options.cascade_depth = 15 options.feature_pool_size = 400 options.num_test_splits = 50 options.oversampling_amount = 5 # options.be_verbose = True # tells what is happening during the training options.num_threads = 4 # number of the threads used to train the model # finally, train the model dlib.train_shape_predictor(xml, name, options)
def run(self, model_name): model_filename = get_model_dlib_shape_predictor_filename(model_name) landmark_filename = get_landmark_filename(type='train', model_name=model_name) os.makedirs(os.path.dirname(model_filename), exist_ok=True) # log our training options to the terminal print(f''' landmark_filename={landmark_filename}, predictor_output_filename={model_filename}, options={self._options}''' ) # train_test the shape predictor dlib.train_shape_predictor( dataset_filename=landmark_filename, predictor_output_filename=model_filename, options=self._options )
def test_shape_predictor_params(treeDepth, nu, cascadeDepth, featurePoolSize, numTestSplits, oversamplingAmount, oversamplingTransJitter, padding, lambdaParam): # Grab the default options for dlib's shape predictor and then set the values # based on the current hyperparameter values, casting to ints when appropriate options = dlib.shape_predictor_training_options() options.tree_depth = int(treeDepth) options.nu = nu options.cascade_depth = int(cascadeDepth) options.feature_pool_size = int(featurePoolSize) options.num_test_splits = int(numTestSplits) options.oversampling_amount = int(oversamplingAmount) options.oversampling_translation_jitter = oversamplingTransJitter options.feature_pool_region_padding = padding options.lambda_param = lambdaParam # Tell dlib to be verbose when training and utilize our supplied number of threads when training options.be_verbose = True options.num_threads = procs # Display the current set of options to our terminal print("[INFO] Starting training process...") print(options) sys.stdout.flush() # Train the model using the current set of hyperparameters dlib.train_shape_predictor(config.TRAIN_PATH, config.TEMP_MODEL_PATH, options) # Take the newly trained shape predictor model and evaluate it on both the training and testing set trainingError = dlib.test_shape_predictor(config.TRAIN_PATH, config.TEMP_MODEL_PATH) testingError = dlib.test_shape_predictor(config.TEST_PATH, config.TEMP_MODEL_PATH) # Display the training and testing errors for the current trial print("[INFO] train error: {}".format(trainingError)) print("[INFO] test error: {}".format(testingError)) sys.stdout.flush() # Return the error on the testing set return testingError
def test_params(cascade_depth, padding, nu, tree_depth, num_trees_per_cascade_level, lambda_param, jitter): options = dlib.shape_predictor_training_options() options.feature_pool_region_padding = padding options.cascade_depth = int(cascade_depth) options.nu = nu options.tree_depth = int(tree_depth) options.oversampling_translation_jitter = jitter options.num_trees_per_cascade_level = int(num_trees_per_cascade_level) options.lambda_param = lambda_param options.num_threads = 4 options.be_verbose = True print("start training") print(options) sys.stdout.flush() dlib.train_shape_predictor(training_xml_path, "bbr_predictor.dat", options) print("\nTraining error: ", dlib.test_shape_predictor(training_xml_path, "bbr_predictor.dat")) err = dlib.test_shape_predictor(testing_xml_path, "bbr_predictor.dat") print("\nTesting error: ", err) sys.stdout.flush() return err
def train(self, images, gt_shapes, bounding_boxes, prefix='', verbose=False): if verbose and self.dlib_options.oversampling_amount > 1: n_menpofit_peturbations = len(bounding_boxes[0]) n_dlib_perturbations = self.dlib_options.oversampling_amount total_perturbations = (n_menpofit_peturbations * n_dlib_perturbations) print_dynamic('{}WARNING: Dlib oversampling is being used. ' '{} = {} * {} total perturbations will be generated ' 'by Dlib!\n'.format(prefix, total_perturbations, n_menpofit_peturbations, n_dlib_perturbations)) im_pixels = [image_to_dlib_pixels(im) for im in images] detections = [] for bboxes, im, gt_s in zip(bounding_boxes, images, gt_shapes): fo_dets = [bounding_box_pointcloud_to_dlib_fo_detection(bb, gt_s) for bb in bboxes] detections.append(fo_dets) if verbose: print_dynamic('{}Performing Dlib training - please see stdout ' 'for verbose output provided by Dlib!'.format(prefix)) # Perform DLIB training self.dlib_options.be_verbose = verbose self.dlib_model = dlib.train_shape_predictor( im_pixels, detections, self.dlib_options) for bboxes, pix, fo_dets in zip(bounding_boxes, im_pixels, detections): for bb, fo_det in zip(bboxes, fo_dets): # Perform prediction pred = dlib_full_object_detection_to_pointcloud( self.dlib_model(pix, fo_det.rect)) # Update bounding box in place bb._from_vector_inplace(pred.bounding_box().as_vector()) if verbose: print_dynamic('{}Training Dlib done.\n'.format(prefix)) return bounding_boxes
fileName = 'face' + str(i + 1) + '.jpg' newImg = removeBackground2(img) cv2.imwrite(fileName, newImg) faceBGRemoved2.append(newImg) ##################################### Align the faces ######################## ## Training a predictor, with thanks to http://dlib.net/train_shape_predictor.py.html options = dlib.shape_predictor_training_options() options.oversampling_amount = 300 options.nu = 0.05 options.tree_depth = 2 options.be_verbose = True os.chdir('C:/Users/SYARLAG1/Desktop/Face-Detection') # this may take a while ... uses the images in the folder. DONT run if predictor.dat already exists dlib.train_shape_predictor('testing_with_face_landmarks.xml', "predictor.dat", options) def faceAlign(im): detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("predictor.dat") imNew = detector(im, 1) return np.matrix([[p.x, p.y] for p in predictor(im, imNew[0]).parts()]) ############################################### Finding the mean image ####### maxRows = 0 maxCols = 0 facesCount = len(facesLst)
ap = argparse.ArgumentParser() ap.add_argument("-t", "--training", required=True, help="path to input training XML file") ap.add_argument("-m", "--model", required=True, help="path serialized dlib shape predictor model") args = vars(ap.parse_args()) print("[INFO] setting shape predictor options...") options = dlib.shape_predictor_training_options() options.tree_depth = 4 options.nu = 0.1 options.cascade_depth = 15 options.feature_pool_size = 400 options.num_test_splits = 50 options.oversampling_amount = 5 options.oversampling_translation_jitter = 0.1 options.be_verbose = True options.num_threads = multiprocessing.cpu_count() print("[INFO] shape predictor options:") print(options) print("[INFO] training shape predictor...") dlib.train_shape_predictor(args["training"], args["model"], options)
# to a high amount (300) effectively boosts the training set size, so # that helps this example. options.oversampling_amount = 300 # I'm also reducing the capacity of the model by explicitly increasing # the regularization (making nu smaller) and by using trees with # smaller depths. options.nu = 0.05 options.tree_depth = 2 options.be_verbose = True # dlib.train_shape_predictor() does the actual training. It will save the # final predictor to predictor.dat. The input is an XML file that lists the # images in the training dataset and also contains the positions of the face # parts. training_xml_path = os.path.join(faces_folder, "training_with_face_landmarks.xml") dlib.train_shape_predictor(training_xml_path, "predictor.dat", options) # Now that we have a model we can test it. dlib.test_shape_predictor() # measures the average distance between a face landmark output by the # shape_predictor and where it should be according to the truth data. print("\nTraining accuracy: {}".format( dlib.test_shape_predictor(training_xml_path, "predictor.dat"))) # The real test is to see how well it does on data it wasn't trained on. We # trained it on a very small dataset so the accuracy is not extremely high, but # it's still doing quite good. Moreover, if you train it on one of the large # face landmarking datasets you will obtain state-of-the-art results, as shown # in the Kazemi paper. testing_xml_path = os.path.join(faces_folder, "testing_with_face_landmarks.xml") print("Testing accuracy: {}".format( dlib.test_shape_predictor(testing_xml_path, "predictor.dat")))
# to a high amount (300) effectively boosts the training set size, so # that helps this example. # options.oversampling_amount = 300 # I'm also reducing the capacity of the model by explicitly increasing # the regularization (making nu smaller) and by using trees with # smaller depths. # options.nu = 0.05 # options.tree_depth = 2 options.be_verbose = True # dlib.train_shape_predictor() does the actual training. It will save the # final predictor to predictor.dat. The input is an XML file that lists the # images in the training dataset and also contains the positions of the face # parts. training_xml_path = os.path.join(image_folder, "4Dlib_training_images_with_landmarks.xml") dlib.train_shape_predictor(training_xml_path, output_folder + "/predictor.dat", options) # Now that we have a model we can test it. dlib.test_shape_predictor() # measures the average distance between a face landmark output by the # shape_predictor and where it should be according to the truth data. print("\nTraining error: {}".format( dlib.test_shape_predictor(training_xml_path, output_folder + "/predictor.dat"))) # The real test is to see how well it does on data it wasn't trained on. We # trained it on a very small dataset so the accuracy is not extremely high, but # it's still doing quite good. Moreover, if you train it on one of the large # face landmarking datasets you will obtain state-of-the-art results, as shown # in the Kazemi paper.
# -*- coding: utf-8 -*- import dlib options = dlib.shape_predictor_training_options() dlib.train_shape_predictor('../materials/recursos/training_clock_points.xml', '../materials/recursos/detector_clock_points.dat', options)
training_options.feature_pool_region_padding = 0 #training_options.num_threads = 2 #setting the be_verbose setting to true so the training data will be printed out training_options.be_verbose = True # the training will take in an xml file with the imgs used for the training dataset # so we need to create that xml with the imgs in the given folder xml_name = raw_input("-----------xml file name (including .xml): ") training_xml_path = os.path.join(img_folder, xml_name) logging.debug("-Set the img xml") # now we create the predictor with the settings and imgs model_name = raw_input("-----------model name (including .dat): ") dlib.train_shape_predictor(training_xml_path, model_name, training_options) logging.debug("-Trained the new predictor") print("\n Training Accuracy: {}".format( dlib.test_shape_predictor(training_xml_path, model_name))) #now we wanna test the trained model with faces diffrent than the ones we used in training #testing_xml_path = os.path.join(img_folder, "testing.xml") #logging.debug("-Tested the new predictor") #print("\n Testing Accuracy: {}".format( dlib.test_shape_predictor(testing_xml_path, "trained_landmark_predictor.dat"))) dlib.hit_enter_to_continue()
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/10/1 10:49 # @Author : 周文帆小组 # @Site : # @File : face_re.py # @Software: PyCharm import dlib, os current_path = os.getcwd() faces_path = current_path + '/faces/' options = dlib.shape_predictor_training_options() options.oversampling_amount = 300 options.nu = 0.05 options.tree_depth = 2 options.be_verbose = True train_path = os.path.join(faces_path, 'training_with_face_landmarks.xml') #利用标记好了的xml文件进行人脸特征检测器训练,xml文件在faces文件夹内 dlib.train_shape_predictor(train_path, 'predictor.dat', options) #打印检测器的识别精度 print('\nTraining accuracy:{}'.format( dlib.test_shape_predictor(train_path, 'predictor.dat')))
import os model_name = "shape_predictor_70_face_landmarks.dat" options = dlib.shape_predictor_training_options() options.cascade_depth = 10 options.num_trees_per_cascade_level = 500 options.tree_depth = 4 options.nu = 0.1 options.oversampling_amount = 20 options.feature_pool_size = 400 options.feature_pool_region_padding = 0 options.lambda_param = 0.1 options.num_test_splits = 20 # Tell the trainer to print status messages to the console so we can # see training options and how long the training will take. options.be_verbose = True training_xml_path = "/home/jash/Desktop/JashWork/Advanced-Computer-Vision/data/models/facial_landmark_data/70_points/training_with_face_landmarks.xml" testing_xml_path = "/home/jash/Desktop/JashWork/Advanced-Computer-Vision/data/models/facial_landmark_data/70_points/testing_with_face_landmarks.xml" output_model_path = "/home/jash/Desktop/JashWork/Advanced-Computer-Vision/data/models/" + model_name if os.path.exists(training_xml_path) and os.path.exists(testing_xml_path): dlib.train_shape_predictor(training_xml_path, output_model_path, options) print("Training error: {}".format( dlib.test_shape_predictor(training_xml_path, output_model_path))) print("Testing error: {}".format( dlib.test_shape_predictor(testing_xml_path, output_model_path)))
options = dlib.shape_predictor_training_options() options.cascade_depth = 10 options.num_trees_per_cascade_level = 500 options.tree_depth = 4 options.nu = 0.1 options.oversampling_amount = 20 options.feature_pool_size = 400 options.feature_pool_region_padding = 0 options.lambda_param = 0.1 options.num_test_splits = 20 options.be_verbose = True trainingXMLPath = os.path.join(fldDataDir, "train_face_landmarks_70.xml") testingXMLPath = os.path.join(fldDataDir, "test_face_landmarks_70.xml") outputModelPath = os.path.join(fldDataDir, modelName) print("entering") if (os.path.exists(trainingXMLPath) and os.path.exists(testingXMLPath)): dlib.train_shape_predictor(trainingXMLPath, outputModelPath, options) print("\nTraining Accuracy: {}".format( dlib.test_shape_predictor(trainingXMLPath, outputModelPath))) print("\nTesting Accuracy: {}".format( dlib.test_shape_predictor(testingXMLPath, outputModelPath))) else: print('training and test XML files not found.') print('Please check paths:') print('train: {}'.format(trainingXmlPath)) print('test: {}'.format(testingXmlPath))
import dlib options = dlib.shape_predictor_training_options() options.feature_pool_region_padding = 0.1 options.cascade_depth = 10 options.landmark_relative_padding_mode = False options.nu = 0.05 options.tree_depth = 2 options.oversampling_translation_jitter = 0.1 options.oversampling_amount = 200 options.num_threads = 4 options.be_verbose = True dlib.train_shape_predictor("face_landmarking.xml", "landmark_predictor.dat", options) print( "\nTraining error: ", dlib.test_shape_predictor("face_landmarking.xml", "landmark_predictor.dat"))
# to a high amount (300) effectively boosts the training set size, so # that helps this example. options.oversampling_amount = 300 # I'm also reducing the capacity of the model by explicitly increasing # the regularization (making nu smaller) and by using trees with # smaller depths. options.nu = 0.05 options.tree_depth = 2 options.be_verbose = True # dlib.train_shape_predictor() does the actual training. It will save the # final predictor to predictor.dat. The input is an XML file that lists the # images in the training dataset and also contains the positions of the face # parts. training_xml_path = os.path.join(faces_folder, "training_with_face_landmarks.xml") dlib.train_shape_predictor(training_xml_path, "predictor.dat", options) # Now that we have a model we can test it. dlib.test_shape_predictor() # measures the average distance between a face landmark output by the # shape_predictor and where it should be according to the truth data. print("\nTraining accuracy: {}".format(dlib.test_shape_predictor(training_xml_path, "predictor.dat"))) # The real test is to see how well it does on data it wasn't trained on. We # trained it on a very small dataset so the accuracy is not extremely high, but # it's still doing quite good. Moreover, if you train it on one of the large # face landmarking datasets you will obtain state-of-the-art results, as shown # in the Kazemi paper. testing_xml_path = os.path.join(faces_folder, "testing_with_face_landmarks.xml") print("Testing accuracy: {}".format(dlib.test_shape_predictor(testing_xml_path, "predictor.dat"))) # Now let's use it as you would in a normal application. First we will load it # from disk. We also need to load a face detector to provide the initial
# the current hyperparameter values options = dlib.shape_predictor_training_options() options.tree_depth = p["tree_depth"] options.nu = p["nu"] options.cascade_depth = p["cascade_depth"] options.feature_pool_size = p["feature_pool_size"] options.num_test_splits = p["num_test_splits"] options.oversampling_amount = p["oversampling_amount"] options.oversampling_translation_jitter = p[ "oversampling_translation_jitter"] # Tell dlib to be verbose when training and utilize the supplied number of threads when training options.be_verbose = True options.num_threads = procs # Train the model using the current set of hyperparameters start = time.time() dlib.train_shape_predictor(TRAIN_PATH, TEMP_MODEL_PATH, options) trainingTime = time.time() - start # Evaluate the model on both the training and testing split trainingError = evaluate_model_acc(TRAIN_PATH, TEMP_MODEL_PATH) testingError = evaluate_model_acc(TEST_PATH, TEMP_MODEL_PATH) # Compute an approximate inference speed using the trained shape predictor predictor = dlib.shape_predictor(TEMP_MODEL_PATH) inferenceSpeed = evaluate_model_speed(predictor, IMAGE_PATH) # Determine the model size modelSize = os.path.getsize(TEMP_MODEL_PATH) # Build the row of data that will be written to our CSV file row = [ p["tree_depth"], p["nu"], p["cascade_depth"], p["feature_pool_size"],
# to a high amount (300) effectively boosts the training set size, so # that helps this example. options.oversampling_amount = 300 # I'm also reducing the capacity of the model by explicitly increasing # the regularization (making nu smaller) and by using trees with # smaller depths. options.nu = 0.05 options.tree_depth = 2 options.be_verbose = True # dlib.train_shape_predictor() does the actual training. It will save the # final predictor to predictor.dat. The input is an XML file that lists the # images in the training dataset and also contains the positions of the face # parts. training_xml_path = os.path.join("/annotation", "training_with_face_landmarks.xml") dlib.train_shape_predictor("/", "predictor.dat", options) # Now that we have a model we can test it. dlib.test_shape_predictor() # measures the average distance between a face landmark output by the # shape_predictor and where it should be according to the truth data. print("\nTraining accuracy: {}".format( dlib.test_shape_predictor(training_xml_path, "predictor.dat"))) # The real test is to see how well it does on data it wasn't trained on. We # trained it on a very small dataset so the accuracy is not extremely high, but # it's still doing quite good. Moreover, if you train it on one of the large # face landmarking datasets you will obtain state-of-the-art results, as shown # in the Kazemi paper. testing_xml_path = os.path.join(faces_folder, "testing_with_face_landmarks.xml") print("Testing accuracy: {}".format( dlib.test_shape_predictor(testing_xml_path, "predictor.dat")))
stream = True if k == 102: # F KEY - ADD TO XML if play == False: box = getEasyBox(frame) filename = os.path.splitext(os.path.split(video_path)[1])[0] filepath = 'images/{}_frame{}.jpg'.format(filename, frame_num) cv2.imwrite('projects/' + filepath, frame_store) rx.appendXML(points, box, filepath, xml_path) print('added') #If this is the first entry, train the predictor using the first entry. if model == False: print('Training initial model') dlib.train_shape_predictor(xml_path, predictor_name, options) predictor = dlib.shape_predictor(predictor_name) print('done') model = True else: print('can only add when paused!') cv2.destroyAllWindows() # Export coeffs to text file if export == True: print('exporting coeffs to text file') cap.set(cv2.CAP_PROP_POS_FRAMES, 0) frame_num = 0 coeffs_store = []
# to a high amount (300) effectively boosts the training set size, so # that helps this example. options.oversampling_amount = 300 # I'm also reducing the capacity of the model by explicitly increasing # the regularization (making nu smaller) and by using trees with # smaller depths. options.nu = 0.05 options.tree_depth = 5 #default 3 options.be_verbose = True options.num_threads = 4 #cpu core number # dlib.train_shape_predictor() does the actual training. It will save the # final predictor to predictor.dat. The input is an XML file that lists the # images in the training dataset and also contains the positions of the face # parts. training_xml_path = os.path.join(faces_folder, "labels_cfrs_train.xml") dlib.train_shape_predictor(training_xml_path, "cfrs_shape_predictor_face_landmarks.dat", options) # Now that we have a model we can test it. dlib.test_shape_predictor() # measures the average distance between a face landmark output by the # shape_predictor and where it should be according to the truth data. print("\nTraining accuracy: {}".format( dlib.test_shape_predictor(training_xml_path, "cfrs_shape_predictor_face_landmarks.dat"))) # The real test is to see how well it does on data it wasn't trained on. We # trained it on a very small dataset so the accuracy is not extremely high, but # it's still doing quite good. Moreover, if you train it on one of the large # face landmarking datasets you will obtain state-of-the-art results, as shown # in the Kazemi paper. testing_xml_path = os.path.join(faces_folder, "labels_cfrs_test.xml") print("Testing accuracy: {}".format( dlib.test_shape_predictor(testing_xml_path,
# this value is, the *longer* it will take to train but (potentially) # the more *accurate* your model will be options.num_test_splits = 50 # controls amount of "jitter" (i.e., data augmentation) when training # the shape predictor -- applies the supplied number of random # deformations, thereby performing regularization and increasing the # ability of our model to generalize options.oversampling_amount = 5 # amount of translation jitter to apply -- the dlib docs recommend # values in the range [0, 0.5] options.oversampling_translation_jitter = 0.1 # tell the dlib shape predictor to be verbose and print out status # messages our model trains options.be_verbose = True # number of threads/CPU cores to be used when training -- we default # this value to the number of available cores on the system, but you # can supply an integer value here if you would like options.num_threads = multiprocessing.cpu_count() # log our training options to the terminal print("[INFO] shape predictor options:") print(options) # train the shape predictor print("[INFO] training shape predictor...") dlib.train_shape_predictor(xml_path, model_path, options)