コード例 #1
0
  def testInterleave(self):
    test_cases = [{
        'tensor': 0,
        'shape': tensor_shape.TensorShape([])
    }, {
        'tensor': np.array([1, 2, 3]),
        'shape': tensor_shape.TensorShape([3])
    }, {
        'tensor': np.array([[1, 2, 3]]),
        'shape': tensor_shape.TensorShape([1, 3])
    }]

    for test_case in test_cases:
      with ops.Graph().as_default() as g:
        dataset = dataset_ops.Dataset.range(42)

        def make_dataset(tensor):

          def dataset_fn(n):
            return dataset_ops.Dataset.from_tensors(tensor).repeat(n)

          return dataset_fn

        dataset = dataset.interleave(
            make_dataset(test_case['tensor']), cycle_length=42)
        iterator = dataset.make_one_shot_iterator()
        get_next = iterator.get_next()
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        train_op.append(get_next)
        mg = meta_graph.create_meta_graph_def(graph=g)
        grappler_item = item.Item(mg)
        op_properties = grappler_item.GetOpProperties()
        self.assertEqual(test_case['shape'],
                         op_properties['IteratorGetNext'][0].shape)
コード例 #2
0
  def testFromStringHandle(self):
    test_cases = [{
        'shape': tensor_shape.TensorShape([])
    }, {
        'shape': tensor_shape.TensorShape([3])
    }, {
        'shape': tensor_shape.TensorShape([1, 2])
    }, {
        'shape': tensor_shape.TensorShape([1, 2, 3])
    }]

    for test_case in test_cases:
      with ops.Graph().as_default() as g:
        iterator = iterator_ops.Iterator.from_structure(dtypes.int64)
        handle = iterator.string_handle()
        iterator = iterator_ops.Iterator.from_string_handle(
            handle, dtypes.int64, output_shapes=test_case['shape'])
        get_next = iterator.get_next()
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        train_op.append(get_next)
        mg = meta_graph.create_meta_graph_def(graph=g)
        grappler_item = item.Item(mg)
        op_properties = grappler_item.GetOpProperties()
        self.assertEqual(test_case['shape'],
                         op_properties['IteratorGetNext'][0].shape)
コード例 #3
0
  def testVirtualCluster(self):
    with ops.Graph().as_default() as g:
      a = random_ops.random_uniform(shape=())
      b = random_ops.random_uniform(shape=())
      c = a + b
      train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
      train_op.append(c)
      mg = meta_graph.create_meta_graph_def(graph=g)
      grappler_item = item.Item(mg)
      device_properties = device_properties_pb2.DeviceProperties(
          type='GPU',
          frequency=1000,
          num_cores=60,
          environment={
              'architecture': '7'
          })
      named_device = device_properties_pb2.NamedDevice(
          properties=device_properties, name='/GPU:0')
      grappler_cluster = cluster.Cluster(devices=[named_device])
      op_perfs, run_time, _ = grappler_cluster.MeasureCosts(grappler_item)
      self.assertGreater(run_time, 0)
      self.assertEqual(len(op_perfs), 15)

      estimated_perf = grappler_cluster.EstimatePerformance(named_device)
      self.assertEqual(7680.0, estimated_perf)
コード例 #4
0
 def test_train_summaries(self):
   with ops.Graph().as_default() as g, self.test_session(g):
     with ops.control_dependencies(self._build_inference_graph()):
       train_op = state_ops.assign_add(variables_lib.get_global_step(), 1)
     loss_op = constant_op.constant(2.0)
     summary.scalar('loss', loss_op)
     writer = learn.graph_actions.get_summary_writer(self._output_dir)
     self._assert_summaries(self._output_dir, writer)
     self._assert_ckpt(self._output_dir, False)
     loss = learn.graph_actions._monitored_train(  # pylint: disable=protected-access
         g,
         output_dir=self._output_dir,
         train_op=train_op,
         loss_op=loss_op,
         steps=1)
     meta_graph_def = meta_graph.create_meta_graph_def(
         graph_def=g.as_graph_def(add_shapes=True),
         saver_def=monitored_session.Scaffold().finalize().saver.saver_def)
     self.assertEqual(2.0, loss)
     self._assert_summaries(
         self._output_dir,
         writer,
         expected_graphs=[g],
         expected_meta_graphs=[meta_graph_def],
         expected_summaries={1: {
             'loss': 2.0
         }})
     self._assert_ckpt(self._output_dir, True)
コード例 #5
0
ファイル: hooks.py プロジェクト: KIngpon/NJUNMT-tf
    def before_run(self, run_context):
        """ Dumps graphs and loads checkpoint if there exits.

        Called before each call to run().

        Args:
            run_context: A `SessionRunContext` object.

        Returns: A `SessionRunArgs` object containing global_step.
        """
        # We do write graph and saver_def at the first call of before_run.
        # We cannot do this in begin, since we let other hooks to change graph and
        # add variables in begin. Graph is finalized after all begin calls.
        if self._is_chief and self._first_call:
            training_util.write_graph(
                ops.get_default_graph().as_graph_def(add_shapes=True),
                self._checkpoint_dir,
                "graph.pbtxt")
            # dump model details "model_analysis.txt"
            dump_model_analysis(self._checkpoint_dir)  # dump model configs
            graph = ops.get_default_graph()
            meta_graph_def = meta_graph.create_meta_graph_def(
                graph_def=graph.as_graph_def(add_shapes=True),
                saver_def=self._saver.saver_def)
            if self._summary_writer is not None:
                self._summary_writer.add_graph(graph)
                self._summary_writer.add_meta_graph(meta_graph_def)
            tf.logging.info("CheckpointSaverHook (before_run): dump graph...")
        self._first_call = False
        return tf.train.SessionRunArgs(self._global_step)
コード例 #6
0
ファイル: supervisor_test.py プロジェクト: KalraA/tensorflow
 def testStandardServicesWithGlobalStep(self):
   logdir = _test_dir("standard_services_with_global_step")
   # Create a checkpoint.
   with tf.Graph().as_default():
     v = tf.Variable([123], name="global_step")
     sv = tf.train.Supervisor(logdir=logdir)
     meta_graph_def = meta_graph.create_meta_graph_def(
         saver_def=sv.saver.saver_def)
     sess = sv.prepare_or_wait_for_session("")
     # This is where the checkpoint will appear, with step number 123.
     save_path = "%s-123" % sv.save_path
     self._wait_for_glob(save_path, 3.0)
     self._wait_for_glob(os.path.join(logdir, "*events*"), 3.0)
     # Wait to make sure everything is written to file before stopping.
     time.sleep(1)
     sv.stop()
   # There should be an event file with a version number.
   rr = _summary_iterator(logdir)
   ev = next(rr)
   self.assertEquals("brain.Event:2", ev.file_version)
   ev = next(rr)
   ev_graph = tf.GraphDef()
   ev_graph.ParseFromString(ev.graph_def)
   self.assertProtoEquals(sess.graph.as_graph_def(add_shapes=True), ev_graph)
   ev = next(rr)
   ev_meta_graph = meta_graph_pb2.MetaGraphDef()
   ev_meta_graph.ParseFromString(ev.meta_graph_def)
   self.assertProtoEquals(meta_graph_def, ev_meta_graph)
   self.assertProtoEquals(
       sess.graph.as_graph_def(add_shapes=True), ev_meta_graph.graph_def)
   ev = next(rr)
   # It is actually undeterministic whether SessionLog.START gets written
   # before the summary or the checkpoint, but this works when run 10000 times.
   self.assertEquals(123, ev.step)
   self.assertEquals(tf.SessionLog.START, ev.session_log.status)
   first = next(rr)
   second = next(rr)
   # It is undeterministic whether the value gets written before the checkpoint
   # since they are on separate threads, so we check for both conditions.
   if first.HasField("summary"):
     self.assertProtoEquals("""value { tag: 'global_step/sec'
                                       simple_value: 0.0 }""",
                            first.summary)
     self.assertEquals(123, second.step)
     self.assertEquals(tf.SessionLog.CHECKPOINT, second.session_log.status)
   else:
     self.assertEquals(123, first.step)
     self.assertEquals(tf.SessionLog.CHECKPOINT, first.session_log.status)
     self.assertProtoEquals("""value { tag: 'global_step/sec'
                                       simple_value: 0.0 }""",
                            second.summary)
   ev = next(rr)
   self.assertEquals(tf.SessionLog.STOP, ev.session_log.status)
   self.assertRaises(StopIteration, lambda: next(rr))
   # There should be a checkpoint file with the variable "foo"
   with tf.Graph().as_default(), self.test_session() as sess:
     v = tf.Variable([-12], name="global_step")
     sav = tf.train.Saver([v])
     sav.restore(sess, save_path)
     self.assertEqual(123, v.eval()[0])
