Example #1
0
def get_ds(cat_desc, regime='e'):
    view_index = 5
    edge_length_threshold = 0.02

    cat_id = cat_desc_to_id(cat_desc)
    model_id = '%s_%s' % (regime, cat_desc)

    image_ds = RenderConfig().get_dataset(cat_id, view_index)
    cloud_ds = get_cloud_manager(
        model_id, pre_sampled=True, n_samples=n_samples).get_lazy_dataset()
    mesh_ds = get_inferred_mesh_dataset(
        model_id, edge_length_threshold=edge_length_threshold)
    gt_mesh_ds = get_mesh_dataset(cat_id)
    voxel_ds = get_voxel_dataset(
        model_id, edge_length_threshold=edge_length_threshold, filled=False)
    selected_template_ds = get_selected_template_idx_dataset(model_id)

    template_meshes = []
    with gt_mesh_ds:
        for template_id in get_template_ids(cat_id):
            mesh = gt_mesh_ds[template_id]
            template_meshes.append(
                {k: np.array(mesh[k]) for k in ('vertices', 'faces')})

    template_mesh_ds = selected_template_ds.map(lambda i: template_meshes[i])

    return Dataset.zip(
        image_ds, gt_mesh_ds, cloud_ds, mesh_ds, voxel_ds, template_mesh_ds)
Example #2
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()
Example #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()
Example #4
0
def create_archive(cat_desc, shape=(192, 256), n_images=8, example_ids=None):
    import os
    from shapenet.core import cat_desc_to_id
    from shapenet.core import get_example_ids
    from shapenet.core.blender_renderings.config import RenderConfig
    from progress.bar import IncrementalBar
    import zipfile
    cat_id = cat_desc_to_id(cat_desc)
    if example_ids is None or len(example_ids) == 0:
        example_ids = get_example_ids(cat_id)
    config = RenderConfig(shape=shape, n_images=n_images)
    zip_path = config.get_zip_path(cat_id)
    with zipfile.ZipFile(zip_path, mode='a', allowZip64=True) as zf:
        bar = IncrementalBar(max=len(example_ids))
        for example_id in example_ids:
            example_dir = config.get_example_dir(cat_id, example_id)
            if not os.path.isdir(example_dir):
                print('No directory at %s' % example_dir)
            else:
                for fn in os.listdir(example_dir):
                    src = os.path.join(example_dir, fn)
                    dst = os.path.join(cat_id, example_id, fn)
                    zf.write(src, dst)
            bar.next()
        bar.finish()
Example #5
0
def get_ds(cat_desc, regime='e'):
    view_index = 5
    edge_length_threshold = 0.02

    cat_id = cat_desc_to_id(cat_desc)
    model_id = '%s_%s' % (regime, cat_desc)

    image_ds = RenderConfig().get_dataset(cat_id, view_index)
    cloud_ds = get_cloud_manager(
        model_id, pre_sampled=True, n_samples=n_samples).get_lazy_dataset()
    mesh_ds = get_inferred_mesh_dataset(
        model_id, edge_length_threshold=edge_length_threshold)
    gt_mesh_ds = get_mesh_dataset(cat_id)
    voxel_ds = get_voxel_dataset(
        model_id, edge_length_threshold=edge_length_threshold, filled=False)
    selected_template_ds = get_selected_template_idx_dataset(model_id)

    template_meshes = []
    with gt_mesh_ds:
        for template_id in get_template_ids(cat_id):
            mesh = gt_mesh_ds[template_id]
            template_meshes.append(
                {k: np.array(mesh[k]) for k in ('vertices', 'faces')})

    template_mesh_ds = selected_template_ds.map(lambda i: template_meshes[i])

    return Dataset.zip(
        image_ds, gt_mesh_ds, cloud_ds, mesh_ds, voxel_ds, template_mesh_ds)
