Exemplo n.º 1
0
def test_image_to_uint8_channels_at_front():
    takeo_copy = takeo.copy()
    np_im = menpo_image_to_uint8(takeo_copy, channels_at_back=False)
    shi = takeo_copy.pixels.shape
    shnp = np_im.shape
    assert np_im.dtype == np.uint8
    assert (shi[0] == shnp[0] and shi[1] == shnp[1] and shi[2] == shnp[2])
Exemplo n.º 2
0
def test_image_to_uint8_channels_at_front():
    takeo_copy = takeo.copy()
    np_im = menpo_image_to_uint8(takeo_copy, channels_at_back=False)
    shi = takeo_copy.pixels.shape
    shnp = np_im.shape
    assert np_im.dtype == np.uint8
    assert (shi[0] == shnp[0] and shi[1] == shnp[1] and shi[2] == shnp[2])
Exemplo n.º 3
0
def test_image_to_uint8():
    takeo_copy = takeo.copy()
    np_im = menpo_image_to_uint8(takeo_copy)
    shi = takeo_copy.pixels.shape
    shnp = np_im.shape
    assert np_im.dtype == np.uint8
    assert (shi[0] == shnp[2] and shi[1] == shnp[0] and shi[2] == shnp[1])
Exemplo n.º 4
0
def test_image_to_uint8():
    takeo_copy = takeo.copy()
    np_im = menpo_image_to_uint8(takeo_copy)
    shi = takeo_copy.pixels.shape
    shnp = np_im.shape
    assert np_im.dtype == np.uint8
    assert (shi[0] == shnp[2] and shi[1] == shnp[0] and shi[2] == shnp[1])
Exemplo n.º 5
0
def test_image_to_uint8_greyscale():
    takeo_copy = takeo.as_greyscale()
    np_im = menpo_image_to_uint8(takeo_copy)
    shi = takeo_copy.pixels.shape
    shnp = np_im.shape
    assert np_im.ndim == 2
    assert np_im.dtype == np.uint8
    assert (shi[1] == shnp[0] and shi[2] == shnp[1])
Exemplo n.º 6
0
def test_image_to_uint8_greyscale():
    takeo_copy = takeo.as_greyscale()
    np_im = menpo_image_to_uint8(takeo_copy)
    shi = takeo_copy.pixels.shape
    shnp = np_im.shape
    assert np_im.ndim == 2
    assert np_im.dtype == np.uint8
    assert (shi[1] == shnp[0] and shi[2] == shnp[1])
Exemplo n.º 7
0
def train_ffld2_detector(positive_images, negative_images, n_components=3,
                         pad_x=6, pad_y=6, interval=5, n_relabel=8,
                         n_datamine=10, max_negatives=24000, C=0.002, J=2.0,
                         overlap=0.5):
    r"""
    Train a DPM using the FFLD2 framework. This is a fairly slow process to
    expect to wait for a while. FFLD2 prints out information at each iteration
    but this will not appear in an IPython notebook, so it is best to run
    this kind of training from the command line.

    This method requires an explicit set of negative images to learn the
    classifier with. The non person images from Pascal VOC 2007 are a good
    example of negative images to train with.

    Parameters
    ----------
    positive_images : `list` of `menpo.image.Image`
        The set of images to learn the detector from. Must have landmarks
        attached to **every** image, a bounding box will be extracted for each
        landmark group.
    negative_images : `list` of `menpo.image.Image`
        The set of images to learn the negative samples of the detector with.
        **No** landmarks need to be attached.
    n_components : `int`
        Number of mixture components (without symmetry).
    pad_x : `int`
        Amount of zero padding in HOG cells (x-direction).
    pad_y : `int`
        Amount of zero padding in HOG cells (y-direction).
    interval : `int`
        Number of levels per octave in the HOG pyramid.
    n_relabel : `int`
        Maximum number of training iterations.
    n_datamine : `int`
        Maximum number of data-mining iterations within each training iteration.
    max_negatives : `int`
        Maximum number of negative images to consider, can be useful for
        reducing training time.
    C : `double`
        SVM regularization constant.
    J : `double`
        SVM positive regularization constant boost.
    overlap : `double`
        Minimum overlap in in latent positive search and non-maxima suppression.

    Returns
    -------
    model : `FFLDMixture`
        The newly trained model.
    """
    positive_image_arrays = []
    negative_image_arrays = []
    positive_bbox_arrays = []

    for image in positive_images:
        image_pixels = menpo_image_to_uint8(image)
        image_pixels = ensure_channel_axis(image_pixels)
        positive_image_arrays.append(image_pixels)
        im_bounding_boxes = []
        for lmark in image.landmarks.values():
            bb = lmark.lms.bounding_box()
            height, width = bb.range()
            min_p, max_p = bb.bounds()
            im_bounding_boxes.append(np.array([min_p[1], min_p[0],
                                               width, height]))
        positive_bbox_arrays.append(im_bounding_boxes)

    for image in negative_images:
        image_pixels = menpo_image_to_uint8(image)
        image_pixels = ensure_channel_axis(image_pixels)
        negative_image_arrays.append(image_pixels)

    return train_model(positive_image_arrays, positive_bbox_arrays,
                       negative_image_arrays, n_components=n_components,
                       pad_x=pad_x, pad_y=pad_y, interval=interval,
                       n_relabel=n_relabel, n_datamine=n_datamine,
                       max_negatives=max_negatives, C=C, J=J, overlap=overlap)
