예제 #1
0
def main(cfg):

    data = dict()
    out_path = pjoin(cfg.out_path, '{}'.format(cfg.exp_name),
                     'ksp_segmentation')

    if (os.path.exists(out_path)):
        print('ouput path {} already exists.'.format(out_path))
        # return cfg
    else:
        os.makedirs(out_path)

    # Set logger
    print('-' * 10)
    print('starting segmentation on: {}'.format(cfg.in_path))
    print('2d locs filename: {}'.format(cfg.locs_fname))
    print('Output path: {}'.format(out_path))
    print('-' * 10)

    precomp_desc_path = pjoin(cfg.in_path, cfg.precomp_dir)
    if (not os.path.exists(precomp_desc_path)):
        os.makedirs(precomp_desc_path)

    with open(pjoin(out_path, 'cfg.yml'), 'w') as outfile:
        yaml.dump(cfg.__dict__, stream=outfile, default_flow_style=False)

    # ---------- Descriptors/superpixel costs
    spext = SuperpixelExtractor(cfg.in_path, cfg.precomp_dir,
                                cfg.slic_compactness, cfg.slic_n_sp)
    spext.run()

    link_agent, desc_df = make_link_agent(cfg)

    print('Building superpixel managers')
    sps_man = spm.SuperpixelManager(cfg.in_path, cfg.precomp_dir,
                                    link_agent.labels, desc_df,
                                    cfg.init_radius)
    print('Using foreground model from model')
    pm = utls.probas_to_df(link_agent.labels, link_agent.obj_preds)

    g_for = gtrack.GraphTracking(link_agent, sps_man=sps_man)

    g_back = gtrack.GraphTracking(link_agent, sps_man=sps_man)

    find_new_forward = True
    find_new_backward = True
    i = 0

    pos_sp_for = []
    pos_sp_back = []
    list_ksp = []
    pos_tls_for = None
    pos_tls_back = None

    dict_ksp = dict()

    # make forward and backward graphs
    g_back.make_graph(desc_df,
                      pm,
                      cfg.pm_thr,
                      cfg.norm_neighbor,
                      direction='backward',
                      labels=link_agent.labels)
    g_for.make_graph(desc_df,
                     pm,
                     cfg.pm_thr,
                     cfg.norm_neighbor,
                     direction='forward',
                     labels=link_agent.labels)

    print("Computing KSP on backward graph.")
    sps = g_back.run()
    dict_ksp['backward_sets'] = sps

    print("Computing KSP on forward graph.")
    sps = g_for.run()
    dict_ksp['forward_sets'] = sps

    all_sps = list(set(dict_ksp['forward_sets'] + dict_ksp['backward_sets']))
    print('got ', len(all_sps), ' unique superpixels')

    # Saving
    fileOut = pjoin(out_path, 'results.npz')
    data = dict()
    data['ksp_scores_mat'] = utls.get_binary_array(link_agent.labels,
                                                   np.array(all_sps))
    data['pm_scores_mat'] = utls.get_pm_array(link_agent.labels, pm)
    data['paths_back'] = dict_ksp['backward_sets']
    data['paths_for'] = dict_ksp['forward_sets']
    data['all_sps'] = all_sps
    print("Saving results and cfg to: " + fileOut)
    np.savez(fileOut, **data)

    print('Finished experiment: ', out_path)

    write_frames_results.main(cfg, out_path)
    comp_scores.main(cfg, out_path)

    return cfg
