Ejemplo n.º 1
0
def make_training_patches_and_fit(arg_cfg, out_dir=None):
    data = dict()

    # Update config
    cfg_dict = cfg.cfg()
    arg_cfg['seq_type'] = cfg.datasetdir_to_type(arg_cfg['ds_dir'])
    cfg_dict.update(arg_cfg)
    conf = cfg.Bunch(cfg_dict)
    conf.out_dir_prefix = 'exp_vilar'

    # Write config to result dir
    if (out_dir is None):
        if ('dataOutDir' not in arg_cfg):
            conf.dataOutDir = utls.getDataOutDir(conf.dataOutRoot, conf.ds_dir,
                                                 conf.resultDir,
                                                 conf.out_dir_prefix,
                                                 conf.testing)
    else:
        conf.dataOutDir = out_dir

    conf.myGaze_fg = utls.readCsv(
        os.path.join(conf.root_path, conf.ds_dir, conf.locs_dir,
                     conf.csvFileName_fg))

    # Set logger
    utls.setup_logging(conf.dataOutDir)

    logger = logging.getLogger('vilar')

    logger.info('---------------------------')
    logger.info('Extracting training a patches and fit on: ' + conf.ds_dir)
    logger.info('type of sequence: ' + conf.seq_type)
    logger.info('gaze filename: ' + conf.csvFileName_fg)
    logger.info('Result dir:')
    logger.info(conf.dataOutDir)
    logger.info('n_frames : ' + str(conf.vilar_n_frames))
    logger.info('(gamma,C) = (' + str(conf.gamma) + ', ' + str(conf.C) + ')')
    logger.info(conf.dataOutDir)
    logger.info('---------------------------')

    # Make frame file names
    gt_dir = os.path.join(conf.root_path, conf.ds_dir, conf.truth_dir)
    gtFileNames = utls.makeFrameFileNames(conf.frame_prefix, conf.frameDigits,
                                          conf.truth_dir, conf.root_path,
                                          conf.ds_dir, conf.frame_extension)

    conf.frameFileNames = utls.makeFrameFileNames(conf.frame_prefix,
                                                  conf.frameDigits,
                                                  conf.frameDir,
                                                  conf.root_path, conf.ds_dir,
                                                  conf.frame_extension)

    # conf.myGaze_fg = utls.readCsv(conf.csvName_fg)
    conf.myGaze_fg = utls.readCsv(
        os.path.join(conf.root_path, conf.ds_dir, conf.locs_dir,
                     conf.csvFileName_fg))

    # conf.myGaze_bg = utls.readCsv(conf.csvName_bg)
    gt_positives = utls.getPositives(gtFileNames)

    conf.precomp_desc_path = os.path.join(conf.dataOutRoot, conf.ds_dir,
                                          conf.feats_dir)

    my_dataset = ds.DatasetVilar(conf)

    with open(os.path.join(conf.dataOutDir, 'cfg.yml'), 'w') as outfile:
        yaml.dump(conf, outfile, default_flow_style=True)

    # Extract seen and unseen patches (to fit SVC)
    if (os.path.exists(
            os.path.join(conf.dataOutDir, 'vilar', 'vilar_seen_patches_df.p'))
            & os.path.exists(
                os.path.join(conf.dataOutDir, 'vilar',
                             'vilar_unseen_patches_df.p'))):
        logger.info('seen and unseen patches already computed. Skipping.')
    else:
        my_dataset.calc_training_patches(save=True)

    if (not os.path.exists(os.path.join(conf.dataOutDir, 'clf.p'))):

        my_dataset.load_patches()

        X_pos = df_to_mat(my_dataset.seen_patches_df)
        X_neg = df_to_mat(my_dataset.unseen_patches_df)
        X_train = np.concatenate((X_pos, X_neg))

        y_pos = np.ones(df_to_mat(my_dataset.seen_patches_df).shape[0])
        y_neg = np.zeros(df_to_mat(my_dataset.unseen_patches_df).shape[0])
        y_train = np.concatenate((y_pos, y_neg))

        clf = SVC(gamma=conf.gamma,
                  C=conf.C,
                  class_weight='balanced',
                  verbose=True)
        logger.info('fitting')
        clf.fit(X_train, y_train)
        with open(os.path.join(conf.dataOutDir, 'clf.p'), 'wb') as f:
            pk.dump(clf, f)
        logger.info('Saved classifier.')
    else:
        with open(os.path.join(conf.dataOutDir, 'clf.p'), 'rb') as f:
            clf = pk.load(f)

    return conf, clf
]
root_dir = '/home/laurent.lejeune/medical-labeling/'
frame_prefix = 'frame_'
frameDigits = 4
frameDir = 'input-frames'
frame_extension = '.png'