Exemplo n.º 8
0
def train_ffld2_detector(positive_images,
                         negative_images,
                         n_components=3,
                         pad_x=6,
                         pad_y=6,
                         interval=5,
                         n_relabel=8,
                         n_datamine=10,
                         max_negatives=24000,
                         C=0.002,
                         J=2.0,
                         overlap=0.5):
    r"""
    Train a DPM using the FFLD2 framework. This is a fairly slow process to
    expect to wait for a while. FFLD2 prints out information at each iteration
    but this will not appear in an IPython notebook, so it is best to run
    this kind of training from the command line.

    This method requires an explicit set of negative images to learn the
    classifier with. The non person images from Pascal VOC 2007 are a good
    example of negative images to train with.

    Parameters
    ----------
    positive_images : `list` of `menpo.image.Image`
        The set of images to learn the detector from. Must have landmarks
        attached to **every** image, a bounding box will be extracted for each
        landmark group.
    negative_images : `list` of `menpo.image.Image`
        The set of images to learn the negative samples of the detector with.
        **No** landmarks need to be attached.
    n_components : `int`
        Number of mixture components (without symmetry).
    pad_x : `int`
        Amount of zero padding in HOG cells (x-direction).
    pad_y : `int`
        Amount of zero padding in HOG cells (y-direction).
    interval : `int`
        Number of levels per octave in the HOG pyramid.
    n_relabel : `int`
        Maximum number of training iterations.
    n_datamine : `int`
        Maximum number of data-mining iterations within each training iteration.
    max_negatives : `int`
        Maximum number of negative images to consider, can be useful for
        reducing training time.
    C : `double`
        SVM regularization constant.
    J : `double`
        SVM positive regularization constant boost.
    overlap : `double`
        Minimum overlap in in latent positive search and non-maxima suppression.

    Returns
    -------
    model : `FFLDMixture`
        The newly trained model.
    """
    positive_image_arrays = []
    negative_image_arrays = []
    positive_bbox_arrays = []

    for image in positive_images:
        image_pixels = menpo_image_to_uint8(image)
        image_pixels = ensure_channel_axis(image_pixels)
        positive_image_arrays.append(image_pixels)
        im_bounding_boxes = []
        for lmark in image.landmarks.values():
            bb = lmark.bounding_box()
            height, width = bb.range()
            min_p, max_p = bb.bounds()
            im_bounding_boxes.append(
                np.array([min_p[1], min_p[0], width, height]))
        positive_bbox_arrays.append(im_bounding_boxes)

    for image in negative_images:
        image_pixels = menpo_image_to_uint8(image)
        image_pixels = ensure_channel_axis(image_pixels)
        negative_image_arrays.append(image_pixels)

    return train_model(positive_image_arrays,
                       positive_bbox_arrays,
                       negative_image_arrays,
                       n_components=n_components,
                       pad_x=pad_x,
                       pad_y=pad_y,
                       interval=interval,
                       n_relabel=n_relabel,
                       n_datamine=n_datamine,
                       max_negatives=max_negatives,
                       C=C,
                       J=J,
                       overlap=overlap)
Exemplo n.º 9
0
def train_dlib_detector(images, epsilon=0.01, add_left_right_image_flips=False,
                        verbose_stdout=False, C=5, detection_window_size=6400,
                        num_threads=None):
    r"""
    Train a dlib detector with the given list of images.

    This is intended to easily train a list of menpo images that have their
    bounding boxes attached as landmarks. Each landmark group on the image
    will have a tight bounding box extracted from it and then dlib will
    train given these images.

    Parameters
    ----------
    images : `list` of `menpo.image.Image`
        The set of images to learn the detector from. Must have landmarks
        attached to **every** image, a bounding box will be extracted for each
        landmark group.
    epsilon : `float`, optional
        The stopping epsilon.  Smaller values make the trainer's solver more
        accurate but might take longer to train.
    add_left_right_image_flips : `bool`, optional
        If ``True``, assume the objects are left/right symmetric and add in
        left right flips of the training images.  This doubles the size of the
        training dataset.
    verbose_stdout : `bool`, optional
        If ``True``, will allow dlib to output its verbose messages. These
        will only be printed to the stdout, so will **not** appear in an IPython
        notebook.
    C : `int`, optional
        C is the usual SVM C regularization parameter.  Larger values of C will
        encourage the trainer to fit the data better but might lead to
        overfitting.
    detection_window_size : `int`, optional
        The number of pixels inside the sliding window used. The default
        parameter of ``6400 = 80 * 80`` window size.
    num_threads : `int` > 0 or ``None``
        How many threads to use for training. If ``None``, will query
        multiprocessing for the number of cores.

    Returns
    -------
    detector : `dlib.simple_object_detector`
        The trained detector. To save this detector, call save on the returned
        object and pass a string path.

    Examples
    --------
    Training a simple object detector from a list of menpo images and save it
    for later use:

    >>> images = list(mio.import_images('./images/path'))
    >>> in_memory_detector = train_dlib_detector(images, verbose_stdout=True)
    >>> in_memory_detector.save('in_memory_detector.svm')
    """
    rectangles = [[pointgraph_to_rect(lgroup.lms.bounding_box())
                  for lgroup in im.landmarks.values()]
                  for im in images]
    image_pixels = [menpo_image_to_uint8(im) for im in images]

    if num_threads is None:
        import multiprocessing

        num_threads = multiprocessing.cpu_count()

    options = dlib.simple_object_detector_training_options()
    options.epsilon = epsilon
    options.add_left_right_image_flips = add_left_right_image_flips
    options.be_verbose = verbose_stdout
    options.C = C
    options.detection_window_size = detection_window_size
    options.num_threads = num_threads

    return dlib.train_simple_object_detector(image_pixels, rectangles, options)
