Exemple #1
0
def estim_model_classes_group(list_images, nb_classes, dict_features,
                              sp_size=30, sp_regul=0.2,
                              use_scaler=True, pca_coef=None, model_type='GMM',
                              nb_workers=NB_THREADS):
    """ estimate a model from sequence of input images and return it as result

    :param [ndarray] list_images:
    :param int nb_classes: number of classes
    :param int sp_size: initial size of a superpixel(meaning edge lenght)
    :param float sp_regul: regularisation in range(0;1) where "0" gives elastic
        and "1" nearly square slic
    :param {str: [str]} dict_features: list of features to be extracted
    :param float pca_coef: range (0, 1) or None
    :param bool use_scaler: whether use a scaler
    :param str model_type: model type
    :param int nb_workers: number of jobs running in parallel
    :return:
    """
    list_slic, list_features = list(), list()
    _wrapper_compute = partial(compute_color2d_superpixels_features,
                               sp_size=sp_size, sp_regul=sp_regul,
                               dict_features=dict_features)
    iterate = WrapExecuteSequence(_wrapper_compute, list_images,
                                  desc='compute SLIC & features',
                                  nb_workers=nb_workers)
    for slic, features in iterate:
        list_slic.append(slic)
        list_features.append(features)

    features = np.concatenate(tuple(list_features), axis=0)
    features = np.nan_to_num(features)

    model = estim_class_model(features, nb_classes, model_type, pca_coef, use_scaler)

    return model, list_features
Exemple #2
0
def estim_model_classes_group(list_images,
                              nb_classes=4,
                              clr_space='rgb',
                              sp_size=30,
                              sp_regul=0.2,
                              dict_features=FTS_SET_SIMPLE,
                              pca_coef=None,
                              proba_type='GMM',
                              nb_jobs=NB_THREADS):
    """ estimate a model from sequence of input images and return it as result

    :param [ndarray] list_images:
    :param int nb_classes: number of clasees
    :param str clr_space: chose the color space
    :param int sp_size: initial size of a superpixel(meaning edge lenght)
    :param float sp_regul: regularisation in range(0;1) where "0" gives elastic
                   and "1" nearly square slic
    :param {str: [str]} dict_features: list of features to be extracted
    :param float pca_coef: range (0, 1) or None
    :param str proba_type: model type
    :param int nb_jobs: number of jobs running in parallel
    :return:
    """
    list_slic, list_features = list(), list()
    wrapper_compute = partial(compute_color2d_superpixels_features,
                              sp_size=sp_size,
                              sp_regul=sp_regul,
                              dict_features=dict_features,
                              clr_space=clr_space,
                              fts_norm=False)
    iterate = tl_expt.WrapExecuteSequence(wrapper_compute,
                                          list_images,
                                          nb_jobs=nb_jobs)
    for slic, features in iterate:
        list_slic.append(slic)
        list_features.append(features)

    # for img in list_images:
    #     slic, features = compute_color2d_superpixels_features(img, sp_size,
    #                     sp_regul, dict_features, clr_space, fts_norm=False)
    #     list_slic.append(slic)
    #     list_features.append(features)

    features = np.concatenate(tuple(list_features), axis=0)
    features = np.nan_to_num(features)

    # scaling
    scaler = preprocessing.StandardScaler()
    scaler.fit(features)
    features = scaler.transform(features)

    pca = None
    if pca_coef is not None:
        pca = decomposition.PCA(pca_coef)
        features = pca.fit_transform(features)

    model = seg_gc.estim_class_model(features, nb_classes, proba_type)
    return scaler, pca, model
Exemple #3
0
def pipe_gray3d_slic_features_model_graphcut(
        image,
        nb_classes,
        dict_features,
        spacing=(12, 1, 1),
        sp_size=15,
        sp_regul=0.2,
        gc_regul=0.1,
):
    """ complete pipe-line for segmentation using superpixels, extracting features
    and graphCut segmentation

    :param ndarray image: input RGB image
    :param int sp_size: initial size of a superpixel(meaning edge lenght)
    :param float sp_regul: regularisation in range(0;1) where "0" gives elastic
           and "1" nearly square segments
    :param int nb_classes: number of classes to be segmented(indexing from 0)
    :param tuple(int,int,int) spacing:
    :param float gc_regul: regularisation for GC
    :return list(list(int)): segmentation matrix maping each pixel into a class

    >>> np.random.seed(0)
    >>> image = np.random.random((5, 125, 150)) / 2.
    >>> image[:, :, :75] += 0.5
    >>> segm = pipe_gray3d_slic_features_model_graphcut(image, 2, {'color': ['mean']})
    >>> segm.shape
    (5, 125, 150)
    """
    logging.info('PIPELINE Superpixels-Features-GraphCut')
    slic = segment_slic_img3d_gray(image,
                                   sp_size=sp_size,
                                   relative_compact=sp_regul,
                                   space=spacing)
    # plt.imshow(segments)
    logging.info('extract segments/superpixels features.')
    # f = features.computeColourMean(image, segments)
    features, _ = compute_selected_features_gray3d(image, slic, dict_features)
    # merge features together
    logging.debug('list of features RAW: %r', features.shape)
    features[np.isnan(features)] = 0

    logging.info('norm all features.')
    features, _ = norm_features(features)
    logging.debug('list of features NORM: %r', features.shape)

    model = estim_class_model(features, nb_classes)
    proba = model.predict_proba(features)
    logging.debug('list of probabilities: %r', proba.shape)

    # resultGraph = graphCut.segment_graph_cut_int_vals(segments, prob, gcReg)
    graph_labels = segment_graph_cut_general(slic, proba, image, features,
                                             gc_regul)

    return graph_labels[slic]
Exemple #4
0
def pipe_color2d_slic_features_model_graphcut(image,
                                              nb_classes,
                                              dict_features,
                                              sp_size=30,
                                              sp_regul=0.2,
                                              pca_coef=None,
                                              use_scaler=True,
                                              estim_model='GMM',
                                              gc_regul=1.,
                                              gc_edge_type='model',
                                              debug_visual=None):
    """ complete pipe-line for segmentation using superpixels, extracting features
    and graphCut segmentation

    :param ndarray image: input RGB image
    :param int nb_classes: number of classes to be segmented(indexing from 0)
    :param int sp_size: initial size of a superpixel(meaning edge length)
    :param float sp_regul: regularisation in range(0,1) where 0 gives elastic
                   and 1 nearly square slic
    :param dict dict_features: {clr: list(str)}
    :param float pca_coef: range (0, 1) or None
    :param str estim_model: estimating model
    :param float gc_regul: GC regularisation
    :param str gc_edge_type: graphCut edge type
    :param bool use_scaler: using scaler block in pipeline
    :param debug_visual: dict
    :return list(list(int)): segmentation matrix maping each pixel into a class

    >>> np.random.seed(0)
    >>> image = np.random.random((125, 150, 3)) / 2.
    >>> image[:, :75] += 0.5
    >>> segm, seg_soft = pipe_color2d_slic_features_model_graphcut(
    ...                                         image, 2, {'color': ['mean']})
    >>> segm.shape
    (125, 150)
    >>> seg_soft.shape
    (125, 150, 2)
    """
    logging.info('PIPELINE Superpixels-Features-GMM-GraphCut')
    slic, features = compute_color2d_superpixels_features(image,
                                                          dict_features,
                                                          sp_size=sp_size,
                                                          sp_regul=sp_regul)

    if debug_visual is not None:
        if image.ndim == 2:  # duplicate channels to be like RGB
            image = np.rollaxis(np.tile(image, (3, 1, 1)), 0, 3)
        debug_visual['image'] = image
        debug_visual['slic'] = slic
        debug_visual['slic_mean'] = sk_color.label2rgb(slic, image, kind='avg')

    model = estim_class_model(features, nb_classes, estim_model, pca_coef,
                              use_scaler)
    proba = model.predict_proba(features)
    logging.debug('list of probabilities: %r', proba.shape)

    # gmm = mixture.GaussianMixture(n_components=nb_classes,
    #                               covariance_type='full', max_iter=1)
    # gmm.fit(features, np.argmax(proba, axis=1))
    # proba = gmm.predict_proba(features)

    segm_soft = proba[slic]

    graph_labels = segment_graph_cut_general(slic,
                                             proba,
                                             image,
                                             features,
                                             gc_regul,
                                             gc_edge_type,
                                             debug_visual=debug_visual)
    segm = graph_labels[slic]
    return segm, segm_soft
Exemple #5
0
def pipe_color2d_slic_features_gmm_graphcut(image,
                                            nb_classes=3,
                                            clr_space='rgb',
                                            sp_size=30,
                                            sp_regul=0.2,
                                            gc_regul=1.,
                                            dict_features=FTS_SET_SIMPLE,
                                            proba_type='GMM',
                                            gc_edge_type='model_lT',
                                            pca_coef=None,
                                            dict_debug_imgs=None):
    """ complete pipe-line for segmentation using superpixels, extracting features
    and graphCut segmentation

    :param float gc_regul: GC regularisation
    :param image: input RGB image
    :param int sp_size: initial size of a superpixel(meaning edge lenght)
    :param float sp_regul: regularisation in range(0;1) where "0" gives elastic
                   and "1" nearly square slic
    :param int nb_classes: number of classes to be segmented(indexing from 0)
    :param dict_features: {clr: [str], ...}
    :param str clr_space: use color space
    :param float gc_regul: GC regularisation
    :param str gc_edge_type: graphCut edge type
    :param float pca_coef: range (0, 1) or None
    :param dict_debug_imgs: {str: ...}
    :return [[int]]: segmentation matrix maping each pixel into a class

    >>> np.random.seed(0)
    >>> image = np.random.random((125, 150, 3)) / 2.
    >>> image[:, :75] += 0.5
    >>> segm = pipe_color2d_slic_features_gmm_graphcut(image, nb_classes=2)
    >>> segm.shape
    (125, 150)
    """
    logging.info('PIPELINE Superpixels-Features-GMM-GraphCut')
    slic, features = compute_color2d_superpixels_features(
        image, clr_space, sp_size, sp_regul, dict_features)

    if dict_debug_imgs is not None:
        if image.ndim == 2:  # duplicate channels to be like RGB
            image = np.rollaxis(np.tile(image, (3, 1, 1)), 0, 3)
        dict_debug_imgs['image'] = image
        dict_debug_imgs['slic'] = slic
        dict_debug_imgs['slic_mean'] = sk_color.label2rgb(slic,
                                                          image,
                                                          kind='avg')

    if pca_coef is not None:
        pca = decomposition.PCA(pca_coef)
        features = pca.fit_transform(features)

    model = seg_gc.estim_class_model(features, nb_classes, proba_type)
    proba = model.predict_proba(features)
    logging.debug('list of probabilities: %s', repr(proba.shape))

    gmm = mixture.GaussianMixture(n_components=nb_classes,
                                  covariance_type='full',
                                  max_iter=1)
    gmm.fit(features, np.argmax(proba, axis=1))
    proba = gmm.predict_proba(features)

    graph_labels = seg_gc.segment_graph_cut_general(
        slic,
        proba,
        image,
        features,
        gc_regul,
        gc_edge_type,
        dict_debug_imgs=dict_debug_imgs)
    segm = graph_labels[slic]
    return segm