Exemple #1
0
def run_lmc(ds_train, ds_test, local_feature_list, lifted_feature_list,
            mc_params, gamma):
    mc_nodes, _, _, _ = lifted_multicut_workflow(ds_train, ds_test, 0, 0,
                                                 local_feature_list,
                                                 lifted_feature_list,
                                                 mc_params)
    return ds_test.project_mc_result(0, mc_nodes)
def run_lmc(ds_train_name, ds_test_name, mc_params, save_path):

    assert os.path.exists(os.path.split(save_path)[0]), "Please choose an existing folder to save your results"

    # if you have added multiple segmentations, you can choose on which one to run
    # experiments with the seg_id
    seg_id = 0

    # these strings encode the features that are used for the local features
    feature_list = ['raw', 'prob', 'reg']

    # these strings encode the features that will be used for the lifted edges
    feature_list_lifted = ['cluster', 'reg']

    # this factor determines the weighting of lifted vs. local edge costs
    gamma = 2.

    meta.load()
    ds_train = meta.get_dataset(ds_train_name)
    ds_test  = meta.get_dataset(ds_test_name)

    # need to make filters for the trainset beforehand
    ds_train.make_filters(0, mc_params.anisotropy_factor)
    ds_train.make_filters(1, mc_params.anisotropy_factor)

    # use this for running the mc without defected slices
    mc_nodes, _, _, _ = lifted_multicut_workflow(
            ds_train, ds_test,
            seg_id, seg_id,
            feature_list, feature_list_lifted,
            mc_params, gamma = gamma)

    # use this for running the mc with defected slices
    #mc_nodes, _, _, _ = lifted_multicut_workflow_with_defect_correction(
    #        ds_train, ds_test,
    #        seg_id, seg_id,
    #        feature_list, feature_list_lifted,
    #        mc_params, gamma = gamma)

    segmentation = ds_test.project_mc_result(seg_id, mc_nodes)
    vigra.writeHDF5(segmentation, save_path, 'data', compression = 'gzip')
def run_lifted_mc(meta_folder,
                  train_folder,
                  ds_train_names,
                  ds_test_name,
                  save_path,
                  results_name,
                  pre_save_path=None):
    assert os.path.exists(os.path.split(
        save_path)[0]), "Please choose an existing folder to save your results"

    merge_segments = True
    compute_mc = True
    if os.path.isfile(save_path):  # Nothing to do
        compute_mc = False
        merge_segments = False
    else:  # Final result needs to be computed
        if pre_save_path is not None:
            if os.path.isfile(pre_save_path):
                compute_mc = False

    if compute_mc:

        seg_id = 0

        feature_list = ['raw', 'prob', 'reg']
        feature_list_lifted = ['cluster', 'reg']

        gamma = 2.

        ds_train = [
            load_dataset(train_folder, name) for name in ds_train_names
            if name != ds_test_name
        ]
        ds_test = load_dataset(meta_folder, ds_test_name)

        mc_nodes, _, _, _ = lifted_multicut_workflow(ds_train,
                                                     ds_test,
                                                     seg_id,
                                                     seg_id,
                                                     feature_list,
                                                     feature_list_lifted,
                                                     gamma=gamma)

        segmentation = ds_test.project_mc_result(seg_id, mc_nodes)

        # Save in case sth in the following function goes wrong
        if pre_save_path is not None:
            vigra.writeHDF5(segmentation,
                            pre_save_path,
                            results_name,
                            compression='gzip')

    if merge_segments:

        if not compute_mc:
            assert pre_save_path is not None, 'Investigate code, this must not happen!'
            segmentation = vigra.readHDF5(pre_save_path, results_name)

        # # Relabel with connected components
        # segmentation = vigra.analysis.labelVolume(segmentation.astype('uint32'))
        # Merge small segments
        segmentation = merge_small_segments(segmentation.astype('uint32'), 100)

        # Store the final result
        vigra.writeHDF5(segmentation,
                        save_path,
                        results_name,
                        compression='gzip')
