Esempio n. 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)
Esempio n. 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()
Esempio n. 3
0
def get_data(model_id, example_ids=None):
    edge_length_threshold = 0.02
    builder = get_builder(model_id)
    cat_id = builder.cat_id

    with get_ffd_dataset(cat_id, edge_length_threshold=0.02) as ffd_ds:
        template_ids, bs, ps = zip(*builder.get_ffd_data(ffd_ds))

    with get_template_mesh_dataset(cat_id, edge_length_threshold) as mesh_ds:
        faces = [np.array(mesh_ds[e]['faces']) for e in template_ids]

    predictions_ds = get_predictions_dataset(model_id)
    mesh_ds = get_mesh_dataset(cat_id)
    image_ds = RenderConfig().get_dataset(cat_id, builder.view_index)
    zipped = Dataset.zip(predictions_ds, mesh_ds, image_ds)
    with zipped:
        if example_ids is None:
            example_ids = list(predictions_ds.keys())
            random.shuffle(example_ids)
        for example_id in example_ids:
            print(example_id)
            pred, mesh, image = zipped[example_id]
            i = np.argmax(pred['probs'])
            dp = np.array(pred['dp'][i])
            b = bs[i]
            p = ps[i]
            yield example_id, b, p, dp, faces[i], mesh, image
Esempio n. 4
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)
Esempio n. 5
0
def _get_template_mesh_dataset(cat_id, edge_length_threshold=None):
    import shapenet.core.meshes as m
    if edge_length_threshold is None:
        return m.get_mesh_dataset(cat_id).subset(get_template_ids(cat_id))
    else:
        return get_split_template_mesh_dataset(
            cat_id=cat_id, edge_length_threshold=edge_length_threshold)
Esempio n. 6
0
def get_data(model_id, example_ids=None):
    edge_length_threshold = 0.02
    builder = get_builder(model_id)
    cat_id = builder.cat_id

    with get_ffd_dataset(cat_id, edge_length_threshold=0.02) as ffd_ds:
        template_ids, bs, ps = zip(*builder.get_ffd_data(ffd_ds))

    with get_template_mesh_dataset(cat_id, edge_length_threshold) as mesh_ds:
        faces = [np.array(mesh_ds[e]['faces']) for e in template_ids]

    predictions_ds = get_predictions_dataset(model_id)
    mesh_ds = get_mesh_dataset(cat_id)
    image_ds = RenderConfig().get_dataset(cat_id, builder.view_index)
    zipped = Dataset.zip(predictions_ds, mesh_ds, image_ds)
    with zipped:
        if example_ids is None:
            example_ids = list(predictions_ds.keys())
            random.shuffle(example_ids)
        for example_id in example_ids:
            print(example_id)
            pred, mesh, image = zipped[example_id]
            i = np.argmax(pred['probs'])
            dp = np.array(pred['dp'][i])
            b = bs[i]
            p = ps[i]
            yield example_id, b, p, dp, faces[i], mesh, image
Esempio n. 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()
Esempio n. 8
0
def get_template_mesh_dataset(cat_id, edge_length_threshold=None):
    import shapenet.core.meshes as m
    if edge_length_threshold is None:
        return m.get_mesh_dataset(cat_id).subset(get_template_ids(cat_id))
    else:
        return get_split_template_mesh_dataset(
            cat_id=cat_id,
            edge_length_threshold=edge_length_threshold)
Esempio n. 9
0
def remove_empty_meshes(cat_id):
    from shapenet.core.meshes import get_mesh_dataset
    to_remove = []
    with get_mesh_dataset(cat_id, mode='a') as ds:
        for example_id, mesh in ds.items():
            if len(mesh['faces']) == 0:
                to_remove.append(example_id)
        for t in to_remove:
            del ds[t]
    return to_remove
Esempio n. 10
0
    def get_lazy_dataset(self):
        from shapenet.core.meshes import get_mesh_dataset
        example_ids = get_example_ids(self._cat_id, 'eval')
        mesh_ds = get_mesh_dataset(self._cat_id).subset(example_ids)

        def map_fn(mesh):
            vertices = mesh['vertices']
            offset, scale_factor = get_normalization_params(vertices)
            return dict(offset=[float(o) for o in offset],
                        scale_factor=float(scale_factor))

        return mesh_ds.map(map_fn)