Example #6
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()
Example #7
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()
def create_split_mesh(
        cat_desc, edge_length_threshold, overwrite=False,
        start_threshold=None):
    """Create split mesh data for templates."""
    from shapenet.core import cat_desc_to_id
    from shapenet.core.meshes.config import get_mesh_config
    from template_ffd.templates.ids import get_template_ids
    cat_id = cat_desc_to_id(cat_desc)
    example_ids = get_template_ids(cat_id)
    config = get_mesh_config(edge_length_threshold)
    init = None if start_threshold is None else get_mesh_config(
        start_threshold)
    config.create_cat_data(cat_id, example_ids, overwrite, init)
def create_point_clouds(n_samples,
                        cat_desc,
                        example_ids=None,
                        normals=False,
                        overwrite=False):
    from shapenet.core.point_clouds import PointCloudAutoSavingManager
    from shapenet.core.point_clouds import CloudNormalAutoSavingManager
    from shapenet.core import cat_desc_to_id
    cat_id = cat_desc_to_id(cat_desc)
    if normals:
        manager = CloudNormalAutoSavingManager(cat_id, n_samples, example_ids)
    else:
        manager = PointCloudAutoSavingManager(cat_id, n_samples, example_ids)
    manager.save_all(overwrite=overwrite)
Example #10
0
def create_ffd(
        n=3, cat_descs=None, edge_length_threshold=None, n_samples=None,
        overwrite=False):
    from shapenet.core import cat_desc_to_id
    from template_ffd.templates.ids import get_templated_cat_ids
    from template_ffd.templates.ffd import create_ffd_data
    if cat_descs is None or len(cat_descs) == 0:
        cat_ids = get_templated_cat_ids()
    else:
        cat_ids = [cat_desc_to_id(c) for c in cat_descs]
    for cat_id in cat_ids:
        create_ffd_data(
            cat_id, n=n, edge_length_threshold=edge_length_threshold,
            n_samples=n_samples, overwrite=overwrite)
def create_split_mesh(cat_desc,
                      edge_length_threshold,
                      overwrite=False,
                      start_threshold=None):
    """Create split mesh data for templates."""
    from shapenet.core import cat_desc_to_id
    from shapenet.core.meshes.config import get_mesh_config
    from template_ffd.templates.ids import get_template_ids
    cat_id = cat_desc_to_id(cat_desc)
    example_ids = get_template_ids(cat_id)
    config = get_mesh_config(edge_length_threshold)
    init = None if start_threshold is None else get_mesh_config(
        start_threshold)
    config.create_cat_data(cat_id, example_ids, overwrite, init)
Example #12
0
def main(cat_descs, voxel_dim, overwrite):
    from shapenet.core import cat_desc_to_id, get_cat_ids
    from shapenet.core import get_example_ids
    if len(cat_descs) == 0:
        cat_ids = get_cat_ids()
    else:
        cat_ids = [cat_desc_to_id(cat_desc) for cat_desc in cat_descs]
    for i, cat_id in enumerate(cat_ids):
        print('Processing cat_id %s, %d / %d' % (cat_id, i + 1, len(cat_ids)))
        example_ids = get_example_ids(cat_id)
        convert_multi(cat_id,
                      example_ids,
                      overwrite=overwrite,
                      voxel_dim=voxel_dim)
Example #13
0
def create_voxels(model_id, edge_length_threshold, filled, overwrite,
                  cat_desc):
    if model_id is None:
        if cat_desc is None:
            raise ValueError('One of model_id or cat_desc must be supplied')
        from template_ffd.data.voxels import create_filled_gt_data
        from shapenet.core import cat_desc_to_id
        cat_id = cat_desc_to_id(cat_desc)
        create_filled_gt_data(cat_id, overwrite=overwrite)
    else:
        from template_ffd.inference.voxels import create_voxel_data
        create_voxel_data(model_id,
                          edge_length_threshold,
                          filled=filled,
                          overwrite=overwrite)