コード例 #7
0
  def testKeepNodes(self):
    g = ops.Graph()
    with g.as_default():
      a1 = variables.VariableV1(
          1.0)  # Must be preserved since it's in the collection 'variables'.
      a2 = constant_op.constant(0, shape=[50, 50], name='keep')
      ops.add_to_collection('a2', a2)  # Explicitly add to collection.
      with g._attr_scope(
          {'_grappler_do_not_remove': attr_value_pb2.AttrValue(b=True)}):
        a3 = constant_op.constant(0, name='keep2')
      b = constant_op.constant(1, shape=[100, 10])
      c = constant_op.constant(0, shape=[10, 30])
      d = math_ops.matmul(b, c)
      ops.add_to_collection('train_op', d)  # d is the fetch node.

    # Optimize the graph.
    mg = meta_graph.create_meta_graph_def(graph=g)
    config = config_pb2.ConfigProto()
    rewriter_config = config.graph_options.rewrite_options
    rewriter_config.min_graph_nodes = -1
    optimized_graph = tf_optimizer.OptimizeGraph(config, mg)

    # Check that the nodes referenced in various collections have been preserved
    optimized_graph_nodes = [node.name for node in optimized_graph.node]
    expected_nodes = [
        d.op.name, a1.op.name, a2.op.name, a3.op.name, 'Variable/initial_value',
        'Variable/Assign'
    ]
    self.assertEqual(len(optimized_graph_nodes), len(expected_nodes))
    self.assertAllInSet(optimized_graph_nodes, expected_nodes)
コード例 #8
0
  def testBasicMemory(self):
    """Make sure arguments can be passed correctly."""
    with test_util.device(use_gpu=False):
      a = constant_op.constant(10, name="a")
      b = constant_op.constant(20, name="b")
      c = math_ops.add_n([a, b], name="c")
      d = math_ops.add_n([b, c], name="d")
      train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
      train_op.append(d)
      mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())

    report = cost_analyzer.GenerateMemoryReport(mg)

    # Print the report to make it easier to debug
    print("{}".format(report))

    # Check the report
    self.assertTrue(
        "Peak usage for device /job:localhost/replica:0/task:0/device:CPU:0: "
        "16 bytes"
        in report)
    self.assertTrue("  a:0 uses 4 bytes" in report)
    self.assertTrue("  b:0 uses 4 bytes" in report)
    self.assertTrue("  c:0 uses 4 bytes" in report)
    self.assertTrue("  d:0 uses 4 bytes" in report)
コード例 #9
0
ファイル: item_test.py プロジェクト: AnddyWang/tensorflow
  def testUpdates(self):
    with ops.Graph().as_default() as g:
      a = constant_op.constant(10)
      b = constant_op.constant(20)
      c = a + b
      train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
      train_op.append(c)
      mg = meta_graph.create_meta_graph_def(graph=g)
      grappler_item = item.Item(mg)

    initial_tf_item = grappler_item.tf_item
    no_change_tf_item = grappler_item.tf_item
    self.assertEqual(initial_tf_item, no_change_tf_item)

    # Modify the placement.
    for node in grappler_item.metagraph.graph_def.node:
      node.device = '/cpu:0'
    new_tf_item = grappler_item.tf_item
    self.assertNotEqual(initial_tf_item, new_tf_item)

    # Assign the same placement.
    for node in grappler_item.metagraph.graph_def.node:
      node.device = '/cpu:0'
    newest_tf_item = grappler_item.tf_item
    self.assertEqual(new_tf_item, newest_tf_item)
コード例 #10
0
  def testPaddedBatch(self):
    test_cases = [{
        'tensor': 0,
        'shape': tensor_shape.TensorShape([None])
    }, {
        'tensor': np.array([1, 2, 3]),
        'shape': tensor_shape.TensorShape([None, 4])
    }, {
        'tensor': np.array([[1, 2, 3]]),
        'shape': tensor_shape.TensorShape([None, 2, 4])
    }]

    for test_case in test_cases:
      with ops.Graph().as_default() as g:
        dataset = dataset_ops.Dataset.from_tensors(test_case['tensor'])
        dataset = dataset.padded_batch(42, padded_shapes=test_case['shape'][1:])
        iterator = dataset.make_one_shot_iterator()
        get_next = iterator.get_next()
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        train_op.append(get_next)
        mg = meta_graph.create_meta_graph_def(graph=g)
        grappler_item = item.Item(mg)
        op_properties = grappler_item.GetOpProperties()
        inferred_shape = self.as_tensor_shape(
            op_properties['IteratorGetNext'][0].shape)
        self.assertTrue(test_case['shape'].dims[0].is_compatible_with(
            inferred_shape[0]))
        self.assertEqual(test_case['shape'][1:], inferred_shape[1:])
コード例 #11
0
  def testSmallNetwork(self):
    image = array_ops.placeholder(dtypes.float32, shape=[1, 28, 28, 1])
    label = array_ops.placeholder(dtypes.float32, shape=[1, 10])
    w = variables.Variable(
        random_ops.truncated_normal([5, 5, 1, 32], stddev=0.1))
    b = variables.Variable(random_ops.truncated_normal([32], stddev=0.1))
    conv = nn_ops.conv2d(image, w, strides=[1, 1, 1, 1], padding="SAME")
    h_conv = nn_ops.relu(conv + b)
    h_conv_flat = array_ops.reshape(h_conv, [1, -1])

    w_fc = variables.Variable(
        random_ops.truncated_normal([25088, 10], stddev=0.1))
    b_fc = variables.Variable(random_ops.truncated_normal([10], stddev=0.1))
    y_conv = nn_ops.softmax(math_ops.matmul(h_conv_flat, w_fc) + b_fc)

    cross_entropy = math_ops.reduce_mean(-math_ops.reduce_sum(
        label * math_ops.log(y_conv), reduction_indices=[1]))
    _ = adam.AdamOptimizer(1e-4).minimize(cross_entropy)

    mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())
    report = cost_analyzer.GenerateCostReport(mg)

    self.assertTrue(b"MatMul" in report)
    self.assertTrue(b"ApplyAdam" in report)
    self.assertTrue(b"Conv2D" in report)
    self.assertTrue(b"Conv2DBackpropInput" in report)
    self.assertTrue(b"Conv2DBackpropFilter" in report)
    self.assertTrue(b"Softmax" in report)

    # Also print the report to make it easier to debug
    print("{}".format(report))
コード例 #12
0
  def testSimpleSwap(self):
    """Check that the swap annotations are followed."""
    a = constant_op.constant(10, name='a')
    b = constant_op.constant(20, name='b')
    c = math_ops.add_n([a, b], name='c')
    d = math_ops.add_n([b, c], name='d')
    train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
    train_op.append(d)

    d.op.node_def.attr['_swap_to_host'].i = 0

    mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())

    rewriter_config = rewriter_config_pb2.RewriterConfig(
        memory_optimization=rewriter_config_pb2.RewriterConfig.MANUAL)
    graph = tf_optimizer.OptimizeGraph(rewriter_config, mg)

    self.assertEqual(len(graph.node), 6)
    self.assertItemsEqual([node.name for node in graph.node], [
        'a',
        'b',
        'c',
        'd',
        'swap_in_d_0',
        'swap_out_d_0',
    ])
    for node in graph.node:
      if node.name == 'swap_in_d_0':
        self.assertEqual('swap_out_d_0', node.input[0])
        self.assertEqual('^b', node.input[1])
      elif node.name == 'swap_out_d_0':
        self.assertEqual('b', node.input[0])
      elif node.name == 'd':
        self.assertEqual('swap_in_d_0', node.input[0])
        self.assertEqual('c', node.input[1])
コード例 #13
0
  def testSimpleSwap(self):
    """Check that the swap annotations are followed."""
    a = variables.Variable(10, name='a')
    b = variables.Variable(20, name='b')
    c = math_ops.add_n([a, b], name='c')
    d = math_ops.add_n([b, c], name='d')
    train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
    train_op.append(d)

    d.op.node_def.attr['_swap_to_host'].i = 0

    mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())
    graph_size = len(mg.graph_def.node)

    rewriter_config = rewriter_config_pb2.RewriterConfig(
        disable_model_pruning=True,
        memory_optimization=rewriter_config_pb2.RewriterConfig.MANUAL)
    graph = tf_optimizer.OptimizeGraph(rewriter_config, mg)

    self.assertEqual(len(graph.node), graph_size + 2)
    self.assertTrue(
        set([node.name for node in graph.node]) > set(
            ['a', 'b', 'c', 'd', 'swap_in_d_0', 'swap_out_d_0']))
    for node in graph.node:
      if node.name == 'swap_in_d_0':
        self.assertEqual('swap_out_d_0', node.input[0])
        self.assertEqual('^b/read', node.input[1])
      elif node.name == 'swap_out_d_0':
        self.assertEqual('b/read', node.input[0])
      elif node.name == 'd':
        self.assertEqual('swap_in_d_0', node.input[0])
        self.assertEqual('c', node.input[1])
