Beispiel #1
0
def vis_voxels(model_id, edge_length_threshold, filled, shuffle=False):
    from mayavi import mlab
    from util3d.mayavi_vis import vis_voxels
    from shapenet.core import cat_desc_to_id
    from template_ffd.inference.voxels import get_voxel_dataset
    from template_ffd.data.voxels import get_gt_voxel_dataset
    from template_ffd.model import load_params
    from template_ffd.data.ids import get_example_ids
    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    gt_ds = get_gt_voxel_dataset(cat_id, filled)
    inf_ds = get_voxel_dataset(model_id, edge_length_threshold)
    example_ids = get_example_ids(cat_id, 'eval')
    if shuffle:
        example_ids = list(example_ids)
        example_ids.shuffle

    with gt_ds:
        with inf_ds:
            for example_id in example_ids:
                gt = gt_ds[example_id].data
                inf = inf_ds[example_id].data
                vis_voxels(gt, color=(0, 0, 1))
                mlab.figure()
                vis_voxels(inf, color=(0, 1, 0))
                mlab.show()
Beispiel #2
0
def vis_mesh(model_id, edge_length_threshold, shuffle=False, wireframe=False):
    import numpy as np
    from mayavi import mlab
    from shapenet.core.meshes import get_mesh_dataset
    from shapenet.core import cat_desc_to_id
    from util3d.mayavi_vis import vis_mesh
    from template_ffd.inference.meshes import get_inferred_mesh_dataset
    from template_ffd.model import load_params
    import random

    def vis(mesh, **kwargs):
        v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
        vis_mesh(v, f, include_wireframe=wireframe, **kwargs)

    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    inf_mesh_dataset = get_inferred_mesh_dataset(
        model_id, edge_length_threshold)
    with inf_mesh_dataset:
        with get_mesh_dataset(cat_id) as gt_mesh_dataset:
            example_ids = list(inf_mesh_dataset.keys())
            if shuffle:
                random.shuffle(example_ids)

            for example_id in example_ids:
                inf = inf_mesh_dataset[example_id]
                gt = gt_mesh_dataset[example_id]
                mlab.figure()
                vis(inf, color=(0, 1, 0), opacity=0.2)
                mlab.figure()
                vis(gt, opacity=0.2)
                mlab.show()
Beispiel #3
0
def vis_mesh(model_id, edge_length_threshold, shuffle=False, wireframe=False):
    import numpy as np
    from mayavi import mlab
    from shapenet.core.meshes import get_mesh_dataset
    from shapenet.core import cat_desc_to_id
    from util3d.mayavi_vis import vis_mesh
    from template_ffd.inference.meshes import get_inferred_mesh_dataset
    from template_ffd.model import load_params
    import random

    def vis(mesh, **kwargs):
        v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
        vis_mesh(v, f, include_wireframe=wireframe, **kwargs)

    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    inf_mesh_dataset = get_inferred_mesh_dataset(model_id,
                                                 edge_length_threshold)
    with inf_mesh_dataset:
        with get_mesh_dataset(cat_id) as gt_mesh_dataset:
            example_ids = list(inf_mesh_dataset.keys())
            if shuffle:
                random.shuffle(example_ids)

            for example_id in example_ids:
                inf = inf_mesh_dataset[example_id]
                gt = gt_mesh_dataset[example_id]
                mlab.figure()
                vis(inf, color=(0, 1, 0), opacity=0.2)
                mlab.figure()
                vis(gt, opacity=0.2)
                mlab.show()
Beispiel #4
0
def vis_voxels(model_id, edge_length_threshold, filled, shuffle=False):
    from mayavi import mlab
    from util3d.mayavi_vis import vis_voxels
    from shapenet.core import cat_desc_to_id
    from template_ffd.inference.voxels import get_voxel_dataset
    from template_ffd.data.voxels import get_gt_voxel_dataset
    from template_ffd.model import load_params
    from template_ffd.data.ids import get_example_ids
    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    gt_ds = get_gt_voxel_dataset(cat_id, filled)
    inf_ds = get_voxel_dataset(model_id, edge_length_threshold)
    example_ids = get_example_ids(cat_id, 'eval')
    if shuffle:
        example_ids = list(example_ids)
        example_ids.shuffle

    with gt_ds:
        with inf_ds:
            for example_id in example_ids:
                gt = gt_ds[example_id].data
                inf = inf_ds[example_id].data
                vis_voxels(gt, color=(0, 0, 1))
                mlab.figure()
                vis_voxels(inf, color=(0, 1, 0))
                mlab.show()