Example #14
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()
Example #15
0
def check_mesh_data(cat_desc):
    from shapenet.core import cat_desc_to_id, get_example_ids
    from shapenet.core.meshes import get_mesh_dataset
    cat_id = cat_desc_to_id(cat_desc)
    example_ids = get_example_ids(cat_id)
    n_absent = 0
    with get_mesh_dataset(cat_id) as ds:
        for example_id in example_ids:
            if example_id not in ds:
                n_absent += 1

    n = len(example_ids)
    if n_absent == 0:
        print('All %d %s meshes present!' % (n, cat_desc))
    else:
        print('%d / %d %s meshes absent' % (n_absent, n, cat_desc))
Example #16
0
def vis(cat, n_images, view_index=5, example_ids=None):
    import matplotlib.pyplot as plt
    from shapenet.core import cat_desc_to_id, get_example_ids
    from shapenet.core.blender_renderings.config import RenderConfig
    cat_id = cat_desc_to_id(cat)
    config = RenderConfig(n_images=n_images)
    dataset = config.get_dataset(cat_id, view_index)
    if example_ids is not None and len(example_ids) > 0:
        dataset = dataset.subset(example_ids)
    else:
        example_ids = get_example_ids(cat_id)
    with dataset:
        for example_id in example_ids:
            plt.imshow(dataset[example_id])
            plt.title(example_id)
            plt.show()
Example #17
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))
Example #18
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
Example #19
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))
Example #20
0
def create_ffd(n=3,
               cat_descs=None,
               edge_length_threshold=None,
               n_samples=None,
               overwrite=False):
    from shapenet.core import cat_desc_to_id
    from template_ffd.templates.ids import get_templated_cat_ids
    from template_ffd.templates.ffd import create_ffd_data
    if cat_descs is None or len(cat_descs) == 0:
        cat_ids = get_templated_cat_ids()
    else:
        cat_ids = [cat_desc_to_id(c) for c in cat_descs]
    for cat_id in cat_ids:
        create_ffd_data(cat_id,
                        n=n,
                        edge_length_threshold=edge_length_threshold,
                        n_samples=n_samples,
                        overwrite=overwrite)
Example #21
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))
Example #22
0
def get_chamfer_average(model_id, pre_sampled=True, cat_desc=None, **kwargs):
    import os
    from shapenet.core import cat_desc_to_id
    manager = get_chamfer_manager(model_id, pre_sampled, **kwargs)

    values = None
    if os.path.isfile(manager.path):
        with manager.get_saving_dataset('r') as ds:
            values = np.array(tuple(ds.values()))
    if values is None or len(values) == 0:
        manager.save_all()
    with manager.get_saving_dataset('r') as ds:
        if cat_desc is not None:
            if not isinstance(cat_desc, (list, tuple, set)):
                cat_desc = [cat_desc]
            cat_id = set(cat_desc_to_id(cat_desc))
            ds = ds.filter_keys(lambda key: key[0] in cat_id)
        values = np.array(tuple(ds.values()))
    return np.mean(values)
Example #23
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
Example #24
0
def check_archive(cat_desc, voxel_dim, example_ids=None):
    from shapenet.core import cat_desc_to_id, get_example_ids
    from shapenet.core.voxels.config import VoxelConfig
    cat_id = cat_desc_to_id(cat_desc)
    if example_ids is None or len(example_ids) == 0:
        example_ids = get_example_ids(cat_id)
    config = VoxelConfig(voxel_dim=voxel_dim)
    n_absent = 0
    with config.get_zip_file(cat_id) as zf:
        namelist = set(zf.namelist())
    for example_id in example_ids:
        if config.get_binvox_subpath(cat_id, example_id) not in namelist:
            n_absent += 1

    if n_absent == 0:
        print('All %d %s voxels present' % (len(example_ids), cat_desc))
    else:
        print('%d / %d voxel files missing from %s' %
              (n_absent, len(example_ids), cat_desc))