Esempio n. 11
0
def _main():
    from path import get_zip_file
    from points import get_points
    # from expert_verified import get_seg_image
    from shapenet.core.meshes import get_mesh_dataset
    # import matplotlib.pyplot as plt
    from mayavi import mlab
    cat_id = '02691156'
    # example_id = '1a04e3eab45ca15dd86060f189eb133'
    # example_id = '1a6ad7a24bb89733f412783097373bdc'
    example_id = '1a9b552befd6306cc8f2d5fe7449af61'
    with get_zip_file() as f:
        points = get_points(f, cat_id, example_id)
        labels = get_point_labels(f, cat_id, example_id)
        names = get_segmentation_names(cat_id)
        # image = get_seg_image(f, cat_id, example_id)
    print(len(points))
    names = ['unlabelled'] + names

    clouds = []
    for i, name in enumerate(names):
        cloud = points[labels == i]
        clouds.append(cloud)
        print('%s: %d' % (name, len(cloud)))

    with get_mesh_dataset(cat_id) as mesh_ds:
        example = mesh_ds[example_id]
        vertices, faces = (np.array(example[k]) for k in ('vertices', 'faces'))

    x, z, y = vertices.T
    mlab.triangular_mesh(x, y, z, faces, color=(1, 1, 1))
    mlab.triangular_mesh(x,
                         y,
                         z,
                         faces,
                         color=(0, 0, 0),
                         representation='wireframe')

    colors = np.random.uniform(size=(np.max(labels) + 1, 3))
    for color, cloud in zip(colors, clouds):
        x, z, y = cloud.T
        mlab.points3d(x,
                      y,
                      z,
                      color=tuple(color),
                      opacity=0.8,
                      scale_factor=0.02)
    print(np.min(points, axis=0), np.max(points, axis=0))
    print(np.min(vertices, axis=0), np.max(vertices, axis=0))
    # plt.imshow(image)
    # plt.show(block=False)
    mlab.show()
Esempio n. 12
0
    def get_lazy_dataset(self):
        from shapenet.core.meshes import get_mesh_dataset
        example_ids = get_example_ids(self._cat_id, 'eval')
        mesh_ds = get_mesh_dataset(self._cat_id).subset(example_ids)

        def map_fn(mesh):
            vertices = mesh['vertices']
            offset, scale_factor = get_normalization_params(vertices)
            return dict(
                offset=[float(o) for o in offset],
                scale_factor=float(scale_factor))

        return mesh_ds.map(map_fn)
Esempio n. 13
0
def vis_clouds(model_id,
               pre_sampled=True,
               n_samples=1024,
               edge_length_threshold=0.1,
               shuffle=False):
    import random
    import numpy as np
    from mayavi import mlab
    import matplotlib.pyplot as plt
    from dids import Dataset
    from shapenet.core.blender_renderings.config import RenderConfig
    from shapenet.core.meshes import get_mesh_dataset
    from util3d.mayavi_vis import vis_point_cloud
    from util3d.mayavi_vis import vis_mesh
    from template_ffd.data.ids import get_example_ids
    from template_ffd.inference.clouds import get_inferred_cloud_dataset
    from template_ffd.model import get_builder
    builder = get_builder(model_id)
    cat_id = builder.cat_id
    kwargs = dict(model_id=model_id, n_samples=n_samples)
    if not pre_sampled:
        kwargs['edge_length_threshold'] = edge_length_threshold
    cloud_dataset = get_inferred_cloud_dataset(pre_sampled=pre_sampled,
                                               **kwargs)
    image_dataset = RenderConfig().get_dataset(cat_id, builder.view_index)

    example_ids = get_example_ids(cat_id, 'eval')
    if shuffle:
        example_ids = list(example_ids)
        random.shuffle(example_ids)
    mesh_dataset = get_mesh_dataset(cat_id)
    zipped_dataset = Dataset.zip(image_dataset, cloud_dataset, mesh_dataset)
    # zipped_dataset = Dataset.zip(image_dataset, cloud_dataset)
    with zipped_dataset:
        for example_id in example_ids:
            image, cloud, mesh = zipped_dataset[example_id]
            # image, cloud = zipped_dataset[example_id]
            plt.imshow(image)
            vis_point_cloud(np.array(cloud),
                            color=(0, 1, 0),
                            scale_factor=0.01)
            v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
            vis_mesh(v,
                     f,
                     color=(0, 0, 1),
                     opacity=0.1,
                     include_wireframe=False)
            plt.show(block=False)
            mlab.show()
            plt.close()