Beispiel #5
0
def get_iou_dataset(model_id, edge_length_threshold=0.1, filled=False):
    from shapenet.core import cat_desc_to_id
    from template_ffd.data.ids import get_example_ids
    from template_ffd.model import load_params
    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    n_eval = len(get_example_ids(cat_id, 'eval'))

    manager = IouAutoSavingManager(model_id=model_id,
                                   edge_length_threshold=edge_length_threshold,
                                   filled=filled)
    with manager.get_saving_dataset() as ds:
        needs_calc = len(ds) < n_eval
    if needs_calc:
        manager.save_all()
    return manager.get_saving_dataset()
Beispiel #6
0
def report_emd_average(model_id, pre_sampled=True, **kwargs):
    from shapenet.core import cat_desc_to_id
    from template_ffd.data.ids import get_example_ids
    from template_ffd.model import load_params
    manager = get_emd_manager(model_id, pre_sampled, **kwargs)
    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    n_eval = len(get_example_ids(cat_id, 'eval'))
    with manager.get_saving_dataset('r') as ds:
        if len(ds) == n_eval:
            values = np.array([ds.values()])
        else:
            values = None
    if values is None:
        with manager.get_saved_dataset() as ds:
            values = np.array([ds.values()])
    print(np.mean(values))
Beispiel #7
0
    def get_lazy_dataset(self):
        cat_id = cat_desc_to_id(load_params(self._model_id)['cat_desc'])
        example_ids = get_example_ids(cat_id, 'eval')
        inferred_dataset = get_voxel_dataset(
            self._model_id, self._edge_length_threshold, self._voxel_config,
            filled=self._filled, example_ids=example_ids)

        gt_dataset = get_gt_voxel_dataset(cat_id, filled=self._filled)

        voxel_datasets = Dataset.zip(inferred_dataset, gt_dataset)
        voxel_datasets = voxel_datasets.subset(example_ids)

        def map_fn(v):
            return intersection_over_union(v[0].data, v[1].data)

        iou_dataset = voxel_datasets.map(map_fn)
        return iou_dataset
Beispiel #8
0
def report_emd_average(model_id, pre_sampled=True, **kwargs):
    import os
    from shapenet.core import cat_desc_to_id
    from template_ffd.data.ids import get_example_ids
    from template_ffd.model import load_params
    manager = get_emd_manager(model_id, pre_sampled, **kwargs)
    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    n_eval = len(get_example_ids(cat_id, 'eval'))
    values = None
    if os.path.isfile(manager.path):
        with manager.get_saving_dataset('r') as ds:
            if len(ds) == n_eval:
                values = np.array(tuple(ds.values()))
    if values is None:
        manager.save_all()
        with manager.get_saving_dataset('r') as ds:
            values = np.array(tuple(ds.values()))
    print(np.mean(values))
Beispiel #9
0
def report_chamfer_average(model_id, pre_sampled=True, **kwargs):
    import os
    from shapenet.core import cat_desc_to_id
    from template_ffd.data.ids import get_example_ids
    from template_ffd.model import load_params
    manager = get_chamfer_manager(model_id, pre_sampled, **kwargs)
    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    n_eval = len(get_example_ids(cat_id, 'eval'))
    values = None
    if os.path.isfile(manager.path):
        with manager.get_saving_dataset('r') as ds:
            if len(ds) == n_eval:
                values = np.array(tuple(ds.values()))
    if values is None:
        manager.save_all()
        with manager.get_saving_dataset('r') as ds:
            values = np.array(tuple(ds.values()))
    print(np.mean(values))
Beispiel #10
0
    def get_lazy_dataset(self):
        cat_id = cat_desc_to_id(load_params(self._model_id)['cat_desc'])
        example_ids = get_example_ids(cat_id, 'eval')
        inferred_dataset = get_voxel_dataset(self._model_id,
                                             self._edge_length_threshold,
                                             self._voxel_config,
                                             filled=self._filled,
                                             example_ids=example_ids)

        gt_dataset = get_gt_voxel_dataset(cat_id, filled=self._filled)

        voxel_datasets = Dataset.zip(inferred_dataset, gt_dataset)
        voxel_datasets = voxel_datasets.subset(example_ids)

        def map_fn(v):
            return intersection_over_union(v[0].data, v[1].data)

        iou_dataset = voxel_datasets.map(map_fn)
        return iou_dataset
Beispiel #11
0
def get_iou_dataset(
        model_id, edge_length_threshold=0.1, filled=False, recalc=False):
    from shapenet.core import cat_desc_to_id
    from template_ffd.data.ids import get_example_ids
    from template_ffd.model import load_params
    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    n_eval = len(get_example_ids(cat_id, 'eval'))

    manager = IouAutoSavingManager(
        model_id=model_id,
        edge_length_threshold=edge_length_threshold,
        filled=filled
    )
    if not recalc:
        with manager.get_saving_dataset() as ds:
            recalc = len(ds) < n_eval
    if recalc:
        manager.save_all()
    return manager.get_saving_dataset('r')
