Exemple #1
0
 def test_op(self):
     """Tests merged summary with different types of data."""
     name = "my_mesh"
     tensor_data = test_utils.get_random_mesh(
         100, add_faces=True, add_colors=True
     )
     config_dict = {"foo": 1}
     with tf.compat.v1.Graph().as_default():
         tensor_summary = summary.op(
             name,
             tensor_data.vertices,
             faces=tensor_data.faces,
             colors=tensor_data.colors,
             config_dict=config_dict,
         )
         with self.test_session() as sess:
             proto = self.pb_via_op(tensor_summary)
             self.verify_proto(proto, name)
             plugin_metadata = metadata.parse_plugin_metadata(
                 proto.value[0].metadata.plugin_data.content
             )
             self.assertEqual(
                 json.dumps(config_dict, sort_keys=True),
                 plugin_metadata.json_config,
             )
Exemple #2
0
def run():
    """Runs session with a mesh summary."""
    # Mesh summaries only work on TensorFlow 1.x.
    if int(tf.__version__.split(".")[0]) > 1:
        raise ImportError("TensorFlow 1.x is required to run this demo.")
    # Flag mesh_path is required.
    if FLAGS.mesh_path is None:
        raise ValueError(
            "Flag --mesh_path is required and must contain path to PLY file."
        )
    # Camera and scene configuration.
    config_dict = {"camera": {"cls": "PerspectiveCamera", "fov": 75}}

    # Read sample PLY file.
    vertices, colors, faces = demo_utils.read_ascii_ply(FLAGS.mesh_path)

    # Add batch dimension.
    vertices = np.expand_dims(vertices, 0)
    faces = np.expand_dims(faces, 0)
    colors = np.expand_dims(colors, 0)

    # Create placeholders for tensors representing the mesh.
    step = tf.placeholder(tf.int32, ())
    vertices_tensor = tf.placeholder(tf.float32, vertices.shape)
    faces_tensor = tf.placeholder(tf.int32, faces.shape)
    colors_tensor = tf.placeholder(tf.int32, colors.shape)

    # Change colors over time.
    t = tf.cast(step, tf.float32) / _MAX_STEPS
    transformed_colors = t * (255 - colors) + (1 - t) * colors

    meshes_summary = mesh_summary.op(
        "mesh_color_tensor",
        vertices=vertices_tensor,
        faces=faces_tensor,
        colors=transformed_colors,
        config_dict=config_dict,
    )

    # Create summary writer and session.
    writer = tf.summary.FileWriter(FLAGS.logdir)
    sess = tf.Session()

    for i in range(_MAX_STEPS):
        summary = sess.run(
            meshes_summary,
            feed_dict={
                vertices_tensor: vertices,
                faces_tensor: faces,
                colors_tensor: colors,
                step: i,
            },
        )
        writer.add_summary(summary, global_step=i)
Exemple #3
0
 def add_summary(self, data, phase, data_type, name):
     if(data_type == 'scalar'):
         self.tensorboard_ops[phase].append(tf.summary.scalar(name, data))
         log_message('module_base', '---Scalar {} added to tensorboard.---'.format(name))
     elif(data_type == 'image'):
         self.tensorboard_ops[phase].append(tf.summary.image(name, data))
         log_message('module_base', '---Image {} added to tensorboard.---'.format(name))
     elif(data_type == 'voxel'):
         #dereferenced packed data with "**"
         self.tensorboard_ops[phase].append(mesh_summary.op(name, **data))#vertices = data['vertices'], config_dict = data['config_dict']))
         log_message('module_base', '---Voxel {} added to tensorboard.---'.format(name))
     else:
         raise NotImplementedError()