Exemplo n.º 10
0
def train_dlib_detector(images,
                        output_path,
                        epsilon=0.01,
                        add_left_right_image_flips=False,
                        verbose_stdout=False,
                        C=5,
                        detection_window_size=6400,
                        num_threads=None):
    r"""
    Train a dlib detector with the given list of images.

    This is intended to easily train a list of menpo images that have their
    bounding boxes attached as landmarks. Each landmark group on the image
    will have a tight bounding box extracted from it and then dlib will
    train given these images. At the moment, the output is written to file
    and must be loaded back up when training is completed. No verbose
    messages will be provided by default.

    Parameters
    ----------
    images : list of menpo.image.Image
        The set of images to learn the detector from. Must have landmarks
        attached to **every** image, a bounding box will be extracted for each
        landmark group.
    output_path : Path or str
        The output path for dlib to save the detector to.
    epsilon : float, optional
        The stopping epsilon.  Smaller values make the trainer's solver more
        accurate but might take longer to train.
    add_left_right_image_flips : bool, optional
        If ``True``, assume the objects are left/right symmetric and add in
        left right flips of the training images.  This doubles the size of the
        training dataset.
    verbose_stdout : bool, optional
        If ``True``, will allow dlib to output its verbose messages. These
        will only be printed to the stdout, so will **not** appear in an IPython
        notebook.
    C : int, optional
        C is the usual SVM C regularization parameter.  Larger values of C will
        encourage the trainer to fit the data better but might lead to
        overfitting.
    detection_window_size : int, optional
        The number of pixels inside the sliding window used. The default
        parameter of 6400 = 80 * 80 window size.
    num_threads : int > 0 or None
        How many threads to use for training. If ``None``, will query
        multiprocessing for the number of cores.
    """
    rectangles = [[
        pointgraph_to_rect(lgroup.lms.bounding_box())
        for lgroup in im.landmarks.values()
    ] for im in images]
    images = [menpo_image_to_uint8(im) for im in images]

    if num_threads is None:
        import multiprocessing

        num_threads = multiprocessing.cpu_count()

    options = dlib.simple_object_detector_training_options()
    options.epsilon = epsilon
    options.add_left_right_image_flips = add_left_right_image_flips
    options.be_verbose = verbose_stdout
    options.C = C
    options.detection_window_size = detection_window_size
    options.num_threads = num_threads

    output_path_str = str(output_path)
    dlib.train_simple_object_detector(images, rectangles, output_path_str,
                                      options)
Exemplo n.º 11
0
def train_dlib_detector(images,
                        epsilon=0.01,
                        add_left_right_image_flips=False,
                        verbose_stdout=False,
                        C=5,
                        detection_window_size=6400,
                        num_threads=None):
    r"""
    Train a dlib detector with the given list of images.

    This is intended to easily train a list of menpo images that have their
    bounding boxes attached as landmarks. Each landmark group on the image
    will have a tight bounding box extracted from it and then dlib will
    train given these images.

    Parameters
    ----------
    images : `list` of `menpo.image.Image`
        The set of images to learn the detector from. Must have landmarks
        attached to **every** image, a bounding box will be extracted for each
        landmark group.
    epsilon : `float`, optional
        The stopping epsilon.  Smaller values make the trainer's solver more
        accurate but might take longer to train.
    add_left_right_image_flips : `bool`, optional
        If ``True``, assume the objects are left/right symmetric and add in
        left right flips of the training images.  This doubles the size of the
        training dataset.
    verbose_stdout : `bool`, optional
        If ``True``, will allow dlib to output its verbose messages. These
        will only be printed to the stdout, so will **not** appear in an IPython
        notebook.
    C : `int`, optional
        C is the usual SVM C regularization parameter.  Larger values of C will
        encourage the trainer to fit the data better but might lead to
        overfitting.
    detection_window_size : `int`, optional
        The number of pixels inside the sliding window used. The default
        parameter of ``6400 = 80 * 80`` window size.
    num_threads : `int` > 0 or ``None``
        How many threads to use for training. If ``None``, will query
        multiprocessing for the number of cores.

    Returns
    -------
    detector : `dlib.simple_object_detector`
        The trained detector. To save this detector, call save on the returned
        object and pass a string path.

    Examples
    --------
    Training a simple object detector from a list of menpo images and save it
    for later use:

    >>> images = list(mio.import_images('./images/path'))
    >>> in_memory_detector = train_dlib_detector(images, verbose_stdout=True)
    >>> in_memory_detector.save('in_memory_detector.svm')
    """
    rectangles = [[
        pointgraph_to_rect(lgroup.lms.bounding_box())
        for lgroup in im.landmarks.values()
    ] for im in images]
    image_pixels = [menpo_image_to_uint8(im) for im in images]

    if num_threads is None:
        import multiprocessing

        num_threads = multiprocessing.cpu_count()

    options = dlib.simple_object_detector_training_options()
    options.epsilon = epsilon
    options.add_left_right_image_flips = add_left_right_image_flips
    options.be_verbose = verbose_stdout
    options.C = C
    options.detection_window_size = detection_window_size
    options.num_threads = num_threads

    return dlib.train_simple_object_detector(image_pixels, rectangles, options)