예제 #2
0
def main(cfg):

    data = dict()

    d = datetime.datetime.now()
    cfg.run_dir = pjoin(cfg.out_path, '{}'.format(cfg.exp_name))

    if (os.path.exists(cfg.run_dir)):
        print('run dir {} already exists.'.format(cfg.run_dir))
        return cfg
    else:
        os.makedirs(cfg.run_dir)

    # Set logger
    utls.setup_logging(cfg.run_dir)
    logger = logging.getLogger('ksp')

    logger.info('-' * 10)
    logger.info('starting experiment on: {}'.format(cfg.in_path))
    logger.info('2d locs filename: {}'.format(cfg.csv_fname))
    logger.info('Output path: {}'.format(cfg.run_dir))
    logger.info('-' * 10)

    precomp_desc_path = pjoin(cfg.in_path, cfg.precomp_dir)
    if (not os.path.exists(precomp_desc_path)):
        os.makedirs(precomp_desc_path)

    with open(pjoin(cfg.run_dir, 'cfg.yml'), 'w') as outfile:
        yaml.dump(cfg.__dict__, stream=outfile, default_flow_style=False)

    # ---------- Descriptors/superpixel costs
    dm = DataManager(cfg.in_path, cfg.precomp_dir, feats_mode=cfg.feats_mode)
    dm.calc_superpix(cfg.slic_compactness, cfg.slic_n_sp)

    link_agent, desc_df = make_link_agent(cfg)

    logger.info('Building superpixel managers')
    sps_man = spm.SuperpixelManager(cfg.in_path,
                                    cfg.precomp_dir,
                                    link_agent.labels,
                                    desc_df,
                                    init_radius=cfg.sp_trans_init_radius,
                                    hoof_n_bins=cfg.hoof_n_bins)

    locs2d_sps = link_agent.get_all_entrance_sps(desc_df)
    desc_df['positive'] = locs2d_sps

    if (cfg.use_siam_pred):
        pm = utls.probas_to_df(link_agent.labels, link_agent.obj_preds)
    else:
        pm = utls.calc_pm(desc_df, desc_df['positive'], cfg.bag_n_feats,
                          cfg.bag_t, cfg.bag_max_depth, cfg.bag_max_samples,
                          cfg.bag_jobs)

    ksp_scores_mat = []

    g_for = gtrack.GraphTracking(link_agent, sps_man=sps_man)

    g_back = gtrack.GraphTracking(link_agent, sps_man=sps_man)

    find_new_forward = True
    find_new_backward = True
    i = 0

    pos_sp_for = []
    pos_sp_back = []
    list_ksp = []
    pos_tls_for = None
    pos_tls_back = None

    dict_ksp = dict()
    while ((find_new_forward or find_new_backward) and (i < cfg.n_iters_ksp)):

        logger.info("i: " + str(i + 1))

        if ((i > 0) & find_new_forward):
            g_for.merge_tracklets_temporally(pos_tls_for, pm, desc_df,
                                             cfg.pm_thr)

        if ((i > 0) & find_new_backward):
            g_back.merge_tracklets_temporally(pos_tls_back, pm, desc_df,
                                              cfg.pm_thr)
        # Make backward graph
        if (find_new_backward):

            g_back.makeFullGraph(desc_df,
                                 pm,
                                 cfg.pm_thr,
                                 cfg.hoof_tau_u,
                                 cfg.norm_neighbor,
                                 direction='backward',
                                 labels=link_agent.labels)

            logger.info("Computing KSP on backward graph. (i: {}".format(i +
                                                                         1))
            g_back.run()
            dict_ksp['backward_sets'], pos_tls_back = utls.ksp2sps(
                g_back.kspSet, g_back.tracklets)
            dict_ksp['backward_tracklets'] = g_back.tracklets
            dict_ksp['backward_costs'] = g_back.costs

        # Make forward graph
        if (find_new_forward):

            g_for.makeFullGraph(desc_df,
                                pm,
                                cfg.pm_thr,
                                cfg.hoof_tau_u,
                                cfg.norm_neighbor,
                                direction='forward',
                                labels=link_agent.labels)

            logger.info("Computing KSP on forward graph. (i: {})".format(i +
                                                                         1))
            g_for.run()
            dict_ksp['forward_sets'], pos_tls_for = utls.ksp2sps(
                g_for.kspSet, g_for.tracklets)
            dict_ksp['forward_tracklets'] = g_for.tracklets

        if ((find_new_forward or find_new_backward)):

            ksp_scores_mat = utls.sp_tuples_to_mat(
                dict_ksp['forward_sets'] + dict_ksp['backward_sets'],
                link_agent.labels)

            # Update marked superpixels if graph is not "finished"
            if (find_new_forward):
                marked_for = [
                    m for sublist in dict_ksp['forward_sets'] for m in sublist
                ]
                pos_sp_for.append(len(marked_for))

                logger.info("""Forward graph. Number of positive sps
                            of ksp at iteration {}: {}""".format(
                    i + 1, len(marked_for)))
                if (i > 0):
                    if (pos_sp_for[-1] <= pos_sp_for[-2]):
                        find_new_forward = False

            if (find_new_backward):
                marked_back = [
                    m for sublist in dict_ksp['backward_sets'] for m in sublist
                ]
                pos_sp_back.append(len(marked_back))

                logger.info("""Backward graph. Number of positive sps of
                                ksp at iteration {}: {} """.format(
                    i + 1, len(marked_back)))
                if (i > 0):
                    if (pos_sp_back[-1] <= pos_sp_back[-2]):
                        find_new_backward = False

            list_ksp.append(dict_ksp)

            n_pix_ksp = np.sum((ksp_scores_mat > 0).ravel())
            logger.info("""Number hit pixels of ksp at iteration {}:
                        {}""".format(i + 1, n_pix_ksp))

            fileOut = pjoin(cfg.run_dir, 'pm_scores_iter_{}.npz'.format(i))
            data = dict()
            data['ksp_scores_mat'] = ksp_scores_mat
            np.savez(fileOut, **data)

            # Recompute PM values
            if (i + 1 < cfg.n_iters_ksp):
                desc_df = merge_positives(desc_df, marked_for, marked_back)
                pm = utls.calc_pm(desc_df, desc_df['positive'],
                                  cfg.bag_n_feats, cfg.bag_t,
                                  cfg.bag_max_depth, cfg.bag_max_samples,
                                  cfg.bag_jobs)

            i += 1

    # Saving
    fileOut = pjoin(cfg.run_dir, 'results.npz')
    data = dict()
    data['n_iters_ksp'] = cfg.n_iters_ksp
    data['ksp_scores_mat'] = ksp_scores_mat
    data['pm_scores_mat'] = utls.get_pm_array(link_agent.labels, pm)
    data['paths_back'] = dict_ksp['backward_sets']
    data['paths_for'] = dict_ksp['forward_sets']
    logger.info("Saving results and cfg to: " + fileOut)
    np.savez(fileOut, **data)

    logger.info("done")

    logger.info('Finished experiment: ' + cfg.run_dir)

    write_frames_results.main(cfg, cfg.run_dir, logger)
    comp_scores.main(cfg)

    return cfg
