def mesh_pb(tag, vertices, faces=None, colors=None, config_dict=None, description=None): """Create a mesh summary to save in pb format. Args: tag: String tag for the summary. vertices: numpy array of shape `[dim_1, ..., dim_n, 3]` representing the 3D coordinates of vertices. faces: numpy array of shape `[dim_1, ..., dim_n, 3]` containing indices of vertices within each triangle. colors: numpy array of shape `[dim_1, ..., dim_n, 3]` containing colors for each vertex. config_dict: Dictionary with ThreeJS classes names and configuration. description: Optional long-form description for this summary, as a constant `str`. Markdown is supported. Defaults to empty. Returns: Instance of tf.Summary class. """ json_config = _get_json_config(config_dict) summaries = [] tensors = [ metadata.MeshTensor(vertices, plugin_data_pb2.MeshPluginData.VERTEX, tf.float32), metadata.MeshTensor(faces, plugin_data_pb2.MeshPluginData.FACE, tf.int32), metadata.MeshTensor(colors, plugin_data_pb2.MeshPluginData.COLOR, tf.uint8), ] tensors = [tensor for tensor in tensors if tensor.data is not None] components = metadata.get_components_bitmask( [tensor.content_type for tensor in tensors]) for tensor in tensors: shape = tensor.data.shape shape = [dim if dim is not None else -1 for dim in shape] tensor_proto = tensor_util.make_tensor_proto(tensor.data, dtype=tensor.data_type) summary_metadata = metadata.create_summary_metadata( tag, None, # display_name tensor.content_type, components, shape, description, json_config=json_config, ) instance_tag = metadata.get_instance_name(tag, tensor.content_type) summaries.append((instance_tag, summary_metadata, tensor_proto)) summary = summary_pb2.Summary() for instance_tag, summary_metadata, tensor_proto in summaries: summary.value.add(tag=instance_tag, metadata=summary_metadata, tensor=tensor_proto) return summary
def _get_tensor_summary( name, display_name, description, tensor, content_type, components, json_config ): """Creates a tensor summary with summary metadata. Args: name: Uniquely identifiable name of the summary op. Could be replaced by combination of name and type to make it unique even outside of this summary. display_name: Will be used as the display name in TensorBoard. Defaults to `name`. description: A longform readable description of the summary data. Markdown is supported. tensor: Tensor to display in summary. content_type: Type of content inside the Tensor. components: Bitmask representing present parts (vertices, colors, etc.) that belong to the summary. json_config: A string, JSON-serialized dictionary of ThreeJS classes configuration. Returns: Tensor summary with metadata. """ import torch from tensorboard.plugins.mesh import metadata tensor = torch.as_tensor(tensor) tensor_metadata = metadata.create_summary_metadata( name, display_name, content_type, components, tensor.shape, description, json_config=json_config, ) tensor = TensorProto( dtype="DT_FLOAT", float_val=tensor.reshape(-1).tolist(), tensor_shape=TensorShapeProto( dim=[ TensorShapeProto.Dim(size=tensor.shape[0]), TensorShapeProto.Dim(size=tensor.shape[1]), TensorShapeProto.Dim(size=tensor.shape[2]), ] ), ) tensor_summary = Summary.Value( tag=metadata.get_instance_name(name, content_type), tensor=tensor, metadata=tensor_metadata, ) return tensor_summary
def pb(name, vertices, faces=None, colors=None, display_name=None, description=None, config_dict=None): """Create a mesh summary to save in pb format. Args: name: A name for this summary operation. vertices: numpy array of shape `[dim_1, ..., dim_n, 3]` representing the 3D coordinates of vertices. faces: numpy array of shape `[dim_1, ..., dim_n, 3]` containing indices of vertices within each triangle. colors: numpy array of shape `[dim_1, ..., dim_n, 3]` containing colors for each vertex. display_name: If set, will be used as the display name in TensorBoard. Defaults to `name`. description: A longform readable description of the summary data. Markdown is supported. config_dict: Dictionary with ThreeJS classes names and configuration. Returns: Instance of tf.Summary class. """ display_name = _get_display_name(name, display_name) json_config = _get_json_config(config_dict) summaries = [] tensors = [(vertices, plugin_data_pb2.MeshPluginData.VERTEX, tf.float32), (faces, plugin_data_pb2.MeshPluginData.FACE, tf.int32), (colors, plugin_data_pb2.MeshPluginData.COLOR, tf.uint8)] for tensor, content_type, data_type in tensors: if tensor is None: continue tensor_shape = tensor.shape tensor = tf.compat.v1.make_tensor_proto(tensor, dtype=data_type) summary_metadata = metadata.create_summary_metadata( name, display_name, content_type, tensor_shape, description, json_config=json_config) tag = metadata.get_instance_name(name, content_type) summaries.append((tag, summary_metadata, tensor)) summary = tf.Summary() for tag, summary_metadata, tensor in summaries: tf_summary_metadata = tf.SummaryMetadata.FromString( summary_metadata.SerializeToString()) summary.value.add(tag=tag, metadata=tf_summary_metadata, tensor=tensor) return summary
def _get_tensor_summary( name, display_name, description, tensor, content_type, components, json_config, collections, ): """Creates a tensor summary with summary metadata. Args: name: Uniquely identifiable name of the summary op. Could be replaced by combination of name and type to make it unique even outside of this summary. display_name: Will be used as the display name in TensorBoard. Defaults to `tag`. description: A longform readable description of the summary data. Markdown is supported. tensor: Tensor to display in summary. content_type: Type of content inside the Tensor. components: Bitmask representing present parts (vertices, colors, etc.) that belong to the summary. json_config: A string, JSON-serialized dictionary of ThreeJS classes configuration. collections: List of collections to add this summary to. Returns: Tensor summary with metadata. """ tensor = tf.convert_to_tensor(value=tensor) shape = tensor.shape.as_list() shape = [dim if dim is not None else -1 for dim in shape] tensor_metadata = metadata.create_summary_metadata( name, display_name, content_type, components, shape, description, json_config=json_config, ) tensor_summary = tf.compat.v1.summary.tensor_summary( metadata.get_instance_name(name, content_type), tensor, summary_metadata=tensor_metadata, collections=collections, ) return tensor_summary
def _create_metadata(self, shape=None): """Creates metadata with dummy data.""" self.name = 'unique_name' self.display_name = 'my mesh' self.json_config = '{}' if shape is None: shape = [1, 100, 3] self.shape = shape self.summary_metadata = metadata.create_summary_metadata( self.name, self.display_name, plugin_data_pb2.MeshPluginData.ContentType.Value('VERTEX'), self.shape, json_config=self.json_config)
def _create_metadata(self, shape=None): """Creates metadata with dummy data.""" self.name = "unique_name" self.display_name = "my mesh" self.json_config = "{}" if shape is None: shape = [1, 100, 3] self.shape = shape self.components = 14 self.summary_metadata = metadata.create_summary_metadata( self.name, self.display_name, plugin_data_pb2.MeshPluginData.ContentType.Value("VERTEX"), self.components, self.shape, json_config=self.json_config, )
def _write_summary(name, description, tensor, content_type, components, json_config, step): """Creates a tensor summary with summary metadata. Args: name: A name for this summary. The summary tag used for TensorBoard will be this name prefixed by any active name scopes. description: Optional long-form description for this summary, as a constant `str`. Markdown is supported. Defaults to empty. tensor: Tensor to display in summary. content_type: Type of content inside the Tensor. components: Bitmask representing present parts (vertices, colors, etc.) that belong to the summary. json_config: A string, JSON-serialized dictionary of ThreeJS classes configuration. step: Explicit `int64`-castable monotonic step value for this summary. If omitted, this defaults to `tf.summary.experimental.get_step()`, which must not be None. Returns: A boolean indicating if summary was saved successfully or not. """ tensor = tf.convert_to_tensor(value=tensor) shape = tensor.shape.as_list() shape = [dim if dim is not None else -1 for dim in shape] tensor_metadata = metadata.create_summary_metadata( name, None, # display_name content_type, components, shape, description, json_config=json_config, ) return tf.summary.write( tag=metadata.get_instance_name(name, content_type), tensor=tensor, step=step, metadata=tensor_metadata, )