Esempio n. 14
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))
Esempio n. 15
0
    def get_lazy_dataset(self):
        from shapenet.core.meshes import get_mesh_dataset
        from util3d.mesh.sample import sample_faces

        def map_fn(mesh):
            v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
            return sample_faces(v, f, self._n_samples)

        mesh_dataset = get_mesh_dataset(self._cat_id)
        if self._example_ids is not None:
            mesh_dataset = mesh_dataset.subset(self._example_ids)

        with mesh_dataset:
            keys = [k for k, v in mesh_dataset.items() if len(v['faces']) > 0]
        mesh_dataset = mesh_dataset.subset(keys)

        return mesh_dataset.map(map_fn)
Esempio n. 16
0
def vis_clouds(
        model_id, pre_sampled=True, n_samples=1024, edge_length_threshold=0.1,
        shuffle=False):
    import random
    import numpy as np
    from mayavi import mlab
    import matplotlib.pyplot as plt
    from dids import Dataset
    from shapenet.core.blender_renderings.config import RenderConfig
    from shapenet.core.meshes import get_mesh_dataset
    from util3d.mayavi_vis import vis_point_cloud
    from util3d.mayavi_vis import vis_mesh
    from template_ffd.data.ids import get_example_ids
    from template_ffd.inference.clouds import get_inferred_cloud_dataset
    from template_ffd.model import get_builder
    builder = get_builder(model_id)
    cat_id = builder.cat_id
    kwargs = dict(model_id=model_id, n_samples=n_samples)
    if not pre_sampled:
        kwargs['edge_length_threshold'] = edge_length_threshold
    cloud_dataset = get_inferred_cloud_dataset(
        pre_sampled=pre_sampled, **kwargs)
    image_dataset = RenderConfig().get_dataset(cat_id, builder.view_index)

    example_ids = get_example_ids(cat_id, 'eval')
    if shuffle:
        example_ids = list(example_ids)
        random.shuffle(example_ids)
    mesh_dataset = get_mesh_dataset(cat_id)
    zipped_dataset = Dataset.zip(image_dataset, cloud_dataset, mesh_dataset)
    # zipped_dataset = Dataset.zip(image_dataset, cloud_dataset)
    with zipped_dataset:
        for example_id in example_ids:
            image, cloud, mesh = zipped_dataset[example_id]
            # image, cloud = zipped_dataset[example_id]
            plt.imshow(image)
            vis_point_cloud(
                np.array(cloud), color=(0, 1, 0), scale_factor=0.01)
            v, f = (np.array(mesh[k]) for k in ('vertices', 'faces'))
            vis_mesh(
                v, f, color=(0, 0, 1), opacity=0.1, include_wireframe=False)
            plt.show(block=False)
            mlab.show()
            plt.close()
Esempio n. 17
0
    def get_lazy_dataset(self):
        import numpy as np
        from shapenet.core.meshes import get_mesh_dataset
        from shapenet.core.annotations.datasets import PointCloudDataset
        from dids import Dataset
        vertices_dataset = get_mesh_dataset(self._cat_id).map(
            lambda mesh: np.array(mesh['vertices']))
        points_dataset = PointCloudDataset(self._cat_id)
        zipped = Dataset.zip(vertices_dataset, points_dataset)

        def map_fn(inputs):
            vertices, points = inputs
            b, p = _calculate_ffd(self._n, vertices, points)
            return dict(b=b, p=p)

        with points_dataset:
            keys = [k for k in get_template_ids(self._cat_id)
                    if k in points_dataset]

        return zipped.map(map_fn).subset(keys)
