Beispiel #1
0
    def log_image(self, step, tag, val):
        '''
        Write an image event.

        :param int step: Time step (x-axis in TensorBoard graphs)
        :param str tag: Label for this value
        :param numpy.ndarray val: Image in RGB format with values from
            0 to 255; a 3-D array with index order (row, column, channel).
            `val.shape[-1] == 3`
        '''
        # TODO: support floating-point tensors, 4-D tensors, grayscale
        if len(val.shape) != 3:
            raise ValueError(
                '`log_image` value should be a 3-D tensor, instead got shape %s'
                % (val.shape, ))
        if val.shape[2] != 3:
            raise ValueError(
                'Last dimension of `log_image` value should be 3 (RGB), '
                'instead got shape %s' % (val.shape, ))
        fakefile = StringIO()
        png.Writer(size=(val.shape[1], val.shape[0])).write(
            fakefile, val.reshape(val.shape[0], val.shape[1] * val.shape[2]))
        encoded = fakefile.getvalue()
        # https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/core/framework/summary.proto
        RGB = 3
        image = Summary.Image(height=val.shape[0],
                              width=val.shape[1],
                              colorspace=RGB,
                              encoded_image_string=encoded)
        summary = Summary(value=[Summary.Value(tag=tag, image=image)])
        self._add_event(step, summary)
Beispiel #2
0
def make_image(tensor, height, width, channel):
    """Convert an numpy representation image to Image protobuf"""
    image = Image.fromarray(tensor)
    output = StringIO()
    image.save(output, format='PNG')
    image_string = output.getvalue()
    output.close()
    return Summary.Image(height=height,
                         width=width,
                         colorspace=channel,
                         encoded_image_string=image_string)
Beispiel #3
0
def make_image(tensor):
    """Convert an numpy representation image to Image protobuf"""
    from PIL import Image
    height, width, channel = tensor.shape
    image = Image.fromarray(tensor)
    import io
    output = io.BytesIO()
    image.save(output, format='PNG')
    image_string = output.getvalue()
    output.close()
    return Summary.Image(height=height,
                         width=width,
                         colorspace=channel,
                         encoded_image_string=image_string)
Beispiel #4
0
    def image(self, tag, image, step):
        image = np.asarray(image)
        if image.ndim == 2:
            image = image[:, :, None]
        if image.shape[-1] == 1:
            image = np.repeat(image, 3, axis=-1)

        bytesio = io.BytesIO()
        PIL.Image.fromarray(image).save(bytesio, 'PNG')
        image_summary = Summary.Image(encoded_image_string=bytesio.getvalue(),
                                      colorspace=3,
                                      height=image.shape[0],
                                      width=image.shape[1])
        self._write_event(Summary.Value(tag=tag, image=image_summary), step)
    def add_entry(self, index, tag, value, **kwargs):
        if "image" in kwargs and value is not None:
            image_string = tf.image.encode_jpeg(value,
                                                optimize_size=True,
                                                quality=80)
            summary_value = Summary.Image(width=value.shape[1],
                                          height=value.shape[0],
                                          colorspace=value.shape[2],
                                          encoded_image_string=image_string)
        else:
            summary_value = Summary.Value(tag=tag, simple_value=value)

        if summary_value is not None:
            entry = Summary(value=[summary_value])
            self._train_writer.add_summary(entry, index)