Beispiel #12
0
def _create_unfilled_voxel_data(
        model_id, edge_length_threshold=0.1, voxel_config=None,
        overwrite=False, example_ids=None):
    from template_ffd.data.ids import get_example_ids
    from shapenet.core import cat_desc_to_id
    from template_ffd.model import load_params
    import numpy as np
    from progress.bar import IncrementalBar
    if voxel_config is None:
        voxel_config = _default_config
    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    if example_ids is None:
        example_ids = get_example_ids(cat_id, 'eval')
    mesh_dataset = get_inferred_mesh_dataset(model_id, edge_length_threshold)
    voxel_dataset = get_voxel_dataset(
        model_id, edge_length_threshold, voxel_config, filled=False,
        auto_save=False)
    if not overwrite:
        example_ids = [i for i in example_ids if i not in voxel_dataset]
    if len(example_ids) == 0:
        return
    print('Creating %d voxels for model %s' % (len(example_ids), model_id))

    kwargs = dict(
        voxel_dim=voxel_config.voxel_dim,
        exact=voxel_config.exact,
        dc=voxel_config.dc,
        aw=voxel_config.aw)

    with mesh_dataset:
        bar = IncrementalBar(max=len(example_ids))
        for example_id in example_ids:
            bar.next()
            mesh = mesh_dataset[example_id]
            vertices, faces = (
                np.array(mesh[k]) for k in ('vertices', 'faces'))
            binvox_path = voxel_dataset.path(example_id)
            # x, z, y = vertices.T
            # vertices = np.stack([x, y, z], axis=1)
            bio.mesh_to_binvox(
                vertices, faces, binvox_path, **kwargs)
        bar.finish()
def get_hist_data(model_id, n_bins, mode):
    from shapenet.core import cat_desc_to_id
    from template_ffd.templates.ids import get_template_ids
    from template_ffd.model import load_params
    from template_ffd.inference.predictions import get_predictions_dataset
    cat_id = cat_desc_to_id(load_params(model_id)['cat_desc'])
    n_templates = len(get_template_ids(cat_id))

    counts = np.zeros((n_bins, ), dtype=np.int32)
    argmax_counts = np.zeros((n_templates, ), dtype=np.int32)

    with get_predictions_dataset(model_id) as dataset:
        for example_id in dataset:
            probs = np.array(dataset[example_id]['probs'])
            counts[int(np.max(probs) * n_bins)] += 1
            # prob_indices = np.array(0.999*probs * n_bins, dtype=np.int32)
            # for pi in prob_indices:
            #     counts[pi] += 1
            argmax_counts[np.argmax(probs)] += 1

    counts = counts / np.sum(counts)
    argmax_counts = argmax_counts / np.sum(argmax_counts)
    return counts, argmax_counts
Beispiel #14
0
    def get_lazy_dataset(self):
        cat_id = cat_desc_to_id(load_params(self._model_id)['cat_desc'])
        if not isinstance(cat_id, (list, tuple)):
            cat_id = [cat_id]
        inferred_dataset = get_voxel_dataset(self._model_id,
                                             self._edge_length_threshold,
                                             self._voxel_config,
                                             filled=self._filled)

        gt_dataset = get_gt_voxel_dataset(cat_id, filled=self._filled)
        gt_dataset = gt_dataset.map_keys(lambda key: key[:2])

        with inferred_dataset:
            keys = tuple(inferred_dataset.keys())

        voxel_datasets = Dataset.zip(inferred_dataset, gt_dataset)
        voxel_datasets = voxel_datasets.subset(keys)

        def map_fn(v):
            return intersection_over_union(v[0].dense_data(),
                                           v[1].dense_data())

        iou_dataset = voxel_datasets.map(map_fn)
        return iou_dataset
    print(ret)
    return list(ret)


descs = (
    'plane',
    'bench',
    'car',
    'chair',
    'sofa',
    'cabinet',
    'monitor',
    'lamp',
    'speaker',
    'pistol',
    'table',
    'cellphone',
    'watercraft',
)

model_ids = tuple('e_%s_v8' % c for c in descs)
template_idx = tuple(get_top_k(get_template_counts(m), 2) for m in model_ids)
params = load_params(model_ids[0])

params['cat_desc'] = descs
params['template_idxs'] = template_idx
params['use_bn_bugged_version'] = False

with open(path, 'w') as fp:
    json.dump(params, fp)