コード例 #14
0
  def _assertEventsWithGraph(self, test_dir, g, has_shapes):
    meta_graph_def = meta_graph.create_meta_graph_def(
        graph_def=g.as_graph_def(add_shapes=has_shapes))

    rr = self._EventsReader(test_dir)

    # The first event should list the file_version.
    ev = next(rr)
    self._assertRecent(ev.wall_time)
    self.assertEquals("brain.Event:2", ev.file_version)

    # The next event should have the graph.
    ev = next(rr)
    self._assertRecent(ev.wall_time)
    self.assertEquals(0, ev.step)
    ev_graph = graph_pb2.GraphDef()
    ev_graph.ParseFromString(ev.graph_def)
    self.assertProtoEquals(g.as_graph_def(add_shapes=has_shapes), ev_graph)

    # The next event should have the metagraph.
    ev = next(rr)
    self._assertRecent(ev.wall_time)
    self.assertEquals(0, ev.step)
    ev_meta_graph = meta_graph_pb2.MetaGraphDef()
    ev_meta_graph.ParseFromString(ev.meta_graph_def)
    self.assertProtoEquals(meta_graph_def, ev_meta_graph)

    # We should be done.
    self.assertRaises(StopIteration, lambda: next(rr))
コード例 #15
0
  def testKeepNodes(self):
    g = ops.Graph()
    with g.as_default():
      a1 = variables.VariableV1(
          1.0)  # Must be preserved since it's in the collection 'variables'.
      a2 = constant_op.constant(0, shape=[50, 50], name='keep')
      ops.add_to_collection('a2', a2)  # Explicitly add to collection.
      b = constant_op.constant(1, shape=[100, 10])
      c = constant_op.constant(0, shape=[10, 30])
      d = math_ops.matmul(b, c)
      ops.add_to_collection('train_op', d)  # d is the fetch node.

    # Optimize the graph.
    mg = meta_graph.create_meta_graph_def(graph=g)
    rewriter_config = rewriter_config_pb2.RewriterConfig()
    rewriter_config.min_graph_nodes = -1
    optimized_graph = tf_optimizer.OptimizeGraph(rewriter_config, mg)

    # Check that the nodes referenced in various collections have been preserved
    self.assertEqual(len(optimized_graph.node), 5)
    self.assertEqual(d.op.name, optimized_graph.node[0].name)
    self.assertEqual(a1.op.name, optimized_graph.node[1].name)
    self.assertEqual('Variable/initial_value', optimized_graph.node[2].name)
    self.assertEqual(a2.op.name, optimized_graph.node[3].name)
    self.assertEqual('Variable/Assign', optimized_graph.node[4].name)
コード例 #16
0
  def testFromGenerator(self):
    test_cases = [{
        'tensor': 0,
        'shape': tensor_shape.TensorShape([])
    }, {
        'tensor': np.array([1, 2, 3]),
        'shape': tensor_shape.TensorShape([3])
    }, {
        'tensor': np.array([[1, 2, 3]]),
        'shape': tensor_shape.TensorShape([1, 3])
    }]

    for test_case in test_cases:

      def make_generator(tensor):

        def generator():
          yield tensor

        return generator

      with ops.Graph().as_default() as g:
        dataset = dataset_ops.Dataset.from_generator(
            make_generator(test_case['tensor']),
            dtypes.int64,
            output_shapes=test_case['shape'])
        iterator = dataset.make_one_shot_iterator()
        get_next = iterator.get_next()
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        train_op.append(get_next)
        mg = meta_graph.create_meta_graph_def(graph=g)
        grappler_item = item.Item(mg)
        op_properties = grappler_item.GetOpProperties()
        self.assertEqual(test_case['shape'],
                         op_properties['IteratorGetNext'][0].shape)
コード例 #17
0
  def testMap(self):
    test_cases = [{
        'tensor': 0,
        'shape': tensor_shape.TensorShape([])
    }, {
        'tensor': np.array([1, 2, 3]),
        'shape': tensor_shape.TensorShape([3])
    }, {
        'tensor': np.array([[1, 2, 3]]),
        'shape': tensor_shape.TensorShape([3, 1])
    }, {
        'tensor': np.array([[[1, 2, 3], [4, 5, 6]]]),
        'shape': tensor_shape.TensorShape([3, 2, 1])
    }]

    for test_case in test_cases:
      with ops.Graph().as_default() as g:
        dataset = dataset_ops.Dataset.from_tensors(test_case['tensor'])
        dataset = dataset.map(array_ops.transpose)
        iterator = dataset.make_one_shot_iterator()
        get_next = iterator.get_next()
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        train_op.append(get_next)
        mg = meta_graph.create_meta_graph_def(graph=g)
        grappler_item = item.Item(mg)
        op_properties = grappler_item.GetOpProperties()
        self.assertEqual(test_case['shape'],
                         op_properties['IteratorGetNext'][0].shape)
コード例 #18
0
 def GetOptimizedGraph():
   mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())
   config = config_pb2.ConfigProto()
   config.graph_options.rewrite_options.CopyFrom(
       rewriter_config_pb2.RewriterConfig(
           constant_folding=rewriter_config_pb2.RewriterConfig.OFF,
           memory_optimization=rewriter_config_pb2.RewriterConfig.MANUAL))
   return tf_optimizer.OptimizeGraph(config, mg)
コード例 #19
0
ファイル: item_test.py プロジェクト: AnddyWang/tensorflow
 def testImportantOps(self):
   with ops.Graph().as_default() as g:
     a = constant_op.constant(10)
     b = constant_op.constant(20)
     c = a + b
     train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
     train_op.append(c)
     mg = meta_graph.create_meta_graph_def(graph=g)
     grappler_item = item.Item(mg)
     op_list = grappler_item.IdentifyImportantOps()
     self.assertEqual([b'Const', b'Const_1', b'add'], op_list)
コード例 #20
0
ファイル: item_test.py プロジェクト: AnddyWang/tensorflow
  def testInvalidItem(self):
    with ops.Graph().as_default() as g:
      a = constant_op.constant(10)
      b = constant_op.constant(20)
      c = a + b  # pylint: disable=unused-variable
      mg = meta_graph.create_meta_graph_def(graph=g)

    # The train op isn't specified: this should raise an InvalidArgumentError
    # exception.
    with self.assertRaises(errors_impl.InvalidArgumentError):
      item.Item(mg)
コード例 #21
0
 def testRange(self):
   with ops.Graph().as_default() as g:
     dataset = dataset_ops.Dataset.range(42)
     iterator = dataset.make_one_shot_iterator()
     get_next = iterator.get_next()
     train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
     train_op.append(get_next)
     mg = meta_graph.create_meta_graph_def(graph=g)
     grappler_item = item.Item(mg)
     op_properties = grappler_item.GetOpProperties()
     self.assertEqual(tensor_shape.scalar(),
                      op_properties['IteratorGetNext'][0].shape)
コード例 #22
0
  def testNoLogdirButExplicitSummaryWriter(self):
    logdir = self._test_dir("explicit_summary_writer")
    with ops.Graph().as_default():
      summary.scalar("c1", constant_op.constant(1))
      summary.scalar("c2", constant_op.constant(2))
      summary.scalar("c3", constant_op.constant(3))
      summ = summary.merge_all()
      sw = writer.FileWriter(logdir)
      sv = supervisor.Supervisor(logdir="", summary_op=None, summary_writer=sw)
      meta_graph_def = meta_graph.create_meta_graph_def()
      sess = sv.prepare_or_wait_for_session("")
      sv.summary_computed(sess, sess.run(summ))
      sess.close()
      # Wait to make sure everything is written to file before stopping.
      time.sleep(1)
      sv.stop()

    # Check the summary was written to 'logdir'
    rr = _summary_iterator(logdir)

    # The first event should list the file_version.
    ev = next(rr)
    self.assertEquals("brain.Event:2", ev.file_version)

    # The next one has the graph.
    ev = next(rr)
    ev_graph = graph_pb2.GraphDef()
    ev_graph.ParseFromString(ev.graph_def)
    self.assertProtoEquals(sess.graph.as_graph_def(add_shapes=True), ev_graph)

    # Stored MetaGraphDef
    ev = next(rr)
    ev_meta_graph = meta_graph_pb2.MetaGraphDef()
    ev_meta_graph.ParseFromString(ev.meta_graph_def)
    self.assertProtoEquals(meta_graph_def, ev_meta_graph)
    self.assertProtoEquals(
        sess.graph.as_graph_def(add_shapes=True), ev_meta_graph.graph_def)

    # The next one should have the values from the summary.
    ev = next(rr)
    self.assertProtoEquals("""
      value { tag: 'c1' simple_value: 1.0 }
      value { tag: 'c2' simple_value: 2.0 }
      value { tag: 'c3' simple_value: 3.0 }
      """, ev.summary)

    # The next one should be a stop message if we closed cleanly.
    ev = next(rr)
    self.assertEquals(event_pb2.SessionLog.STOP, ev.session_log.status)

    # We should be done.
    self.assertRaises(StopIteration, lambda: next(rr))
