def get_image_dataset(render_config, cat_id, view_index): from shapenet.image import with_background import random if isinstance(view_index, int): dataset = render_config.get_dataset(cat_id, view_index) elif isinstance(view_index, (list, tuple)): dataset = render_config.get_multi_view_dataset(cat_id) def key_fn(example_id): return example_id, random.sample(view_index, 1)[0] dataset = dataset.map_keys(key_fn) else: raise TypeError('view_index must be an int or list/tuple of ints') return dataset.map(lambda x: with_background(x, 255))
def save(): import tensorflow as tf from util3d.mesh.obj_io import write_obj from shapenet.image import with_background from template_ffd.model import get_builder builder = get_builder(model_id) mesh_fn = builder.get_prediction_to_mesh_fn(0.02) cloud_fn = builder.get_prediction_to_cloud_fn() graph = tf.Graph() with graph.as_default(): image = tf.placeholder(shape=(192, 256, 3), dtype=tf.uint8) std_image = tf.image.per_image_standardization(image) std_image = tf.expand_dims(std_image, axis=0) example_id = tf.constant(['blah'], dtype=tf.string) spec = builder.get_estimator_spec( dict(example_id=example_id, image=std_image), None, tf.estimator.ModeKeys.PREDICT) predictions = spec.predictions probs_tf = predictions['probs'] dp_tf = predictions['dp'] saver = tf.train.Saver() with tf.Session(graph=graph) as sess: saver.restore(sess, tf.train.latest_checkpoint(builder.model_dir)) for fn in fns: path = os.path.join(folder, fn) image_data = np.array(imread(path)) if image_data.shape[-1] == 4: image_data = with_background(image_data, (255, 255, 255)) probs, dp = sess.run( [probs_tf, dp_tf], feed_dict={image: image_data}) probs = probs[0] dp = dp[0] mesh = mesh_fn(probs, dp) cloud = cloud_fn(probs, dp)['cloud'] v, ov, f = ( mesh[k] for k in('vertices', 'original_vertices', 'faces')) path = '%s.obj' % path[:-4] write_obj(path, v, f) p2 = '%s_template.obj' % path[:-4] np.save('%s_cloud.npy' % path[:-4], cloud) write_obj(p2, ov, f)
def get_image_dataset(cat_ids, example_ids, view_indices, render_config=None): from shapenet.image import with_background from dids.core import BiKeyDataset if render_config is None: from shapenet.core.blender_renderings.config import RenderConfig render_config = RenderConfig() if isinstance(cat_ids, str): cat_ids = [cat_ids] example_ids = [example_ids] if isinstance(view_indices, int): view_indices = [view_indices] datasets = { c: render_config.get_multi_view_dataset( c, view_indices=view_indices, example_ids=eid) for c, eid in zip(cat_ids, example_ids)} dataset = BiKeyDataset(datasets).map( lambda image: with_background(image, 255)) dataset = dataset.map_keys( lambda key: (key[0], (key[1], key[2])), lambda key: (key[0],) + key[1]) 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]) # image = with_background(image, 255) # plt.figure() # plt.imshow(image) # plt.show()
def gen(): for example_id, path in zip(example_ids, paths): image = np.array(PIL.Image.open(path)) image = with_background(image, 255) yield example_id, image
def get_fixed_meshes_dir(): return os.path.join(blender_renderings_dir, '_fixed_meshes') def get_fixed_meshes_zip_path(cat_id): return os.path.join(get_fixed_meshes_dir(), '%s.zip' % cat_id) if __name__ == '__main__': from shapenet.core import cat_desc_to_id, get_example_ids from shapenet.image import load_image_from_zip, with_background import random import zipfile import matplotlib.pyplot as plt from config import RenderConfig cat_desc = 'plane' cat_id = cat_desc_to_id(cat_desc) example_ids = get_example_ids(cat_id) random.shuffle(example_ids) config = RenderConfig() with zipfile.ZipFile(config.get_zip_path(cat_id), 'r') as zf: for example_id in example_ids: subpath = get_example_image_subpath(cat_id, example_id, config.view_angle(5)) image = load_image_from_zip(zf, subpath) image = with_background(image, 255) plt.imshow(image) plt.show()
import os import matplotlib.pyplot as plt from shapenet.image import with_background from shapenet.core.blender_renderings.config import RenderConfig from shapenet.core import cat_desc_to_id, get_example_ids cat_desc = 'plane' view_index = 5 config = RenderConfig() view_angle = config.view_angle(view_index) cat_id = cat_desc_to_id(cat_desc) example_ids = get_example_ids(cat_id) path = config.get_zip_path(cat_id) if not os.path.isfile(path): raise IOError('No renderings at %s' % path) with config.get_dataset(cat_id, view_index) as ds: ds = ds.map(lambda image: with_background(image, 255)) for example_id in ds: image = ds[example_id] plt.imshow(image) plt.show()
def map_np(self, example_id): return with_background(self._dataset[example_id], 255)