Example #25
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')
Example #26
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()
Example #27
0
def test(resourceid,keyname):
    img_inp,x,pt_gt,training_flag,loss,optimizer,batchno,batchnoinc,mindist,loss_nodecay,dists_forward,dists_backward,dist0=build_graph(resourceid,"test")
    config=tf.ConfigProto()
    config.gpu_options.allow_growth=True
    config.allow_soft_placement=True
    saver=tf.train.Saver()
    cat_id=cat_desc_to_id('plane')
    length=len(get_example_ids(cat_id,'eval'))
    fout = open("%s/%s.v.txt"%(dumpdir,keyname),'wb')
    with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())
        saver.restore(sess,"%s/%s.ckpt"%(dumpdir,keyname))
        values=[]
        for i in xrange(0,length):
            _,pred,total_loss,_,forward,backward,_=sess.run([batchnoinc,x,loss,loss_nodecay,dists_forward,dists_backward,dist0],
                    feed_dict={training_flag:False})
            
            print i,total_loss
            values.append(total_loss)
        print np.mean(values)
Example #28
0
def main(_):
    from shapenet.core.renderings.renderings_manager import get_base_manager
    from shapenet.core import cat_desc_to_id
    from shapenet.core import cat_id_to_desc
    cat = FLAGS.cat
    if cat is None or len(cat) == 0:
        from shapenet.r2n2 import get_cat_ids
        cat_ids = get_cat_ids()
    else:
        cat_ids = [cat_desc_to_id(c) for c in FLAGS.cat]

    print('Required renderings:')
    for cat_id in cat_ids:
        manager = get_base_manager(
            dim=FLAGS.dim,
            turntable=FLAGS.turntable,
            n_views=FLAGS.n_views,
        )
        n = len(tuple(manager.needs_rendering_keys(cat_ids=[cat_id])))
        n_total = len(tuple(manager.view_manager.keys(cat_ids=[cat_id])))
        cat = cat_id_to_desc(cat_id)
        print('%s: %s\n  %d / %d' % (cat_id, cat, n, n_total))
Example #29
0
def check_zip(cat_desc, shape, n_images):
    import zipfile
    from shapenet.core.blender_renderings.config import RenderConfig
    from shapenet.core import cat_desc_to_id, get_example_ids
    cat_id = cat_desc_to_id(cat_desc)

    config = RenderConfig(shape=shape, n_images=n_images)
    rendered_ids = set()
    with zipfile.ZipFile(config.get_zip_path(cat_id)) as zf:
        for name in zf.namelist():
            rendered_ids.add(name.split('/')[1])

    not_rendered_count = 0
    example_ids = get_example_ids(cat_id)
    for example_id in example_ids:
        if example_id not in rendered_ids:
            print(example_id)
            not_rendered_count += 1

    if not_rendered_count > 0:
        print('%d / %d not rendered' % (not_rendered_count, len(example_ids)))
    else:
        print('All %d %ss rendered!' % (len(example_ids), cat_desc))
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
Example #31
0
def create_archive(cat_desc, voxel_dim, example_ids=None, overwrite=False):
    import os
    import zipfile
    from shapenet.core.voxels.config import VoxelConfig
    from shapenet.core import get_example_ids, cat_desc_to_id
    cat_id = cat_desc_to_id(cat_desc)

    if example_ids is None or len(example_ids) == 0:
        example_ids = get_example_ids(cat_id)
    config = VoxelConfig(voxel_dim)
    with zipfile.ZipFile(config.get_zip_path(cat_id), 'a') as zf:
        if not overwrite:
            namelist = set(zf.namelist())
        for example_id in example_ids:
            dst = config.get_binvox_subpath(cat_id, example_id)
            if not overwrite and dst in namelist:
                continue
            src = config.get_binvox_path(cat_id, example_id)
            if os.path.isfile(src):
                zf.write(src, dst)
            else:
                print('No file at %s for %s/%s: skipping' %
                      (src, cat_id, example_id))
Example #32
0
def vis(cat_desc, regime='e', shuffle=True):
    import matplotlib.pyplot as plt
    from mayavi import mlab
    from util3d.mayavi_vis import vis_point_cloud, vis_voxels

    all_ds = get_ds(cat_desc, regime)
    cat_id = cat_desc_to_id(cat_desc)
    example_ids = list(get_example_ids(cat_id, 'eval'))
    random.shuffle(example_ids)

    def vis_mesh(mesh, include_wireframe=False, **kwargs):
        from util3d.mayavi_vis import vis_mesh as vm
        v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
        vm(v, f, include_wireframe=include_wireframe, **kwargs)

    with all_ds:
        for example_id in example_ids:
            print(example_id)
            image, gt_mesh, cloud, mesh, voxels, template_mesh = \
                all_ds[example_id]
            plt.imshow(image)
            mlab.figure()
            vis_mesh(gt_mesh, color=(0, 0, 1))
            mlab.figure()
            vis_mesh(mesh, color=(0, 1, 0))
            mlab.figure()
            vis_mesh(template_mesh, color=(1, 0, 0))
            mlab.figure()
            vis_point_cloud(np.array(cloud),
                            scale_factor=0.01,
                            color=(0, 1, 0))
            mlab.figure()
            vis_voxels(voxels.data, color=(0, 1, 0))

            plt.show(block=False)
            mlab.show()
            plt.close()
Example #33
0
def vis(cat_desc, regime='e', shuffle=True):
    import matplotlib.pyplot as plt
    from mayavi import mlab
    from util3d.mayavi_vis import vis_point_cloud, vis_voxels

    all_ds = get_ds(cat_desc, regime)
    cat_id = cat_desc_to_id(cat_desc)
    example_ids = list(get_example_ids(cat_id, 'eval'))
    random.shuffle(example_ids)

    def vis_mesh(mesh, include_wireframe=False, **kwargs):
        from util3d.mayavi_vis import vis_mesh as vm
        v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
        vm(v, f, include_wireframe=include_wireframe, **kwargs)

    with all_ds:
        for example_id in example_ids:
            print(example_id)
            image, gt_mesh, cloud, mesh, voxels, template_mesh = \
                all_ds[example_id]
            plt.imshow(image)
            mlab.figure()
            vis_mesh(gt_mesh, color=(0, 0, 1))
            mlab.figure()
            vis_mesh(mesh, color=(0, 1, 0))
            mlab.figure()
            vis_mesh(template_mesh, color=(1, 0, 0))
            mlab.figure()
            vis_point_cloud(
                np.array(cloud), scale_factor=0.01, color=(0, 1, 0))
            mlab.figure()
            vis_voxels(voxels.data, color=(0, 1, 0))

            plt.show(block=False)
            mlab.show()
            plt.close()
Example #34
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
Example #35
0
from shapenet.core import cat_desc_to_id

from template_ffd.inference.predictions import get_predictions_dataset
from template_ffd.data.ids import get_example_ids
from template_ffd.model import get_builder


regime = 'e'
cat_desc = 'chair'
view_index = 5
edge_length_threshold = 0.02

shuffle = True
k = 3

cat_id = cat_desc_to_id(cat_desc)
model_id = '%s_%s' % (regime, cat_desc)
builder = get_builder(model_id)

image_ds = RenderConfig().get_dataset(cat_id, view_index)
gt_mesh_ds = get_mesh_dataset(cat_id)
predictions_ds = get_predictions_dataset(model_id)

top_k_mesh_fn = builder.get_prediction_to_top_k_mesh_fn(
    edge_length_threshold, k)

all_ds = Dataset.zip(image_ds, gt_mesh_ds, predictions_ds)


def vis():
Example #36
0
 def cat_id(self):
     return cat_desc_to_id(self.params['cat_desc'])