コード例 #23
0
ファイル: writer.py プロジェクト: dan-lennox/tensorflow
  def __init__(self, event_writer, graph=None, graph_def=None):
    """Creates a `SummaryWriter` and an event file.

    On construction the summary writer creates a new event file in `logdir`.
    This event file will contain `Event` protocol buffers constructed when you
    call one of the following functions: `add_summary()`, `add_session_log()`,
    `add_event()`, or `add_graph()`.

    If you pass a `Graph` to the constructor it is added to
    the event file. (This is equivalent to calling `add_graph()` later).

    TensorBoard will pick the graph from the file and display it graphically so
    you can interactively explore the graph you built. You will usually pass
    the graph from the session in which you launched it:

    ```python
    ...create a graph...
    # Launch the graph in a session.
    sess = tf.Session()
    # Create a summary writer, add the 'graph' to the event file.
    writer = tf.summary.FileWriter(<some-directory>, sess.graph)
    ```


    Args:
      event_writer: An EventWriter. Implements add_event and get_logdir.
      graph: A `Graph` object, such as `sess.graph`.
      graph_def: DEPRECATED: Use the `graph` argument instead.
    """
    self.event_writer = event_writer
    # For storing used tags for session.run() outputs.
    self._session_run_tags = {}
    if graph is not None or graph_def is not None:
      # Calling it with both graph and graph_def for backward compatibility.
      self.add_graph(graph=graph, graph_def=graph_def)
      # Also export the meta_graph_def in this case.
      # graph may itself be a graph_def due to positional arguments
      maybe_graph_as_def = (graph.as_graph_def(add_shapes=True)
                            if isinstance(graph, ops.Graph) else graph)
      self.add_meta_graph(
          meta_graph.create_meta_graph_def(graph_def=graph_def or
                                           maybe_graph_as_def))

    # This set contains tags of Summary Values that have been encountered
    # already. The motivation here is that the SummaryWriter only keeps the
    # metadata property (which is a SummaryMetadata proto) of the first Summary
    # Value encountered for each tag. The SummaryWriter strips away the
    # SummaryMetadata for all subsequent Summary Values with tags seen
    # previously. This saves space.
    self._seen_summary_tags = set()
コード例 #24
0
ファイル: item_test.py プロジェクト: ChengYuXiang/tensorflow
 def testColocationContraints(self):
   with ops.Graph().as_default() as g:
     c = constant_op.constant([10])
     v = variables.Variable([3], dtype=dtypes.int32)
     i = gen_array_ops._ref_identity(v)
     a = state_ops.assign(i, c)
     train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
     train_op.append(a)
     mg = meta_graph.create_meta_graph_def(graph=g)
     grappler_item = item.Item(mg)
     groups = grappler_item.GetColocationGroups()
     self.assertEqual(len(groups), 1)
     self.assertItemsEqual(
         groups[0], ['Assign', 'RefIdentity', 'Variable', 'Variable/Assign'])
コード例 #25
0
  def testChiefCanWriteEvents(self):
    logdir = _test_dir("can_write")
    with tf.Graph().as_default():
      tf.summary.scalar("c1", tf.constant(1))
      tf.summary.scalar("c2", tf.constant(2))
      tf.summary.scalar("c3", tf.constant(3))
      summ = tf.summary.merge_all()
      sv = tf.train.Supervisor(is_chief=True, logdir=logdir, summary_op=None)
      meta_graph_def = meta_graph.create_meta_graph_def()
      sess = sv.prepare_or_wait_for_session("")
      sv.summary_computed(sess, sess.run(summ))
      sess.close()
      # Wait to make sure everything is written to file before stopping.
      time.sleep(1)
      sv.stop()

    rr = _summary_iterator(logdir)

    # The first event should list the file_version.
    ev = next(rr)
    self.assertEquals("brain.Event:2", ev.file_version)

    # The next one has the graph.
    ev = next(rr)
    ev_graph = tf.GraphDef()
    ev_graph.ParseFromString(ev.graph_def)
    self.assertProtoEquals(sess.graph.as_graph_def(add_shapes=True), ev_graph)

    # Stored MetaGraphDef
    ev = next(rr)
    ev_meta_graph = meta_graph_pb2.MetaGraphDef()
    ev_meta_graph.ParseFromString(ev.meta_graph_def)
    self.assertProtoEquals(meta_graph_def, ev_meta_graph)
    self.assertProtoEquals(
        sess.graph.as_graph_def(add_shapes=True), ev_meta_graph.graph_def)
    # The next one should have the values from the summary.
    ev = next(rr)
    self.assertProtoEquals("""
      value { tag: 'c1' simple_value: 1.0 }
      value { tag: 'c2' simple_value: 2.0 }
      value { tag: 'c3' simple_value: 3.0 }
      """, ev.summary)

    # The next one should be a stop message if we closed cleanly.
    ev = next(rr)
    self.assertEquals(tf.SessionLog.STOP, ev.session_log.status)

    # We should be done.
    self.assertRaises(StopIteration, lambda: next(rr))
コード例 #26
0
    def before_run(self, run_context):  # pylint: disable=unused-argument
        if self._timer.last_triggered_step() is None:
            # Write graph in the first call.
            training_util.write_graph(
                ops.get_default_graph().as_graph_def(add_shapes=True),
                self._checkpoint_dir, "graph.pbtxt")
            saver_def = self._saver.saver_def if self._saver else None
            graph = ops.get_default_graph()
            meta_graph_def = meta_graph.create_meta_graph_def(
                graph_def=graph.as_graph_def(add_shapes=True),
                saver_def=saver_def)
            self._summary_writer.add_graph(graph)
            self._summary_writer.add_meta_graph(meta_graph_def)

        return SessionRunArgs(self._global_step_tensor)
コード例 #27
0
 def testColocationConstraints(self):
     with ops.Graph().as_default() as g:
         c = constant_op.constant([10])
         v = variables.VariableV1([3], dtype=dtypes.int32)
         i = gen_array_ops.ref_identity(v)
         a = state_ops.assign(i, c)
         train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
         train_op.append(a)
         mg = meta_graph.create_meta_graph_def(graph=g)
         grappler_item = item.Item(mg)
         groups = grappler_item.GetColocationGroups()
         self.assertEqual(len(groups), 1)
         self.assertItemsEqual(
             groups[0],
             ['Assign', 'RefIdentity', 'Variable', 'Variable/Assign'])
コード例 #28
0
    def testStandardServicesWithoutGlobalStep(self):
        logdir = _test_dir("standard_services_without_global_step")
        # Create a checkpoint.
        with tf.Graph().as_default():
            v = tf.Variable([1.0], name="foo")
            tf.scalar_summary(["v"], v)
            sv = tf.train.Supervisor(logdir=logdir)
            meta_graph_def = meta_graph.create_meta_graph_def(
                saver_def=sv.saver.saver_def)
            sess = sv.prepare_or_wait_for_session("")
            save_path = sv.save_path
            self._wait_for_glob(save_path, 3.0)
            self._wait_for_glob(os.path.join(logdir, "*events*"), 3.0)
            # Wait to make sure everything is written to file before stopping.
            time.sleep(1)
            sv.stop()
        # There should be an event file with a version number.
        rr = _summary_iterator(logdir)
        ev = next(rr)
        self.assertEquals("brain.Event:2", ev.file_version)
        ev = next(rr)
        ev_graph = tf.GraphDef()
        ev_graph.ParseFromString(ev.graph_def)
        self.assertProtoEquals(sess.graph.as_graph_def(add_shapes=True),
                               ev_graph)

        # Stored MetaGraphDef
        ev = next(rr)
        ev_meta_graph = meta_graph_pb2.MetaGraphDef()
        ev_meta_graph.ParseFromString(ev.meta_graph_def)
        self.assertProtoEquals(meta_graph_def, ev_meta_graph)
        self.assertProtoEquals(sess.graph.as_graph_def(add_shapes=True),
                               ev_meta_graph.graph_def)

        ev = next(rr)
        self.assertProtoEquals("value { tag: 'v' simple_value: 1.0 }",
                               ev.summary)

        ev = next(rr)
        self.assertEquals(tf.SessionLog.STOP, ev.session_log.status)

        self.assertRaises(StopIteration, lambda: next(rr))
        # There should be a checkpoint file with the variable "foo"
        with tf.Graph().as_default(), self.test_session() as sess:
            v = tf.Variable([10.10], name="foo")
            sav = tf.train.Saver([v])
            sav.restore(sess, save_path)
            self.assertEqual(1.0, v.eval()[0])
