예제 #1
0
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)
예제 #2
0
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")))
예제 #3
0
 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)
예제 #4
0
 def dlib(threads=4):
     '''same options used by dlib shape predictors'''
     options = dlib.shape_predictor_training_options()
     options.oversampling_amount = 40
     options.num_threads = threads
     options.cascade_depth = 15
     options.feature_pool_size = 800
     options.num_test_splits = 150
     options.be_verbose = True
     return options
예제 #5
0
def config_to_training_options(config: dict):
    result = dlib.shape_predictor_training_options()

    for option, value in config.items():
        if not value:
            continue
        if hasattr(result, option):
            setattr(result, option, value)

    return result
예제 #6
0
 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)
예제 #7
0
def get_training_options(tree_depth=4, nu=0.1, cascade_depth=15, verbose=True, 
                         pool_size=1000, num_test_splits=250, oversampling=10, num_threads=8):
    options = dlib.shape_predictor_training_options()
    options.tree_depth = tree_depth
    options.nu = nu
    options.cascade_depth = cascade_depth
    options.feature_pool_size = pool_size
    options.num_test_splits = num_test_splits
    options.oversampling_amount = oversampling
    options.be_verbose = verbose
    options.num_threads = num_threads
    return options
    def __init__(self, model):
        if isinstance(model, STRING_TYPES) or isinstance(model, Path):
            m_path = Path(model)
            if not Path(m_path).exists():
                raise ValueError('Model {} does not exist.'.format(m_path))
            model = dlib.shape_predictor(str(m_path))

        # Dlib doesn't expose any information about how the model was built,
        # so we just create dummy options
        self.algorithm = DlibAlgorithm(dlib.shape_predictor_training_options(),
                                       n_iterations=0)
        self.algorithm.dlib_model = model
        self.scales = [1]
예제 #9
0
 def create(tree_depth, cascades, pool_size, splits,
            threads=4, oversampling=20):
     '''returns the option obj with the given values'''
     options = dlib.shape_predictor_training_options()
     options.tree_depth = tree_depth
     options.nu = 0.1
     options.num_threads = threads
     options.cascade_depth = cascades
     options.be_verbose = True
     options.feature_pool_size = pool_size
     options.num_test_splits = splits
     options.oversampling_amount = oversampling
     return options
예제 #10
0
 def fastest(threads=4, oversampling=20):
     '''use to train a minified shape-predictor, but
     still accurate'''
     options = dlib.shape_predictor_training_options()
     options.tree_depth = 3
     options.nu = 0.1
     options.num_threads = threads
     options.cascade_depth = 13
     options.be_verbose = True
     options.feature_pool_size = 100
     options.num_test_splits = 250
     options.oversampling_amount = oversampling
     return options
예제 #11
0
 def optimal(threads=4, oversampling=20, tree_depth=10):
     '''train a balanced shape predictor
     in terms of speed, accuracy and size'''
     options = dlib.shape_predictor_training_options()
     options.tree_depth = 3
     options.nu = 0.1
     options.num_threads = threads
     options.cascade_depth = tree_depth
     options.be_verbose = True
     options.feature_pool_size = 150
     options.num_test_splits = 350
     options.oversampling_amount = oversampling
     return options
예제 #12
0
 def accurate(threads=4, oversampling=20):
     '''use to train an accurate shape-predictor, decrease [oversampling]
     for a faster training'''
     options = dlib.shape_predictor_training_options()
     options.tree_depth = 4
     options.nu = 0.1
     options.num_threads = threads
     options.cascade_depth = 15
     options.be_verbose = True
     options.feature_pool_size = 800
     options.num_test_splits = 150 + 50
     options.oversampling_amount = oversampling
     return options
예제 #13
0
    def __init__(self, model):
        if isinstance(model, STRING_TYPES) or isinstance(model, Path):
            m_path = Path(model)
            if not Path(m_path).exists():
                raise ValueError('Model {} does not exist.'.format(m_path))
            model = dlib.shape_predictor(str(m_path))

        # Dlib doesn't expose any information about how the model was buit,
        # so we just create dummy options
        self.algorithm = DlibAlgorithm(dlib.shape_predictor_training_options(),
                                       n_iterations=0)
        self.algorithm.dlib_model = model
        self.scales = [1]