Exemple #4
0
    def mesh_summary(self, tag, vertices, faces=None, colors=None, step=0):

        """Log a list of mesh images."""
        if colors is None:
            colors = tf.constant(np.zeros_like(vertices))
        vertices = tf.constant(vertices)
        if faces is not None:
            faces = tf.constant(faces)
        meshes_summares=[]
        for i in range(vertices.shape[0]):
            meshes_summares.append(meshsummary.op(
                tag, vertices=vertices, faces=faces, colors=colors, config_dict=self.config_dict))

        sess = tf.Session()
        summaries = sess.run(meshes_summares)
        for summary in summaries:
            self.writer.add_summary(summary, step)
    def setUp(self):
        # We use numpy.random to generate meshes. We seed to avoid non-determinism
        # in this test.
        np.random.seed(17)

        # Log dir to save temp events into.
        self.log_dir = self.get_temp_dir()

        # Create mesh summary.
        with tf.compat.v1.Graph().as_default():
            tf_placeholder = tf.compat.v1.placeholder
            sess = tf.compat.v1.Session()
            point_cloud = test_utils.get_random_mesh(1000)
            point_cloud_vertices = tf_placeholder(tf.float32,
                                                  point_cloud.vertices.shape)

            mesh_no_color = test_utils.get_random_mesh(2000, add_faces=True)
            mesh_no_color_extended = test_utils.get_random_mesh(2500,
                                                                add_faces=True)
            mesh_no_color_vertices = tf_placeholder(tf.float32, [1, None, 3])
            mesh_no_color_faces = tf_placeholder(tf.int32, [1, None, 3])

            mesh_color = test_utils.get_random_mesh(3000,
                                                    add_faces=True,
                                                    add_colors=True)
            mesh_color_vertices = tf_placeholder(tf.float32,
                                                 mesh_color.vertices.shape)
            mesh_color_faces = tf_placeholder(tf.int32, mesh_color.faces.shape)
            mesh_color_colors = tf_placeholder(tf.uint8,
                                               mesh_color.colors.shape)

            self.data = [
                point_cloud, mesh_no_color, mesh_no_color_extended, mesh_color
            ]

            # In case when name is present and display_name is not, we will reuse name
            # as display_name. Summaries below intended to test both cases.
            self.names = ["point_cloud", "mesh_no_color", "mesh_color"]
            summary.op(self.names[0],
                       point_cloud_vertices,
                       description="just point cloud")
            summary.op(self.names[1],
                       mesh_no_color_vertices,
                       faces=mesh_no_color_faces,
                       display_name="name_to_display_in_ui",
                       description="beautiful mesh in grayscale")
            summary.op(self.names[2],
                       mesh_color_vertices,
                       faces=mesh_color_faces,
                       colors=mesh_color_colors,
                       description="mesh with random colors")

            merged_summary_op = tf.compat.v1.summary.merge_all()
            self.runs = ["bar"]
            self.steps = 20
            bar_directory = os.path.join(self.log_dir, self.runs[0])
            with tensorboard_test_util.FileWriterCache.get(
                    bar_directory) as writer:
                writer.add_graph(sess.graph)
                for step in range(self.steps):
                    # Alternate between two random meshes with different number of
                    # vertices.
                    no_color = mesh_no_color if step % 2 == 0 else mesh_no_color_extended
                    with patch.object(time, 'time', return_value=step):
                        writer.add_summary(sess.run(merged_summary_op,
                                                    feed_dict={
                                                        point_cloud_vertices:
                                                        point_cloud.vertices,
                                                        mesh_no_color_vertices:
                                                        no_color.vertices,
                                                        mesh_no_color_faces:
                                                        no_color.faces,
                                                        mesh_color_vertices:
                                                        mesh_color.vertices,
                                                        mesh_color_faces:
                                                        mesh_color.faces,
                                                        mesh_color_colors:
                                                        mesh_color.colors,
                                                    }),
                                           global_step=step)

        # Start a server that will receive requests.
        self.multiplexer = event_multiplexer.EventMultiplexer({
            "bar":
            bar_directory,
        })
        self.context = base_plugin.TBContext(logdir=self.log_dir,
                                             multiplexer=self.multiplexer)
        self.plugin = mesh_plugin.MeshPlugin(self.context)
        # Wait until after plugin construction to reload the multiplexer because the
        # plugin caches data from the multiplexer upon construction and this affects
        # logic tested later down.
        # TODO(https://github.com/tensorflow/tensorboard/issues/2579): Eliminate the
        # caching of data at construction time and move this Reload() up to just
        # after the multiplexer is created.
        self.multiplexer.Reload()
        wsgi_app = application.TensorBoardWSGI([self.plugin])
        self.server = werkzeug_test.Client(wsgi_app, wrappers.BaseResponse)
        self.routes = self.plugin.get_plugin_apps()
