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