예제 #14
0
    def _setup_dlib_options(self, feature_padding, n_pixel_pairs,
                            distance_prior_weighting, regularisation_weight,
                            n_split_tests, n_trees, n_dlib_perturbations,
                            n_tree_levels):
        check_int = partial(checks.check_multi_scale_param, self.n_scales,
                            (int,))
        check_float = partial(checks.check_multi_scale_param, self.n_scales,
                              (float,))
        feature_padding = check_int('feature_padding', feature_padding)
        n_pixel_pairs = check_int('n_pixel_pairs', n_pixel_pairs)
        distance_prior_weighting = check_float('distance_prior_weighting',
                                               distance_prior_weighting)
        regularisation_weight = check_float('regularisation_weight',
                                            regularisation_weight)
        n_split_tests = check_int('n_split_tests', n_split_tests)
        n_trees = check_int('n_trees', n_trees)
        n_dlib_perturbations = check_int('n_dlib_perturbations',
                                         n_dlib_perturbations)
        n_tree_levels = check_int('n_tree_levels', n_tree_levels)
        self._dlib_options_templates = []
        for j in range(self.n_scales):
            new_opts = dlib.shape_predictor_training_options()

            # Size of region within which to sample features for the feature
            # pool, e.g a padding of 0.5 would cause the algorithm to sample
            # pixels from a box that was 2x2 pixels
            new_opts.feature_pool_region_padding = feature_padding[j]
            # P parameter form Kazemi paper
            new_opts.feature_pool_size = n_pixel_pairs[j]
            # Controls how tight the feature sampling should be. Lower values
            # enforce closer features. Opposite of explanation from Kazemi
            # paper, lambda
            new_opts.lambda_param = distance_prior_weighting[j]
            # Boosting regularization parameter - nu from Kazemi paper, larger
            # values may cause overfitting but improve performance on training
            # data
            new_opts.nu = regularisation_weight[j]
            # S from Kazemi paper - Number of split features at each node to
            # sample. The one that gives the best split is chosen.
            new_opts.num_test_splits = n_split_tests[j]
            # K from Kazemi paper - number of weak regressors
            new_opts.num_trees_per_cascade_level = n_trees[j]
            # R from Kazemi paper - amount of times other shapes are sampled
            # as example initialisations
            new_opts.oversampling_amount = n_dlib_perturbations[j]
            # F from Kazemi paper - number of levels in the tree (depth of tree)
            new_opts.tree_depth = n_tree_levels[j]

            self._dlib_options_templates.append(new_opts)
    def _setup_dlib_options(self, feature_padding, n_pixel_pairs,
                            distance_prior_weighting, regularisation_weight,
                            n_split_tests, n_trees, n_dlib_perturbations,
                            n_tree_levels):
        check_int = partial(checks.check_multi_scale_param, self.n_scales,
                            (int, ))
        check_float = partial(checks.check_multi_scale_param, self.n_scales,
                              (float, ))
        feature_padding = check_int('feature_padding', feature_padding)
        n_pixel_pairs = check_int('n_pixel_pairs', n_pixel_pairs)
        distance_prior_weighting = check_float('distance_prior_weighting',
                                               distance_prior_weighting)
        regularisation_weight = check_float('regularisation_weight',
                                            regularisation_weight)
        n_split_tests = check_int('n_split_tests', n_split_tests)
        n_trees = check_int('n_trees', n_trees)
        n_dlib_perturbations = check_int('n_dlib_perturbations',
                                         n_dlib_perturbations)
        n_tree_levels = check_int('n_tree_levels', n_tree_levels)
        self._dlib_options_templates = []
        for j in range(self.n_scales):
            new_opts = dlib.shape_predictor_training_options()

            # Size of region within which to sample features for the feature
            # pool, e.g a padding of 0.5 would cause the algorithm to sample
            # pixels from a box that was 2x2 pixels
            new_opts.feature_pool_region_padding = feature_padding[j]
            # P parameter from Kazemi paper
            new_opts.feature_pool_size = n_pixel_pairs[j]
            # Controls how tight the feature sampling should be. Lower values
            # enforce closer features. Opposite of explanation from Kazemi
            # paper, lambda
            new_opts.lambda_param = distance_prior_weighting[j]
            # Boosting regularization parameter - nu from Kazemi paper, larger
            # values may cause overfitting but improve performance on training
            # data
            new_opts.nu = regularisation_weight[j]
            # S from Kazemi paper - Number of split features at each node to
            # sample. The one that gives the best split is chosen.
            new_opts.num_test_splits = n_split_tests[j]
            # K from Kazemi paper - number of weak regressors
            new_opts.num_trees_per_cascade_level = n_trees[j]
            # R from Kazemi paper - amount of times other shapes are sampled
            # as example initialisations
            new_opts.oversampling_amount = n_dlib_perturbations[j]
            # F from Kazemi paper - number of levels in the tree (depth of tree)
            new_opts.tree_depth = n_tree_levels[j]

            self._dlib_options_templates.append(new_opts)
예제 #16
0
    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)
예제 #17
0
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")
예제 #18
0
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 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
예제 #21
0
def copy_dlib_options(options):
    new_options = dlib.shape_predictor_training_options()
    for p in sorted(filter(lambda x: '__' not in x, dir(options))):
        setattr(new_options, p, getattr(options, p))
    return new_options
예제 #22
0
def copy_dlib_options(options):
    new_options = dlib.shape_predictor_training_options()
    for p in sorted(filter(lambda x: '__' not in x, dir(options))):
        setattr(new_options, p, getattr(options, p))
    return new_options
예제 #23
0
# In this example we are going to train a face detector based on the small
# faces dataset in the examples/faces directory.  This means you need to supply
# the path to this faces folder as a command line argument so we will know
# where it is.
if len(sys.argv) != 2:
    print(
        "Give the path to the examples/faces directory as the argument to this "
        "program. For example, if you are in the python_examples folder then "
        "execute this program by running:\n"
        "    ./train_shape_predictor.py ../examples/faces"
    )
    exit()
faces_folder = sys.argv[1]

options = dlib.shape_predictor_training_options()
# Now make the object responsible for training the model.
# This algorithm has a bunch of parameters you can mess with.  The
# documentation for the shape_predictor_trainer explains all of them.
# You should also read Kazemi's paper which explains all the parameters
# in great detail.  However, here I'm just setting three of them
# differently than their default values.  I'm doing this because we
# have a very small dataset.  In particular, setting the oversampling
# 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
예제 #24
0
def train_model(name, xml):
    '''train and return a dlib shape predictor.
    Training options are:
    cascade_depth:
        The number of cascades created to train the model with. The number of
        trees in the model is = cascade_depth * num_trees_per_cascade_level.
        > default: 10

    feature_pool_region_padding:
        Size of region within which to sample features for the feature pool,
        e.g a padding of 0.5 would cause the algorithm to sample pixels from
        a box that was 2x2 pixels
        > default: 0

    feature_pool_size:
        Number of pixels used to generate features for the random trees at
        each cascade. So in general larger settings of this parameter give
        better accuracy but make the algorithm run slower.
        > default: 400

    lambda_param:
        Controls how tight the feature sampling should be. Lower values enforce
        closer features. To decide how to split nodes in the regression tree
        the algorithm looks at pairs of pixels in the image. These pixel pairs
        are sampled randomly but with a preference for selecting pixels that
        are near each other.
        > default: 0.1

    nu:
        The regularization parameter.
        Larger values of this parameter will cause the algorithm to fit
        the training data better but may also cause overfitting.
        The value must be 0 < nu <= 1.
        > default: 0.1

    num_test_splits:
        Number of split features at each node to sample.
        Larger values of this parameter will usually give more accurate
        outputs but take longer to train.
        > default: 20

    num_trees_per_cascade_level:
        The number of trees created for each cascade.
        > default: 500

    oversampling_amount:
        The number of randomly selected initial starting points sampled
        for each training example. This parameter controls the number of
        randomly selected deformation applied to the training data.
        So the bigger this parameter the better (excepting that larger
        values make training take longer).
        > default: 20

    oversampling_translation_jitter:
        When generating the get_oversampling_amount() factor of extra training
        samples you can also jitter the bounding box by adding random small
        translational shifts. For instance, if you set it to 0.1 then it would
        randomly translate the bounding boxes by between 0% and 10% their
        width and height in the x and y directions respectively. Doing this is
        essentially equivalent to randomly jittering the bounding boxes in the
        training data. So doing this kind of jittering can help make the
        learned model more robust against slightly misplaced bounding boxes.
        > default: 0

    random_seed:
        The random seed used by the internal random number generator
        > default: ""

    tree_depth:
        The depth of the trees used in each cascade.
        There are pow(2, get_tree_depth()) leaves in each tree.
        > default: 4
    '''
    options = dlib.shape_predictor_training_options()
    options.tree_depth = 3  # 4
    options.nu = 0.1
    options.num_threads = 8
    options.cascade_depth = 12  # 15
    options.be_verbose = True

    options.feature_pool_size = 400 + 20  # 400
    options.num_test_splits = 20 + 5
    options.oversampling_amount = 20
    # options.oversampling_translation_jitter = 0.1

    dlib.train_shape_predictor(xml, name, options)
