tfplot/ops.py
def plot(plot_func, in_tensors, name='Plot',
         **kwargs):
    '''
    Create a TensorFlow op which draws plot in an image. The resulting
    image is in a 3-D uint8 tensor.

    Given a python function ``plot_func``, which takes numpy arrays as its
    inputs (the evaluations of ``in_tensors``) and returns a matplotlib
    `Figure` object as its outputs, wrap this function as a TensorFlow op.
    The returning figure will be rendered as a RGB-A image upon execution.

    Args:
      plot_func: a python function or callable
        The function which accepts numpy `ndarray` objects as an argument
        that match the corresponding `tf.Tensor` objects in ``in_tensors``.
        It should return a new instance of ``matplotlib.figure.Figure``,
        which contains the resulting plot image.
      in_tensors: A list of `tf.Tensor` objects.
      name: A name for the operation (optional).
      kwargs: Additional keyword arguments passed to ``plot_func`` (optional).

    Returns:
      A single `uint8` `Tensor` of shape ``(?, ?, 4)``, containing the plot
      image that ``plot_func`` computes.
    '''

    if not hasattr(plot_func, '__call__'):
        raise TypeError("plot_func should be callable")

    if not isinstance(name, six.string_types):
        raise TypeError("name should be str or unicode, " +
                        "given {}".format(type(name)))

    if not isinstance(in_tensors, (list, tuple)):
        if isinstance(in_tensors, (tf.Tensor, np.ndarray, np.number)):
            in_tensors = [in_tensors]
        else:
            raise TypeError("in_tensors should be a list of Tensors, " +
                            "given {}".format(type(in_tensors)))

    in_tensors = [tf.convert_to_tensor(t) for t in in_tensors]

    def _render_image(*args):
        # `args` is (a tuple of) python values
        args = tuple(
            arg.numpy() if tensor_util.is_tensor(arg) else arg \
            for arg in args
        )

        # for tf.string tensors, decode into unicode if necessary.
        args = tuple(
            (decode_bytes_if_necessary(arg) if t.dtype == tf.string else arg) \
            for (arg, t) in zip(args, in_tensors)
        )
        fig = plot_func(*args, **kwargs)

        if not isinstance(fig, Figure):
            raise TypeError("The returned value should be a " +
                            "matplotlib.figure.Figure object, " +
                            "but given {}".format(type(fig)))

        # render fig into numpy array.
        try:
            image = figure.to_array(fig)
        except KeyError as e:
            # matplotlib is not thread-safe (see issue #17)
            logger.warn("KeyError during to_array(): {}".format(str(e)))

            # To workaround this, simply fallback to return a black screen
            w, h = image = fig.canvas.get_width_height()
            image = np.zeros([h, w, 4], dtype=np.uint8)
            return image

        return image

    im = py_func(_render_image, in_tensors, Tout=tf.uint8,
                 name=name)
    im.set_shape([None, None, 4])
    return im

    Create a TensorFlow op which draws plot in an image. The resulting
    image is in a 3-D uint8 tensor.

    Given a python function ``plot_func``, which takes numpy arrays as its
    inputs (the evaluations of ``in_tensors``) and returns a matplotlib
    `Figure` object as its outputs, wrap this function as a TensorFlow op.
    The returning figure will be rendered as a RGB-A image upon execution.

    Args:
      plot_func: a python function or callable
        The function which accepts numpy `ndarray` objects as an argument
        that match the corresponding `tf.Tensor` objects in ``in_tensors``.
        It should return a new instance of ``matplotlib.figure.Figure``,
        which contains the resulting plot image.
      in_tensors: A list of `tf.Tensor` objects.
      name: A name for the operation (optional).
      kwargs: Additional keyword arguments passed to ``plot_func`` (optional).

    Returns:
      A single `uint8` `Tensor` of shape ``(?, ?, 4)``, containing the plot
      image that ``plot_func`` computes.
    
Create a TensorFlow op which draws plot in an image. The resulting
image is in a 3-D uint8 tensor.


Create a TensorFlow op which draws plot in an image . The resulting image is in a 3 - D uint8 tensor .
def plot(plot_func, in_tensors, name='Plot',
         **kwargs):
    if not hasattr(plot_func, '__call__'):
        raise TypeError("plot_func should be callable")
    if not isinstance(name, six.string_types):
        raise TypeError("name should be str or unicode, " +
                        "given {}".format(type(name)))
    if not isinstance(in_tensors, (list, tuple)):
        if isinstance(in_tensors, (tf.Tensor, np.ndarray, np.number)):
            in_tensors = [in_tensors]
        else:
            raise TypeError("in_tensors should be a list of Tensors, " +
                            "given {}".format(type(in_tensors)))
    in_tensors = [tf.convert_to_tensor(t) for t in in_tensors]
    def _render_image(*args):
        args = tuple(
            arg.numpy() if tensor_util.is_tensor(arg) else arg \
            for arg in args
        )
        args = tuple(
            (decode_bytes_if_necessary(arg) if t.dtype == tf.string else arg) \
            for (arg, t) in zip(args, in_tensors)
        )
        fig = plot_func(*args, **kwargs)
        if not isinstance(fig, Figure):
            raise TypeError("The returned value should be a " +
                            "matplotlib.figure.Figure object, " +
                            "but given {}".format(type(fig)))
        try:
            image = figure.to_array(fig)
        except KeyError as e:
            logger.warn("KeyError during to_array(): {}".format(str(e)))
            w, h = image = fig.canvas.get_width_height()
            image = np.zeros([h, w, 4], dtype=np.uint8)
            return image
        return image
    im = py_func(_render_image, in_tensors, Tout=tf.uint8,
                 name=name)
    im.set_shape([None, None, 4])
    return im
def plot ( plot_func , in_tensors , name = 'Plot' , ** kwargs ) : if not hasattr ( plot_func , '__call__' ) : raise TypeError ( "plot_func should be callable" ) if not isinstance ( name , six . string_types ) : raise TypeError ( "name should be str or unicode, " + "given {}" . format ( type ( name ) ) ) if not isinstance ( in_tensors , ( list , tuple ) ) : if isinstance ( in_tensors , ( tf . Tensor , np . ndarray , np . number ) ) : in_tensors = [ in_tensors ] else : raise TypeError ( "in_tensors should be a list of Tensors, " + "given {}" . format ( type ( in_tensors ) ) ) in_tensors = [ tf . convert_to_tensor ( t ) for t in in_tensors ] def _render_image ( * args ) : args = tuple ( arg . numpy ( ) if tensor_util . is_tensor ( arg ) else arg for arg in args ) args = tuple ( ( decode_bytes_if_necessary ( arg ) if t . dtype == tf . string else arg ) for ( arg , t ) in zip ( args , in_tensors ) ) fig = plot_func ( * args , ** kwargs ) if not isinstance ( fig , Figure ) : raise TypeError ( "The returned value should be a " + "matplotlib.figure.Figure object, " + "but given {}" . format ( type ( fig ) ) ) try : image = figure . to_array ( fig ) except KeyError as e : logger . warn ( "KeyError during to_array(): {}" . format ( str ( e ) ) ) w , h = image = fig . canvas . get_width_height ( ) image = np . zeros ( [ h , w , 4 ] , dtype = np . uint8 ) return image return image im = py_func ( _render_image , in_tensors , Tout = tf . uint8 , name = name ) im . set_shape ( [ None , None , 4 ] ) return im
Create a TensorFlow op which draws plot in an image.
Create a TensorFlow op which draws plot in an image .
'''
    Create a TensorFlow op which draws plot in an image. The resulting
    image is in a 3-D uint8 tensor.

    Given a python function ``plot_func``, which takes numpy arrays as its
    inputs (the evaluations of ``in_tensors``) and returns a matplotlib
    `Figure` object as its outputs, wrap this function as a TensorFlow op.
    The returning figure will be rendered as a RGB-A image upon execution.

    Args:
      plot_func: a python function or callable
        The function which accepts numpy `ndarray` objects as an argument
        that match the corresponding `tf.Tensor` objects in ``in_tensors``.
        It should return a new instance of ``matplotlib.figure.Figure``,
        which contains the resulting plot image.
      in_tensors: A list of `tf.Tensor` objects.
      name: A name for the operation (optional).
      kwargs: Additional keyword arguments passed to ``plot_func`` (optional).

    Returns:
      A single `uint8` `Tensor` of shape ``(?, ?, 4)``, containing the plot
      image that ``plot_func`` computes.
    ''' # `args` is (a tuple of) python values # for tf.string tensors, decode into unicode if necessary. # render fig into numpy array. # matplotlib is not thread-safe (see issue #17) # To workaround this, simply fallback to return a black screen
A single `uint8` `Tensor` of shape ``(?, ?, 4)``, containing the plot
image that ``plot_func`` computes.
A single ` uint8 ` ` Tensor ` of shape ` ` ( ? ? 4 ) ` ` containing the plot image that ` ` plot_func ` ` computes .
plot_func
a python function or callable
The function which accepts numpy `ndarray` objects as an argument
that match the corresponding `tf.Tensor` objects in ``in_tensors``.
It should return a new instance of ``matplotlib.figure.Figure``,
which contains the resulting plot image.
a python function or callable The function which accepts numpy ` ndarray ` objects as an argument that match the corresponding ` tf . Tensor ` objects in ` ` in_tensors ` ` . It should return a new instance of ` ` matplotlib . figure . Figure ` ` which contains the resulting plot image .
in_tensors
A list of `tf.Tensor` objects.
A list of ` tf . Tensor ` objects .
name
A name for the operation (optional).
A name for the operation ( optional ) .
kwargs
Additional keyword arguments passed to ``plot_func`` (optional).
Additional keyword arguments passed to ` ` plot_func ` ` ( optional ) .