Esempio n. 18
0
    def get_lazy_dataset(self):
        import numpy as np
        from shapenet.core.meshes import get_mesh_dataset
        from shapenet.core.annotations.datasets import PointCloudDataset
        from dids import Dataset
        vertices_dataset = get_mesh_dataset(self._cat_id).map(
            lambda mesh: np.array(mesh['vertices']))
        points_dataset = PointCloudDataset(self._cat_id)
        zipped = Dataset.zip(vertices_dataset, points_dataset)

        def map_fn(inputs):
            vertices, points = inputs
            b, p = _calculate_ffd(self._n, vertices, points)
            return dict(b=b, p=p)

        with points_dataset:
            keys = [k for k in get_template_ids(self._cat_id)
                    if k in points_dataset]

        return zipped.map(map_fn).subset(keys)
Esempio n. 19
0
    def vis_all(zipfile, cat_id, example_id):
        points = get_points(zipfile, cat_id, example_id)
        expert_labels = get_expert_point_labels(zipfile, cat_id, example_id)
        labels = get_point_labels(zipfile, cat_id, example_id)
        image = get_seg_image(zipfile, cat_id, example_id)
        print(len(points))

        with get_mesh_dataset(cat_id) as mesh_ds:
            example = mesh_ds[example_id]
            vertices, faces = (np.array(example[k])
                               for k in ('vertices', 'faces'))

        def vis(vertices, faces, points, labels):
            x, z, y = vertices.T
            mlab.triangular_mesh(x, y, z, faces, color=(1, 1, 1))
            mlab.triangular_mesh(x,
                                 y,
                                 z,
                                 faces,
                                 color=(0, 0, 0),
                                 representation='wireframe')

            n = np.max(labels) + 1
            colors = ((1, 1, 1), (0, 1, 0), (0, 0, 1), (1, 0, 0), (0, 1, 1),
                      (1, 1, 0), (1, 0, 1), (0, 0, 0))
            colors = colors[:n]
            for i, c in enumerate(colors):
                x, z, y = points[labels == i].T
                mlab.points3d(x, y, z, color=c, opacity=0.8, scale_factor=0.02)

        print(np.min(points, axis=0), np.max(points, axis=0))
        print(np.min(vertices, axis=0), np.max(vertices, axis=0))
        mlab.figure()
        vis(vertices, faces, points, expert_labels)
        mlab.figure()
        vis(vertices, faces, points, labels)
        plt.imshow(image)
        plt.show(block=False)
        mlab.show()
        plt.close()
Esempio n. 20
0

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():

    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)
Esempio n. 21
0
#!/usr/bin/python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from shapenet.core.meshes import get_mesh_dataset
from shapenet.core import cat_desc_to_id
from util3d.mayavi_vis import vis_mesh
from mayavi import mlab

desc = 'plane'

cat_id = cat_desc_to_id(desc)
with get_mesh_dataset(cat_id) as mesh_dataset:
    for example_id in mesh_dataset:
        example_group = mesh_dataset[example_id]
        vertices, faces = (np.array(example_group[k])
                           for k in ('vertices', 'faces'))
        vis_mesh(vertices, faces, color=(0, 0, 1), axis_order='xzy')
        mlab.show()
Esempio n. 22
0
#!/usr/bin/python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import dids
from shapenet.core.point_clouds import get_point_cloud_dataset
from shapenet.core.meshes import get_mesh_dataset
from shapenet.core import cat_desc_to_id, get_test_train_split

cat_desc = 'plane'
cat_id = cat_desc_to_id(cat_desc)

cloud_ds = get_point_cloud_dataset(cat_id, 1024)
mesh_ds = get_mesh_dataset(cat_id)

zipped_ds = dids.Dataset.zip(mesh_ds, cloud_ds)


def vis(mesh, cloud):
    import numpy as np
    from util3d.mayavi_vis import vis_mesh, vis_point_cloud
    from mayavi import mlab
    vertices, faces = (np.array(mesh[k]) for k in ('vertices', 'faces'))
    vis_mesh(vertices, faces, color=(0, 0, 1), axis_order='xzy')
    vis_point_cloud(np.array(cloud),
                    color=(0, 1, 0),
                    scale_factor=0.01,
                    axis_order='xzy')
    mlab.show()