overfeat_pdname = 'overfeat.p'

for i in range(len(all_datasets)):
    print('Converting ' + all_datasets[i])
    overfeat_path = os.path.join(root_dir, all_datasets[i], 'EE',
                                 'overfeat_wide')
    overfeat_files = sorted(
        glob.glob(os.path.join(overfeat_path, 'frame*.npz')))
    frame_filenames = utls.makeFrameFileNames(frame_prefix, frameDigits,
                                              frameDir, root_dir,
                                              all_datasets[i], frame_extension)
    feats = []
    for j in range(len(overfeat_files)):
        print('File {}/{}'.format(j + 1, len(overfeat_files)))
        feat = np.load(overfeat_files[j])['features']
        for l in range(feat.shape[0]):
            feats.append((j, int(feat[l, 0]), feat[l, 1:]))

    feat_pd = pd.DataFrame(feats, columns=['frame', 'sp_label', 'desc'])

    feat_pd.to_pickle(
        os.path.join(root_dir, all_datasets[i], 'precomp_descriptors',
                     overfeat_pdname))
Ejemplo n.º 3
0
    return out

dir_root = '/home/laurent.lejeune/medical-labeling/'
ds_dir = ['Dataset12']

feat_path = 'precomp_descriptors/unet/feat.h5'


for i in range(len(ds_dir)):

    print('Interpolating Unet features on:')
    print(ds_dir[i])
    labels = np.load(os.path.join(dir_root,                                      ds_dir[i],'input-frames',                                         'sp_labels.npz'))['sp_labels']
    frameFileNames = utls.makeFrameFileNames(
    'frame_', 4, 'input-frames',
        dir_root, ds_dir[i], 'png')

    im = utls.imread(frameFileNames[0])
    img_height = im.shape[0]
    img_width = im.shape[1]

    # initialize hdf5 files
    hdInFile = h5py.File(os.path.join(dir_root,ds_dir[i],feat_path), 'r')
    feats_arr = hdInFile['raw_feat'][...]
    hdInFile.close()

    modelDepth = 4

    # get feat properties
    feat_h = feats_arr.shape[1]
Ejemplo n.º 4
0
def run_on_dset(dataset_dir):

    # read image
    labelMatPath = 'EE/sp_labels_ml.mat'
    data_root_dir = '/home/laurent.lejeune/medical-labeling/'
    frame_extension = '.png'
    frame_dir = 'input-frames'
    frame_prefix = 'frame_'
    frame_digits = 4

    wide = True
    patch_size = 231

    if (wide):
        feat_out_dir = 'EE/overfeat_wide'
    else:
        feat_out_dir = 'EE/overfeat'

    labels = scipy.io.loadmat(
        os.path.join(data_root_dir, dataset_dir, labelMatPath))['labels']

    filenames = utls.makeFrameFileNames(frame_prefix, frame_digits, frame_dir,
                                        data_root_dir, dataset_dir,
                                        frame_extension)

    # initialize overfeat. Note that this takes time, so do it only once if possible
    overfeat_dir = '/home/laurent.lejeune/Documents/OverFeat'
    overfeat_extr = OverfeatFeatureExtractor(
        os.path.join(overfeat_dir, 'data', 'default'),
        os.path.join(overfeat_dir, 'bin', 'linux_64', 'cuda',
                     'overfeatcmd_cuda'))

    out_path = os.path.join(data_root_dir, dataset_dir, feat_out_dir)

    if not os.path.exists(out_path):
        os.makedirs(out_path)

    selem = square(2)
    for i in range(len(filenames)):
        patches = []
        this_filename = os.path.splitext(os.path.basename(filenames[i]))[0]
        outfile = os.path.join(data_root_dir, dataset_dir, feat_out_dir,
                               this_filename)
        if (not os.path.isfile(outfile + '.npz')):
            print('frame {}/{}'.format(i + 1, len(filenames)))
            features = []
            image = utls.imread(filenames[i])
            if (image.shape[2] > 3): image = image[:, :, 0:3]
            for j in np.unique(labels[:, :, i]):
                if (wide):
                    this_mask = labels[:, :, i] == j
                    this_mask = binary_dilation(this_mask, selem)
                    this_mask_idx = np.where(this_mask)
                    this_mask_labels = np.unique(labels[this_mask_idx[0],
                                                        this_mask_idx[1], i])
                    this_mask = np.in1d(labels[:, :, i],
                                        this_mask_labels).reshape(
                                            image.shape[0], image.shape[1])
                else:
                    this_mask = labels[:, :, i] == j
                i_mask, j_mask = np.where(this_mask)
                w = max(j_mask) - min(j_mask)
                h = max(i_mask) - min(i_mask)
                if (w < h):
                    cols_to_add = h - w + 1
                    idx_i = np.arange(min(i_mask), max(i_mask) + 1).astype(int)
                    idx_j = np.arange(
                        min(j_mask) - np.floor(cols_to_add / 2),
                        max(j_mask) + np.ceil(cols_to_add / 2)).astype(int)
                elif (w > h):
                    rows_to_add = w - h + 1
                    idx_i = np.arange(
                        min(i_mask) - np.floor(rows_to_add / 2),
                        max(i_mask) + np.ceil(rows_to_add / 2)).astype(int)
                    idx_j = np.arange(min(j_mask), max(j_mask) + 1).astype(int)
                else:
                    idx_i = np.arange(min(i_mask), max(i_mask) + 1)
                    idx_j = np.arange(min(j_mask), max(j_mask) + 1)
                patches.append(
                    resize(
                        image.take(idx_i, mode='wrap',
                                   axis=0).take(idx_j, mode='wrap', axis=1),
                        (patch_size, patch_size)).astype(np.float32))

            X = np.asarray(patches)
            features = overfeat_extr.get_feats_overfeat(
                X, np.unique(labels[:, :, i]), '/tmp')
            data = dict()
            data['features'] = features

            np.savez_compressed(outfile + '.npz', **data)
            scipy.io.savemat(outfile + '.mat', data)
        else:
            print("File: " + outfile + " exists. Delete to recompute...")