Example #37
0
        file_index = get_file_index(zip_file)
        for example_id in example_ids:
            bar.next()
            render_example(
                config, cat_id, example_id, zip_file,
                overwrite, call_kwargs, blender_path=blender_path,
                verbose=verbose, file_index=file_index)
    bar.finish()


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('cat', type=str)
    parser.add_argument('--shape', type=int, nargs=2, default=[192, 256])
    parser.add_argument('--scale', type=float, default=None)
    parser.add_argument('--blender_path', type=str, default='blender')
    parser.add_argument('-n', '--n_images', type=int, default=8)
    parser.add_argument('-d', '--debug', action='store_true')
    parser.add_argument('-r', '--reverse', action='store_true')
    parser.add_argument('-o', '--overwrite', action='store_true')
    parser.add_argument('-i', '--example_ids', nargs='*')
    parser.add_argument('-f', '--fixed_meshes', action='store_true')
    parser.add_argument('-v', '--verbose', action='store_true')
    args = parser.parse_args()
    config = RenderConfig(args.shape, args.n_images, args.scale)
    cat_id = cat_id = cat_desc_to_id(args.cat)
    render_cat(config, cat_id, args.overwrite, args.reverse, args.debug,
               args.example_ids, args.fixed_meshes, args.blender_path,
               args.verbose)
Example #38
0
        dataset = dataset.map(load_image_from_file)
        dataset = dataset.map_keys(key_fn, inverse_key_fn)
        if example_ids is not None:
            dataset = dataset.subset(example_ids)
        return dataset


if __name__ == '__main__':
    # import numpy as np
    import matplotlib.pyplot as plt
    from shapenet.core import cat_desc_to_id
    # from zipfile import ZipFile
    # from shapenet.image import load_image_from_zip
    from shapenet.image import with_background
    cat_desc = 'plane'
    cat_id = cat_desc_to_id(cat_desc)
    view_index = 5
    config = RenderConfig()
    with config.get_dataset(cat_id, view_index) as ds:
        for k, v in ds.items():
            plt.imshow(with_background(v, 255))
            plt.title(k)
            plt.show()
    # with ZipFile(config.get_zip_path(cat_id)) as zf:
    #     for i in range(config.n_images):
    #         subpath = config.get_example_image_subpath(
    #             cat_id, example_id, config.view_angle(i))
    #         image = load_image_from_zip(zf, subpath)
    #
    #         plt.figure()
    #         plt.imshow(np.array(image)[..., :3])
Example #39
0
def generate_point_cloud_data(cat_desc, samples, normals, overwrite=False):
    from shapenet.core import cat_desc_to_id
    from shapenet.core.point_clouds import generate_point_cloud_data

    cat_id = cat_desc_to_id(cat_desc)
    generate_point_cloud_data(cat_id, samples, normals, overwrite=overwrite)
Example #40
0
    datasets = {}
    for cat_id, e_ids in zip(cat_ids, example_ids):
        manager = PointCloudAutoSavingManager(cat_id, n_samples)
        if not os.path.isfile(manager.path):
            manager.save_all()
        datasets[cat_id] = manager.get_saving_dataset(
            mode='r').subset(e_ids)
    return BiKeyDataset(datasets).map(
        lambda x: sample_points(np.array(x, dtype=np.float32), n_resamples))


if __name__ == '__main__':
    from shapenet.core import cat_desc_to_id
    from template_ffd.data.ids import get_example_ids
    import random
    cat_ids = [cat_desc_to_id(i) for i in ('plane', 'car')]
    view_indices = [1, 5, 6]
    mode = 'train'
    example_ids = [get_example_ids(cat_id, mode) for cat_id in cat_ids]
    image_dataset = get_image_dataset(cat_ids, example_ids, view_indices)
    cloud_dataset = get_cloud_dataset(cat_ids, example_ids)

    image_dataset.open()
    cloud_dataset.open()

    keys = list((tuple(k) for k in image_dataset.keys()))
    random.shuffle(keys)

    def vis(image, cloud):
        import matplotlib.pyplot as plt
        from util3d.mayavi_vis import vis_point_cloud, mlab