コード例 #29
0
  def testSmallNetwork(self):
    image = array_ops.placeholder(dtypes.float32, shape=[1, 28, 28, 1])
    label = array_ops.placeholder(dtypes.float32, shape=[1, 10])
    w = variables.Variable(
        random_ops.truncated_normal([5, 5, 1, 32], stddev=0.1))
    b = variables.Variable(random_ops.truncated_normal([32], stddev=0.1))
    conv = nn_ops.conv2d(image, w, strides=[1, 1, 1, 1], padding="SAME")
    h_conv = nn_ops.relu(conv + b)
    h_conv_flat = array_ops.reshape(h_conv, [1, -1])

    w_fc = variables.Variable(
        random_ops.truncated_normal([25088, 10], stddev=0.1))
    b_fc = variables.Variable(random_ops.truncated_normal([10], stddev=0.1))
    y_conv = nn_ops.softmax(math_ops.matmul(h_conv_flat, w_fc) + b_fc)

    cross_entropy = math_ops.reduce_mean(-math_ops.reduce_sum(
        label * math_ops.log(y_conv), reduction_indices=[1]))
    _ = adam.AdamOptimizer(1e-4).minimize(cross_entropy)

    mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())
    report = cost_analyzer.GenerateCostReport(mg)

    # Print the report to make it easier to debug
    print("{}".format(report))

    self.assertTrue(b"MatMul" in report)
    self.assertTrue(b"ApplyAdam" in report)
    self.assertTrue(b"Conv2D" in report)
    self.assertTrue(b"Conv2DBackpropInput" in report)
    self.assertTrue(b"Conv2DBackpropFilter" in report)
    self.assertTrue(b"Softmax" in report)

    for op_type in [
        b"MatMul", b"Conv2D", b"Conv2DBackpropInput", b"Conv2DBackpropFilter"
    ]:
      matcher = re.compile(
          br"\s+" + op_type + br",\s*(\d+),\s*(\d+),\s*([\d\.eE+-]+)%,\s*" +
          br"([\d\.eE+-]+)%,\s*(-?\d+),\s*(\d+),", re.MULTILINE)
      m = matcher.search(report)

      op_count = int(m.group(1))
      # upper = int(m.group(5))
      lower = int(m.group(6))
      if op_type is b"MatMul":
        self.assertEqual(3, op_count)
      else:
        self.assertEqual(1, op_count)
      self.assertTrue(0 <= lower)
コード例 #30
0
 def testBuild(self):
     graph = GraphPlacerTest._buildInception()
     mg = meta_graph.create_meta_graph_def(graph=graph)
     #gcluster = cluster.Cluster(devices=None) # Automatically generates local machine cluster
     gcluster = GraphPlacerTest._buildCluster()
     print(gcluster.ListDevices())  # Print clust info
     # Spend 15 seconds trying to optimize the placement of the model. This
     # should give us enough time to exercise the code, but not enough to find
     # a good placement, so we'll just check for legality.
     placed_mg = graph_placer.PlaceGraph(mg,
                                         allotted_time=300,
                                         cluster=gcluster,
                                         verbose=True)
     placed_g = placed_mg.graph_def
     meta_graph.export_scoped_meta_graph(filename="./g/g.meta",
                                         graph_def=placed_g)
コード例 #31
0
 def before_run(self, run_context):
     if self._timer.last_triggered_step() is None:
         # We do write graph and saver_def at the first call of before_run.
         # We cannot do this in begin, since we let other hooks to change graph and
         # add variables in begin. Graph is finalized after all begin calls.
         training_util.write_graph(
             ops.get_default_graph().as_graph_def(add_shapes=True),
             self._checkpoint_dir, "graph.pbtxt")
         graph = ops.get_default_graph()
         meta_graph_def = meta_graph.create_meta_graph_def(
             graph_def=graph.as_graph_def(add_shapes=True),
             saver_def=self._saver.saver_def)
         self._summary_writer.add_graph(graph)
         self._summary_writer.add_meta_graph(meta_graph_def)
     requests = {"global_steps": self._global_step_tensor}
     return SessionRunArgs(requests)
コード例 #32
0
  def testContext(self):
    with ops.Graph().as_default() as g:
      a = random_ops.random_uniform(shape=())
      b = random_ops.random_uniform(shape=())
      c = a + b
      train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
      train_op.append(c)
      mg = meta_graph.create_meta_graph_def(graph=g)
      grappler_item = item.Item(mg)

    with cluster.Provision(
        disable_detailed_stats=False, disable_timeline=False) as gcluster:
      op_perfs, run_time, step_stats = gcluster.MeasureCosts(grappler_item)
      self.assertTrue(run_time > 0)
      self.assertEqual(len(op_perfs), 7)
      self.assertTrue(step_stats.dev_stats)
コード例 #33
0
  def testNoDetailedStats(self):
    with ops.Graph().as_default() as g:
      a = random_ops.random_uniform(shape=())
      b = random_ops.random_uniform(shape=())
      c = a + b
      train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
      train_op.append(c)
      mg = meta_graph.create_meta_graph_def(graph=g)
      grappler_item = item.Item(mg)
      grappler_cluster = cluster.Cluster(disable_detailed_stats=True)

      op_perfs, run_time, step_stats = grappler_cluster.MeasureCosts(
          grappler_item)
      self.assertTrue(run_time > 0)
      self.assertEqual(len(op_perfs), 0)
      self.assertEqual(len(step_stats.dev_stats), 0)
コード例 #34
0
  def testContext(self):
    with ops.Graph().as_default() as g:
      a = random_ops.random_uniform(shape=())
      b = random_ops.random_uniform(shape=())
      c = a + b
      train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
      train_op.append(c)
      mg = meta_graph.create_meta_graph_def(graph=g)
      grappler_item = item.Item(mg)

    with cluster.Provision(
        disable_detailed_stats=False, disable_timeline=False) as gcluster:
      op_perfs, run_time, step_stats = gcluster.MeasureCosts(grappler_item)
      self.assertTrue(run_time > 0)
      self.assertEqual(len(op_perfs), 4)
      self.assertTrue(step_stats.dev_stats)
コード例 #35
0
ファイル: hooks.py プロジェクト: JeroenZegers/Nabu-MSSS
    def before_run(self, run_context):  # pylint: disable=unused-argument
        if self._timer.last_triggered_step() is None:
            # We do write graph and saver_def at the first call of before_run.
            # We cannot do this in begin, since we let other hooks to change graph and
            # add variables in begin. Graph is finalized after all begin calls.
            training_util.write_graph(
                ops.get_default_graph().as_graph_def(add_shapes=True),
                self._checkpoint_dir, "graph.pbtxt")
            saver_def = self._get_saver().saver_def if self._get_saver(
            ) else None
            graph = ops.get_default_graph()
            meta_graph_def = meta_graph.create_meta_graph_def(
                graph_def=graph.as_graph_def(add_shapes=True),
                saver_def=saver_def)

        return SessionRunArgs(self._global_step_tensor)
コード例 #36
0
ファイル: cluster_test.py プロジェクト: zmk520/tensorflow
    def testNoDetailedStats(self):
        with ops.Graph().as_default() as g:
            a = random_ops.random_uniform(shape=())
            b = random_ops.random_uniform(shape=())
            c = a + b
            train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
            train_op.append(c)
            mg = meta_graph.create_meta_graph_def(graph=g)
            grappler_item = item.Item(mg)
            grappler_cluster = cluster.Cluster(disable_detailed_stats=True)

            op_perfs, run_time, step_stats = grappler_cluster.MeasureCosts(
                grappler_item)
            self.assertTrue(run_time > 0)
            self.assertEqual(len(op_perfs), 0)
            self.assertEqual(len(step_stats.dev_stats), 0)
コード例 #37
0
  def before_run(self, run_context):  # pylint: disable=unused-argument
    if self._timer.last_triggered_step() is None:
      # Write graph in the first call.
      training_util.write_graph(
          ops.get_default_graph().as_graph_def(add_shapes=True),
          self._checkpoint_dir,
          "graph.pbtxt")
      saver_def = self._saver.saver_def if self._saver else None
      graph = ops.get_default_graph()
      meta_graph_def = meta_graph.create_meta_graph_def(
          graph_def=graph.as_graph_def(add_shapes=True),
          saver_def=saver_def)
      self._summary_writer.add_graph(graph)
      self._summary_writer.add_meta_graph(meta_graph_def)

    return SessionRunArgs(self._global_step_tensor)
