Ejemplo n.º 1
0
  def __init__(self, path):
    """Opens file `path` and creates a `TFRecordWriter` writing to it.

    Args:
      path: The path to the TFRecords file.

    Raises:
      IOError: If `path` cannot be opened for writing.
    """
    self._writer = pywrap_tensorflow.PyRecordWriter_New(path)
    if self._writer is None:
      raise IOError("Could not write to %s." % path)
Ejemplo n.º 2
0
def ngraph_to_tensorboard(graph, start_tensorboard=False):
    """
    Given an ngraph `graph` in the form of an op or list of ops, translate this into a TensorFlow
    compatible format and launch Tensorboard to visualize it.
    Arguments:
        graph: Op or list of Ops - Graph to visualize.
        start_tensorboard: Bool - Whether to launch tensorboard with the right `--log_dir`
            argument.  If false, then the User is responsible for taking the printed out file
            path and using that as an argument to tensorboard.
    Returns:
        Path to generated TensorFlow `Record` format (with a length delimited and checksummed
            append type structure) where each entry is a binary encoded `Event` protobuf record.
    """
    if not TF_IMPORT_SUCCESS:
        raise ImportError("Tensorflow is not installed, yet it is required ",
                          "to export Nervana Graph IR to Tensorboard")
    dname = tempfile.mkdtemp()
    fname = tempfile.mktemp(dir=dname, prefix='events.out.tfevents.ngraph.')

    tf_graph_def = ngraph_to_tf_graph_def(graph)
    with errors.raise_exception_on_not_ok_status() as status:
        writer = pywrap_tensorflow.PyRecordWriter_New(compat.as_bytes(fname),
                                                      compat.as_bytes(''),
                                                      status)

    ev = event_pb2.Event()
    ev.graph_def = tf_graph_def.SerializeToString()
    writer.WriteRecord(ev.SerializeToString())

    # Upstream API change to writer.Close coming in TF 1.3
    # https://github.com/tensorflow/tensorflow/commit/abae4305a6208bde6072d257a6ce734a8d369089#diff-39e499ff7be73304a41e23efd8432f40L45
    if tf.__version__ >= '1.3':
        with errors.raise_exception_on_not_ok_status() as status:
            writer.Close(status)
    else:
        writer.Close()

    logger.warning("Tensorboard output written to %s", fname)

    if start_tensorboard:
        logger.warning("Starting Tensorboard with `tensorboard --logdir %s`",
                       os.path.dirname(fname))
        subprocess.check_call(
            ['tensorboard', '--logdir',
             os.path.dirname(fname)])
    else:
        logger.warning(
            "You'll need to manually start Tensorboard with `tensorboard --logdir %s`",
            os.path.dirname(fname))
    return fname
Ejemplo n.º 3
0
  def __init__(self, path, options=None):
    """Opens file `path` and creates a `TFRecordWriter` writing to it.

    Args:
      path: The path to the TFRecords file.
      options: (optional) A TFRecordOptions object.

    Raises:
      IOError: If `path` cannot be opened for writing.
    """
    compression_type = TFRecordOptions.get_compression_type_string(options)

    with errors.raise_exception_on_not_ok_status() as status:
      self._writer = pywrap_tensorflow.PyRecordWriter_New(
          compat.as_bytes(path), compat.as_bytes(compression_type), status)
Ejemplo n.º 4
0
    def __init__(self, path, options=None):
        """Opens file `path` and creates a `TFRecordWriter` writing to it.

    Args:
      path: The path to the TFRecords file.
      options: (optional) A TFRecordOptions object.

    Raises:
      IOError: If `path` cannot be opened for writing.
    """
        compression_type_string = options.get_type_as_string(
        ) if options else ""

        self._writer = pywrap_tensorflow.PyRecordWriter_New(
            compat.as_bytes(path), compat.as_bytes(compression_type_string))
        if self._writer is None:
            raise IOError("Could not write to %s." % path)
Ejemplo n.º 5
0
  def __init__(self, path, options=None):
    """Opens file `path` and creates a `TFRecordWriter` writing to it.

    Args:
      path: The path to the TFRecords file.
      options: (optional) String specifying compression type,
          `TFRecordCompressionType`, or `TFRecordOptions` object.

    Raises:
      IOError: If `path` cannot be opened for writing.
      ValueError: If valid compression_type can't be determined from `options`.
    """
    if not isinstance(options, TFRecordOptions):
      options = TFRecordOptions(compression_type=options)

    with errors.raise_exception_on_not_ok_status() as status:
      # pylint: disable=protected-access
      self._writer = pywrap_tensorflow.PyRecordWriter_New(
          compat.as_bytes(path), options._as_record_writer_options(), status)
Ejemplo n.º 6
0
 def _make_writer(self):
     with errors.raise_exception_on_not_ok_status() as status:
         return pywrap_tensorflow.PyRecordWriter_New(
             self.path, compat.as_bytes(''), status)