예제 #3
0
]

# all_datasets = [
#     'Dataset12', 'Dataset13'
# ]

# all_datasets = [
#     'Dataset21', 'Dataset22', 'Dataset23', 'Dataset24', 'Dataset25'
# ]

# all_datasets = [
#     'Dataset32', 'Dataset33', 'Dataset34', 'Dataset35'
# ]

all_confs = []

confs_fold = []

# Run KSP on all seqs with first gaze-set and make prediction experiment
for i in range(len(all_datasets)):
    set_confs = []
    extra_cfg['ds_dir'] = all_datasets[i]
    print("dset: " + all_datasets[i])
    for k in [1]:
        #for k in ['_missing']:
        #for k in [1, 2, 3, 4, 5]:
        extra_cfg['csvFileName_fg'] = 'video' + str(k) + '.csv'
        conf, logger = iterative_ksp.main(extra_cfg)
        writef.main(conf, logger=logger)
        #conf = test_trans_costs.main(extra_cfg)
예제 #4
0
import os
from ksptrack import iterative_ksp
import numpy as np
import datetime
import numpy as np
import matplotlib.pyplot as plt
from ksptrack.utils import write_frames_results as writef
from ksptrack.cfgs import cfg

dir_ = [
    '/home/laurent.lejeune/medical-labeling/Dataset10/results/2017-12-06_14-26-17_exp',
    '/home/krakapwa/otlshare/laurent.lejeune/medical-labeling/Dataset11/results/2017-12-06_16-16-27_exp',
    '/home/krakapwa/otlshare/laurent.lejeune/medical-labeling/Dataset12/results/2017-12-06_17-28-07_exp',
    '/home/krakapwa/otlshare/laurent.lejeune/medical-labeling/Dataset13/results/2017-12-06_20-50-10_exp'
]

for d in dir_:

    conf_path = os.path.join(d, 'cfg.yml')

    conf = cfg.load_and_convert(conf_path)
    conf.dataOutImageResultDir = 'results'

    # Write result frames
    writef.main(conf)