コード例 #38
0
    def testSmallNetworkCost(self):
        image = array_ops.placeholder(dtypes.float32, shape=[1, 28, 28, 1])
        label = array_ops.placeholder(dtypes.float32, shape=[1, 10])
        w = variables.Variable(
            random_ops.truncated_normal([5, 5, 1, 32], stddev=0.1))
        b = variables.Variable(random_ops.truncated_normal([32], stddev=0.1))
        conv = nn_ops.conv2d(image, w, strides=[1, 1, 1, 1], padding="SAME")
        h_conv = nn_ops.relu(conv + b)
        h_conv_flat = array_ops.reshape(h_conv, [1, -1])

        w_fc = variables.Variable(
            random_ops.truncated_normal([25088, 10], stddev=0.1))
        b_fc = variables.Variable(random_ops.truncated_normal([10],
                                                              stddev=0.1))
        y_conv = nn_ops.softmax(math_ops.matmul(h_conv_flat, w_fc) + b_fc)

        cross_entropy = math_ops.reduce_mean(
            -math_ops.reduce_sum(label * math_ops.log(y_conv), axis=[1]))
        _ = adam.AdamOptimizer(1e-4).minimize(cross_entropy)

        mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())
        report = cost_analyzer.GenerateCostReport(mg)

        # Print the report to make it easier to debug
        print("{}".format(report))

        self.assertTrue(b"MatMul" in report)
        self.assertTrue(b"ApplyAdam" in report)
        self.assertTrue(b"Conv2D" in report)
        self.assertTrue(b"Conv2DBackpropFilter" in report)
        self.assertTrue(b"Softmax" in report)

        for op_type in [b"MatMul", b"Conv2D", b"Conv2DBackpropFilter"]:
            matcher = re.compile(
                br"\s+" + op_type +
                br",\s*(\d+),\s*(\d+),\s*([\d\.eE+-]+)%,\s*" +
                br"([\d\.eE+-]+)%,\s*(-?\d+),\s*(\d+),", re.MULTILINE)
            m = matcher.search(report)

            op_count = int(m.group(1))
            # upper = int(m.group(5))
            lower = int(m.group(6))
            if op_type == b"MatMul":
                self.assertEqual(3, op_count)
            else:
                self.assertEqual(1, op_count)
            self.assertTrue(0 <= lower)
コード例 #39
0
    def testSupportDevices(self):
        gpu_type = test_util.gpu_device_type()
        gpu_name = test_util.gpu_device_name()
        with ops.Graph().as_default() as g:
            a = random_ops.random_uniform(shape=(2, 3))
            b = random_ops.random_uniform(shape=(2, 3))
            c = a + b
            dims = math_ops.range(0, array_ops.rank(c), 1)
            d = math_ops.reduce_sum(a, axis=dims)
            train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
            train_op.append(d)
            mg = meta_graph.create_meta_graph_def(graph=g)
            grappler_item = item.Item(mg)

            device_properties = device_properties_pb2.DeviceProperties(
                type=gpu_type, frequency=1000, num_cores=60)
            named_gpu = device_properties_pb2.NamedDevice(
                properties=device_properties, name=gpu_name)
            device_properties = device_properties_pb2.DeviceProperties(
                type='CPU', frequency=3000, num_cores=6)
            named_cpu = device_properties_pb2.NamedDevice(
                properties=device_properties, name='/CPU:0')
            virtual_cluster = cluster.Cluster(devices=[named_cpu, named_gpu])
            supported_dev = virtual_cluster.GetSupportedDevices(grappler_item)
            self.assertEqual(supported_dev['add'], ['/CPU:0', gpu_name])
            self.assertEqual(supported_dev['Sum'], ['/CPU:0', gpu_name])
            self.assertEqual(supported_dev['range'], ['/CPU:0', gpu_name])

            real_cluster = cluster.Cluster()
            supported_dev = real_cluster.GetSupportedDevices(grappler_item)
            if test.is_gpu_available():
                self.assertEqual(supported_dev['add'], [
                    '/job:localhost/replica:0/task:0/device:CPU:0',
                    '/job:localhost/replica:0/task:0' + gpu_name
                ])
                self.assertEqual(supported_dev['Sum'], [
                    '/job:localhost/replica:0/task:0/device:CPU:0',
                    '/job:localhost/replica:0/task:0' + gpu_name
                ])
                # The axis tensor must reside on the host
                self.assertEqual(
                    supported_dev['range'],
                    ['/job:localhost/replica:0/task:0/device:CPU:0'])
            else:
                self.assertEqual(
                    supported_dev['add'],
                    ['/job:localhost/replica:0/task:0/device:CPU:0'])
コード例 #40
0
  def testStandardServicesWithoutGlobalStep(self):
    logdir = self._test_dir("standard_services_without_global_step")
    # Create a checkpoint.
    with ops.Graph().as_default():
      v = variables.VariableV1([1.0], name="foo")
      summary.scalar("v", v[0])
      sv = supervisor.Supervisor(logdir=logdir)
      meta_graph_def = meta_graph.create_meta_graph_def(
          saver_def=sv.saver.saver_def)
      sess = sv.prepare_or_wait_for_session("")
      save_path = sv.save_path
      self._wait_for_glob(save_path, 3.0)
      self._wait_for_glob(
          os.path.join(logdir, "*events*"), 3.0, for_checkpoint=False)
      # Wait to make sure everything is written to file before stopping.
      time.sleep(1)
      sv.stop()
    # There should be an event file with a version number.
    rr = _summary_iterator(logdir)
    ev = next(rr)
    self.assertEquals("brain.Event:2", ev.file_version)
    ev = next(rr)
    ev_graph = graph_pb2.GraphDef()
    ev_graph.ParseFromString(ev.graph_def)
    self.assertProtoEquals(sess.graph.as_graph_def(add_shapes=True), ev_graph)

    # Stored MetaGraphDef
    ev = next(rr)
    ev_meta_graph = meta_graph_pb2.MetaGraphDef()
    ev_meta_graph.ParseFromString(ev.meta_graph_def)
    self.assertProtoEquals(meta_graph_def, ev_meta_graph)
    self.assertProtoEquals(
        sess.graph.as_graph_def(add_shapes=True), ev_meta_graph.graph_def)

    ev = next(rr)
    self.assertProtoEquals("value { tag: 'v' simple_value: 1.0 }", ev.summary)

    ev = next(rr)
    self.assertEquals(event_pb2.SessionLog.STOP, ev.session_log.status)

    self.assertRaises(StopIteration, lambda: next(rr))
    # There should be a checkpoint file with the variable "foo"
    with ops.Graph().as_default(), self.cached_session() as sess:
      v = variables.VariableV1([10.10], name="foo")
      sav = saver_lib.Saver([v])
      sav.restore(sess, save_path)
      self.assertEqual(1.0, self.evaluate(v)[0])
コード例 #41
0
  def testNoSwapping(self):
    """Make sure the graph is preserved when there is nothing to swap."""
    a = constant_op.constant(10, name='a')
    b = constant_op.constant(20, name='b')
    c = math_ops.add_n([a, b], name='c')
    d = math_ops.add_n([b, c], name='d')
    train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
    train_op.append(d)
    mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())

    rewriter_config = rewriter_config_pb2.RewriterConfig(
        memory_optimization=rewriter_config_pb2.RewriterConfig.MANUAL)
    graph = tf_optimizer.OptimizeGraph(rewriter_config, mg)

    self.assertEqual(len(graph.node), 4)
    self.assertItemsEqual([node.name
                           for node in graph.node], ['a', 'b', 'c', 'd'])
コード例 #42
0
 def test_train(self):
   with tf.Graph().as_default() as g, self.test_session(g):
     with tf.control_dependencies(self._build_inference_graph()):
       train_op = tf.assign_add(tf.contrib.framework.get_global_step(), 1)
     self._assert_summaries(self._output_dir)
     self._assert_ckpt(self._output_dir, False)
     loss = learn.graph_actions._monitored_train(  # pylint: disable=protected-access
         g,
         output_dir=self._output_dir,
         train_op=train_op,
         loss_op=tf.constant(2.0),
         steps=1)
     meta_graph_def = meta_graph.create_meta_graph_def()
     self.assertEqual(2.0, loss)
     self._assert_summaries(self._output_dir, expected_graphs=[g],
                            expected_meta_graphs=[meta_graph_def])
     self._assert_ckpt(self._output_dir, True)
コード例 #43
0
 def after_create_session(self, session, coord):
     global_step = session.run(self._global_step_tensor)
     # We do write graph and saver_def at the first call of before_run.
     # We cannot do this in begin, since we let other hooks to change graph and
     # add variables in begin. Graph is finalized after all begin calls.
     training_util.write_graph(
         ops.get_default_graph().as_graph_def(add_shapes=True),
         self._checkpoint_dir, "graph.pbtxt")
     saver_def = self._get_saver().saver_def if self._get_saver() else None
     graph = ops.get_default_graph()
     meta_graph_def = meta_graph.create_meta_graph_def(
         graph_def=graph.as_graph_def(add_shapes=True), saver_def=saver_def)
     self._summary_writer.add_graph(graph)
     self._summary_writer.add_meta_graph(meta_graph_def)
     # The checkpoint saved here is the state at step "global_step".
     self._save(session, global_step)
     self._timer.update_last_triggered_step(global_step)