예제 #25
0
    def parameter_model(self, tree, nu, threads, cascade_depth,
                        feature_pool_size, test_splits, os):
        """!
        Réglage du modèle
        @param tree = [num_trees,tree_depth]
            @param --num-trees      number of regression trees (default = 500)
            @param --tree-depth    choice of tree depth (default = 4)
                # define the depth of each regression tree -- there will be a total
                # of 2^tree_depth leaves in each tree; small values of tree_depth
                # will be *faster* but *less accurate* while larger values will
                # generate trees that are *deeper*, *more accurate*, but will run
                # *far slower* when making predictions
                # Typical values for tree_depth are in the range [2, 8].



        @param --nu            regularization parameter (default = 0.1)
            Values closer to 1 will make our model fit the training data closer,
            but could potentially lead to overfitting.
            Values closer to  0 will help our model generalize;
            however, there is a caveat to the generalization power
            — the closer nu is to  0, the more training data you’ll need.
            Typically, for small values of nu you’ll need 1000s of training examples.

        @param --threads       number of threads to be used (default = 1)



        @param --cascade-depth  choice of cascade depth (default = 15)
            # the cascade_depth will have a dramatic impact on both the accuracy and the output file size of your model.
            # in the range [6, 18],

        @param --feature-pool-size choice of feature pool size (default = 500) (could be 400)
            # The feature_pool_size controls the number of pixels used
            # to generate features for the random trees in each cascade.
            # My recommendation here is that you should use large values for feature_pools_size
            # if inference speed is not a concern. Otherwise, you should use smaller values for faster prediction speed



        @param --test-splits    number of test splits (default = 20)
            # selects best features at each cascade when training -- the larger
            # this value is, the *longer* it will take to train but (potentially)
            # the more *accurate* your model will be

        @param --oversampling oversampling amount (default = 10) (could be 5)
            # 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
            # in range (0,50) : 50 means nb_image * 50
            # !!!! can increase a lot training time !!!!
        """
        num_trees, tree_depth = tree[0], tree[1]
        options = dlib.shape_predictor_training_options()
        options.num_trees_per_cascade_level = num_trees
        options.nu = nu
        options.num_threads = threads
        options.tree_depth = tree_depth
        options.cascade_depth = cascade_depth
        options.feature_pool_size = feature_pool_size
        options.num_test_splits = test_splits
        options.oversampling_amount = os
        options.be_verbose = True
        self.options = options
        return self.options