Exemple #4
0
def main():
    args = process_command_line()

    out_folder = args.output_folder
    assert os.path.exists(
        out_folder), "Please choose an existing folder for the output"
    cache_folder = os.path.join(out_folder, "cache")

    # init the cache when running experiments the first time
    # if the meta set wasn't saved yet, we need to recreate the cache
    if not os.path.exists(os.path.join(cache_folder, "meta_dict.pkl")):
        init(args.data_folder, cache_folder, args.snemi_mode)

    meta = MetaSet(cache_folder)
    meta.load()

    ds_train = meta.get_dataset("ds_train")
    ds_test = meta.get_dataset("ds_test")

    # experiment settings
    exp_params = ExperimentSettings()

    exp_params.set_rfcache(os.path.join(cache_folder, "rf_cache"))

    # use extra 2d features
    exp_params.set_use2d(True)

    # parameters for learning
    exp_params.set_fuzzy_learning(True)
    exp_params.set_ntrees(500)

    # parameters for lifted multicut
    exp_params.set_lifted_neighborhood(3)

    # features used
    local_feats_list = ("raw", "prob", "reg", "topo")
    # we don't use the multicut feature here, because it can take too long
    lifted_feats_list = ("cluster", "reg")

    if args.snemi_mode:
        exp_params.set_anisotropy(5.)
        exp_params.set_weighting_scheme("all")
        exp_params.set_solver("multicut_exact")
        gamma = 10000.
    else:
        exp_params.set_anisotropy(25.)
        exp_params.set_weighting_scheme("z")
        exp_params.set_solver("multicut_fusionmoves")
        gamma = 2.

    seg_id = 0

    if args.use_lifted:
        print "Starting Lifted Multicut Workflow"

        # have to make filters first due to cutouts...
        ds_train.make_filters(0, exp_params.anisotropy_factor)
        ds_train.make_filters(1, exp_params.anisotropy_factor)
        ds_test.make_filters(0, exp_params.anisotropy_factor)
        ds_test.make_filters(1, exp_params.anisotropy_factor)

        mc_node, mc_edges, mc_energy, t_inf = lifted_multicut_workflow(
            ds_train,
            ds_test,
            seg_id,
            seg_id,
            local_feats_list,
            lifted_feats_list,
            exp_params,
            gamma=gamma,
            weight_z_lifted=True)

        save_path = os.path.join(out_folder,
                                 "lifted_multicut_segmentation.tif")

    else:
        print "Starting Multicut Workflow"
        mc_node, mc_edges, mc_energy, t_inf = multicut_workflow(
            ds_train, ds_test, seg_id, seg_id, local_feats_list, exp_params)

        save_path = os.path.join(out_folder, "multicut_segmentation.tif")

    mc_seg = ds_test.project_mc_result(seg_id, mc_node)

    print "Saving Result to", save_path
    vigra.impex.writeVolume(mc_seg, save_path, '')
def main():
    args = process_command_line()

    out_folder = args.output_folder
    assert os.path.exists(
        out_folder), "Please choose an existing folder for the output"
    cache_folder = os.path.join(out_folder, "cache")

    # if the meta set wasn't saved yet, we need to recreate the cache
    if not os.path.exists(os.path.join(cache_folder, "meta_dict.pkl")):
        init(args.data_folder, cache_folder)

    meta = MetaSet(cache_folder)
    meta.load()

    ds_train = meta.get_dataset("ds_train")
    ds_test = meta.get_dataset("ds_test")

    # experiment settings
    exp_params = ExperimentSettings()

    exp_params.set_rfcache(os.path.join(cache_folder, "rf_cache"))

    # use extra 2d features
    exp_params.set_use2d(True)

    # parameters for learning
    exp_params.set_anisotropy(25.)
    exp_params.set_learn2d(True)
    exp_params.set_ignore_mask(False)
    exp_params.set_ntrees(500)

    exp_params.set_weighting_scheme("z")
    exp_params.set_solver("multicut_fusionmoves")

    local_feats_list = ("raw", "prob", "reg", "topo")
    lifted_feats_list = ("mc", "cluster", "reg")

    seg_id = 0

    if args.use_lifted:
        print "Starting Lifted Multicut Workflow"

        # have to make filters first due to cutouts...
        ds_train.make_filters(0, exp_params.anisotropy_factor)
        ds_train.make_filters(1, exp_params.anisotropy_factor)
        ds_test.make_filters(0, exp_params.anisotropy_factor)
        ds_test.make_filters(1, exp_params.anisotropy_factor)

        mc_node, mc_edges, mc_energy, t_inf = lifted_multicut_workflow(
            ds_train,
            ds_test,
            seg_id,
            seg_id,
            local_feats_list,
            lifted_feats_list,
            exp_params,
            gamma=2.,
            warmstart=False,
            weight_z_lifted=True)

        save_path_seg = os.path.join(out_folder,
                                     "lifted_multicut_segmentation.tif")
        save_path_edge = os.path.join(out_folder,
                                      "lifted_multicut_labeling.tif")

    else:
        print "Starting Multicut Workflow"
        mc_node, mc_edges, mc_energy, t_inf = multicut_workflow(
            ds_train, ds_test, seg_id, seg_id, local_feats_list, exp_params)

        save_path_seg = os.path.join(out_folder, "multicut_segmentation.tif")
        save_path_edge = os.path.join(out_folder, "multicut_labeling.tif")

    mc_seg = ds_test.project_mc_result(seg_id, mc_node)

    print "Saving Segmentation Result to", save_path_seg
    vigra.impex.writeVolume(mc_seg, save_path_seg, '')

    # need to bring results back to the isbi challenge format...
    edge_vol = edges_to_volume(ds_test._rag(seg_id), mc_edges)
    print "Saving Edge Labeling Result to", save_path_edge
    vigra.impex.writeVolume(edge_vol, save_path_edge, '', dtype=np.uint8)