Ejemplo n.º 5
0
def main(arg_cfg):
    data = dict()

    #Update config
    cfg_dict = cfg.cfg()
    arg_cfg['seq_type'] = cfg.datasetdir_to_type(arg_cfg['ds_dir'])
    cfg_dict.update(arg_cfg)
    conf = cfg.Bunch(cfg_dict)

    #Write config to result dir
    conf.dataOutDir = utls.getDataOutDir(conf.dataOutRoot, conf.ds_dir,
                                         conf.resultDir, conf.out_dir_prefix,
                                         conf.testing)

    #Set logger
    utls.setup_logging(conf.dataOutDir)

    logger = logging.getLogger('feat_extr')

    logger.info('---------------------------')
    logger.info('starting feature extraction on: ' + conf.ds_dir)
    logger.info('type of sequence: ' + conf.seq_type)
    logger.info('gaze filename: ' + conf.csvFileName_fg)
    logger.info('features type: ' + conf.feat_extr_algorithm)
    logger.info('Result dir:')
    logger.info(conf.dataOutDir)
    logger.info('---------------------------')

    #Make frame file names
    gt_dir = os.path.join(conf.root_path, conf.ds_dir, conf.truth_dir)
    gtFileNames = utls.makeFrameFileNames(conf.frame_prefix, conf.truth_dir,
                                          conf.root_path, conf.ds_dir,
                                          conf.frame_extension)

    conf.frameFileNames = utls.makeFrameFileNames(conf.frame_prefix,
                                                  conf.frame_dir,
                                                  conf.root_path, conf.ds_dir,
                                                  conf.frame_extension)

    conf.myGaze_fg = utls.readCsv(
        os.path.join(conf.root_path, conf.ds_dir, conf.locs_dir,
                     conf.csvFileName_fg))

    #conf.myGaze_bg = utls.readCsv(conf.csvName_bg)
    gt_positives = utls.getPositives(gtFileNames)

    if (conf.labelMatPath != ''):
        conf.labelMatPath = os.path.join(conf.dataOutRoot, conf.ds_dir,
                                         conf.frameDir, conf.labelMatPath)

    conf.precomp_desc_path = os.path.join(conf.dataOutRoot, conf.ds_dir,
                                          conf.feats_dir)

    # ---------- Descriptors/superpixel costs
    my_dataset = ds.Dataset(conf)

    my_dataset.load_superpix_from_file()
    my_dataset.calc_sp_feats_unet_gaze_rec(save=True)

    with open(os.path.join(conf.dataOutDir, 'cfg.yml'), 'w') as outfile:
        yaml.dump(conf, outfile, default_flow_style=True)

    logger.info('Finished feature extraction: ' + conf.ds_dir)

    return conf