def image_boxes(tag, tensor_image, tensor_boxes, rescale=0.8, mean_pixs=(122.7717, 115.9465, 102.9801)): '''Outputs a `Summary` protocol buffer with images.''' tensor_image = make_np(tensor_image, 'IMG') tensor_boxes = make_np(tensor_boxes) if isinstance(tensor_image, list): res = [] for i, t in enumerate(tensor_image): t = t + mean_pixs rois = tensor_boxes[tensor_boxes[:, 0] == i, 1:5] image = make_image(t.astype(np.uint8, copy=False), rescale=rescale, rois=rois) res.append( Summary( value=[Summary.Value(tag=tag + ('_%d' % i), image=image)])) return res else: tensor_image = tensor_image + mean_pixs # Assuming the values are within [0,255] now rois = tensor_boxes[:, 1:5] image = make_image(tensor_image.astype(np.uint8, copy=False), rescale=rescale, rois=rois) return Summary(value=[Summary.Value(tag=tag, image=image)])
def text(tag, text): '''Outputs a `Summary` protocol buffer with text.''' PluginData = [SummaryMetadata.PluginData(plugin_name='text')] smd = SummaryMetadata(plugin_data=PluginData) tensor = TensorProto(dtype='DT_STRING', string_val=[text.encode(encoding='utf_8')], tensor_shape=TensorShapeProto(dim=[TensorShapeProto.Dim(size=1)])) return Summary(value=[Summary.Value(node_name=tag, metadata=smd, tensor=tensor)])
def image(tag, tensor, rescale=0.6, mean_pixs=(122.7717, 115.9465, 102.9801)): '''Outputs a `Summary` protocol buffer with images.''' tensor = make_np(tensor, 'IMG') if isinstance(tensor, list): res = [] for i, t in enumerate(tensor): t = t + mean_pixs image = make_image(t.astype(np.uint8, copy=False), rescale=rescale) res.append(Summary(value=[Summary.Value(tag=tag + ('_%d' % i), image=image)])) else: tensor = tensor + mean_pixs # Assuming the values are within [0,255] now image = make_image(tensor.astype(np.uint8, copy=False), rescale=rescale) return Summary(value=[Summary.Value(tag=tag, image=image)])
def memory(tag, tensor, rescale=4.0, value_scale=1.0): tensor = make_np(tensor, 'MEM') if isinstance(tensor, list): res = [] for i, t in enumerate(tensor): t = np.minimum(t * (255. / value_scale), 255.) t = np.concatenate((t, t, t), axis=2) image = make_image(t.astype(np.uint8, copy=False), rescale=rescale) res.append( Summary( value=[Summary.Value(tag=tag + ('_%d' % i), image=image)])) return res else: t = np.minimum(tensor * (255. / value_scale), 255.) tensor = np.concatenate((t, t, t), axis=2) image = make_image(tensor.astype(np.uint8, copy=False), rescale=rescale) return Summary(value=[Summary.Value(tag=tag, image=image)])
def make_image(tensor, rescale): '''Convert an numpy representation image to Image protobuf.''' height, width, channel = tensor.shape theight = int(height * rescale) twidth = int(width * rescale) image = Image.fromarray(tensor) image = image.resize((twidth, theight), Image.ANTIALIAS) output = io.BytesIO() image.save(output, format='PNG') image_string = output.getvalue() output.close() return Summary.Image(height=theight, width=twidth, colorspace=channel, encoded_image_string=image_string)
def scalar(name, scalar): '''Assume for the scalar values are NOT needed to fetch.''' assert np.isscalar(scalar), 'ERROR: scalar values are not to be fetched' scalar = np.float32(scalar) return Summary(value=[Summary.Value(tag=name, simple_value=scalar)])
def histogram_with_values(name, values, bins): hist = make_histogram(values.astype(np.float32, copy=False), bins) return Summary(value=[Summary.Value(tag=name, histo=hist)])
def image(tag, tensor, rescale=0.6, mean_pixs=(122.7717, 115.9465, 102.9801)): '''Outputs a `Summary` protocol buffer with images.''' tensor = make_np(tensor, 'IMG') + mean_pixs # Assuming the values are within [0,255] now image = make_image(tensor.astype(np.uint8, copy=False), rescale=rescale) return Summary(value=[Summary.Value(tag=tag, image=image)])
def histogram(name, values, bins): '''Outputs a `Summary` protocol buffer with a histogram.''' values = make_np(values) hist = make_histogram(values.astype(np.float32, copy=False), bins) return Summary(value=[Summary.Value(tag=name, histo=hist)])