def test_get_labels_lut_empty_subset(self):
     
     lut = du.get_labels_lut(self.labels, [])
     
     assert_is_instance(lut, np.ndarray)
     assert_equals(lut.dtype, int)
     assert_equals(lut.ndim, 1)
     assert_equals(lut.shape, (len(self.labels)+1,))
     
     assert_list_equal(lut.tolist(), [0]*(len(self.labels)+1))
 def test_get_labels_lut_empty_labels(self):
     
     lut = du.get_labels_lut([], self.subset)
     
     assert_is_instance(lut, np.ndarray)
     assert_equals(lut.dtype, int)
     assert_equals(lut.ndim, 1)
     assert_equals(lut.shape, (1,))
     
     assert_list_equal(lut.tolist(), [0])
Beispiel #3
0
    def test_get_labels_lut_empty_labels(self):

        lut = du.get_labels_lut([], self.subset)

        assert_is_instance(lut, np.ndarray)
        assert_equals(lut.dtype, int)
        assert_equals(lut.ndim, 1)
        assert_equals(lut.shape, (1, ))

        assert_list_equal(lut.tolist(), [0])
Beispiel #4
0
    def test_get_labels_lut_empty_subset(self):

        lut = du.get_labels_lut(self.labels, [])

        assert_is_instance(lut, np.ndarray)
        assert_equals(lut.dtype, int)
        assert_equals(lut.ndim, 1)
        assert_equals(lut.shape, (len(self.labels) + 1, ))

        assert_list_equal(lut.tolist(), [0] * (len(self.labels) + 1))
 def test_get_labels_lut(self):
     
     lut = du.get_labels_lut(self.labels, self.subset)
     
     assert_is_instance(lut, np.ndarray)
     assert_equals(lut.dtype, int)
     assert_equals(lut.ndim, 1)
     assert_equals(lut.shape, (9+1+1,))
     
     lut_expected = [0]*(9+1+1)
     lut_expected[4] = 5
     lut_expected[5] = 2
     lut_expected[7] = 2
     lut_expected[9] = 9
     
     assert_list_equal(lut.tolist(), lut_expected)
Beispiel #6
0
    def test_get_labels_lut(self):

        lut = du.get_labels_lut(self.labels, self.subset)

        assert_is_instance(lut, np.ndarray)
        assert_equals(lut.dtype, int)
        assert_equals(lut.ndim, 1)
        assert_equals(lut.shape, (9 + 1 + 1, ))

        lut_expected = [0] * (9 + 1 + 1)
        lut_expected[4] = 5
        lut_expected[5] = 2
        lut_expected[7] = 2
        lut_expected[9] = 9

        assert_list_equal(lut.tolist(), lut_expected)
def pascal_context_to_lmdb(dir_imgs,
                           dir_segm_labels,
                           fpath_labels_list,
                           fpath_labels_list_subset,
                           dst_prefix,
                           dir_dst,
                           val_list=None):
    '''
    Fine intersection of filename in both directories and create
    one lmdb directory for each
    val_list - list of entities to exclude from train (validation subset e.g. ['2008_000002', '2010_000433'])
    '''
    if dst_prefix is None:
        dst_prefix = ''
        
    labels_list = get_labels_list(fpath_labels_list)
    labels_59_list = get_labels_list(fpath_labels_list_subset)
    
    #print labels_list
    #print labels_59_list
    labels_lut = du.get_labels_lut(labels_list, labels_59_list)
    def apply_labels_lut(m):
        return labels_lut[m]
    
    paths_imgs = fs.gen_paths(dir_imgs, fs.filter_is_img)
    
    paths_segm_labels = fs.gen_paths(dir_segm_labels)
     
    paths_pairs = fs.fname_pairs(paths_imgs, paths_segm_labels)    
    paths_imgs, paths_segm_labels = map(list, zip(*paths_pairs))
    
    #for a, b in paths_pairs:
    #    print a,b
    
    if val_list is not None:
        # do train/val split
        
        train_idx, val_idx = du.get_train_val_split_from_names(paths_imgs, val_list)
        
        # images
        paths_imgs_train = [paths_imgs[i] for i in train_idx]
        fpath_lmdb_imgs_train = os.path.join(dir_dst,
                                       '%scontext_imgs_train_lmdb' % dst_prefix)
        to_lmdb.imgs_to_lmdb(paths_imgs_train,
                             fpath_lmdb_imgs_train)
        
        paths_imgs_val = [paths_imgs[i] for i in val_idx]
        fpath_lmdb_imgs_val = os.path.join(dir_dst,
                                       '%scontext_imgs_val_lmdb' % dst_prefix)
        to_lmdb.imgs_to_lmdb(paths_imgs_val,
                             fpath_lmdb_imgs_val)
        
        # ground truth
        paths_segm_labels_train = [paths_segm_labels[i] for i in train_idx]
        fpath_lmdb_segm_labels_train = os.path.join(dir_dst,
                                              '%scontext_labels_train_lmdb' % dst_prefix)
        to_lmdb.matfiles_to_lmdb(paths_segm_labels_train,
                                 fpath_lmdb_segm_labels_train,
                                 'LabelMap',
                                 lut=apply_labels_lut)
        
        paths_segm_labels_val = [paths_segm_labels[i] for i in val_idx]
        fpath_lmdb_segm_labels_val = os.path.join(dir_dst,
                                              '%scontext_labels_val_lmdb' % dst_prefix)
        to_lmdb.matfiles_to_lmdb(paths_segm_labels_val,
                                 fpath_lmdb_segm_labels_val,
                                 'LabelMap',
                                 lut=apply_labels_lut)
        
        return len(paths_imgs_train), len(paths_imgs_val),\
            fpath_lmdb_imgs_train, fpath_lmdb_segm_labels_train, fpath_lmdb_imgs_val, fpath_lmdb_segm_labels_val
        
    else:
        fpath_lmdb_imgs = os.path.join(dir_dst,
                                       '%scontext_imgs_lmdb' % dst_prefix)
        to_lmdb.imgs_to_lmdb(paths_imgs,
                             fpath_lmdb_imgs)
        
        fpath_lmdb_segm_labels = os.path.join(dir_dst,
                                              '%scontext_labels_lmdb' % dst_prefix)
        to_lmdb.matfiles_to_lmdb(paths_segm_labels,
                                 fpath_lmdb_segm_labels,
                                 'LabelMap',
                                 lut=apply_labels_lut)
        
        return len(paths_imgs), fpath_lmdb_imgs, fpath_lmdb_segm_labels
Beispiel #8
0
def pascal_context_to_lmdb(dir_imgs,
                           dir_segm_labels,
                           fpath_labels_list,
                           fpath_labels_list_subset,
                           dst_prefix,
                           dir_dst,
                           val_list=None):
    '''
    Fine intersection of filename in both directories and create
    one lmdb directory for each
    val_list - list of entities to exclude from train (validation subset e.g. ['2008_000002', '2010_000433'])
    '''
    if dst_prefix is None:
        dst_prefix = ''

    print 'begin'
    labels_list = get_labels_list(fpath_labels_list)
    labels_59_list = get_labels_list(fpath_labels_list_subset)

    #print labels_list
    #print labels_59_list
    labels_lut = du.get_labels_lut(labels_list, labels_59_list)

    def apply_labels_lut(m):
        return labels_lut[m]

    print 'plus loin'
    paths_imgs = fs.gen_paths(dir_imgs, fs.filter_is_img)

    paths_segm_labels = fs.gen_paths(dir_segm_labels)

    paths_pairs = fs.fname_pairs(paths_imgs, paths_segm_labels)
    paths_imgs, paths_segm_labels = map(list, zip(*paths_pairs))

    print 'avant if'
    #for a, b in paths_pairs:
    #    print a,b

    if val_list is not None:
        # do train/val split

        train_idx, val_idx = du.get_train_val_split_from_names(
            paths_imgs, val_list)

        # images
        print 'begin images'
        print 'train'
        paths_imgs_train = [paths_imgs[i] for i in train_idx]
        fpath_lmdb_imgs_train = os.path.join(
            dir_dst, '%scontext_imgs_train_lmdb' % dst_prefix)
        to_lmdb.imgs_to_lmdb(paths_imgs_train, fpath_lmdb_imgs_train)

        print 'val'
        paths_imgs_val = [paths_imgs[i] for i in val_idx]
        fpath_lmdb_imgs_val = os.path.join(
            dir_dst, '%scontext_imgs_val_lmdb' % dst_prefix)
        to_lmdb.imgs_to_lmdb(paths_imgs_val, fpath_lmdb_imgs_val)

        # ground truth
        print 'begin labels'
        print 'train'
        paths_segm_labels_train = [paths_segm_labels[i] for i in train_idx]
        fpath_lmdb_segm_labels_train = os.path.join(
            dir_dst, '%scontext_labels_train_lmdb' % dst_prefix)
        to_lmdb.matfiles_to_lmdb(paths_segm_labels_train,
                                 fpath_lmdb_segm_labels_train,
                                 'LabelMap',
                                 lut=apply_labels_lut)

        print 'val'
        paths_segm_labels_val = [paths_segm_labels[i] for i in val_idx]
        fpath_lmdb_segm_labels_val = os.path.join(
            dir_dst, '%scontext_labels_val_lmdb' % dst_prefix)
        to_lmdb.matfiles_to_lmdb(paths_segm_labels_val,
                                 fpath_lmdb_segm_labels_val,
                                 'LabelMap',
                                 lut=apply_labels_lut)

        return len(paths_imgs_train), len(paths_imgs_val),\
            fpath_lmdb_imgs_train, fpath_lmdb_segm_labels_train, fpath_lmdb_imgs_val, fpath_lmdb_segm_labels_val

    else:
        print 'dans else'
        fpath_lmdb_imgs = os.path.join(dir_dst,
                                       '%scontext_imgs_lmdb' % dst_prefix)
        to_lmdb.imgs_to_lmdb(paths_imgs, fpath_lmdb_imgs)

        fpath_lmdb_segm_labels = os.path.join(
            dir_dst, '%scontext_labels_lmdb' % dst_prefix)
        to_lmdb.matfiles_to_lmdb(paths_segm_labels,
                                 fpath_lmdb_segm_labels,
                                 'LabelMap',
                                 lut=apply_labels_lut)

        return len(paths_imgs), fpath_lmdb_imgs, fpath_lmdb_segm_labels