Exemple #6
0
        'fov': 75
    },
    'lights': [{
        'cls': 'AmbientLight',
        'color': '#ffffff',
        'intensity': 0.75,
    }, {
        'cls': 'DirectionalLight',
        'color': '#ffffff',
        'intensity': 0.75,
        'position': [0, -1, 2],
    }],
    'material': {
        'cls': 'MeshStandardMaterial',
        'roughness': 1,
        'metalness': 0
    }
}
log_dir = '/home/yu/PycharmProjects/feastnet/Direction/data'
# vertices_tensor = tf.placeholder(tf.float32, [1,None,3])
sess = tf.Session()
for i in range(3):
    feed_dict, epoch_end = rf.get_feed()
    vertices_tensor = tf.expand_dims(feed_dict['input'], 0)
    print(vertices_tensor.shape)
    meshes_summary = mesh_summary.op('mesh_color_tensor',
                                     vertices=vertices_tensor,
                                     config_dict=config_dict)
    writer = tf.summary.FileWriter(log_dir)
    writer.add_summary(meshes_summary)
Exemple #7
0
model.build_model()

# Summary the variables
ptraps = tf.summary.scalar('Particle Loss', model.train_particleLoss)
ptraprs = tf.summary.scalar('Particle Reconstruct Loss', model.train_error)
ptrapss = tf.summary.scalar('Particle Simulation Loss',
                            model.train_particleSimLoss)
vals = tf.summary.scalar('Validation Loss',
                         model.val_particleLoss,
                         collections=None)
ptrapfs = tf.summary.scalar('Particle HQPool Loss', model.train_HQPLoss)
ptrapps = tf.summary.scalar('Particle Pool Align Loss', model.train_PALoss)

from tensorboard.plugins.mesh import summary as mesh_summary
pc_rec = mesh_summary.op('Reconstruction',
                         vertices=tf.expand_dims(model.val_rec[0, :, :], 0),
                         colors=tf.constant([[[109, 131, 70]]],
                                            shape=[1, args.voxel_size, 3]))
pc_gt = mesh_summary.op('Ground truth',
                        vertices=tf.expand_dims(model.val_gt[0, :, :], 0),
                        colors=tf.constant([[[0, 154, 214]]],
                                           shape=[1, args.voxel_size, 3]))

merged_train = tf.summary.merge([ptraps, ptraprs, ptrapss, ptrapfs, ptrapps])
merged_val = tf.summary.merge([vals])
merged_mesh = tf.summary.merge([pc_rec, pc_gt])
# merged_train = tf.summary.merge_all()

# Create session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
Exemple #8
0
def training(sess):
    print('load data from: ', flags.data_dir)
    if 'mnist' in flags.data_dir:
        train_data = read_mnist_data(os.path.join(flags.data_dir, 'mnist_train.csv'))
        valid_data = read_mnist_data(os.path.join(flags.data_dir, 'mnist_test.csv'))
    elif 'modelnet40' in flags.data_dir:
        train_data, valid_data, _ = read_modelnet40_data(flags.data_dir, flags.num_pt)
    else:
        print('data path unrecognised!')
        return
    batch_size = flags.batch_size

    model = network(sess, flags)
    init_temp = 1.
    final_temp = 0.01
    decay = (final_temp/init_temp)**(1./float(flags.max_epoch))

    trainable_var = tf.trainable_variables()
    part_var = []
    print('Trainable var list: ')
    for idx, v in enumerate(trainable_var):
        print('  var {:3}: {:20}   {}'.format(idx, str(v.get_shape()), v.name))

    init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    sess.run(init_op)

    model_dir = os.path.join(flags.model_dir, flags.model_name)
    if not os.path.exists(model_dir): 
        os.makedirs(model_dir)
    saver = tf.train.Saver(max_to_keep=1, save_relative_paths=True)
    if flags.load_model:
        checkpoint = tf.train.get_checkpoint_state(model_dir) 
        if checkpoint and checkpoint.model_checkpoint_path:
            saver.restore(sess, checkpoint.model_checkpoint_path)
            print('model loaded: ', checkpoint.model_checkpoint_path )
        else:
            print('model not found')
    summary_writer = tf.summary.FileWriter(model_dir, sess.graph)
    xyz_ph = tf.placeholder(tf.float32, shape=[flags.batch_size, flags.num_pt, 3], name='xyz_ph')
    rgb_ph = tf.placeholder(tf.float32, shape=[flags.batch_size, flags.num_pt, 3], name='rgb_ph')
    point_summary = mesh_summary.op('point_cloud', vertices=xyz_ph, colors=rgb_ph)
    train_acc_ph = tf.placeholder(tf.float32, shape=[], name='train_acc_ph')
    train_acc_summary = tf.summary.scalar('train_acc', train_acc_ph)
    test_acc_ph = tf.placeholder(tf.float32, shape=[], name='test_acc_ph')
    test_acc_summary = tf.summary.scalar('test_acc', test_acc_ph)
    temp_summary = tf.summary.scalar('temperature', model.t)
    merged = tf.summary.merge_all()

    start_time = time.time()
    print('start training')
    temp = init_temp
    for epoch in range(flags.max_epoch):
        # training
        loss_list = []
        acc_list = []
        opt_time = []
        end_flag = False
        random.shuffle(train_data)
        bar = progressbar.ProgressBar(maxval=len(train_data)/batch_size+len(valid_data)/batch_size, \
                                      widgets=[progressbar.Bar('=', '[', ']'), ' ', 
                                               progressbar.Percentage()])
        all_t = 0
        for t in range(int(len(train_data)/batch_size)):
            batch_data = get_a_batch(train_data, t*batch_size, batch_size)
            acc, loss, _ = model.train(batch_data, temp)
            loss_list.append(loss)
            acc_list.append(acc)
            all_t += 1
            bar.update(all_t)
        loss_train = np.mean(loss_list)
        acc_train = np.mean(acc_list)

        # validating
        loss_list = []
        acc_list = []
        end_flag = False
        pos = 0
        for t in range(int(len(valid_data)/batch_size)):
            batch_data = get_a_batch(valid_data, t*batch_size, batch_size)
            acc, loss, sampled_points, score = model.validate(batch_data, temp)
            loss_list.append(loss)
            acc_list.append(acc)
            all_t += 1
            bar.update(all_t)
        bar.finish()
        loss_valid = np.mean(loss_list)
        acc_valid = np.mean(acc_list)
        points_rgb = (np.stack([score, np.zeros_like(score), np.zeros_like(score)], axis=2)*255).astype(int)
        points_xyz = batch_data[0]

        info_train = '| Epoch:{:3d}'.format(epoch) + \
                     '| TrainLoss: {:2.5f}'.format(loss_train) + \
                     '| TestLoss: {:2.5f}'.format(loss_valid) + \
                     '| TrainAcc: {:2.5f}'.format(acc_train) + \
                     '| TestAcc: {:2.5f}'.format(acc_valid) + \
                     '| Time(min): {:2.1f}'.format((time.time() - start_time)/60.) + \
                     '| Temp: {:1.5f}'.format(temp)
        print(info_train)
        summary = sess.run(merged, feed_dict={xyz_ph: points_xyz,
                                              rgb_ph: points_rgb,
                                              train_acc_ph: acc_train,
                                              test_acc_ph: acc_valid,
                                              model.t: temp})
        summary_writer.add_summary(summary, epoch)
        if flags.save_model and epoch == flags.max_epoch-1:
            saver.save(sess, os.path.join(model_dir, 'network') , global_step=epoch)
        if flags.sample_mode in ['normal', 'determine', 'concrete']:
            temp *= decay
  def setUp(self):
    # We use numpy.random to generate meshes. We seed to avoid non-determinism
    # in this test.
    np.random.seed(17)

    # Log dir to save temp events into.
    self.log_dir = self.get_temp_dir()

    # Create mesh summary.
    tf.compat.v1.reset_default_graph()
    sess = tf.compat.v1.Session()
    point_cloud = test_utils.get_random_mesh(1000)
    point_cloud_vertices = tf.compat.v1.placeholder(tf.float32,
                                                    point_cloud.vertices.shape)

    mesh_no_color = test_utils.get_random_mesh(2000, add_faces=True)
    mesh_no_color_vertices = tf.compat.v1.placeholder(
        tf.float32, mesh_no_color.vertices.shape)
    mesh_no_color_faces = tf.compat.v1.placeholder(tf.int32,
                                                   mesh_no_color.faces.shape)

    mesh_color = test_utils.get_random_mesh(
        3000, add_faces=True, add_colors=True)
    mesh_color_vertices = tf.compat.v1.placeholder(tf.float32,
                                                   mesh_color.vertices.shape)
    mesh_color_faces = tf.compat.v1.placeholder(tf.int32,
                                                mesh_color.faces.shape)
    mesh_color_colors = tf.compat.v1.placeholder(tf.uint8,
                                                 mesh_color.colors.shape)
    self.data = [point_cloud, mesh_no_color, mesh_color]

    # In case when name is present and display_name is not, we will reuse name
    # as display_name. Summaries below intended to test both cases.
    self.names = ["point_cloud", "mesh_no_color", "mesh_color"]
    summary.op(
        self.names[0],
        point_cloud_vertices,
        description="just point cloud")
    summary.op(
        self.names[1],
        mesh_no_color_vertices,
        faces=mesh_no_color_faces,
        display_name="name_to_display_in_ui",
        description="beautiful mesh in grayscale")
    summary.op(
        self.names[2],
        mesh_color_vertices,
        faces=mesh_color_faces,
        colors=mesh_color_colors,
        description="mesh with random colors")

    merged_summary_op = tf.compat.v1.summary.merge_all()
    self.runs = ["bar"]
    self.steps = 20
    bar_directory = os.path.join(self.log_dir, self.runs[0])
    with tensorboard_test_util.FileWriterCache.get(bar_directory) as writer:
      writer.add_graph(sess.graph)
      for step in range(self.steps):
        writer.add_summary(
            sess.run(
                merged_summary_op,
                feed_dict={
                    point_cloud_vertices: point_cloud.vertices,
                    mesh_no_color_vertices: mesh_no_color.vertices,
                    mesh_no_color_faces: mesh_no_color.faces,
                    mesh_color_vertices: mesh_color.vertices,
                    mesh_color_faces: mesh_color.faces,
                    mesh_color_colors: mesh_color.colors,
                }),
            global_step=step)

    # Start a server that will receive requests.
    self.multiplexer = event_multiplexer.EventMultiplexer({
        "bar": bar_directory,
    })
    self.context = base_plugin.TBContext(
        logdir=self.log_dir, multiplexer=self.multiplexer)
    self.plugin = mesh_plugin.MeshPlugin(self.context)
    wsgi_app = application.TensorBoardWSGIApp(
        self.log_dir, [self.plugin],
        self.multiplexer,
        reload_interval=0,
        path_prefix="")
    self.server = werkzeug_test.Client(wsgi_app, wrappers.BaseResponse)
    self.multiplexer.Reload()
    self.routes = self.plugin.get_plugin_apps()
Exemple #10
0
    points_f = result
    points = np.concatenate([points_v, points_f], axis=1)
    
    colors_v = np.ones_like(points_v) * [0, 0, 255]
    colors_f = np.ones_like(points_f) * [0, 255, 0]
    colors = np.concatenate([colors_v, colors_f], axis=1)
        
        # %%
    tf.compat.v1.disable_eager_execution()

    points_tensor = tf.compat.v1.placeholder(tf.float32, points.shape)
    colors_tensor = tf.compat.v1.placeholder(tf.int32, colors.shape)
    
    summary = mesh_summary.op(
        'v_color_tensor',
        vertices=points_tensor,
        colors=colors_tensor,
        config_dict=config_dict
    )
    
    # Create summary writer and session.
    writer = tf.compat.v1.summary.FileWriter(log_dir)
    sess = tf.compat.v1.Session()
    
    # %%
    
    summaries = sess.run([summary], feed_dict={
        points_tensor: points,
        colors_tensor: colors,
    })
    # Save summaries.
    for summary in summaries: