Best Python code snippet using avocado_python
summary_ops_test.py
Source:summary_ops_test.py
...49 with context.eager_mode():50 with summary_ops.create_file_writer_v2(logdir).as_default():51 output = summary_ops.write('tag', 42, step=12)52 self.assertTrue(output.numpy())53 events = events_from_logdir(logdir)54 self.assertEqual(2, len(events))55 self.assertEqual(12, events[1].step)56 value = events[1].summary.value[0]57 self.assertEqual('tag', value.tag)58 self.assertEqual(42, to_numpy(value))59 def testWrite_fromFunction(self):60 logdir = self.get_temp_dir()61 with context.eager_mode():62 writer = summary_ops.create_file_writer_v2(logdir)63 @def_function.function64 def f():65 with writer.as_default():66 return summary_ops.write('tag', 42, step=12)67 output = f()68 self.assertTrue(output.numpy())69 events = events_from_logdir(logdir)70 self.assertEqual(2, len(events))71 self.assertEqual(12, events[1].step)72 value = events[1].summary.value[0]73 self.assertEqual('tag', value.tag)74 self.assertEqual(42, to_numpy(value))75 def testWrite_metadata(self):76 logdir = self.get_temp_dir()77 metadata = summary_pb2.SummaryMetadata()78 metadata.plugin_data.plugin_name = 'foo'79 with context.eager_mode():80 with summary_ops.create_file_writer_v2(logdir).as_default():81 summary_ops.write('obj', 0, 0, metadata=metadata)82 summary_ops.write('bytes', 0, 0, metadata=metadata.SerializeToString())83 m = constant_op.constant(metadata.SerializeToString())84 summary_ops.write('string_tensor', 0, 0, metadata=m)85 events = events_from_logdir(logdir)86 self.assertEqual(4, len(events))87 self.assertEqual(metadata, events[1].summary.value[0].metadata)88 self.assertEqual(metadata, events[2].summary.value[0].metadata)89 self.assertEqual(metadata, events[3].summary.value[0].metadata)90 def testWrite_name(self):91 @def_function.function92 def f():93 output = summary_ops.write('tag', 42, step=12, name='anonymous')94 self.assertTrue(output.name.startswith('anonymous'))95 f()96 def testWrite_ndarray(self):97 logdir = self.get_temp_dir()98 with context.eager_mode():99 with summary_ops.create_file_writer_v2(logdir).as_default():100 summary_ops.write('tag', [[1, 2], [3, 4]], step=12)101 events = events_from_logdir(logdir)102 value = events[1].summary.value[0]103 self.assertAllEqual([[1, 2], [3, 4]], to_numpy(value))104 def testWrite_tensor(self):105 logdir = self.get_temp_dir()106 with context.eager_mode():107 t = constant_op.constant([[1, 2], [3, 4]])108 with summary_ops.create_file_writer_v2(logdir).as_default():109 summary_ops.write('tag', t, step=12)110 expected = t.numpy()111 events = events_from_logdir(logdir)112 value = events[1].summary.value[0]113 self.assertAllEqual(expected, to_numpy(value))114 def testWrite_tensor_fromFunction(self):115 logdir = self.get_temp_dir()116 with context.eager_mode():117 writer = summary_ops.create_file_writer_v2(logdir)118 @def_function.function119 def f(t):120 with writer.as_default():121 summary_ops.write('tag', t, step=12)122 t = constant_op.constant([[1, 2], [3, 4]])123 f(t)124 expected = t.numpy()125 events = events_from_logdir(logdir)126 value = events[1].summary.value[0]127 self.assertAllEqual(expected, to_numpy(value))128 def testWrite_stringTensor(self):129 logdir = self.get_temp_dir()130 with context.eager_mode():131 with summary_ops.create_file_writer_v2(logdir).as_default():132 summary_ops.write('tag', [b'foo', b'bar'], step=12)133 events = events_from_logdir(logdir)134 value = events[1].summary.value[0]135 self.assertAllEqual([b'foo', b'bar'], to_numpy(value))136 @test_util.run_gpu_only137 def testWrite_gpuDeviceContext(self):138 logdir = self.get_temp_dir()139 with context.eager_mode():140 with summary_ops.create_file_writer_v2(logdir).as_default():141 with ops.device('/GPU:0'):142 value = constant_op.constant(42.0)143 step = constant_op.constant(12, dtype=dtypes.int64)144 summary_ops.write('tag', value, step=step).numpy()145 empty_metadata = summary_pb2.SummaryMetadata()146 events = events_from_logdir(logdir)147 self.assertEqual(2, len(events))148 self.assertEqual(12, events[1].step)149 self.assertEqual(42, to_numpy(events[1].summary.value[0]))150 self.assertEqual(empty_metadata, events[1].summary.value[0].metadata)151 @test_util.also_run_as_tf_function152 def testWrite_noDefaultWriter(self):153 # Use assertAllEqual instead of assertFalse since it works in a defun.154 self.assertAllEqual(False, summary_ops.write('tag', 42, step=0))155 @test_util.also_run_as_tf_function156 def testWrite_noStep_okayIfAlsoNoDefaultWriter(self):157 # Use assertAllEqual instead of assertFalse since it works in a defun.158 self.assertAllEqual(False, summary_ops.write('tag', 42))159 def testWrite_noStep(self):160 logdir = self.get_temp_dir()161 with context.eager_mode():162 with summary_ops.create_file_writer_v2(logdir).as_default():163 with self.assertRaisesRegex(ValueError, 'No step set'):164 summary_ops.write('tag', 42)165 def testWrite_noStep_okayIfNotRecordingSummaries(self):166 logdir = self.get_temp_dir()167 with context.eager_mode():168 with summary_ops.create_file_writer_v2(logdir).as_default():169 with summary_ops.record_if(False):170 self.assertFalse(summary_ops.write('tag', 42))171 def testWrite_usingDefaultStep(self):172 logdir = self.get_temp_dir()173 try:174 with context.eager_mode():175 with summary_ops.create_file_writer_v2(logdir).as_default():176 summary_ops.set_step(1)177 summary_ops.write('tag', 1.0)178 summary_ops.set_step(2)179 summary_ops.write('tag', 1.0)180 mystep = variables.Variable(10, dtype=dtypes.int64)181 summary_ops.set_step(mystep)182 summary_ops.write('tag', 1.0)183 mystep.assign_add(1)184 summary_ops.write('tag', 1.0)185 events = events_from_logdir(logdir)186 self.assertEqual(5, len(events))187 self.assertEqual(1, events[1].step)188 self.assertEqual(2, events[2].step)189 self.assertEqual(10, events[3].step)190 self.assertEqual(11, events[4].step)191 finally:192 # Reset to default state for other tests.193 summary_ops.set_step(None)194 def testWrite_usingDefaultStepConstant_fromFunction(self):195 logdir = self.get_temp_dir()196 try:197 with context.eager_mode():198 writer = summary_ops.create_file_writer_v2(logdir)199 @def_function.function200 def f():201 with writer.as_default():202 summary_ops.write('tag', 1.0)203 summary_ops.set_step(1)204 f()205 summary_ops.set_step(2)206 f()207 events = events_from_logdir(logdir)208 self.assertEqual(3, len(events))209 self.assertEqual(1, events[1].step)210 # The step value will still be 1 because the value was captured at the211 # time the function was first traced.212 self.assertEqual(1, events[2].step)213 finally:214 # Reset to default state for other tests.215 summary_ops.set_step(None)216 def testWrite_usingDefaultStepVariable_fromFunction(self):217 logdir = self.get_temp_dir()218 try:219 with context.eager_mode():220 writer = summary_ops.create_file_writer_v2(logdir)221 @def_function.function222 def f():223 with writer.as_default():224 summary_ops.write('tag', 1.0)225 mystep = variables.Variable(0, dtype=dtypes.int64)226 summary_ops.set_step(mystep)227 f()228 mystep.assign_add(1)229 f()230 mystep.assign(10)231 f()232 events = events_from_logdir(logdir)233 self.assertEqual(4, len(events))234 self.assertEqual(0, events[1].step)235 self.assertEqual(1, events[2].step)236 self.assertEqual(10, events[3].step)237 finally:238 # Reset to default state for other tests.239 summary_ops.set_step(None)240 def testWrite_usingDefaultStepConstant_fromLegacyGraph(self):241 logdir = self.get_temp_dir()242 try:243 with context.graph_mode():244 writer = summary_ops.create_file_writer_v2(logdir)245 summary_ops.set_step(1)246 with writer.as_default():247 write_op = summary_ops.write('tag', 1.0)248 summary_ops.set_step(2)249 with self.cached_session() as sess:250 sess.run(writer.init())251 sess.run(write_op)252 sess.run(write_op)253 sess.run(writer.flush())254 events = events_from_logdir(logdir)255 self.assertEqual(3, len(events))256 self.assertEqual(1, events[1].step)257 # The step value will still be 1 because the value was captured at the258 # time the graph was constructed.259 self.assertEqual(1, events[2].step)260 finally:261 # Reset to default state for other tests.262 summary_ops.set_step(None)263 def testWrite_usingDefaultStepVariable_fromLegacyGraph(self):264 logdir = self.get_temp_dir()265 try:266 with context.graph_mode():267 writer = summary_ops.create_file_writer_v2(logdir)268 mystep = variables.Variable(0, dtype=dtypes.int64)269 summary_ops.set_step(mystep)270 with writer.as_default():271 write_op = summary_ops.write('tag', 1.0)272 first_assign_op = mystep.assign_add(1)273 second_assign_op = mystep.assign(10)274 with self.cached_session() as sess:275 sess.run(writer.init())276 sess.run(mystep.initializer)277 sess.run(write_op)278 sess.run(first_assign_op)279 sess.run(write_op)280 sess.run(second_assign_op)281 sess.run(write_op)282 sess.run(writer.flush())283 events = events_from_logdir(logdir)284 self.assertEqual(4, len(events))285 self.assertEqual(0, events[1].step)286 self.assertEqual(1, events[2].step)287 self.assertEqual(10, events[3].step)288 finally:289 # Reset to default state for other tests.290 summary_ops.set_step(None)291 def testWrite_usingDefaultStep_fromAsDefault(self):292 logdir = self.get_temp_dir()293 try:294 with context.eager_mode():295 writer = summary_ops.create_file_writer_v2(logdir)296 with writer.as_default(step=1):297 summary_ops.write('tag', 1.0)298 with writer.as_default():299 summary_ops.write('tag', 1.0)300 with writer.as_default(step=2):301 summary_ops.write('tag', 1.0)302 summary_ops.write('tag', 1.0)303 summary_ops.set_step(3)304 summary_ops.write('tag', 1.0)305 events = events_from_logdir(logdir)306 self.assertListEqual([1, 1, 2, 1, 3], [e.step for e in events[1:]])307 finally:308 # Reset to default state for other tests.309 summary_ops.set_step(None)310 def testWrite_usingDefaultStepVariable_fromAsDefault(self):311 logdir = self.get_temp_dir()312 try:313 with context.eager_mode():314 writer = summary_ops.create_file_writer_v2(logdir)315 mystep = variables.Variable(1, dtype=dtypes.int64)316 with writer.as_default(step=mystep):317 summary_ops.write('tag', 1.0)318 with writer.as_default():319 mystep.assign(2)320 summary_ops.write('tag', 1.0)321 with writer.as_default(step=3):322 summary_ops.write('tag', 1.0)323 summary_ops.write('tag', 1.0)324 mystep.assign(4)325 summary_ops.write('tag', 1.0)326 events = events_from_logdir(logdir)327 self.assertListEqual([1, 2, 3, 2, 4], [e.step for e in events[1:]])328 finally:329 # Reset to default state for other tests.330 summary_ops.set_step(None)331 def testWrite_usingDefaultStep_fromSetAsDefault(self):332 logdir = self.get_temp_dir()333 try:334 with context.eager_mode():335 writer = summary_ops.create_file_writer_v2(logdir)336 mystep = variables.Variable(1, dtype=dtypes.int64)337 writer.set_as_default(step=mystep)338 summary_ops.write('tag', 1.0)339 mystep.assign(2)340 summary_ops.write('tag', 1.0)341 writer.set_as_default(step=3)342 summary_ops.write('tag', 1.0)343 writer.flush()344 events = events_from_logdir(logdir)345 self.assertListEqual([1, 2, 3], [e.step for e in events[1:]])346 finally:347 # Reset to default state for other tests.348 summary_ops.set_step(None)349 def testWrite_usingDefaultStepVariable_fromSetAsDefault(self):350 logdir = self.get_temp_dir()351 try:352 with context.eager_mode():353 writer = summary_ops.create_file_writer_v2(logdir)354 writer.set_as_default(step=1)355 summary_ops.write('tag', 1.0)356 writer.set_as_default(step=2)357 summary_ops.write('tag', 1.0)358 writer.set_as_default()359 summary_ops.write('tag', 1.0)360 writer.flush()361 events = events_from_logdir(logdir)362 self.assertListEqual([1, 2, 2], [e.step for e in events[1:]])363 finally:364 # Reset to default state for other tests.365 summary_ops.set_step(None)366 def testWrite_recordIf_constant(self):367 logdir = self.get_temp_dir()368 with context.eager_mode():369 with summary_ops.create_file_writer_v2(logdir).as_default():370 self.assertTrue(summary_ops.write('default', 1, step=0))371 with summary_ops.record_if(True):372 self.assertTrue(summary_ops.write('set_on', 1, step=0))373 with summary_ops.record_if(False):374 self.assertFalse(summary_ops.write('set_off', 1, step=0))375 events = events_from_logdir(logdir)376 self.assertEqual(3, len(events))377 self.assertEqual('default', events[1].summary.value[0].tag)378 self.assertEqual('set_on', events[2].summary.value[0].tag)379 def testWrite_recordIf_constant_fromFunction(self):380 logdir = self.get_temp_dir()381 with context.eager_mode():382 writer = summary_ops.create_file_writer_v2(logdir)383 @def_function.function384 def f():385 with writer.as_default():386 # Use assertAllEqual instead of assertTrue since it works in a defun.387 self.assertAllEqual(summary_ops.write('default', 1, step=0), True)388 with summary_ops.record_if(True):389 self.assertAllEqual(summary_ops.write('set_on', 1, step=0), True)390 with summary_ops.record_if(False):391 self.assertAllEqual(summary_ops.write('set_off', 1, step=0), False)392 f()393 events = events_from_logdir(logdir)394 self.assertEqual(3, len(events))395 self.assertEqual('default', events[1].summary.value[0].tag)396 self.assertEqual('set_on', events[2].summary.value[0].tag)397 def testWrite_recordIf_callable(self):398 logdir = self.get_temp_dir()399 with context.eager_mode():400 step = variables.Variable(-1, dtype=dtypes.int64)401 def record_fn():402 step.assign_add(1)403 return int(step % 2) == 0404 with summary_ops.create_file_writer_v2(logdir).as_default():405 with summary_ops.record_if(record_fn):406 self.assertTrue(summary_ops.write('tag', 1, step=step))407 self.assertFalse(summary_ops.write('tag', 1, step=step))408 self.assertTrue(summary_ops.write('tag', 1, step=step))409 self.assertFalse(summary_ops.write('tag', 1, step=step))410 self.assertTrue(summary_ops.write('tag', 1, step=step))411 events = events_from_logdir(logdir)412 self.assertEqual(4, len(events))413 self.assertEqual(0, events[1].step)414 self.assertEqual(2, events[2].step)415 self.assertEqual(4, events[3].step)416 def testWrite_recordIf_callable_fromFunction(self):417 logdir = self.get_temp_dir()418 with context.eager_mode():419 writer = summary_ops.create_file_writer_v2(logdir)420 step = variables.Variable(-1, dtype=dtypes.int64)421 @def_function.function422 def record_fn():423 step.assign_add(1)424 return math_ops.equal(step % 2, 0)425 @def_function.function426 def f():427 with writer.as_default():428 with summary_ops.record_if(record_fn):429 return [430 summary_ops.write('tag', 1, step=step),431 summary_ops.write('tag', 1, step=step),432 summary_ops.write('tag', 1, step=step)]433 self.assertAllEqual(f(), [True, False, True])434 self.assertAllEqual(f(), [False, True, False])435 events = events_from_logdir(logdir)436 self.assertEqual(4, len(events))437 self.assertEqual(0, events[1].step)438 self.assertEqual(2, events[2].step)439 self.assertEqual(4, events[3].step)440 def testWrite_recordIf_tensorInput_fromFunction(self):441 logdir = self.get_temp_dir()442 with context.eager_mode():443 writer = summary_ops.create_file_writer_v2(logdir)444 @def_function.function(input_signature=[445 tensor_spec.TensorSpec(shape=[], dtype=dtypes.int64)])446 def f(step):447 with writer.as_default():448 with summary_ops.record_if(math_ops.equal(step % 2, 0)):449 return summary_ops.write('tag', 1, step=step)450 self.assertTrue(f(0))451 self.assertFalse(f(1))452 self.assertTrue(f(2))453 self.assertFalse(f(3))454 self.assertTrue(f(4))455 events = events_from_logdir(logdir)456 self.assertEqual(4, len(events))457 self.assertEqual(0, events[1].step)458 self.assertEqual(2, events[2].step)459 self.assertEqual(4, events[3].step)460 def testWriteRawPb(self):461 logdir = self.get_temp_dir()462 pb = summary_pb2.Summary()463 pb.value.add().simple_value = 42.0464 with context.eager_mode():465 with summary_ops.create_file_writer_v2(logdir).as_default():466 output = summary_ops.write_raw_pb(pb.SerializeToString(), step=12)467 self.assertTrue(output.numpy())468 events = events_from_logdir(logdir)469 self.assertEqual(2, len(events))470 self.assertEqual(12, events[1].step)471 self.assertProtoEquals(pb, events[1].summary)472 def testWriteRawPb_fromFunction(self):473 logdir = self.get_temp_dir()474 pb = summary_pb2.Summary()475 pb.value.add().simple_value = 42.0476 with context.eager_mode():477 writer = summary_ops.create_file_writer_v2(logdir)478 @def_function.function479 def f():480 with writer.as_default():481 return summary_ops.write_raw_pb(pb.SerializeToString(), step=12)482 output = f()483 self.assertTrue(output.numpy())484 events = events_from_logdir(logdir)485 self.assertEqual(2, len(events))486 self.assertEqual(12, events[1].step)487 self.assertProtoEquals(pb, events[1].summary)488 def testWriteRawPb_multipleValues(self):489 logdir = self.get_temp_dir()490 pb1 = summary_pb2.Summary()491 pb1.value.add().simple_value = 1.0492 pb1.value.add().simple_value = 2.0493 pb2 = summary_pb2.Summary()494 pb2.value.add().simple_value = 3.0495 pb3 = summary_pb2.Summary()496 pb3.value.add().simple_value = 4.0497 pb3.value.add().simple_value = 5.0498 pb3.value.add().simple_value = 6.0499 pbs = [pb.SerializeToString() for pb in (pb1, pb2, pb3)]500 with context.eager_mode():501 with summary_ops.create_file_writer_v2(logdir).as_default():502 output = summary_ops.write_raw_pb(pbs, step=12)503 self.assertTrue(output.numpy())504 events = events_from_logdir(logdir)505 self.assertEqual(2, len(events))506 self.assertEqual(12, events[1].step)507 expected_pb = summary_pb2.Summary()508 for i in range(6):509 expected_pb.value.add().simple_value = i + 1.0510 self.assertProtoEquals(expected_pb, events[1].summary)511 def testWriteRawPb_invalidValue(self):512 logdir = self.get_temp_dir()513 with context.eager_mode():514 with summary_ops.create_file_writer_v2(logdir).as_default():515 with self.assertRaisesRegex(516 errors.DataLossError,517 'Bad tf.compat.v1.Summary binary proto tensor string'):518 summary_ops.write_raw_pb('notaproto', step=12)519 @test_util.also_run_as_tf_function520 def testGetSetStep(self):521 try:522 self.assertIsNone(summary_ops.get_step())523 summary_ops.set_step(1)524 # Use assertAllEqual instead of assertEqual since it works in a defun.525 self.assertAllEqual(1, summary_ops.get_step())526 summary_ops.set_step(constant_op.constant(2))527 self.assertAllEqual(2, summary_ops.get_step())528 finally:529 # Reset to default state for other tests.530 summary_ops.set_step(None)531 def testGetSetStep_variable(self):532 with context.eager_mode():533 try:534 mystep = variables.Variable(0)535 summary_ops.set_step(mystep)536 self.assertAllEqual(0, summary_ops.get_step().read_value())537 mystep.assign_add(1)538 self.assertAllEqual(1, summary_ops.get_step().read_value())539 # Check that set_step() properly maintains reference to variable.540 del mystep541 self.assertAllEqual(1, summary_ops.get_step().read_value())542 summary_ops.get_step().assign_add(1)543 self.assertAllEqual(2, summary_ops.get_step().read_value())544 finally:545 # Reset to default state for other tests.546 summary_ops.set_step(None)547 def testGetSetStep_variable_fromFunction(self):548 with context.eager_mode():549 try:550 @def_function.function551 def set_step(step):552 summary_ops.set_step(step)553 return summary_ops.get_step()554 @def_function.function555 def get_and_increment():556 summary_ops.get_step().assign_add(1)557 return summary_ops.get_step()558 mystep = variables.Variable(0)559 self.assertAllEqual(0, set_step(mystep))560 self.assertAllEqual(0, summary_ops.get_step().read_value())561 self.assertAllEqual(1, get_and_increment())562 self.assertAllEqual(2, get_and_increment())563 # Check that set_step() properly maintains reference to variable.564 del mystep565 self.assertAllEqual(3, get_and_increment())566 finally:567 # Reset to default state for other tests.568 summary_ops.set_step(None)569 @test_util.also_run_as_tf_function570 def testSummaryScope(self):571 with summary_ops.summary_scope('foo') as (tag, scope):572 self.assertEqual('foo', tag)573 self.assertEqual('foo/', scope)574 with summary_ops.summary_scope('bar') as (tag, scope):575 self.assertEqual('foo/bar', tag)576 self.assertEqual('foo/bar/', scope)577 with summary_ops.summary_scope('with/slash') as (tag, scope):578 self.assertEqual('foo/with/slash', tag)579 self.assertEqual('foo/with/slash/', scope)580 with ops.name_scope(None, skip_on_eager=False):581 with summary_ops.summary_scope('unnested') as (tag, scope):582 self.assertEqual('unnested', tag)583 self.assertEqual('unnested/', scope)584 @test_util.also_run_as_tf_function585 def testSummaryScope_defaultName(self):586 with summary_ops.summary_scope(None) as (tag, scope):587 self.assertEqual('summary', tag)588 self.assertEqual('summary/', scope)589 with summary_ops.summary_scope(None, 'backup') as (tag, scope):590 self.assertEqual('backup', tag)591 self.assertEqual('backup/', scope)592 @test_util.also_run_as_tf_function593 def testSummaryScope_handlesCharactersIllegalForScope(self):594 with summary_ops.summary_scope('f?o?o') as (tag, scope):595 self.assertEqual('f?o?o', tag)596 self.assertEqual('foo/', scope)597 # If all characters aren't legal for a scope name, use default name.598 with summary_ops.summary_scope('???', 'backup') as (tag, scope):599 self.assertEqual('???', tag)600 self.assertEqual('backup/', scope)601 @test_util.also_run_as_tf_function602 def testSummaryScope_nameNotUniquifiedForTag(self):603 constant_op.constant(0, name='foo')604 with summary_ops.summary_scope('foo') as (tag, _):605 self.assertEqual('foo', tag)606 with summary_ops.summary_scope('foo') as (tag, _):607 self.assertEqual('foo', tag)608 with ops.name_scope('with', skip_on_eager=False):609 constant_op.constant(0, name='slash')610 with summary_ops.summary_scope('with/slash') as (tag, _):611 self.assertEqual('with/slash', tag)612 def testAllV2SummaryOps(self):613 logdir = self.get_temp_dir()614 def define_ops():615 result = []616 # TF 2.0 summary ops617 result.append(summary_ops.write('write', 1, step=0))618 result.append(summary_ops.write_raw_pb(b'', step=0, name='raw_pb'))619 # TF 1.x tf.contrib.summary ops620 result.append(summary_ops.generic('tensor', 1, step=1))621 result.append(summary_ops.scalar('scalar', 2.0, step=1))622 result.append(summary_ops.histogram('histogram', [1.0], step=1))623 result.append(summary_ops.image('image', [[[[1.0]]]], step=1))624 result.append(summary_ops.audio('audio', [[1.0]], 1.0, 1, step=1))625 return result626 with context.graph_mode():627 ops_without_writer = define_ops()628 with summary_ops.create_file_writer_v2(logdir).as_default():629 with summary_ops.record_if(True):630 ops_recording_on = define_ops()631 with summary_ops.record_if(False):632 ops_recording_off = define_ops()633 # We should be collecting all ops defined with a default writer present,634 # regardless of whether recording was set on or off, but not those defined635 # without a writer at all.636 del ops_without_writer637 expected_ops = ops_recording_on + ops_recording_off638 self.assertCountEqual(expected_ops, summary_ops.all_v2_summary_ops())639 def testShouldRecordSummaries_defaultState(self):640 logdir = self.get_temp_dir()641 with context.eager_mode():642 self.assertAllEqual(False, summary_ops.should_record_summaries())643 w = summary_ops.create_file_writer_v2(logdir)644 self.assertAllEqual(False, summary_ops.should_record_summaries())645 with w.as_default():646 # Should be enabled only when default writer is registered.647 self.assertAllEqual(True, summary_ops.should_record_summaries())648 self.assertAllEqual(False, summary_ops.should_record_summaries())649 with summary_ops.record_if(True):650 # Should be disabled when no default writer, even with record_if(True).651 self.assertAllEqual(False, summary_ops.should_record_summaries())652 def testShouldRecordSummaries_constants(self):653 logdir = self.get_temp_dir()654 with context.eager_mode():655 with summary_ops.create_file_writer_v2(logdir).as_default():656 with summary_ops.record_if(True):657 self.assertAllEqual(True, summary_ops.should_record_summaries())658 with summary_ops.record_if(False):659 self.assertAllEqual(False, summary_ops.should_record_summaries())660 with summary_ops.record_if(True):661 self.assertAllEqual(True, summary_ops.should_record_summaries())662 def testShouldRecordSummaries_variable(self):663 logdir = self.get_temp_dir()664 with context.eager_mode():665 with summary_ops.create_file_writer_v2(logdir).as_default():666 cond = variables.Variable(False)667 with summary_ops.record_if(cond):668 self.assertAllEqual(False, summary_ops.should_record_summaries())669 cond.assign(True)670 self.assertAllEqual(True, summary_ops.should_record_summaries())671 def testShouldRecordSummaries_callable(self):672 logdir = self.get_temp_dir()673 with context.eager_mode():674 with summary_ops.create_file_writer_v2(logdir).as_default():675 cond_box = [False]676 cond = lambda: cond_box[0]677 with summary_ops.record_if(cond):678 self.assertAllEqual(False, summary_ops.should_record_summaries())679 cond_box[0] = True680 self.assertAllEqual(True, summary_ops.should_record_summaries())681 def testShouldRecordSummaries_fromFunction(self):682 logdir = self.get_temp_dir()683 with context.eager_mode():684 writer = summary_ops.create_file_writer_v2(logdir)685 @def_function.function(input_signature=[686 tensor_spec.TensorSpec(shape=[], dtype=dtypes.bool)])687 def f(cond):688 results = []689 results.append(summary_ops.should_record_summaries())690 with writer.as_default():691 results.append(summary_ops.should_record_summaries())692 with summary_ops.record_if(False):693 results.append(summary_ops.should_record_summaries())694 with summary_ops.record_if(cond):695 results.append(summary_ops.should_record_summaries())696 return results697 self.assertAllEqual([False, True, False, True], f(True))698 self.assertAllEqual([False, True, False, False], f(False))699 def testHasDefaultWriter_checkWriter(self):700 logdir = self.get_temp_dir()701 with context.eager_mode():702 with self.subTest(name='has_writer'):703 with summary_ops.create_file_writer_v2(logdir).as_default():704 self.assertTrue(summary_ops.has_default_writer())705 with self.subTest(name='no_writer'):706 self.assertFalse(summary_ops.has_default_writer())707class SummaryWriterTest(test_util.TensorFlowTestCase):708 def testCreate_withInitAndClose(self):709 logdir = self.get_temp_dir()710 with context.eager_mode():711 writer = summary_ops.create_file_writer_v2(712 logdir, max_queue=1000, flush_millis=1000000)713 get_total = lambda: len(events_from_logdir(logdir))714 self.assertEqual(1, get_total()) # file_version Event715 # Calling init() again while writer is open has no effect716 writer.init()717 self.assertEqual(1, get_total())718 with writer.as_default():719 summary_ops.write('tag', 1, step=0)720 self.assertEqual(1, get_total())721 # Calling .close() should do an implicit flush722 writer.close()723 self.assertEqual(2, get_total())724 def testCreate_fromFunction(self):725 logdir = self.get_temp_dir()726 @def_function.function727 def f():728 # Returned SummaryWriter must be stored in a non-local variable so it729 # lives throughout the function execution.730 if not hasattr(f, 'writer'):731 f.writer = summary_ops.create_file_writer_v2(logdir)732 with context.eager_mode():733 f()734 event_files = gfile.Glob(os.path.join(logdir, '*'))735 self.assertEqual(1, len(event_files))736 def testCreate_graphTensorArgument_raisesError(self):737 logdir = self.get_temp_dir()738 with context.graph_mode():739 logdir_tensor = constant_op.constant(logdir)740 with context.eager_mode():741 with self.assertRaisesRegex(742 ValueError, 'Invalid graph Tensor argument.*logdir'):743 summary_ops.create_file_writer_v2(logdir_tensor)744 self.assertEmpty(gfile.Glob(os.path.join(logdir, '*')))745 def testCreate_fromFunction_graphTensorArgument_raisesError(self):746 logdir = self.get_temp_dir()747 @def_function.function748 def f():749 summary_ops.create_file_writer_v2(constant_op.constant(logdir))750 with context.eager_mode():751 with self.assertRaisesRegex(752 ValueError, 'Invalid graph Tensor argument.*logdir'):753 f()754 self.assertEmpty(gfile.Glob(os.path.join(logdir, '*')))755 def testCreate_fromFunction_unpersistedResource_raisesError(self):756 logdir = self.get_temp_dir()757 @def_function.function758 def f():759 with summary_ops.create_file_writer_v2(logdir).as_default():760 pass # Calling .as_default() is enough to indicate use.761 with context.eager_mode():762 # TODO(nickfelt): change this to a better error763 with self.assertRaisesRegex(764 errors.NotFoundError, 'Resource.*does not exist'):765 f()766 # Even though we didn't use it, an event file will have been created.767 self.assertEqual(1, len(gfile.Glob(os.path.join(logdir, '*'))))768 def testCreate_immediateSetAsDefault_retainsReference(self):769 logdir = self.get_temp_dir()770 try:771 with context.eager_mode():772 summary_ops.create_file_writer_v2(logdir).set_as_default()773 summary_ops.flush()774 finally:775 # Ensure we clean up no matter how the test executes.776 summary_ops._summary_state.writer = None # pylint: disable=protected-access777 def testCreate_immediateAsDefault_retainsReference(self):778 logdir = self.get_temp_dir()779 with context.eager_mode():780 with summary_ops.create_file_writer_v2(logdir).as_default():781 summary_ops.flush()782 def testCreate_avoidsFilenameCollision(self):783 logdir = self.get_temp_dir()784 with context.eager_mode():785 for _ in range(10):786 summary_ops.create_file_writer_v2(logdir)787 event_files = gfile.Glob(os.path.join(logdir, '*'))788 self.assertLen(event_files, 10)789 def testCreate_graphMode_avoidsFilenameCollision(self):790 logdir = self.get_temp_dir()791 with context.graph_mode(), ops.Graph().as_default():792 writer = summary_ops.create_file_writer_v2(logdir)793 with self.cached_session() as sess:794 for _ in range(10):795 sess.run(writer.init())796 sess.run(writer.close())797 event_files = gfile.Glob(os.path.join(logdir, '*'))798 self.assertLen(event_files, 10)799 def testNoSharing(self):800 # Two writers with the same logdir should not share state.801 logdir = self.get_temp_dir()802 with context.eager_mode():803 writer1 = summary_ops.create_file_writer_v2(logdir)804 with writer1.as_default():805 summary_ops.write('tag', 1, step=1)806 event_files = gfile.Glob(os.path.join(logdir, '*'))807 self.assertEqual(1, len(event_files))808 file1 = event_files[0]809 writer2 = summary_ops.create_file_writer_v2(logdir)810 with writer2.as_default():811 summary_ops.write('tag', 1, step=2)812 event_files = gfile.Glob(os.path.join(logdir, '*'))813 self.assertEqual(2, len(event_files))814 event_files.remove(file1)815 file2 = event_files[0]816 # Extra writes to ensure interleaved usage works.817 with writer1.as_default():818 summary_ops.write('tag', 1, step=1)819 with writer2.as_default():820 summary_ops.write('tag', 1, step=2)821 events = iter(events_from_file(file1))822 self.assertEqual('brain.Event:2', next(events).file_version)823 self.assertEqual(1, next(events).step)824 self.assertEqual(1, next(events).step)825 self.assertRaises(StopIteration, lambda: next(events))826 events = iter(events_from_file(file2))827 self.assertEqual('brain.Event:2', next(events).file_version)828 self.assertEqual(2, next(events).step)829 self.assertEqual(2, next(events).step)830 self.assertRaises(StopIteration, lambda: next(events))831 def testNoSharing_fromFunction(self):832 logdir = self.get_temp_dir()833 @def_function.function834 def f1():835 if not hasattr(f1, 'writer'):836 f1.writer = summary_ops.create_file_writer_v2(logdir)837 with f1.writer.as_default():838 summary_ops.write('tag', 1, step=1)839 @def_function.function840 def f2():841 if not hasattr(f2, 'writer'):842 f2.writer = summary_ops.create_file_writer_v2(logdir)843 with f2.writer.as_default():844 summary_ops.write('tag', 1, step=2)845 with context.eager_mode():846 f1()847 event_files = gfile.Glob(os.path.join(logdir, '*'))848 self.assertEqual(1, len(event_files))849 file1 = event_files[0]850 f2()851 event_files = gfile.Glob(os.path.join(logdir, '*'))852 self.assertEqual(2, len(event_files))853 event_files.remove(file1)854 file2 = event_files[0]855 # Extra writes to ensure interleaved usage works.856 f1()857 f2()858 events = iter(events_from_file(file1))859 self.assertEqual('brain.Event:2', next(events).file_version)860 self.assertEqual(1, next(events).step)861 self.assertEqual(1, next(events).step)862 self.assertRaises(StopIteration, lambda: next(events))863 events = iter(events_from_file(file2))864 self.assertEqual('brain.Event:2', next(events).file_version)865 self.assertEqual(2, next(events).step)866 self.assertEqual(2, next(events).step)867 self.assertRaises(StopIteration, lambda: next(events))868 def testMaxQueue(self):869 logdir = self.get_temp_dir()870 with context.eager_mode():871 with summary_ops.create_file_writer_v2(872 logdir, max_queue=1, flush_millis=999999).as_default():873 get_total = lambda: len(events_from_logdir(logdir))874 # Note: First tf.compat.v1.Event is always file_version.875 self.assertEqual(1, get_total())876 summary_ops.write('tag', 1, step=0)877 self.assertEqual(1, get_total())878 # Should flush after second summary since max_queue = 1879 summary_ops.write('tag', 1, step=0)880 self.assertEqual(3, get_total())881 def testWriterFlush(self):882 logdir = self.get_temp_dir()883 get_total = lambda: len(events_from_logdir(logdir))884 with context.eager_mode():885 writer = summary_ops.create_file_writer_v2(886 logdir, max_queue=1000, flush_millis=1000000)887 self.assertEqual(1, get_total()) # file_version Event888 with writer.as_default():889 summary_ops.write('tag', 1, step=0)890 self.assertEqual(1, get_total())891 writer.flush()892 self.assertEqual(2, get_total())893 summary_ops.write('tag', 1, step=0)894 self.assertEqual(2, get_total())895 # Exiting the "as_default()" should do an implicit flush896 self.assertEqual(3, get_total())897 def testFlushFunction(self):898 logdir = self.get_temp_dir()899 with context.eager_mode():900 writer = summary_ops.create_file_writer_v2(901 logdir, max_queue=999999, flush_millis=999999)902 with writer.as_default():903 get_total = lambda: len(events_from_logdir(logdir))904 # Note: First tf.compat.v1.Event is always file_version.905 self.assertEqual(1, get_total())906 summary_ops.write('tag', 1, step=0)907 summary_ops.write('tag', 1, step=0)908 self.assertEqual(1, get_total())909 summary_ops.flush()910 self.assertEqual(3, get_total())911 # Test "writer" parameter912 summary_ops.write('tag', 1, step=0)913 self.assertEqual(3, get_total())914 summary_ops.flush(writer=writer)915 self.assertEqual(4, get_total())916 summary_ops.write('tag', 1, step=0)917 self.assertEqual(4, get_total())918 summary_ops.flush(writer=writer._resource) # pylint:disable=protected-access919 self.assertEqual(5, get_total())920 @test_util.assert_no_new_tensors921 def testNoMemoryLeak_graphMode(self):922 logdir = self.get_temp_dir()923 with context.graph_mode(), ops.Graph().as_default():924 summary_ops.create_file_writer_v2(logdir)925 @test_util.assert_no_new_pyobjects_executing_eagerly926 def testNoMemoryLeak_eagerMode(self):927 logdir = self.get_temp_dir()928 with summary_ops.create_file_writer_v2(logdir).as_default():929 summary_ops.write('tag', 1, step=0)930 def testClose_preventsLaterUse(self):931 logdir = self.get_temp_dir()932 with context.eager_mode():933 writer = summary_ops.create_file_writer_v2(logdir)934 writer.close()935 writer.close() # redundant close() is a no-op936 writer.flush() # redundant flush() is a no-op937 with self.assertRaisesRegex(RuntimeError, 'already closed'):938 writer.init()939 with self.assertRaisesRegex(RuntimeError, 'already closed'):940 with writer.as_default():941 self.fail('should not get here')942 with self.assertRaisesRegex(RuntimeError, 'already closed'):943 writer.set_as_default()944 def testClose_closesOpenFile(self):945 try:946 import psutil # pylint: disable=g-import-not-at-top947 except ImportError:948 raise unittest.SkipTest('test requires psutil')949 proc = psutil.Process()950 get_open_filenames = lambda: set(info[0] for info in proc.open_files())951 logdir = self.get_temp_dir()952 with context.eager_mode():953 writer = summary_ops.create_file_writer_v2(logdir)954 files = gfile.Glob(os.path.join(logdir, '*'))955 self.assertEqual(1, len(files))956 eventfile = files[0]957 self.assertIn(eventfile, get_open_filenames())958 writer.close()959 self.assertNotIn(eventfile, get_open_filenames())960 def testDereference_closesOpenFile(self):961 try:962 import psutil # pylint: disable=g-import-not-at-top963 except ImportError:964 raise unittest.SkipTest('test requires psutil')965 proc = psutil.Process()966 get_open_filenames = lambda: set(info[0] for info in proc.open_files())967 logdir = self.get_temp_dir()968 with context.eager_mode():969 writer = summary_ops.create_file_writer_v2(logdir)970 files = gfile.Glob(os.path.join(logdir, '*'))971 self.assertEqual(1, len(files))972 eventfile = files[0]973 self.assertIn(eventfile, get_open_filenames())974 del writer975 self.assertNotIn(eventfile, get_open_filenames())976class SummaryWriterSavedModelTest(test_util.TensorFlowTestCase):977 def testWriter_savedAsModuleProperty_loadInEagerMode(self):978 with context.eager_mode():979 class Model(module.Module):980 def __init__(self, model_dir):981 self._writer = summary_ops.create_file_writer_v2(982 model_dir, experimental_trackable=True)983 @def_function.function(input_signature=[984 tensor_spec.TensorSpec(shape=[], dtype=dtypes.int64)985 ])986 def train(self, step):987 with self._writer.as_default():988 summary_ops.write('tag', 'foo', step=step)989 return constant_op.constant(0)990 logdir = self.get_temp_dir()991 to_export = Model(logdir)992 pre_save_files = set(events_from_multifile_logdir(logdir))993 export_dir = os.path.join(logdir, 'export')994 saved_model_save.save(995 to_export, export_dir, signatures={'train': to_export.train})996 # Reset context to ensure we don't share any resources with saving code.997 context._reset_context() # pylint: disable=protected-access998 with context.eager_mode():999 restored = saved_model_load.load(export_dir)1000 restored.train(1)1001 restored.train(2)1002 post_restore_files = set(events_from_multifile_logdir(logdir))1003 restored2 = saved_model_load.load(export_dir)1004 restored2.train(3)1005 restored2.train(4)1006 files_to_events = events_from_multifile_logdir(logdir)1007 post_restore2_files = set(files_to_events)1008 self.assertLen(files_to_events, 3)1009 def unwrap_singleton(iterable):1010 self.assertLen(iterable, 1)1011 return next(iter(iterable))1012 restore_file = unwrap_singleton(post_restore_files - pre_save_files)1013 restore2_file = unwrap_singleton(post_restore2_files - post_restore_files)1014 restore_events = files_to_events[restore_file]1015 restore2_events = files_to_events[restore2_file]1016 self.assertLen(restore_events, 3)1017 self.assertEqual(1, restore_events[1].step)1018 self.assertEqual(2, restore_events[2].step)1019 self.assertLen(restore2_events, 3)1020 self.assertEqual(3, restore2_events[1].step)1021 self.assertEqual(4, restore2_events[2].step)1022 def testWriter_savedAsModuleProperty_loadInGraphMode(self):1023 with context.eager_mode():1024 class Model(module.Module):1025 def __init__(self, model_dir):1026 self._writer = summary_ops.create_file_writer_v2(1027 model_dir, experimental_trackable=True)1028 @def_function.function(input_signature=[1029 tensor_spec.TensorSpec(shape=[], dtype=dtypes.int64)1030 ])1031 def train(self, step):1032 with self._writer.as_default():1033 summary_ops.write('tag', 'foo', step=step)1034 return constant_op.constant(0)1035 logdir = self.get_temp_dir()1036 to_export = Model(logdir)1037 pre_save_files = set(events_from_multifile_logdir(logdir))1038 export_dir = os.path.join(logdir, 'export')1039 saved_model_save.save(1040 to_export, export_dir, signatures={'train': to_export.train})1041 # Reset context to ensure we don't share any resources with saving code.1042 context._reset_context() # pylint: disable=protected-access1043 def load_and_run_model(sess, input_values):1044 """Load and run the SavedModel signature in the TF 1.x style."""1045 model = saved_model_loader.load(sess, [tag_constants.SERVING], export_dir)1046 signature = model.signature_def['train']1047 inputs = list(signature.inputs.values())1048 assert len(inputs) == 1, inputs1049 outputs = list(signature.outputs.values())1050 assert len(outputs) == 1, outputs1051 input_tensor = sess.graph.get_tensor_by_name(inputs[0].name)1052 output_tensor = sess.graph.get_tensor_by_name(outputs[0].name)1053 for v in input_values:1054 sess.run(output_tensor, feed_dict={input_tensor: v})1055 with context.graph_mode(), ops.Graph().as_default():1056 # Since writer shared_name is fixed, within a single session, all loads of1057 # this SavedModel will refer to a single writer resouce, so it will be1058 # initialized only once and write to a single file.1059 with self.session() as sess:1060 load_and_run_model(sess, [1, 2])1061 load_and_run_model(sess, [3, 4])1062 post_restore_files = set(events_from_multifile_logdir(logdir))1063 # New session will recreate the resource and write to a second file.1064 with self.session() as sess:1065 load_and_run_model(sess, [5, 6])1066 files_to_events = events_from_multifile_logdir(logdir)1067 post_restore2_files = set(files_to_events)1068 self.assertLen(files_to_events, 3)1069 def unwrap_singleton(iterable):1070 self.assertLen(iterable, 1)1071 return next(iter(iterable))1072 restore_file = unwrap_singleton(post_restore_files - pre_save_files)1073 restore2_file = unwrap_singleton(post_restore2_files - post_restore_files)1074 restore_events = files_to_events[restore_file]1075 restore2_events = files_to_events[restore2_file]1076 self.assertLen(restore_events, 5)1077 self.assertEqual(1, restore_events[1].step)1078 self.assertEqual(2, restore_events[2].step)1079 self.assertEqual(3, restore_events[3].step)1080 self.assertEqual(4, restore_events[4].step)1081 self.assertLen(restore2_events, 3)1082 self.assertEqual(5, restore2_events[1].step)1083 self.assertEqual(6, restore2_events[2].step)1084class NoopWriterTest(test_util.TensorFlowTestCase):1085 def testNoopWriter_doesNothing(self):1086 logdir = self.get_temp_dir()1087 with context.eager_mode():1088 writer = summary_ops.create_noop_writer()1089 writer.init()1090 with writer.as_default():1091 result = summary_ops.write('test', 1.0, step=0)1092 writer.flush()1093 writer.close()1094 self.assertFalse(result) # Should have found no active writer1095 files = gfile.Glob(os.path.join(logdir, '*'))1096 self.assertLen(files, 0)1097 def testNoopWriter_asNestedContext_isTransparent(self):1098 logdir = self.get_temp_dir()1099 with context.eager_mode():1100 writer = summary_ops.create_file_writer_v2(logdir)1101 noop_writer = summary_ops.create_noop_writer()1102 with writer.as_default():1103 result1 = summary_ops.write('first', 1.0, step=0)1104 with noop_writer.as_default():1105 result2 = summary_ops.write('second', 1.0, step=0)1106 result3 = summary_ops.write('third', 1.0, step=0)1107 # All ops should have written, including the one inside the no-op writer,1108 # since it doesn't actively *disable* writing - it just behaves as if that1109 # entire `with` block wasn't there at all.1110 self.assertAllEqual([result1, result2, result3], [True, True, True])1111 def testNoopWriter_setAsDefault(self):1112 try:1113 with context.eager_mode():1114 writer = summary_ops.create_noop_writer()1115 writer.set_as_default()1116 result = summary_ops.write('test', 1.0, step=0)1117 self.assertFalse(result) # Should have found no active writer1118 finally:1119 # Ensure we clean up no matter how the test executes.1120 summary_ops._summary_state.writer = None # pylint: disable=protected-access1121class SummaryOpsTest(test_util.TensorFlowTestCase):1122 def tearDown(self):1123 summary_ops.trace_off()1124 super(SummaryOpsTest, self).tearDown()1125 def exec_summary_op(self, summary_op_fn):1126 assert context.executing_eagerly()1127 logdir = self.get_temp_dir()1128 writer = summary_ops.create_file_writer_v2(logdir)1129 with writer.as_default():1130 summary_op_fn()1131 writer.close()1132 events = events_from_logdir(logdir)1133 return events[1]1134 def run_metadata(self, *args, **kwargs):1135 assert context.executing_eagerly()1136 logdir = self.get_temp_dir()1137 writer = summary_ops.create_file_writer_v2(logdir)1138 with writer.as_default():1139 summary_ops.run_metadata(*args, **kwargs)1140 writer.close()1141 events = events_from_logdir(logdir)1142 return events[1]1143 def run_metadata_graphs(self, *args, **kwargs):1144 assert context.executing_eagerly()1145 logdir = self.get_temp_dir()1146 writer = summary_ops.create_file_writer_v2(logdir)1147 with writer.as_default():1148 summary_ops.run_metadata_graphs(*args, **kwargs)1149 writer.close()1150 events = events_from_logdir(logdir)1151 return events[1]1152 def create_run_metadata(self):1153 step_stats = step_stats_pb2.StepStats(dev_stats=[1154 step_stats_pb2.DeviceStepStats(1155 device='cpu:0',1156 node_stats=[step_stats_pb2.NodeExecStats(node_name='hello')])1157 ])1158 return config_pb2.RunMetadata(1159 function_graphs=[1160 config_pb2.RunMetadata.FunctionGraphs(1161 pre_optimization_graph=graph_pb2.GraphDef(1162 node=[node_def_pb2.NodeDef(name='foo')]))1163 ],1164 step_stats=step_stats)1165 def run_trace(self, f, step=1):1166 assert context.executing_eagerly()1167 logdir = self.get_temp_dir()1168 writer = summary_ops.create_file_writer_v2(logdir)1169 summary_ops.trace_on(graph=True, profiler=False)1170 with writer.as_default():1171 f()1172 summary_ops.trace_export(name='foo', step=step)1173 writer.close()1174 events = events_from_logdir(logdir)1175 return events[1]1176 @test_util.run_v2_only1177 def testRunMetadata_usesNameAsTag(self):1178 meta = config_pb2.RunMetadata()1179 with ops.name_scope('foo', skip_on_eager=False):1180 event = self.run_metadata(name='my_name', data=meta, step=1)1181 first_val = event.summary.value[0]1182 self.assertEqual('foo/my_name', first_val.tag)1183 @test_util.run_v2_only1184 def testRunMetadata_summaryMetadata(self):1185 expected_summary_metadata = """1186 plugin_data {1187 plugin_name: "graph_run_metadata"1188 content: "1"1189 }1190 """1191 meta = config_pb2.RunMetadata()1192 event = self.run_metadata(name='my_name', data=meta, step=1)1193 actual_summary_metadata = event.summary.value[0].metadata1194 self.assertProtoEquals(expected_summary_metadata, actual_summary_metadata)1195 @test_util.run_v2_only1196 def testRunMetadata_wholeRunMetadata(self):1197 expected_run_metadata = """1198 step_stats {1199 dev_stats {1200 device: "cpu:0"1201 node_stats {1202 node_name: "hello"1203 }1204 }1205 }1206 function_graphs {1207 pre_optimization_graph {1208 node {1209 name: "foo"1210 }1211 }1212 }1213 """1214 meta = self.create_run_metadata()1215 event = self.run_metadata(name='my_name', data=meta, step=1)1216 first_val = event.summary.value[0]1217 actual_run_metadata = config_pb2.RunMetadata.FromString(1218 first_val.tensor.string_val[0])1219 self.assertProtoEquals(expected_run_metadata, actual_run_metadata)1220 @test_util.run_v2_only1221 def testRunMetadata_usesDefaultStep(self):1222 meta = config_pb2.RunMetadata()1223 try:1224 summary_ops.set_step(42)1225 event = self.run_metadata(name='my_name', data=meta)1226 self.assertEqual(42, event.step)1227 finally:1228 # Reset to default state for other tests.1229 summary_ops.set_step(None)1230 @test_util.run_v2_only1231 def testRunMetadataGraph_usesNameAsTag(self):1232 meta = config_pb2.RunMetadata()1233 with ops.name_scope('foo', skip_on_eager=False):1234 event = self.run_metadata_graphs(name='my_name', data=meta, step=1)1235 first_val = event.summary.value[0]1236 self.assertEqual('foo/my_name', first_val.tag)1237 @test_util.run_v2_only1238 def testRunMetadataGraph_summaryMetadata(self):1239 expected_summary_metadata = """1240 plugin_data {1241 plugin_name: "graph_run_metadata_graph"1242 content: "1"1243 }1244 """1245 meta = config_pb2.RunMetadata()1246 event = self.run_metadata_graphs(name='my_name', data=meta, step=1)1247 actual_summary_metadata = event.summary.value[0].metadata1248 self.assertProtoEquals(expected_summary_metadata, actual_summary_metadata)1249 @test_util.run_v2_only1250 def testRunMetadataGraph_runMetadataFragment(self):1251 expected_run_metadata = """1252 function_graphs {1253 pre_optimization_graph {1254 node {1255 name: "foo"1256 }1257 }1258 }1259 """1260 meta = self.create_run_metadata()1261 event = self.run_metadata_graphs(name='my_name', data=meta, step=1)1262 first_val = event.summary.value[0]1263 actual_run_metadata = config_pb2.RunMetadata.FromString(1264 first_val.tensor.string_val[0])1265 self.assertProtoEquals(expected_run_metadata, actual_run_metadata)1266 @test_util.run_v2_only1267 def testRunMetadataGraph_usesDefaultStep(self):1268 meta = config_pb2.RunMetadata()1269 try:1270 summary_ops.set_step(42)1271 event = self.run_metadata_graphs(name='my_name', data=meta)1272 self.assertEqual(42, event.step)1273 finally:1274 # Reset to default state for other tests.1275 summary_ops.set_step(None)1276 @test_util.run_v2_only1277 def testTrace(self):1278 @def_function.function1279 def f():1280 x = constant_op.constant(2)1281 y = constant_op.constant(3)1282 return x**y1283 event = self.run_trace(f)1284 first_val = event.summary.value[0]1285 actual_run_metadata = config_pb2.RunMetadata.FromString(1286 first_val.tensor.string_val[0])1287 # Content of function_graphs is large and, for instance, device can change.1288 self.assertTrue(hasattr(actual_run_metadata, 'function_graphs'))1289 @test_util.run_v2_only1290 def testTrace_cannotEnableTraceInFunction(self):1291 @def_function.function1292 def f():1293 summary_ops.trace_on(graph=True, profiler=False)1294 x = constant_op.constant(2)1295 y = constant_op.constant(3)1296 return x**y1297 with test.mock.patch.object(logging, 'warn') as mock_log:1298 f()1299 self.assertRegex(1300 str(mock_log.call_args), 'Cannot enable trace inside a tf.function.')1301 @test_util.run_v2_only1302 def testTrace_cannotEnableTraceInGraphMode(self):1303 with test.mock.patch.object(logging, 'warn') as mock_log:1304 with context.graph_mode():1305 summary_ops.trace_on(graph=True, profiler=False)1306 self.assertRegex(1307 str(mock_log.call_args), 'Must enable trace in eager mode.')1308 @test_util.run_v2_only1309 def testTrace_cannotExportTraceWithoutTrace(self):1310 with six.assertRaisesRegex(self, ValueError,1311 'Must enable trace before export.'):1312 summary_ops.trace_export(name='foo', step=1)1313 @test_util.run_v2_only1314 def testTrace_cannotExportTraceInFunction(self):1315 summary_ops.trace_on(graph=True, profiler=False)1316 @def_function.function1317 def f():1318 x = constant_op.constant(2)1319 y = constant_op.constant(3)1320 summary_ops.trace_export(name='foo', step=1)1321 return x**y1322 with test.mock.patch.object(logging, 'warn') as mock_log:1323 f()1324 self.assertRegex(1325 str(mock_log.call_args), 'Cannot export trace inside a tf.function.')1326 @test_util.run_v2_only1327 def testTrace_cannotExportTraceInGraphMode(self):1328 with test.mock.patch.object(logging, 'warn') as mock_log:1329 with context.graph_mode():1330 summary_ops.trace_export(name='foo', step=1)1331 self.assertRegex(1332 str(mock_log.call_args),1333 'Can only export trace while executing eagerly.')1334 @test_util.run_v2_only1335 def testTrace_usesDefaultStep(self):1336 @def_function.function1337 def f():1338 x = constant_op.constant(2)1339 y = constant_op.constant(3)1340 return x**y1341 try:1342 summary_ops.set_step(42)1343 event = self.run_trace(f, step=None)1344 self.assertEqual(42, event.step)1345 finally:1346 # Reset to default state for other tests.1347 summary_ops.set_step(None)1348 @test_util.run_v2_only1349 def testTrace_withProfiler(self):1350 @def_function.function1351 def f():1352 x = constant_op.constant(2)1353 y = constant_op.constant(3)1354 return x**y1355 assert context.executing_eagerly()1356 logdir = self.get_temp_dir()1357 writer = summary_ops.create_file_writer_v2(logdir)1358 summary_ops.trace_on(graph=True, profiler=True)1359 profiler_outdir = self.get_temp_dir()1360 with writer.as_default():1361 f()1362 summary_ops.trace_export(1363 name='foo', step=1, profiler_outdir=profiler_outdir)1364 writer.close()1365 @test_util.run_v2_only1366 def testGraph_graph(self):1367 @def_function.function1368 def f():1369 x = constant_op.constant(2)1370 y = constant_op.constant(3)1371 return x**y1372 def summary_op_fn():1373 summary_ops.graph(f.get_concrete_function().graph)1374 event = self.exec_summary_op(summary_op_fn)1375 self.assertIsNotNone(event.graph_def)1376 @test_util.run_v2_only1377 def testGraph_graphDef(self):1378 @def_function.function1379 def f():1380 x = constant_op.constant(2)1381 y = constant_op.constant(3)1382 return x**y1383 def summary_op_fn():1384 summary_ops.graph(f.get_concrete_function().graph.as_graph_def())1385 event = self.exec_summary_op(summary_op_fn)1386 self.assertIsNotNone(event.graph_def)1387 @test_util.run_v2_only1388 def testGraph_invalidData(self):1389 def summary_op_fn():1390 summary_ops.graph('hello')1391 with self.assertRaisesRegex(1392 ValueError,1393 r'\'graph_data\' is not tf.Graph or tf.compat.v1.GraphDef',1394 ):1395 self.exec_summary_op(summary_op_fn)1396 @test_util.run_v2_only1397 def testGraph_fromGraphMode(self):1398 @def_function.function1399 def f():1400 x = constant_op.constant(2)1401 y = constant_op.constant(3)1402 return x**y1403 @def_function.function1404 def g(graph):1405 summary_ops.graph(graph)1406 def summary_op_fn():1407 graph_def = f.get_concrete_function().graph.as_graph_def(add_shapes=True)1408 func_graph = constant_op.constant(graph_def.SerializeToString())1409 g(func_graph)1410 with self.assertRaisesRegex(1411 ValueError,1412 r'graph\(\) cannot be invoked inside a graph context.',1413 ):1414 self.exec_summary_op(summary_op_fn)1415def events_from_file(filepath):1416 """Returns all events in a single event file.1417 Args:1418 filepath: Path to the event file.1419 Returns:1420 A list of all tf.Event protos in the event file.1421 """1422 records = list(tf_record.tf_record_iterator(filepath))1423 result = []1424 for r in records:1425 event = event_pb2.Event()1426 event.ParseFromString(r)1427 result.append(event)1428 return result1429def events_from_logdir(logdir):1430 """Returns all events in the single eventfile in logdir.1431 Args:1432 logdir: The directory in which the single event file is sought.1433 Returns:1434 A list of all tf.Event protos from the single event file.1435 Raises:1436 AssertionError: If logdir does not contain exactly one file.1437 """1438 assert gfile.Exists(logdir)1439 files = gfile.ListDirectory(logdir)1440 assert len(files) == 1, 'Found not exactly one file in logdir: %s' % files1441 return events_from_file(os.path.join(logdir, files[0]))1442def events_from_multifile_logdir(logdir):1443 """Returns map of filename to events for all `tfevents` files in the logdir.1444 Args:1445 logdir: The directory from which to load events.1446 Returns:1447 A dict mapping from relative filenames to lists of tf.Event protos.1448 Raises:1449 AssertionError: If logdir does not contain exactly one file.1450 """1451 assert gfile.Exists(logdir)1452 files = [file for file in gfile.ListDirectory(logdir) if 'tfevents' in file]1453 return {file: events_from_file(os.path.join(logdir, file)) for file in files}1454def to_numpy(summary_value):1455 return tensor_util.MakeNdarray(summary_value.tensor)1456if __name__ == '__main__':...
monkeypatch.py
Source:monkeypatch.py
...108 "Found log directory outside of given root_logdir, "109 f"dropping given root_logdir for event file in {logdir}"110 )111 root_logdir_arg = ""112 _notify_tensorboard_logdir(logdir, save=save, root_logdir=root_logdir_arg)113 return old_csfw_func(*args, **kwargs)114 writer.orig_create_summary_file_writer = old_csfw_func115 writer.create_summary_file_writer = new_csfw_func116 wandb.patched["tensorboard"].append([module, "create_summary_file_writer"])117def _patch_file_writer(118 writer: Any,119 module: Any,120 save: bool = True,121 root_logdir: str = "",122) -> None:123 # This configures non-TensorFlow Tensorboard logging, or tensorflow <= 1.15124 logdir_hist = []125 class TBXEventFileWriter(writer.EventFileWriter): # type: ignore126 def __init__(self, logdir: str, *args: Any, **kwargs: Any) -> None:127 logdir_hist.append(logdir)128 root_logdir_arg = root_logdir129 if len(set(logdir_hist)) > 1 and root_logdir == "":130 wandb.termwarn(131 "When using several event log directories, "132 'please call `wandb.tensorboard.patch(root_logdir="...")` before `wandb.init`'133 )134 # if the logdir contains the hostname, the writer was not given a logdir.135 # In this case, the logdir is generated and ends with the hostname,136 # update the root_logdir to match.137 hostname = socket.gethostname()138 search = re.search(rf"-\d+_{hostname}", logdir)139 if search:140 root_logdir_arg = logdir[: search.span()[1]]141 elif root_logdir is not None and not os.path.abspath(logdir).startswith(142 os.path.abspath(root_logdir)143 ):144 wandb.termwarn(145 "Found log directory outside of given root_logdir, "146 f"dropping given root_logdir for event file in {logdir}"147 )148 root_logdir_arg = ""149 _notify_tensorboard_logdir(logdir, save=save, root_logdir=root_logdir_arg)150 super().__init__(logdir, *args, **kwargs)151 writer.orig_EventFileWriter = writer.EventFileWriter152 writer.EventFileWriter = TBXEventFileWriter153 wandb.patched["tensorboard"].append([module, "EventFileWriter"])154def _notify_tensorboard_logdir(155 logdir: str, save: bool = True, root_logdir: str = ""156) -> None:157 if wandb.run is not None:...
logging.py
Source:logging.py
1import os2import datetime3from util.meters import set_summary_writer4from util.distributed import master_only_print as print5from util.distributed import master_only6def get_date_uid():7 """Generate a unique id based on date.8 Returns:9 str: Return uid string, e.g. '20171122171307111552'.10 """11 return str(datetime.datetime.now().strftime("%Y_%m%d_%H%M_%S"))12def init_logging(opt):13 date_uid = get_date_uid()14 if opt.name is not None:15 logdir = os.path.join(opt.checkpoints_dir, opt.name)16 else:17 logdir = os.path.join(opt.checkpoints_dir, date_uid)18 opt.logdir = logdir19 return date_uid, logdir20 21@master_only22def make_logging_dir(logdir, date_uid):23 r"""Create the logging directory24 Args:25 logdir (str): Log directory name26 """27 28 print('Make folder {}'.format(logdir))29 os.makedirs(logdir, exist_ok=True)30 tensorboard_dir = os.path.join(logdir, 'tensorboard')31 image_dir = os.path.join(logdir, 'image')32 eval_dir = os.path.join(logdir, 'evaluation')33 os.makedirs(tensorboard_dir, exist_ok=True)34 os.makedirs(image_dir, exist_ok=True)35 os.makedirs(eval_dir, exist_ok=True)36 set_summary_writer(tensorboard_dir)37 loss_log_name = os.path.join(logdir, 'loss_log.txt')38 with open(loss_log_name, "a") as log_file:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!