예제 #26
0
def main():

    # construct the argument parser and parse the arguments
    ap = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    ap.add_argument("-p", "--path", required=True, help="path to dataset")
    ap.add_argument("-v", "--verbose", default=True, help="be_verbose flag")
    ap.add_argument("-cd",
                    "--cascade_depth",
                    type=int,
                    default=10,
                    help="cascade_depth value")
    ap.add_argument("-td",
                    "--tree_depth",
                    type=int,
                    default=4,
                    help="tree_depth value")
    ap.add_argument("-ntpcl",
                    "--num_tress_per_cascade_level",
                    type=int,
                    default=500,
                    help="num_tress_per_cascade_level value")
    ap.add_argument("-nu", "--nu", default=0.1, type=float, help="nu value")
    ap.add_argument("-oa",
                    "--oversampling_amount",
                    type=int,
                    default=20,
                    help="oversampling_amount value")
    ap.add_argument("-fps",
                    "--feature_pool_size",
                    type=int,
                    default=400,
                    help="feature_pool_size value")
    ap.add_argument("-l",
                    "--lambda_param",
                    default=0.1,
                    type=float,
                    help="lambda_param value")
    ap.add_argument("-nts",
                    "--num_test_splits",
                    type=int,
                    default=20,
                    help="num_test_splits value")
    ap.add_argument("-fprp",
                    "--feature_pool_region_padding",
                    default=0,
                    type=int,
                    help="feature_pool_region_padding value")
    ap.add_argument("-rs",
                    "--random_seed",
                    default="",
                    help="random_seed value")
    ap.add_argument("-nt",
                    "--num_threads",
                    default=0,
                    type=int,
                    help="num_threads value")
    ap.add_argument("-m", "--model_name", required=True, help="model_name")
    args = ap.parse_args()

    # In this example we are going to train a face detector based on the small
    # faces dataset in the examples/faces directory.  This means you need to supply
    # the path to this faces folder as a command line argument so we will know
    # where it is.
    faces_folder = args.path

    options = dlib.shape_predictor_training_options()
    # Now make the object responsible for training the model.
    # This algorithm has a bunch of parameters you can mess with.  The
    # documentation for the shape_predictor_trainer explains all of them.
    # You should also read Kazemi's paper which explains all the parameters
    # in great detail.  However, here I'm just setting three of them
    # differently than their default values.  I'm doing this because we
    # have a very small dataset.  In particular, setting the oversampling
    # to a high amount (300) effectively boosts the training set size, so
    # that helps this example.
    # options.oversampling_amount = args.oversampling_amount
    # 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.lambda_param = 0.1
    # options.tree_depth = 5
    # options.be_verbose = True
    # options.num_threads = 0
    # model_name = "predictor_1.dat"
    options.be_verbose = args.verbose
    options.cascade_depth = args.cascade_depth
    options.tree_depth = args.tree_depth
    options.num_tress_per_cascade_level = args.num_tress_per_cascade_level
    options.nu = args.nu
    options.oversampling_amount = args.oversampling_amount
    options.feature_pool_size = args.feature_pool_size
    options.lambda_param = args.lambda_param
    options.num_test_splits = args.num_test_splits
    options.feature_pool_region_padding = args.feature_pool_region_padding
    options.random_seed = args.random_seed
    options.num_threads = args.num_threads
    model_name = args.model_name

    # 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_ibug_300W_train.xml")
    dlib.train_shape_predictor(training_xml_path, model_name, 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, model_name)))
    # 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_ibug_300W.xml")
    print("Testing accuracy: {}".format(
        dlib.test_shape_predictor(testing_xml_path, model_name)))

    # 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
    # estimate of the facial location.
    predictor = dlib.shape_predictor(model_name)
    detector = dlib.get_frontal_face_detector()

    # Now let's run the detector and shape_predictor over the images in the faces
    # folder and display the results.
    print(
        "Showing detections and predictions on the images in the faces folder..."
    )
    win = dlib.image_window()
    for f in glob.glob(os.path.join(faces_folder, "*.jpg")):
        print("Processing file: {}".format(f))
        img = io.imread(f)

        win.clear_overlay()
        win.set_image(img)

        # Ask the detector to find the bounding boxes of each face. The 1 in the
        # second argument indicates that we should upsample the image 1 time. This
        # will make everything bigger and allow us to detect more faces.
        dets = detector(img, 1)
        print("Number of faces detected: {}".format(len(dets)))
        for k, d in enumerate(dets):
            print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
                k, d.left(), d.top(), d.right(), d.bottom()))
            # Get the landmarks/parts for the face in box d.
            shape = predictor(img, d)
            print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
                                                      shape.part(1)))
            # Draw the face landmarks on the screen.
            win.add_overlay(shape)

        win.add_overlay(dets)
        dlib.hit_enter_to_continue()
예제 #27
0

# In this example we are going to train a face detector based on the small
# faces dataset in the examples/faces directory.  This means you need to supply
# the path to this faces folder as a command line argument so we will know
# where it is.
if len(sys.argv) != 2:
    print(
        "Give the path to the examples/faces directory as the argument to this "
        "program. For example, if you are in the python_examples folder then "
        "execute this program by running:\n"
        "    ./train_shape_predictor.py ../examples/faces")
    exit()
faces_folder = sys.argv[1]

options = dlib.shape_predictor_training_options()
# Now make the object responsible for training the model.
# This algorithm has a bunch of parameters you can mess with.  The
# documentation for the shape_predictor_trainer explains all of them.
# You should also read Kazemi's paper which explains all the parameters
# in great detail.  However, here I'm just setting three of them
# differently than their default values.  I'm doing this because we
# have a very small dataset.  In particular, setting the oversampling
# 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
예제 #28
0
'''

import sys
import os.path
import multiprocessing
import dlib

# root of project repository
THE_FILE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.abspath(os.path.join(THE_FILE_DIR, '..', '..'))
sys.path.append(PROJECT_ROOT)

from src.train_test.landmarks.sets import LANDMARKS_MODELS
from src.models.source import get_model_dlib_shape_predictor_filename, get_landmark_filename

DEFAULT_SHAPE_PREDICTOR_OPTIONS = dlib.shape_predictor_training_options()

# define the depth of each regression tree -- there will be a total
# of 2^tree_depth leaves in each tree; small values of tree_depth
# will be *faster* but *less accurate* while larger values will
# generate trees that are *deeper*, *more accurate*, but will run
# *far slower* when making predictions
DEFAULT_SHAPE_PREDICTOR_OPTIONS.tree_depth = 4

# regularization parameter in the range [0, 1] that is used to help
# our model generalize -- values closer to 1 will make our model fit
# the training data better, but could cause overfitting; values closer
# to 0 will help our model generalize but will require us to have
# training data in the order of 1000s of data points
DEFAULT_SHAPE_PREDICTOR_OPTIONS.nu = 0.1