def stream_experiment_data(request, **kwargs): self.assertEqual(kwargs["metadata"], grpc_util.version_metadata()) for run in ("train_1", "train_2"): for tag in ("dense_1/kernel", "dense_1/bias", "text/test"): response = export_service_pb2.StreamExperimentDataResponse( ) response.run_name = run response.tag_name = tag display_name = "%s:%s" % (request.experiment_id, tag) response.tag_metadata.CopyFrom( test_util.scalar_metadata(display_name)) for step in range(2): response.tensors.steps.append(step) response.tensors.wall_times.add( seconds=1571084520 + step, nanos=862939144 if run == "train_1" else 962939144, ) if tag != "text/test": response.tensors.values.append( tensor_util.make_tensor_proto( np.ones([3, 2]) * step)) else: response.tensors.values.append( tensor_util.make_tensor_proto( np.full([3], "a" * (step + 1)))) yield response
def __call__(self): entries = [] for tag, value in self.items(): if isinstance(value, DelayedScalar): entries.append( summary_pb2.Summary.Value(tag=tag, simple_value=value())) elif isinstance(value, Image): image_summary = summary_pb2.Summary.Image( encoded_image_string=value.png, colorspace=value.shape[0], height=value.shape[1], width=value.shape[2]) entries.append( summary_pb2.Summary.Value(tag=tag, image=image_summary)) elif isinstance(value, Text): metadata = summary_pb2.SummaryMetadata( plugin_data=summary_pb2.SummaryMetadata.PluginData( plugin_name='text')) entries.append( summary_pb2.Summary.Value( tag=tag, metadata=metadata, tensor=make_tensor_proto( values=value.text.encode('utf-8'), shape=(1, )))) else: raise NotImplementedError(tag, value) return summary_pb2.Summary(value=entries)
def emit_scalar( self, *, plugin_name, tag, data, step, wall_time, tag_metadata=None, description=None, ): """See `Output`.""" # TODO(#4581): cache summary metadata to emit only once. summary_metadata = summary_pb2.SummaryMetadata( plugin_data=summary_pb2.SummaryMetadata.PluginData( plugin_name=plugin_name, content=tag_metadata ), summary_description=description, data_class=summary_pb2.DataClass.DATA_CLASS_SCALAR, ) tensor_proto = tensor_util.make_tensor_proto(data) event = event_pb2.Event(wall_time=wall_time, step=step) event.summary.value.add( tag=tag, tensor=tensor_proto, metadata=summary_metadata ) self._ev_writer.add_event(event)
def _migrate_graph_event(old_event, experimental_filter_graph=False): result = event_pb2.Event() result.wall_time = old_event.wall_time result.step = old_event.step value = result.summary.value.add(tag=graphs_metadata.RUN_GRAPH_NAME) graph_bytes = old_event.graph_def # TODO(@davidsoergel): Move this stopgap to a more appropriate place. if experimental_filter_graph: try: graph_def = graph_pb2.GraphDef().FromString(graph_bytes) # The reason for the RuntimeWarning catch here is b/27494216, whereby # some proto parsers incorrectly raise that instead of DecodeError # on certain kinds of malformed input. Triggering this seems to require # a combination of mysterious circumstances. except (message.DecodeError, RuntimeWarning): logger.warning( "Could not parse GraphDef of size %d. Skipping.", len(graph_bytes), ) return (old_event, ) # Use the default filter parameters: # limit_attr_size=1024, large_attrs_key="_too_large_attrs" process_graph.prepare_graph_for_ui(graph_def) graph_bytes = graph_def.SerializeToString() value.tensor.CopyFrom(tensor_util.make_tensor_proto([graph_bytes])) value.metadata.plugin_data.plugin_name = graphs_metadata.PLUGIN_NAME # `value.metadata.plugin_data.content` left as the empty proto value.metadata.data_class = summary_pb2.DATA_CLASS_BLOB_SEQUENCE # In the short term, keep both the old event and the new event to # maintain compatibility. return (old_event, result)
def text_pb(tag, data, description=None): """Create a text tf.Summary protobuf. Arguments: tag: String tag for the summary. data: A Python bytestring (of type bytes), a Unicode string, or a numpy data array of those types. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Raises: TypeError: If the type of the data is unsupported. Returns: A `tf.Summary` protobuf object. """ # TODO(nickfelt): remove on-demand imports once dep situation is fixed. from tensorboard.compat import tf try: tensor = tensor_util.make_tensor_proto(data, dtype=tf.string) except TypeError as e: raise TypeError("tensor must be of type string", e) summary_metadata = metadata.create_summary_metadata( display_name=None, description=description) summary = summary_pb2.Summary() summary.value.add(tag=tag, metadata=summary_metadata, tensor=tensor) return summary
def pb(name, data, display_name=None, description=None): """Create a text summary protobuf. Arguments: name: A name for the generated node. Will also serve as a series name in TensorBoard. data: A Python bytestring (of type bytes), or Unicode string. Or a numpy data array of those types. display_name: Optional name for this summary in TensorBoard, as a `str`. Defaults to `name`. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Raises: ValueError: If the type of the data is unsupported. Returns: A `tf.Summary` protobuf object. """ try: tensor = tensor_util.make_tensor_proto(data, dtype=tf.string) except TypeError as e: raise ValueError(e) if display_name is None: display_name = name summary_metadata = metadata.create_summary_metadata( display_name=display_name, description=description) summary = tf.Summary() summary.value.add(tag='%s/text_summary' % name, metadata=summary_metadata, tensor=tensor) return summary
def scalar_pb(tag, data, description=None): """Create a scalar summary_pb2.Summary protobuf. Arguments: tag: String tag for the summary. data: A 0-dimensional `np.array` or a compatible python number type. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Raises: ValueError: If the type or shape of the data is unsupported. Returns: A `summary_pb2.Summary` protobuf object. """ arr = np.array(data) if arr.shape != (): raise ValueError('Expected scalar shape for tensor, got shape: %s.' % arr.shape) if arr.dtype.kind not in ('b', 'i', 'u', 'f'): # bool, int, uint, float raise ValueError('Cast %s to float is not supported' % arr.dtype.name) tensor_proto = tensor_util.make_tensor_proto(arr.astype(np.float32)) summary_metadata = metadata.create_summary_metadata( display_name=None, description=description) summary = summary_pb2.Summary() summary.value.add(tag=tag, metadata=summary_metadata, tensor=tensor_proto) return summary
def pb(name, data, display_name=None, description=None): """Create a legacy scalar summary protobuf. Arguments: name: A unique name for the generated summary, including any desired name scopes. data: A rank-0 `np.array` or array-like form (so raw `int`s and `float`s are fine, too). display_name: Optional name for this summary in TensorBoard, as a `str`. Defaults to `name`. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Returns: A `tf.Summary` protobuf object. """ data = np.array(data) if data.shape != (): raise ValueError('Expected scalar shape for data, saw shape: %s.' % data.shape) if data.dtype.kind not in ('b', 'i', 'u', 'f'): # bool, int, uint, float raise ValueError('Cast %s to float is not supported' % data.dtype.name) tensor = tensor_util.make_tensor_proto(data.astype(np.float32)) if display_name is None: display_name = name summary_metadata = metadata.create_summary_metadata( display_name=display_name, description=description) summary = tf.Summary() summary.value.add(tag='%s/scalar_summary' % name, metadata=summary_metadata, tensor=tensor) return summary
def _create_event_with_float_tensor(self, node_name, output_slot, debug_op, list_of_values): """Creates event with float64 (double) tensors. Args: node_name: The string name of the op. This lacks both the output slot as well as the name of the debug op. output_slot: The number that is the output slot. debug_op: The name of the debug op to use. list_of_values: A python list of values within the tensor. Returns: A `tf.Event` with a summary containing that node name and a float64 tensor with those values. """ event = tf.Event() value = event.summary.value.add( tag=node_name, node_name="%s:%d:%s" % (node_name, output_slot, debug_op), tensor=tensor_util.make_tensor_proto( list_of_values, dtype=tf.float64, shape=[len(list_of_values)])) plugin_content = debugger_event_metadata_pb2.DebuggerEventMetadata( device="/job:localhost/replica:0/task:0/cpu:0", output_slot=output_slot) value.metadata.plugin_data.plugin_name = constants.DEBUGGER_PLUGIN_NAME value.metadata.plugin_data.content = tf.compat.as_bytes( json_format.MessageToJson( plugin_content, including_default_value_fields=True)) return event
def text_pb(tag, data, description=None): """Create a text tf.Summary protobuf. Arguments: tag: String tag for the summary. data: A Python bytestring (of type bytes), a Unicode string, or a numpy data array of those types. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Raises: TypeError: If the type of the data is unsupported. Returns: A `tf.Summary` protobuf object. """ try: tensor = tensor_util.make_tensor_proto(data, dtype=np.object) except TypeError as e: raise TypeError('tensor must be of type string', e) summary_metadata = metadata.create_summary_metadata( display_name=None, description=description) summary = summary_pb2.Summary() summary.value.add(tag=tag, metadata=summary_metadata, tensor=tensor) return summary
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 _migrate_graph_event(old_event, experimental_filter_graph=False): result = event_pb2.Event() result.wall_time = old_event.wall_time result.step = old_event.step value = result.summary.value.add(tag=graphs_metadata.RUN_GRAPH_NAME) graph_bytes = old_event.graph_def # TODO(@davidsoergel): Move this stopgap to a more appropriate place. if experimental_filter_graph: try: graph_def = graph_pb2.GraphDef().FromString(graph_bytes) except message.DecodeError: logger.warning( "Could not parse GraphDef of size %d. Skipping.", len(graph_bytes), ) return (old_event, ) # Use the default filter parameters: # limit_attr_size=1024, large_attrs_key="_too_large_attrs" process_graph.prepare_graph_for_ui(graph_def) graph_bytes = graph_def.SerializeToString() value.tensor.CopyFrom(tensor_util.make_tensor_proto([graph_bytes])) value.metadata.plugin_data.plugin_name = graphs_metadata.PLUGIN_NAME # `value.metadata.plugin_data.content` left as the empty proto value.metadata.data_class = summary_pb2.DATA_CLASS_BLOB_SEQUENCE # In the short term, keep both the old event and the new event to # maintain compatibility. return (old_event, result)
def test_read_tensors(self): res = data_provider_pb2.ReadTensorsResponse() run = res.runs.add(run_name="test") tag = run.tags.add(tag_name="weights") tag.data.step.extend([0, 1, 2]) tag.data.wall_time.extend([1234.0, 1235.0, 1236.0]) tag.data.value.append(tensor_util.make_tensor_proto([0.0, 0.0, 42.0])) tag.data.value.append(tensor_util.make_tensor_proto([1.0, 1.0, 43.0])) tag.data.value.append(tensor_util.make_tensor_proto([2.0, 2.0, 44.0])) self.stub.ReadTensors.return_value = res actual = self.provider.read_tensors( self.ctx, experiment_id="123", plugin_name="histograms", run_tag_filter=provider.RunTagFilter(runs=["test", "nope"]), downsample=3, ) expected = { "test": { "weights": [ provider.TensorDatum( step=0, wall_time=1234.0, numpy=np.array([0.0, 0.0, 42.0]), ), provider.TensorDatum( step=1, wall_time=1235.0, numpy=np.array([1.0, 1.0, 43.0]), ), provider.TensorDatum( step=2, wall_time=1236.0, numpy=np.array([2.0, 2.0, 44.0]), ), ], }, } self.assertEqual(actual, expected) req = data_provider_pb2.ReadTensorsRequest() req.experiment_id = "123" req.plugin_filter.plugin_name = "histograms" req.run_tag_filter.runs.names.extend(["nope", "test"]) # sorted req.downsample.num_points = 3 self.stub.ReadTensors.assert_called_once_with(req)
def pb(name, data, bucket_count=None, display_name=None, description=None): """Create a histogram summary protobuf. Arguments: name: A unique name for the generated summary, including any desired name scopes. data: A `np.array` or array-like form of any shape. Must have type castable to `float`. bucket_count: Optional positive `int`. The output will have this many buckets, except in two edge cases. If there is no data, then there are no buckets. If there is data but all points have the same value, then there is one bucket whose left and right endpoints are the same. display_name: Optional name for this summary in TensorBoard, as a `str`. Defaults to `name`. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Returns: A `tf.Summary` protobuf object. """ if bucket_count is None: bucket_count = DEFAULT_BUCKET_COUNT data = np.array(data).flatten().astype(float) if data.size == 0: buckets = np.array([]).reshape((0, 3)) else: min_ = np.min(data) max_ = np.max(data) range_ = max_ - min_ if range_ == 0: center = min_ buckets = np.array([[center - 0.5, center + 0.5, float(data.size)]]) else: bucket_width = range_ / bucket_count offsets = data - min_ bucket_indices = np.floor(offsets / bucket_width).astype(int) clamped_indices = np.minimum(bucket_indices, bucket_count - 1) one_hots = (np.array([clamped_indices]).transpose() == np.arange(0, bucket_count)) # broadcast assert one_hots.shape == (data.size, bucket_count), ( one_hots.shape, (data.size, bucket_count)) bucket_counts = np.sum(one_hots, axis=0) edges = np.linspace(min_, max_, bucket_count + 1) left_edges = edges[:-1] right_edges = edges[1:] buckets = np.array([left_edges, right_edges, bucket_counts]).transpose() tensor = tensor_util.make_tensor_proto(buckets, dtype=tf.float64) if display_name is None: display_name = name summary_metadata = metadata.create_summary_metadata( display_name=display_name, description=description) summary = tf.Summary() summary.value.add(tag='%s/histogram_summary' % name, metadata=summary_metadata, tensor=tensor) return summary
def histogram_pb(tag, data, buckets=None, description=None): """Create a histogram summary protobuf. Arguments: tag: String tag for the summary. data: A `np.array` or array-like form of any shape. Must have type castable to `float`. buckets: Optional positive `int`. The output will have this many buckets, except in two edge cases. If there is no data, then there are no buckets. If there is data but all points have the same value, then there is one bucket whose left and right endpoints are the same. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Returns: A `summary_pb2.Summary` protobuf object. """ # TODO(nickfelt): remove on-demand imports once dep situation is fixed. from tensorboard.compat import tf bucket_count = DEFAULT_BUCKET_COUNT if buckets is None else buckets data = np.array(data).flatten().astype(float) if data.size == 0: buckets = np.array([]).reshape((0, 3)) else: min_ = np.min(data) max_ = np.max(data) range_ = max_ - min_ if range_ == 0: center = min_ buckets = np.array([[center - 0.5, center + 0.5, float(data.size)]]) else: bucket_width = range_ / bucket_count offsets = data - min_ bucket_indices = np.floor(offsets / bucket_width).astype(int) clamped_indices = np.minimum(bucket_indices, bucket_count - 1) one_hots = (np.array([clamped_indices ]).transpose() == np.arange(0, bucket_count) ) # broadcast assert one_hots.shape == (data.size, bucket_count), (one_hots.shape, (data.size, bucket_count)) bucket_counts = np.sum(one_hots, axis=0) edges = np.linspace(min_, max_, bucket_count + 1) left_edges = edges[:-1] right_edges = edges[1:] buckets = np.array([left_edges, right_edges, bucket_counts]).transpose() tensor = tensor_util.make_tensor_proto(buckets, dtype=tf.float64) summary_metadata = metadata.create_summary_metadata( display_name=None, description=description) summary = summary_pb2.Summary() summary.value.add(tag=tag, metadata=summary_metadata, tensor=tensor) return summary
def test_unknown_summary_passes_through(self): old_event = event_pb2.Event() value = old_event.summary.value.add() value.metadata.plugin_data.plugin_name = "magic" value.metadata.plugin_data.content = b"123" value.tensor.CopyFrom(tensor_util.make_tensor_proto([1, 2])) new_events = self._migrate_event(old_event) self.assertLen(new_events, 1) self.assertIs(new_events[0], old_event)
def histogram_pb(tag, data, buckets=None, description=None): """Create a histogram summary protobuf. Arguments: tag: String tag for the summary. data: A `np.array` or array-like form of any shape. Must have type castable to `float`. buckets: Optional positive `int`. The output shape will always be [buckets, 3]. If there is no data, then an all-zero array of shape [buckets, 3] will be returned. If there is data but all points have the same value, then all buckets' left and right endpoints are the same and only the last bucket has nonzero count. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Returns: A `summary_pb2.Summary` protobuf object. """ bucket_count = DEFAULT_BUCKET_COUNT if buckets is None else buckets data = np.array(data).flatten().astype(float) if bucket_count == 0 or data.size == 0: histogram_buckets = np.zeros((bucket_count, 3)) else: min_ = np.min(data) max_ = np.max(data) range_ = max_ - min_ if range_ == 0: left_edges = right_edges = np.array([min_] * bucket_count) bucket_counts = np.array([0] * (bucket_count - 1) + [data.size]) histogram_buckets = np.array( [left_edges, right_edges, bucket_counts]).transpose() else: bucket_width = range_ / bucket_count offsets = data - min_ bucket_indices = np.floor(offsets / bucket_width).astype(int) clamped_indices = np.minimum(bucket_indices, bucket_count - 1) one_hots = np.array([clamped_indices]).transpose() == np.arange( 0, bucket_count) # broadcast assert one_hots.shape == (data.size, bucket_count), ( one_hots.shape, (data.size, bucket_count), ) bucket_counts = np.sum(one_hots, axis=0) edges = np.linspace(min_, max_, bucket_count + 1) left_edges = edges[:-1] right_edges = edges[1:] histogram_buckets = np.array( [left_edges, right_edges, bucket_counts]).transpose() tensor = tensor_util.make_tensor_proto(histogram_buckets, dtype=np.float64) summary_metadata = metadata.create_summary_metadata( display_name=None, description=description) summary = summary_pb2.Summary() summary.value.add(tag=tag, metadata=summary_metadata, tensor=tensor) return summary
def _filter_graph_defs(event): for v in event.summary.value: if v.metadata.plugin_data.plugin_name != graphs_metadata.PLUGIN_NAME: continue if v.tag == graphs_metadata.RUN_GRAPH_NAME: data = list(v.tensor.string_val) filtered_data = [_filtered_graph_bytes(x) for x in data] filtered_data = [x for x in filtered_data if x is not None] if filtered_data != data: new_tensor = tensor_util.make_tensor_proto( filtered_data, dtype=types_pb2.DT_STRING) v.tensor.CopyFrom(new_tensor)
def _migrate_tagged_run_metadata_event(old_event): result = event_pb2.Event() result.wall_time = old_event.wall_time result.step = old_event.step trm = old_event.tagged_run_metadata value = result.summary.value.add(tag=trm.tag) value.tensor.CopyFrom(tensor_util.make_tensor_proto([trm.run_metadata])) value.metadata.plugin_data.plugin_name = ( graphs_metadata.PLUGIN_NAME_TAGGED_RUN_METADATA) # `value.metadata.plugin_data.content` left empty value.metadata.data_class = summary_pb2.DATA_CLASS_BLOB_SEQUENCE return (result, )
def raw_data_pb(name, true_positive_counts, false_positive_counts, true_negative_counts, false_negative_counts, precision, recall, num_thresholds=None, display_name=None, description=None): """Create a PR curves summary protobuf from raw data values. Args: name: A tag attached to the summary. Used by TensorBoard for organization. true_positive_counts: A rank-1 numpy array of true positive counts. Must contain `num_thresholds` elements and be castable to float32. false_positive_counts: A rank-1 numpy array of false positive counts. Must contain `num_thresholds` elements and be castable to float32. true_negative_counts: A rank-1 numpy array of true negative counts. Must contain `num_thresholds` elements and be castable to float32. false_negative_counts: A rank-1 numpy array of false negative counts. Must contain `num_thresholds` elements and be castable to float32. precision: A rank-1 numpy array of precision values. Must contain `num_thresholds` elements and be castable to float32. recall: A rank-1 numpy array of recall values. Must contain `num_thresholds` elements and be castable to float32. num_thresholds: Number of thresholds, evenly distributed in `[0, 1]`, to compute PR metrics for. Should be an int `>= 2`. display_name: Optional name for this summary in TensorBoard, as a `str`. Defaults to `name`. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Returns: A summary operation for use in a TensorFlow graph. See docs for the `op` method for details on the float32 tensor produced by this summary. """ if display_name is None: display_name = name summary_metadata = metadata.create_summary_metadata( display_name=display_name if display_name is not None else name, description=description or '', num_thresholds=num_thresholds) summary = tf.Summary() data = np.stack( (true_positive_counts, false_positive_counts, true_negative_counts, false_negative_counts, precision, recall)) tensor = tensor_util.make_tensor_proto(np.float32(data), dtype=tf.float32) summary.value.add(tag='%s/pr_curves' % name, metadata=summary_metadata, tensor=tensor) return summary
def _migrate_graph_event(old_event): result = event_pb2.Event() result.wall_time = old_event.wall_time result.step = old_event.step value = result.summary.value.add(tag=graphs_metadata.RUN_GRAPH_NAME) graph_bytes = old_event.graph_def value.tensor.CopyFrom(tensor_util.make_tensor_proto([graph_bytes])) value.metadata.plugin_data.plugin_name = graphs_metadata.PLUGIN_NAME # `value.metadata.plugin_data.content` left as the empty proto value.metadata.data_class = summary_pb2.DATA_CLASS_BLOB_SEQUENCE # In the short term, keep both the old event and the new event to # maintain compatibility. return (old_event, result)
def AddScalarTensor(self, tag, wall_time=0, step=0, value=0): """Add a rank-0 tensor event. Note: This is not related to the scalar plugin; it's just a convenience function to add an event whose contents aren't important. """ tensor = tensor_util.make_tensor_proto(float(value)) event = tf.Event(wall_time=wall_time, step=step, summary=tf.Summary( value=[tf.Summary.Value(tag=tag, tensor=tensor)])) self.AddEvent(event)
def normalize_summary_pb(self, pb): """Pass `pb`'s `TensorProto` through a marshalling roundtrip. `TensorProto`s can be equal in value even if they are not identical in representation, because data can be stored in either the `tensor_content` field or the `${dtype}_value` field. This normalization ensures a canonical form, and should be used before comparing two `Summary`s for equality. """ result = tf.Summary() result.MergeFrom(pb) for value in result.value: if value.HasField('tensor'): new_tensor = tensor_util.make_tensor_proto( tensor_util.make_ndarray(value.tensor)) value.ClearField('tensor') value.tensor.MergeFrom(new_tensor) return result
def pb(name, images, max_outputs=3, display_name=None, description=None): """Create an image summary protobuf. This behaves as if you were to create an `op` with the same arguments (wrapped with constant tensors where appropriate) and then execute that summary op in a TensorFlow session. Arguments: name: A unique name for the generated summary, including any desired name scopes. images: An `np.array` representing pixel data with shape `[k, h, w, c]`, where `k` is the number of images, `w` and `h` are the width and height of the images, and `c` is the number of channels, which should be 1, 3, or 4. max_outputs: Optional `int`. At most this many images will be emitted. If more than this many images are provided, the first `max_outputs` many images will be used and the rest silently discarded. display_name: Optional name for this summary in TensorBoard, as a `str`. Defaults to `name`. description: Optional long-form description for this summary, as a `str`. Markdown is supported. Defaults to empty. Returns: A `tf.Summary` protobuf object. """ images = np.array(images).astype(np.uint8) if images.ndim != 4: raise ValueError('Shape %r must have rank 4' % (images.shape, )) limited_images = images[:max_outputs] encoded_images = [encoder.encode_png(image) for image in limited_images] (width, height) = (images.shape[2], images.shape[1]) content = [str(width), str(height)] + encoded_images tensor = tensor_util.make_tensor_proto(content, dtype=tf.string) if display_name is None: display_name = name summary_metadata = metadata.create_summary_metadata( display_name=display_name, description=description) summary = tf.Summary() summary.value.add(tag='%s/image_summary' % name, metadata=summary_metadata, tensor=tensor) return summary
def call_servo(examples, serving_bundle): """Send an RPC request to the Servomatic prediction service. Args: examples: A list of examples that matches the model spec. serving_bundle: A `ServingBundle` object that contains the information to make the serving request. Returns: A ClassificationResponse or RegressionResponse proto. """ parsed_url = urlparse('http://' + serving_bundle.inference_address) channel = implementations.insecure_channel(parsed_url.hostname, parsed_url.port) stub = prediction_service_pb2.beta_create_PredictionService_stub(channel) if serving_bundle.use_predict: request = predict_pb2.PredictRequest() elif serving_bundle.model_type == 'classification': request = classification_pb2.ClassificationRequest() else: request = regression_pb2.RegressionRequest() request.model_spec.name = serving_bundle.model_name if serving_bundle.model_version is not None: request.model_spec.version.value = serving_bundle.model_version if serving_bundle.signature is not None: request.model_spec.signature_name = serving_bundle.signature if serving_bundle.use_predict: request.inputs[serving_bundle.predict_input_tensor].CopyFrom( tensor_util.make_tensor_proto( values=[ex.SerializeToString() for ex in examples], dtype=types_pb2.DT_STRING)) else: request.input.example_list.examples.extend(examples) if serving_bundle.use_predict: return common_utils.convert_predict_response( stub.Predict(request, 30.0), serving_bundle) # 30 secs timeout elif serving_bundle.model_type == 'classification': return stub.Classify(request, 30.0) # 30 secs timeout else: return stub.Regress(request, 30.0) # 30 secs timeout
def _writeMetadata(self, logdir, summary_metadata, nonce=''): """Write to disk a summary with the given metadata. Arguments: logdir: a string summary_metadata: a `SummaryMetadata` protobuf object nonce: optional; will be added to the end of the event file name to guarantee that multiple calls to this function do not stomp the same file """ summary = summary_pb2.Summary() summary.value.add( tensor=tensor_util.make_tensor_proto(['po', 'ta', 'to'], dtype=tf.string), tag='you_are_it', metadata=summary_metadata) writer = test_util.FileWriter(logdir, filename_suffix=nonce) writer.add_summary(summary.SerializeToString()) writer.close()
def pb(scalars_layout): """Creates a summary that contains a layout. When users navigate to the custom scalars dashboard, they will see a layout based on the proto provided to this function. Args: scalars_layout: The scalars_layout_pb2.Layout proto that specifies the layout. Returns: A summary proto containing the layout. """ assert isinstance(scalars_layout, layout_pb2.Layout) tensor = tensor_util.make_tensor_proto(scalars_layout.SerializeToString(), dtype=tf.string) summary = tf.Summary() summary.value.add(tag=metadata.CONFIG_SUMMARY_TAG, metadata=_create_summary_metadata(), tensor=tensor) return summary
def normalize_summary_pb(self, pb): """Pass `pb`'s `TensorProto` through a marshalling roundtrip. `TensorProto`s can be equal in value even if they are not identical in representation, because data can be stored in either the `tensor_content` field or the `${dtype}_value` field. This normalization ensures a canonical form, and should be used before comparing two `Summary`s for equality. """ result = summary_pb2.Summary() if not isinstance(pb, summary_pb2.Summary): # pb can come from `pb_via_op` which creates a TB Summary. pb = test_util.ensure_tb_summary_proto(pb) result.MergeFrom(pb) for value in result.value: if value.HasField("tensor"): new_tensor = tensor_util.make_tensor_proto( tensor_util.make_ndarray(value.tensor)) value.ClearField("tensor") value.tensor.MergeFrom(new_tensor) return result
def test_graph_sub_plugins(self): # Tests for `graph_run_metadata`, `graph_run_metadata_graph`, # and `graph_keras_model` plugins. We fabricate these since it's # not straightforward to get handles to them. for plugin_name in [ graphs_metadata.PLUGIN_NAME_RUN_METADATA, graphs_metadata.PLUGIN_NAME_RUN_METADATA_WITH_GRAPH, graphs_metadata.PLUGIN_NAME_KERAS_MODEL, ]: with self.subTest(plugin_name): old_event = event_pb2.Event() old_event.step = 123 old_event.wall_time = 456.75 old_value = old_event.summary.value.add() old_value.metadata.plugin_data.plugin_name = plugin_name old_value.metadata.plugin_data.content = b"1" old_tensor = tensor_util.make_tensor_proto(b"2+2=4") # input data are scalar tensors self.assertEqual(tensor_util.make_ndarray(old_tensor).shape, ()) old_value.tensor.CopyFrom(old_tensor) new_events = self._migrate_event(old_event) self.assertLen(new_events, 1) self.assertLen(new_events[0].summary.value, 1) new_value = new_events[0].summary.value[0] ndarray = tensor_util.make_ndarray(new_value.tensor) self.assertEqual(ndarray.shape, (1,)) self.assertEqual(ndarray.item(), b"2+2=4") self.assertEqual( new_value.metadata.data_class, summary_pb2.DATA_CLASS_BLOB_SEQUENCE, ) self.assertEqual( new_value.metadata.plugin_data.plugin_name, plugin_name ) self.assertEqual(new_value.metadata.plugin_data.content, b"1")
def add_event(path): with test_util.FileWriterCache.get(path) as writer: event = tf.Event() event.summary.value.add(tag='tag', tensor=tensor_util.make_tensor_proto(1)) writer.add_event(event)