コード例 #44
0
    def testDebugMode(self):
        """Make sure arguments can be passed correctly."""
        a = constant_op.constant([10, 11], name="a")
        b = constant_op.constant([10], name="b")
        c = math_ops.add(a, b, name="c")
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        train_op.append(c)
        mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())

        report = model_analyzer.GenerateModelReport(mg, debug=True)

        # Check the report headers
        self.assertIn(b"input 0 (int32) has known value", report)
        self.assertIn(b"input 1 (int32) has known value", report)

        # Also print the report to make it easier to debug
        print("{}".format(report))
コード例 #45
0
    def testBasic(self):
        """Make sure arguments can be passed correctly."""
        a = constant_op.constant(10, name='a')
        b = constant_op.constant(20, name='b')
        c = math_ops.add_n([a, b], name='c')
        d = math_ops.add_n([b, c], name='d')
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        train_op.append(d)
        mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())

        rewriter_config = rewriter_config_pb2.RewriterConfig()
        rewriter_config.optimizers.append('constfold')

        graph = tf_optimizer.OptimizeGraph(rewriter_config, mg)

        self.assertEqual(len(graph.node), 5)
        self.assertItemsEqual([node.name for node in graph.node],
                              ['a', 'b', 'c', 'd', 'ConstantFolding/c'])
コード例 #46
0
ファイル: cost_analyzer_test.py プロジェクト: Harryi0/tinyML
  def testVerbose(self):
    """Make sure the full report is generated with verbose=True."""
    a = constant_op.constant(10, name="a")
    b = constant_op.constant(20, name="b")
    c = math_ops.add_n([a, b], name="c")
    d = math_ops.add_n([b, c], name="d")
    train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
    train_op.append(d)
    mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())

    report = cost_analyzer.GenerateCostReport(
        mg, per_node_report=True, verbose=True)

    # Check the report headers
    self.assertTrue(b"Below is the full per-node report:" in report)

    # Also print the report to make it easier to debug
    print("{}".format(report))
コード例 #47
0
  def testOpProperties(self):
    with ops.Graph().as_default() as g:
      a = constant_op.constant(10)
      b = constant_op.constant(20)
      c = a + b
      train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
      train_op.append(c)
      mg = meta_graph.create_meta_graph_def(graph=g)
      grappler_item = item.Item(mg)
      op_properties = grappler_item.GetOpProperties()

      # All the nodes in this model have one scalar output
      for node in grappler_item.metagraph.graph_def.node:
        node_prop = op_properties[node.name]

        self.assertEqual(1, len(node_prop))
        self.assertEqual(dtypes.int32, node_prop[0].dtype)
        self.assertEqual(tensor_shape.scalar(), node_prop[0].shape)
コード例 #48
0
    def before_run(self, run_context):
        """ Dumps graphs and loads checkpoint if there exits.

        Called before each call to run().

        Args:
            run_context: A `SessionRunContext` object.

        Returns: A `SessionRunArgs` object containing global_step.
        """
        # We do write graph and saver_def at the first call of before_run.
        # We cannot do this in begin, since we let other hooks to change graph and
        # add variables in begin. Graph is finalized after all begin calls.
        if self._is_chief and self._first_call:
            training_util.write_graph(
                ops.get_default_graph().as_graph_def(add_shapes=True),
                self._checkpoint_dir, "graph.pbtxt")
            # dump model details "model_analysis.txt"
            dump_model_analysis(self._checkpoint_dir)  # dump model configs
            graph = ops.get_default_graph()
            meta_graph_def = meta_graph.create_meta_graph_def(
                graph_def=graph.as_graph_def(add_shapes=True),
                saver_def=self._saver.saver_def)
            if self._summary_writer is not None:
                self._summary_writer.add_graph(graph)
                self._summary_writer.add_meta_graph(meta_graph_def)
            tf.logging.info("CheckpointSaverHook (before_run): dump graph...")
        checkpoint_path = saver_lib.latest_checkpoint(self._checkpoint_dir)
        if self._first_call:
            if checkpoint_path:
                # reloading model
                self._saver.restore(run_context.session, checkpoint_path)
                gs = run_context.session.run(self._global_step)
                tf.logging.info(
                    "CheckpointSaverHook (before_run): reloading models and reset global_step={}"
                    .format(gs))
                StepTimer.reset_init_triggered_step(gs)
            elif self._reload_var_ops:
                tf.logging.info(
                    "Assign all variables with pretrained variables.")
                run_context.session.run(self._reload_var_ops)
        self._first_call = False
        self._timer.register_before_run()
        return tf.train.SessionRunArgs(self._global_step)
コード例 #49
0
    def testLoops(self):
        g = ops.Graph()
        with g.as_default():

            def _Cond(_, counter):
                return counter < end

            def _Body(buf, counter):
                buf = array_ops.concat([buf, [counter]], 0)
                counter += 1
                return [buf, counter]

            start = array_ops.placeholder(shape=[], dtype=dtypes.int32)
            end = array_ops.placeholder(shape=[], dtype=dtypes.int32)
            init_buf = array_ops.zeros(shape=[0], dtype=dtypes.int32)
            loop_vars = [init_buf, start]
            shape_inv = [
                tensor_shape.TensorShape([None]),
                tensor_shape.TensorShape([])
            ]
            buf, _ = control_flow_ops.while_loop(_Cond, _Body, loop_vars,
                                                 shape_inv)

            f = -array_ops.ones_like(buf, optimize=False)  # pylint: disable=invalid-unary-operand-type
            buf_shape = array_ops.shape(buf)
            f_shape = array_ops.shape(f)
            ops.add_to_collection('train_op', buf_shape)
            ops.add_to_collection('train_op', f_shape)

        # Optimize the graph.
        mg = meta_graph.create_meta_graph_def(graph=g)
        config = config_pb2.ConfigProto()
        rewriter_config = config.graph_options.rewrite_options
        rewriter_config.min_graph_nodes = -1
        optimized_graph = tf_optimizer.OptimizeGraph(config, mg)
        mg.graph_def.CopyFrom(optimized_graph)

        # Check that the nodes referenced in various collections have been preserved
        item = gitem.Item(mg)
        props = item.GetOpProperties()
        buf_prop = props[buf.op.name]
        f_prop = props[f.op.name]
        self.assertEqual(buf_prop, f_prop)
コード例 #50
0
    def testNoSwapping(self):
        """Make sure the graph is preserved when there is nothing to swap."""
        a = variables.Variable(10, name='a')
        b = variables.Variable(20, name='b')
        c = math_ops.add_n([a, b], name='c')
        d = math_ops.add_n([b, c], name='d')
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        train_op.append(d)
        mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())
        graph_size = len(mg.graph_def.node)
        nodes = [node.name for node in mg.graph_def.node]

        rewriter_config = rewriter_config_pb2.RewriterConfig(
            disable_model_pruning=True,
            memory_optimization=rewriter_config_pb2.RewriterConfig.MANUAL)
        graph = tf_optimizer.OptimizeGraph(rewriter_config, mg)

        self.assertEqual(len(graph.node), graph_size)
        self.assertItemsEqual([node.name for node in graph.node], nodes)
コード例 #51
0
 def testVirtualCluster(self):
   with ops.Graph().as_default() as g:
     a = random_ops.random_uniform(shape=())
     b = random_ops.random_uniform(shape=())
     c = a + b
     train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
     train_op.append(c)
     mg = meta_graph.create_meta_graph_def(graph=g)
     grappler_item = item.Item(mg)
     device_properties = device_properties_pb2.DeviceProperties(
         type='GPU', environment={
             'architecture': '7'
         })
     named_device = device_properties_pb2.NamedDevice(
         properties=device_properties, name='/GPU:0')
     grappler_cluster = cluster.Cluster(devices=[named_device])
     op_perfs, run_time, _ = grappler_cluster.MeasureCosts(grappler_item)
     self.assertGreater(run_time, 0)
     self.assertEqual(len(op_perfs), 15)
コード例 #52
0
 def testMemoryEstimates(self):
   with ops.Graph().as_default() as g:
     with ops.device('/job:localhost/replica:0/task:0/device:CPU:0'):
       a = random_ops.random_uniform(shape=())
       b = random_ops.random_uniform(shape=())
       c = a + b
       train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
       train_op.append(c)
       mg = meta_graph.create_meta_graph_def(graph=g)
       grappler_item = item.Item(mg)
       grappler_cluster = cluster.Cluster(
           disable_detailed_stats=True, disable_timeline=True)
       peak_mem = grappler_cluster.DeterminePeakMemoryUsage(grappler_item)
       self.assertLessEqual(1, len(peak_mem))
       snapshot = peak_mem['/job:localhost/replica:0/task:0/device:CPU:0']
       peak_usage = snapshot[0]
       self.assertEqual(52, peak_usage)
       live_tensors = snapshot[1]
       self.assertEqual(15, len(live_tensors))
コード例 #53
0
  def __init__(self, event_writer, graph=None, graph_def=None):
    """Creates a `SummaryWriter` and an event file.

    On construction the summary writer creates a new event file in `logdir`.
    This event file will contain `Event` protocol buffers constructed when you
    call one of the following functions: `add_summary()`, `add_session_log()`,
    `add_event()`, or `add_graph()`.

    If you pass a `Graph` to the constructor it is added to
    the event file. (This is equivalent to calling `add_graph()` later).

    TensorBoard will pick the graph from the file and display it graphically so
    you can interactively explore the graph you built. You will usually pass
    the graph from the session in which you launched it:

    ```python
    ...create a graph...
    # Launch the graph in a session.
    sess = tf.Session()
    # Create a summary writer, add the 'graph' to the event file.
    writer = tf.summary.FileWriter(<some-directory>, sess.graph)
    ```


    Args:
      event_writer: An EventWriter. Implements add_event method.
      graph: A `Graph` object, such as `sess.graph`.
      graph_def: DEPRECATED: Use the `graph` argument instead.
    """
    self.event_writer = event_writer
    # For storing used tags for session.run() outputs.
    self._session_run_tags = {}
    if graph is not None or graph_def is not None:
      # Calling it with both graph and graph_def for backward compatibility.
      self.add_graph(graph=graph, graph_def=graph_def)
      # Also export the meta_graph_def in this case.
      # graph may itself be a graph_def due to positional arguments
      maybe_graph_as_def = (
          graph.as_graph_def(add_shapes=True) if isinstance(graph, ops.Graph)
          else graph)
      self.add_meta_graph(
          meta_graph.create_meta_graph_def(
              graph_def=graph_def or maybe_graph_as_def))
コード例 #54
0
  def testDefaultAttrStrippingUnregisteredOps(self):
    """Verifies that nodes with un-registered ops are not stripped."""
    graph_def = graph_pb2.GraphDef()
    node = graph_def.node.add()
    node.name = "node_with_unreg_op"
    node.op = "unreg_op"
    node.attr["attr_1"].i = 1

    meta_info_def = meta_graph_pb2.MetaGraphDef.MetaInfoDef()
    meta_info_def.stripped_op_list.op.add()

    with self.cached_session():
      meta_graph_def = meta_graph.create_meta_graph_def(
          meta_info_def=meta_info_def, graph_def=graph_def,
          strip_default_attrs=True)
      node_def = test_util.get_node_def_from_graph("node_with_unreg_op",
                                                   meta_graph_def.graph_def)
      self.assertEqual(node_def.attr["attr_1"].i, 1)
      self.assertTrue(meta_graph_def.meta_info_def.stripped_default_attrs)
コード例 #55
0
    def testBasic(self):
        """Make sure arguments can be passed correctly."""
        a = constant_op.constant(10, name='a')
        b = constant_op.constant(20, name='b')
        c = math_ops.add_n([a, b], name='c')
        d = math_ops.add_n([b, c], name='d')
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        # Being a train_op will make 'd' to be added as a fetch node.
        train_op.append(d)
        mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())

        config = config_pb2.ConfigProto()
        rewriter_config = config.graph_options.rewrite_options
        rewriter_config.optimizers.append('constfold')
        rewriter_config.min_graph_nodes = -1

        graph = tf_optimizer.OptimizeGraph(config, mg)

        self.assertEqual(len(graph.node), 1)
        self.assertItemsEqual([node.name for node in graph.node], ['d'])
コード例 #56
0
    def testBasic(self):
        """Make sure arguments can be passed correctly."""
        a = constant_op.constant([10, 11], name="a")
        b = constant_op.constant([10], name="b")
        c = math_ops.add(a, b, name="c")
        d = math_ops.add_n([a, c], name="d")
        train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
        train_op.append(d)
        mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())

        report = model_analyzer.GenerateModelReport(mg)

        # Check the report headers
        self.assertIn(b"a [Const]", report)
        self.assertIn(b"a [Const]", report)
        self.assertIn(b"c [AddV2]", report)
        self.assertIn(b"d [AddN]", report)

        # Also print the report to make it easier to debug
        print("{}".format(report))
コード例 #57
0
 def test_train_loss(self):
   with tf.Graph().as_default() as g, self.test_session(g):
     tf.contrib.framework.create_global_step()
     loss_var = tf.contrib.framework.local_variable(10.0)
     train_op = tf.group(
         tf.assign_add(tf.contrib.framework.get_global_step(), 1),
         tf.assign_add(loss_var, -1.0))
     self._assert_summaries(self._output_dir)
     self._assert_ckpt(self._output_dir, False)
     loss = learn.graph_actions._monitored_train(  # pylint: disable=protected-access
         g,
         output_dir=self._output_dir,
         train_op=train_op,
         loss_op=loss_var.value(),
         steps=6)
     meta_graph_def = meta_graph.create_meta_graph_def()
     self.assertEqual(4.0, loss)
     self._assert_summaries(self._output_dir, expected_graphs=[g],
                            expected_meta_graphs=[meta_graph_def])
     self._assert_ckpt(self._output_dir, True)
コード例 #58
0
 def after_create_session(self, session, coord):
     del coord
     # Ensure summary writer resource has been initialized.
     session.run(summary_ops_v2.summary_writer_initializer_op())
     global_step = session.run(self._global_step_tensor)
     # Write graph and saver_def once graph is finalized, which isn't true yet
     # in begin() since later hooks can still change the graph.
     training_util.write_graph(
         ops.get_default_graph().as_graph_def(add_shapes=True),
         self._checkpoint_dir, "graph.pbtxt")
     saver_def = self._get_saver().saver_def if self._get_saver() else None
     graph = ops.get_default_graph()
     meta_graph_def = meta_graph.create_meta_graph_def(
         graph_def=graph.as_graph_def(add_shapes=True), saver_def=saver_def)
     with ops.default_session(session):
         self._summary_writer.add_graph(graph)
         self._summary_writer.add_meta_graph(meta_graph_def)
     # The checkpoint saved here is the state at step "global_step".
     self._save(session, global_step)
     self._timer.update_last_triggered_step(global_step)
コード例 #59
0
    def testSimpleSwap(self):
        """Check that the swap annotations are followed."""
        with ops.device('/gpu:0'):
            a = variables.VariableV1(10, name='a')
            b = variables.VariableV1(20, name='b')
            c = math_ops.add_n([a, b], name='c')
            d = math_ops.add_n([b, c], name='d')
            train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
            train_op.append(d)

            d.op._set_attr('_swap_to_host', attr_value_pb2.AttrValue(i=0))

            mg = meta_graph.create_meta_graph_def(
                graph=ops.get_default_graph())
            graph_size = len(mg.graph_def.node)

            config = config_pb2.ConfigProto()
            config.graph_options.rewrite_options.CopyFrom(
                rewriter_config_pb2.RewriterConfig(
                    disable_model_pruning=True,
                    meta_optimizer_iterations=rewriter_config_pb2.
                    RewriterConfig.ONE,
                    constant_folding=rewriter_config_pb2.RewriterConfig.OFF,
                    memory_optimization=rewriter_config_pb2.RewriterConfig.
                    MANUAL,
                    min_graph_nodes=-1))
            graph = tf_optimizer.OptimizeGraph(config, mg)

            self.assertEqual(len(graph.node), graph_size + 2)
            self.assertTrue(
                set([node.name for node in graph.node]) > set(
                    ['a', 'b', 'c', 'd', 'swap_in_d_0', 'swap_out_d_0']))
            for node in graph.node:
                if node.name == 'swap_in_d_0':
                    self.assertEqual('swap_out_d_0', node.input[0])
                    self.assertEqual('^b/read', node.input[1])
                elif node.name == 'swap_out_d_0':
                    self.assertEqual('b/read', node.input[0])
                elif node.name == 'd':
                    self.assertEqual('swap_in_d_0', node.input[0])
                    self.assertEqual('c', node.input[1])
コード例 #60
0
  def testBasicCost(self):
    """Make sure arguments can be passed correctly."""
    a = constant_op.constant(10, name="a")
    b = constant_op.constant(20, name="b")
    c = math_ops.add_n([a, b], name="c")
    d = math_ops.add_n([b, c], name="d")
    train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP)
    train_op.append(d)
    mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph())

    report = cost_analyzer.GenerateCostReport(mg)

    # Check the report headers
    self.assertTrue(b"Total time measured in ns (serialized):" in report)
    self.assertTrue(b"Total time measured in ns (actual):" in report)
    self.assertTrue(b"Total time analytical in ns (upper bound):" in report)
    self.assertTrue(b"Total time analytical in ns (lower bound):" in report)
    self.assertTrue(b"Overall efficiency (analytical upper/actual):" in report)
    self.assertTrue(b"Overall efficiency (analytical lower/actual):" in report)

    # Also print the report to make it easier to debug
    print("{}".format(report))