Commit e6cf9885 authored by Yuxin Wu's avatar Yuxin Wu

standardize the range in put_image

parent 3c52b7c1
......@@ -36,6 +36,10 @@ class HistoryBuffer(object):
def __len__(self):
return len(self.buf)
@property
def maxlen(self):
return self.buf.maxlen
class HistoryFramePlayer(ProxyPlayer):
""" Include history frames in state, or use black images.
......
......@@ -70,7 +70,7 @@ class TrainingMonitor(Callback):
def put_image(self, name, val):
"""
Args:
val (np.ndarray): 4D (NHWC) numpy array of images.
val (np.ndarray): 4D (NHWC) numpy array of images in range [0,255].
If channel is 3, assumed to be RGB.
"""
pass
......@@ -135,8 +135,8 @@ class Monitors(TrainingMonitor):
"""
Args:
name (str):
val (np.ndarray): 2D, 3D (HWC) or 4D (NHWC) numpy array of images.
If channel is 3, assumed to be RGB.
val (np.ndarray): 2D, 3D (HWC) or 4D (NHWC) numpy array of images
in range [0,255]. If channel is 3, assumed to be RGB.
"""
assert isinstance(val, np.ndarray)
arr = image_to_nhwc(val)
......
......@@ -5,6 +5,7 @@
import six
import tensorflow as tf
import re
import io
from six.moves import range
from ..utils import logger
......@@ -20,6 +21,9 @@ __all__ = ['create_scalar_summary', 'add_param_summary', 'add_activation_summary
def create_scalar_summary(name, v):
"""
Args:
name (str):
v (float): scalar value
Returns:
tf.Summary: a tf.Summary object with name and simple scalar value v.
"""
......@@ -35,25 +39,28 @@ def create_image_summary(name, val):
Args:
name(str):
val(np.ndarray): 4D tensor of NHWC. assume RGB if C==3.
Can be either float or uint8. Range has to be [0,255].
Returns:
tf.Summary:
"""
assert isinstance(name, six.string_types), type(name)
n, h, w, c = val.shape
val = val.astype('uint8')
s = tf.Summary()
for k in range(n):
tag = name if n == 1 else '{}/{}'.format(name, k)
# imencode assumes BGR
ret, buf = cv2.imencode('.png', val[k, :, :, ::-1])
assert ret, "imencode failed!"
buf = io.BytesIO()
# scipy assumes RGB
scipy.misc.toimage(val[k]).save(buf, format='png')
img = tf.Summary.Image()
img.height = h
img.width = w
# 1 - grayscale 3 - RGB 4 - RGBA
img.colorspace = c
img.encoded_image_string = buf.tostring()
img.encoded_image_string = buf.getvalue()
s.value.add(tag=tag, image=img)
return s
......@@ -171,7 +178,7 @@ def add_moving_summary(v, *args, **kwargs):
try:
import cv2
import scipy.misc
except ImportError:
from ..utils.develop import create_dummy_func
create_image_summary = create_dummy_func('create_image_summary', 'cv2') # noqa
create_image_summary = create_dummy_func('create_image_summary', 'scipy.misc') # noqa
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment