def add_logs(self, **kwargs): if 'logfile' in kwargs: if 'name' in kwargs: log_name = str(kwargs['name']) else: log_name = kwargs['logfile'] event_acc = event_accumulator.EventAccumulator( kwargs['logfile'], self.tf_size_guidance) event_acc.Reload() self.event_accs.append(event_acc) self.log_file_names.append( (kwargs["logfile"], cycle_num, kwargs["logfile"])) elif 'logdir' in kwargs: idx = -1 for root, dirs, files in os.walk(kwargs['logdir']): for file in files: if "events.out" in file: idx += 1 filename = os.path.join(root, file) cycle_num = 0 if 'name' in kwargs: if "cycles_named" in kwargs: cycles_named = kwargs["cycles_named"] else: cycles_named = False if cycles_named: try: found = re.search("cycle_\d", filename) cycle_num = str(found[0].rstrip()[-1]) log_name = kwargs['name'] except: raise ValueError( "Logfile: {} does not contain cycle num" .format(filename)) else: cycle_num = str(idx) log_name = kwargs['name'] else: log_name = filename event_acc = event_accumulator.EventAccumulator( filename, self.tf_size_guidance) self.event_accs.append(event_acc) self.log_file_names.append( (log_name, cycle_num, filename)) if idx >= 0: print("I added " + str(idx + 1) + " different logfiles: " + filename) else: print("No logfiles found") tags = [] for event_acc in self.event_accs: tags.append(event_acc.Tags()) return tags
def testReload(self): gen = _EventGenerator() acc = ea.EventAccumulator(gen) acc.Reload() self.assertEqual(acc.Tags(), self.empty) gen.AddScalar('s1') gen.AddScalar('s2') gen.AddHistogram('hst1') gen.AddHistogram('hst2') gen.AddImage('im1') gen.AddImage('im2') gen.AddAudio('snd1') gen.AddAudio('snd2') acc.Reload() self.assertTagsEqual( acc.Tags(), { ea.IMAGES: ['im1', 'im2'], ea.AUDIO: ['snd1', 'snd2'], ea.SCALARS: ['s1', 's2'], ea.HISTOGRAMS: ['hst1', 'hst2'], ea.COMPRESSED_HISTOGRAMS: ['hst1', 'hst2'], ea.GRAPH: False, ea.META_GRAPH: False, ea.RUN_METADATA: [] })
def testImages(self): gen = _EventGenerator() acc = ea.EventAccumulator(gen) im1 = ea.ImageEvent(wall_time=1, step=10, encoded_image_string=b'big', width=400, height=300) im2 = ea.ImageEvent(wall_time=2, step=12, encoded_image_string=b'small', width=40, height=30) gen.AddImage('im1', wall_time=1, step=10, encoded_image_string=b'big', width=400, height=300) gen.AddImage('im2', wall_time=2, step=12, encoded_image_string=b'small', width=40, height=30) acc.Reload() self.assertEqual(acc.Images('im1'), [im1]) self.assertEqual(acc.Images('im2'), [im2])
def testFirstEventTimestamp(self): """Test that FirstEventTimestamp() returns wall_time of the first event.""" gen = _EventGenerator() acc = ea.EventAccumulator(gen) gen.AddEvent(tf.Event(wall_time=10, step=20, file_version='brain.Event:2')) gen.AddScalar('s1', wall_time=30, step=40, value=20) self.assertEqual(acc.FirstEventTimestamp(), 10)
def testSessionLogStartMessageDiscardsExpiredEvents(self): """Test that SessionLog.START message discards expired events. This discard logic is preferred over the out-of-order step discard logic, but this logic can only be used for event protos which have the SessionLog enum, which was introduced to event.proto for file_version >= brain.Event:2. """ gen = _EventGenerator() acc = ea.EventAccumulator(gen) gen.AddEvent( tf.Event(wall_time=0, step=1, file_version='brain.Event:2')) gen.AddScalar('s1', wall_time=1, step=100, value=20) gen.AddScalar('s1', wall_time=1, step=200, value=20) gen.AddScalar('s1', wall_time=1, step=300, value=20) gen.AddScalar('s1', wall_time=1, step=400, value=20) gen.AddScalar('s2', wall_time=1, step=202, value=20) gen.AddScalar('s2', wall_time=1, step=203, value=20) slog = SessionLog(status=SessionLog.START) gen.AddEvent(tf.Event(wall_time=2, step=201, session_log=slog)) acc.Reload() self.assertEqual([x.step for x in acc.Scalars('s1')], [100, 200]) self.assertEqual([x.step for x in acc.Scalars('s2')], [])
def testEventsDiscardedPerTagAfterRestartForFileVersionLessThan2(self): """Tests that event discards after restart, only affect the misordered tag. If a step value is observed to be lower than what was previously seen, this should force a discard of all previous items that are outdated, but only for the out of order tag. Other tags should remain unaffected. Only file versions < 2 use this out-of-order discard logic. Later versions discard events based on the step value of SessionLog.START. """ warnings = [] self.stubs.Set(logging, 'warn', warnings.append) gen = _EventGenerator() acc = ea.EventAccumulator(gen) gen.AddEvent(tf.Event(wall_time=0, step=0, file_version='brain.Event:1')) gen.AddScalar('s1', wall_time=1, step=100, value=20) gen.AddScalar('s1', wall_time=1, step=200, value=20) gen.AddScalar('s1', wall_time=1, step=300, value=20) gen.AddScalar('s1', wall_time=1, step=101, value=20) gen.AddScalar('s1', wall_time=1, step=201, value=20) gen.AddScalar('s1', wall_time=1, step=301, value=20) gen.AddScalar('s2', wall_time=1, step=101, value=20) gen.AddScalar('s2', wall_time=1, step=201, value=20) gen.AddScalar('s2', wall_time=1, step=301, value=20) acc.Reload() ## Check that we have discarded 200 and 300 self.assertEqual([x.step for x in acc.Scalars('s1')], [100, 101, 201, 301]) ## Check that s1 discards do not affect s2 ## i.e. check that only events from the out of order tag are discarded self.assertEqual([x.step for x in acc.Scalars('s2')], [101, 201, 301])
def testCompressedHistogramsWithEmptyHistogram(self): gen = _EventGenerator() acc = ea.EventAccumulator(gen, compression_bps=(0, 2500, 5000, 7500, 10000)) gen.AddHistogram('hst1', wall_time=1, step=10, hmin=None, hmax=None, hnum=0, hsum=0, hsum_squares=0, hbucket_limit=[1, 2, 3], hbucket=[0, 0, 0]) acc.Reload() # Create the expected values after compressing hst1 expected_vals1 = [ ea.CompressedHistogramValue(bp, val) for bp, val in [(0, 0.0), (2500, 0), (5000, 0), (7500, 0), (10000, 0)] ] expected_cmphst1 = ea.CompressedHistogramEvent( wall_time=1, step=10, compressed_histogram_values=expected_vals1) self.assertEqual(acc.CompressedHistograms('hst1'), [expected_cmphst1])
def testAudio(self): gen = _EventGenerator() acc = ea.EventAccumulator(gen) snd1 = ea.AudioEvent(wall_time=1, step=10, encoded_audio_string=b'big', content_type='audio/wav', sample_rate=44100, length_frames=441000) snd2 = ea.AudioEvent(wall_time=2, step=12, encoded_audio_string=b'small', content_type='audio/wav', sample_rate=44100, length_frames=44100) gen.AddAudio('snd1', wall_time=1, step=10, encoded_audio_string=b'big', content_type='audio/wav', sample_rate=44100, length_frames=441000) gen.AddAudio('snd2', wall_time=2, step=12, encoded_audio_string=b'small', content_type='audio/wav', sample_rate=44100, length_frames=44100) acc.Reload() self.assertEqual(acc.Audio('snd1'), [snd1]) self.assertEqual(acc.Audio('snd2'), [snd2])
def testTFSummaryTensor(self): """Verify processing of tf.summary.tensor.""" event_sink = _EventGenerator(self, zero_out_timestamps=True) writer = SummaryToEventTransformer(event_sink) with self.test_session() as sess: summary_lib.tensor_summary('scalar', constant_op.constant(1.0)) summary_lib.tensor_summary('vector', constant_op.constant([1.0, 2.0, 3.0])) summary_lib.tensor_summary('string', constant_op.constant(six.b('foobar'))) merged = summary_lib.merge_all() summ = sess.run(merged) writer.add_summary(summ, 0) accumulator = ea.EventAccumulator(event_sink) accumulator.Reload() self.assertTagsEqual(accumulator.Tags(), { ea.TENSORS: ['scalar', 'vector', 'string'], }) scalar_proto = accumulator.Tensors('scalar')[0].tensor_proto scalar = tensor_util.MakeNdarray(scalar_proto) vector_proto = accumulator.Tensors('vector')[0].tensor_proto vector = tensor_util.MakeNdarray(vector_proto) string_proto = accumulator.Tensors('string')[0].tensor_proto string = tensor_util.MakeNdarray(string_proto) self.assertTrue(np.array_equal(scalar, 1.0)) self.assertTrue(np.array_equal(vector, [1.0, 2.0, 3.0])) self.assertTrue(np.array_equal(string, six.b('foobar')))
def testGraphFromMetaGraphBecomesAvailable(self): """Test accumulator by writing values and then reading them.""" directory = os.path.join(self.get_temp_dir(), 'metagraph_test_values_dir') if gfile.IsDirectory(directory): gfile.DeleteRecursively(directory) gfile.MkDir(directory) writer = writer_lib.FileWriter(directory, max_queue=100) with ops.Graph().as_default() as graph: _ = constant_op.constant([2.0, 1.0]) # Add a graph to the summary writer. meta_graph_def = saver.export_meta_graph(graph_def=graph.as_graph_def( add_shapes=True)) writer.add_meta_graph(meta_graph_def) writer.flush() # Verify that we can load those events properly acc = ea.EventAccumulator(directory) acc.Reload() self.assertTagsEqual(acc.Tags(), { ea.GRAPH: True, ea.META_GRAPH: True, }) self.assertProtoEquals(graph.as_graph_def(add_shapes=True), acc.Graph()) self.assertProtoEquals(meta_graph_def, acc.MetaGraph())
def testTFSummaryImage(self): """Verify processing of tf.summary.image.""" event_sink = _EventGenerator(self, zero_out_timestamps=True) writer = SummaryToEventTransformer(event_sink) with self.test_session() as sess: ipt = array_ops.ones([10, 4, 4, 3], dtypes.uint8) # This is an interesting example, because the old tf.image_summary op # would throw an error here, because it would be tag reuse. # Using the tf node name instead allows argument re-use to the image # summary. with ops.name_scope('1'): summary_lib.image('images', ipt, max_outputs=1) with ops.name_scope('2'): summary_lib.image('images', ipt, max_outputs=2) with ops.name_scope('3'): summary_lib.image('images', ipt, max_outputs=3) merged = summary_lib.merge_all() writer.add_graph(sess.graph) for i in xrange(10): summ = sess.run(merged) writer.add_summary(summ, global_step=i) accumulator = ea.EventAccumulator(event_sink) accumulator.Reload() tags = [ u'1/images/image', u'2/images/image/0', u'2/images/image/1', u'3/images/image/0', u'3/images/image/1', u'3/images/image/2' ] self.assertTagsEqual(accumulator.Tags(), { ea.IMAGES: tags, ea.GRAPH: True, ea.META_GRAPH: False, })
def testExpiredDataDiscardedAfterRestartForFileVersionLessThan2(self): """Tests that events are discarded after a restart is detected. If a step value is observed to be lower than what was previously seen, this should force a discard of all previous items with the same tag that are outdated. Only file versions < 2 use this out-of-order discard logic. Later versions discard events based on the step value of SessionLog.START. """ warnings = [] self.stubs.Set(logging, 'warn', warnings.append) gen = _EventGenerator() acc = ea.EventAccumulator(gen) gen.AddEvent(tf.Event(wall_time=0, step=0, file_version='brain.Event:1')) gen.AddScalar('s1', wall_time=1, step=100, value=20) gen.AddScalar('s1', wall_time=1, step=200, value=20) gen.AddScalar('s1', wall_time=1, step=300, value=20) acc.Reload() ## Check that number of items are what they should be self.assertEqual([x.step for x in acc.Scalars('s1')], [100, 200, 300]) gen.AddScalar('s1', wall_time=1, step=101, value=20) gen.AddScalar('s1', wall_time=1, step=201, value=20) gen.AddScalar('s1', wall_time=1, step=301, value=20) acc.Reload() ## Check that we have discarded 200 and 300 from s1 self.assertEqual([x.step for x in acc.Scalars('s1')], [100, 101, 201, 301])
def testEventsDiscardedPerTagAfterRestart(self): """Tests that event discards after restart, only affect the misordered tag. If a step value is observed to be lower than what was previously seen, this should force a discard of all previous items that are outdated, but only for the out of order tag. Other tags should remain unaffected. """ warnings = [] self.stubs.Set(logging, 'warn', warnings.append) gen = _EventGenerator() acc = ea.EventAccumulator(gen) gen.AddScalar('s1', wall_time=1, step=100, value=20) gen.AddScalar('s1', wall_time=1, step=200, value=20) gen.AddScalar('s1', wall_time=1, step=300, value=20) gen.AddScalar('s1', wall_time=1, step=101, value=20) gen.AddScalar('s1', wall_time=1, step=201, value=20) gen.AddScalar('s1', wall_time=1, step=301, value=20) gen.AddScalar('s2', wall_time=1, step=101, value=20) gen.AddScalar('s2', wall_time=1, step=201, value=20) gen.AddScalar('s2', wall_time=1, step=301, value=20) acc.Reload() ## Check that we have discarded 200 and 300 self.assertEqual([x.step for x in acc.Scalars('s1')], [100, 101, 201, 301]) ## Check that s1 discards do not affect s2 ## i.e. check that only events from the out of order tag are discarded self.assertEqual([x.step for x in acc.Scalars('s2')], [101, 201, 301])
def testCompressedHistograms(self): gen = _EventGenerator() acc = ea.EventAccumulator(gen, compression_bps=(0, 2500, 5000, 7500, 10000)) gen.AddHistogram('hst1', wall_time=1, step=10, hmin=1, hmax=2, hnum=3, hsum=4, hsum_squares=5, hbucket_limit=[1, 2, 3], hbucket=[0, 3, 0]) gen.AddHistogram('hst2', wall_time=2, step=12, hmin=-2, hmax=3, hnum=4, hsum=5, hsum_squares=6, hbucket_limit=[2, 3, 4], hbucket=[1, 3, 0]) acc.Reload() # Create the expected values after compressing hst1 expected_vals1 = [ea.CompressedHistogramValue(bp, val) for bp, val in [( 0, 1.0), (2500, 1.25), (5000, 1.5), (7500, 1.75), (10000, 2.0)]] expected_cmphst1 = ea.CompressedHistogramEvent( wall_time=1, step=10, compressed_histogram_values=expected_vals1) self.assertEqual(acc.CompressedHistograms('hst1'), [expected_cmphst1]) # Create the expected values after compressing hst2 expected_vals2 = [ ea.CompressedHistogramValue(bp, val) for bp, val in [(0, -2), (2500, 2), (5000, 2 + 1 / 3), (7500, 2 + 2 / 3 ), (10000, 3)] ] expected_cmphst2 = ea.CompressedHistogramEvent( wall_time=2, step=12, compressed_histogram_values=expected_vals2) self.assertEqual(acc.CompressedHistograms('hst2'), [expected_cmphst2])
def _fetch_result(self, exp_path, last_accessed): """Fetches the most up to date results if last_accessed is earlier than the events file timestamp. Args: exp_path (string): the path to experiment directory last_accessed (float): the time in seconds since file was last accessed, None for never Returns: bool: if the result was accessed again ResultValue: the new result if accessed, otherwise None float: the new last accessed time """ from tensorflow.python.summary import event_accumulator as ea KEYS = [ u'VALID_denoAcc_silent_1utts_1', u'VALID_denoAcc_silent_2utts_1', u'VALID_denoAcc_silent_3utts_1', u'VALID_denoAcc_silent_4utts_1', u'VALID_denoAcc_silent_5utts_1', u'FINAL_denoAcc_silent_1utts_1', u'FINAL_denoAcc_silent_2utts_1', u'FINAL_denoAcc_silent_3utts_1', u'FINAL_denoAcc_silent_4utts_1', u'FINAL_denoAcc_silent_5utts_1', ] events_file = exp_path + "/tensorboard" # Last accessed is up to date if (last_accessed is not None and os.path.getmtime(exp_path) <= last_accessed): return False, None, 0 last_accessed = time.time() print >> sys.stderr, 'Reading from', events_file, \ '(could take a while ...)' acc = ea.EventAccumulator(events_file, size_guidance={ea.SCALARS: 0}) acc.Reload() available_keys = set(acc.Tags()['scalars']) values = [] for key in KEYS: # Key not available to load yet if key not in available_keys: warn("No results found for {}".format(exp_path)) print "Perhaps your job has died?" return False, None, None if key in available_keys: values.append([scalar.value for scalar in acc.Scalars(key)]) values = zip(*values) if len(values) == 0: assert False best_index, best_value = max([(i, sum(value)) for i, value in enumerate(values)], key=lambda x: x[1]) return True, ResultValue(list(values[best_index][:5]), list(values[best_index][5:])), last_accessed
def plot(params): ''' beautify tf log Use better library (seaborn) to plot tf event file''' log_path = params['logdir'] all_workers = glob.glob(os.path.join(log_path, 'train_*')) smooth_space = int(params['smooth']) color_code = params['color'] tag = params['var'] maxstep = params['maxstep'] x_raw = [] y_raw = [] for worker in all_workers: print(worker) acc = ea.EventAccumulator(worker) acc.Reload() for s in acc.Scalars(tag): if not maxstep or s.step < maxstep: x_raw.append(s.step) y_raw.append(s.value) sorted_xy = sorted(zip(x_raw, y_raw), key=lambda xy: xy[0]) x_raw, y_raw = zip(*sorted_xy) # smooth curve x_smooth = [] y_smooth = [] for i in range(0, len(x_raw), smooth_space): if i + 2 * smooth_space < len(x_raw): x_smooth.append(x_raw[i]) y_smooth.append( sum(y_raw[i:i + smooth_space]) / float(smooth_space)) elif i + smooth_space < len(x_raw): x_smooth.append(x_raw[i]) y_smooth.append(sum(y_raw[i:]) / float(len(x_raw) - i)) x_raw = [x * 1e-6 for x in x_raw] x_smooth = [x * 1e-6 for x in x_smooth] plt.figure() plt.subplot(111) plt.title(params['title'], fontsize=16) plt.plot(x_raw, y_raw, color=colors.to_rgba(color_code, alpha=0.4)) plt.plot(x_smooth, y_smooth, color=color_code, linewidth=1.5) plt.xlabel('Number of Global Training Steps (in millions)', fontsize=14) plt.ylabel('Intrinsic Reward' if params['intrinsic'] else 'Episode Reward', fontsize=14) plt.xticks(fontsize=12) plt.yticks(fontsize=12) #plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0)) plt.gca().xaxis.offsetText.set_fontsize(12) plt.savefig(params['filename']) plt.show()
def testFirstEventTimestampLoadsEvent(self): """Test that FirstEventTimestamp() doesn't discard the loaded event.""" gen = _EventGenerator() acc = ea.EventAccumulator(gen) gen.AddEvent(tf.Event(wall_time=1, step=2, file_version='brain.Event:2')) self.assertEqual(acc.FirstEventTimestamp(), 1) acc.Reload() self.assertEqual(acc.file_version, 2.0)
def testScalars(self): gen = _EventGenerator() acc = ea.EventAccumulator(gen) s1 = ea.ScalarEvent(wall_time=1, step=10, value=32) s2 = ea.ScalarEvent(wall_time=2, step=12, value=64) gen.AddScalar('s1', wall_time=1, step=10, value=32) gen.AddScalar('s2', wall_time=2, step=12, value=64) acc.Reload() self.assertEqual(acc.Scalars('s1'), [s1]) self.assertEqual(acc.Scalars('s2'), [s2])
def testOnlySummaryEventsTriggerDiscards(self): """Test that file version event doesnt trigger data purge.""" gen = _EventGenerator() acc = ea.EventAccumulator(gen) gen.AddScalar('s1', wall_time=1, step=100, value=20) ev1 = tf.Event(wall_time=2, step=0, file_version='0') ev2 = tf.Event(wall_time=3, step=0, graph_def=graph_pb2.GraphDef()) gen.AddEvent(ev1) gen.AddEvent(ev2) acc.Reload() self.assertEqual([x.step for x in acc.Scalars('s1')], [100])
def testActivation(self): gen = _EventGenerator() acc = ea.EventAccumulator(gen) self.assertFalse(acc._activated) with self.assertRaises(RuntimeError): acc.Tags() with self.assertRaises(RuntimeError): acc.Scalars('s1') acc.Reload() self.assertTrue(acc._activated) acc._activated = False
def testReloadPopulatesFirstEventTimestamp(self): """Test that Reload() means FirstEventTimestamp() won't load events.""" gen = _EventGenerator() acc = ea.EventAccumulator(gen) gen.AddEvent(tf.Event(wall_time=1, step=2, file_version='brain.Event:2')) acc.Reload() def _Die(*args, **kwargs): # pylint: disable=unused-argument raise RuntimeError('Load() should not be called') self.stubs.Set(gen, 'Load', _Die) self.assertEqual(acc.FirstEventTimestamp(), 1)
def parse_tag_by_filename(path, logname, tag, filename): ea = event_accumulator.EventAccumulator(os.path.join(path, logname, filename), size_guidance={ # event_accumulator.COMPRESSED_HISTOGRAMS: 0, # 0 = grab all # event_accumulator.IMAGES: 0, # event_accumulator.AUDIO: 0, event_accumulator.SCALARS: 0, # event_accumulator.HISTOGRAMS: 0, }) ea.Reload() try: return ea.Scalars(tag) except: return []
def testHealthPills(self): gen = _EventGenerator(self) acc = ea.EventAccumulator(gen) gen.AddHealthPill(13371337, 41, 'Add', 0, range(1, 13)) gen.AddHealthPill(13381338, 42, 'Add', 1, range(42, 54)) acc = ea.EventAccumulator(gen) acc.Reload() # Retrieve the health pills for each node name. gotten_events = acc.HealthPills('Add') self.assertEquals(2, len(gotten_events)) self._compareHealthPills( ea.HealthPillEvent(wall_time=13371337, step=41, node_name='Add', output_slot=0, value=range(1, 13)), gotten_events[0]) self._compareHealthPills( ea.HealthPillEvent(wall_time=13381338, step=42, node_name='Add', output_slot=1, value=range(42, 54)), gotten_events[1])
def testHistograms(self): gen = _EventGenerator() acc = ea.EventAccumulator(gen) val1 = ea.HistogramValue( min=1, max=2, num=3, sum=4, sum_squares=5, bucket_limit=[1, 2, 3], bucket=[0, 3, 0]) val2 = ea.HistogramValue( min=-2, max=3, num=4, sum=5, sum_squares=6, bucket_limit=[2, 3, 4], bucket=[1, 3, 0]) hst1 = ea.HistogramEvent(wall_time=1, step=10, histogram_value=val1) hst2 = ea.HistogramEvent(wall_time=2, step=12, histogram_value=val2) gen.AddHistogram( 'hst1', wall_time=1, step=10, hmin=1, hmax=2, hnum=3, hsum=4, hsum_squares=5, hbucket_limit=[1, 2, 3], hbucket=[0, 3, 0]) gen.AddHistogram( 'hst2', wall_time=2, step=12, hmin=-2, hmax=3, hnum=4, hsum=5, hsum_squares=6, hbucket_limit=[2, 3, 4], hbucket=[1, 3, 0]) acc.Reload() self.assertEqual(acc.Histograms('hst1'), [hst1]) self.assertEqual(acc.Histograms('hst2'), [hst2])
def logs_to_csv(self): working_dir = os.getcwd() for name in self.all_names: make_dir(self.graph_path + 'seperate_log_csv/' + name) try: if self.mode == 'compare': path = self.log_path + '*' + name + '_t-stamp*' else: path = self.log_path + '*' + name + '*' for folder_path in iglob(path): print('path:', folder_path) csv_name = folder_path.split('\\')[-1] #name = folder_path.split('/')[-1] ea = event_accumulator.EventAccumulator(folder_path) ea.Reload() #self.csv_path_list = [] if self.custom_tags == False: self.tag_list = [] #for tag in self.tag_list: for tag in ea.Tags()['scalars']: try: tag_str = tag.replace(':', '').split(' ')[-1] print(tag_str) #os.chdir(self.graph_path+'seperate_log_csv/'+name+'/') csv_path = self.graph_path + 'seperate_log_csv/' + name + '/' + csv_name + '-tag-' + tag_str + '.csv' #print(pd.DataFrame(ea.Scalars(tag))) pd.DataFrame(ea.Scalars(tag)).to_csv(csv_path) #print(csv_path) #test.to_csv(csv_name+'-tag-'+tag_str+'.csv') #os.chdir(working_dir) if self.custom_tags == False: self.tag_list.append(tag_str) #self.csv_path_list.append(csv_path) except Exception as e: print('Exception:', e) except Exception as e: print('Exception:', e) print('Could not open any log with path:', self.log_path, 'that includes', name)
def plot(params): ''' beautify tf log Use better library (seaborn) to plot tf event file''' log_path = params['logdir'] smooth_space = params['smooth'] color_code = params['color'] acc = ea.EventAccumulator(log_path) acc.Reload() # only support scalar now scalar_list = acc.Tags()['scalars'] x_list = [] y_list = [] x_list_raw = [] y_list_raw = [] for tag in scalar_list: x = [int(s.step) for s in acc.Scalars(tag)] y = [s.value for s in acc.Scalars(tag)] # smooth curve x_ = [] y_ = [] for i in range(0, len(x), smooth_space): x_.append(x[i]) y_.append(sum(y[i:i + smooth_space]) / float(smooth_space)) x_.append(x[-1]) y_.append(y[-1]) x_list.append(x_) y_list.append(y_) # raw curve x_list_raw.append(x) y_list_raw.append(y) for i in range(len(x_list)): plt.figure(i) plt.subplot(111) plt.title(scalar_list[i]) plt.plot(x_list_raw[i], y_list_raw[i], color=colors.to_rgba(color_code, alpha=0.4)) plt.plot(x_list[i], y_list[i], color=color_code, linewidth=1.5) plt.show()
def testTags(self): gen = _EventGenerator() gen.AddScalar('s1') gen.AddScalar('s2') gen.AddHistogram('hst1') gen.AddHistogram('hst2') gen.AddImage('im1') gen.AddImage('im2') acc = ea.EventAccumulator(gen) acc.Reload() self.assertTagsEqual( acc.Tags(), { ea.IMAGES: ['im1', 'im2'], ea.SCALARS: ['s1', 's2'], ea.HISTOGRAMS: ['hst1', 'hst2'], ea.COMPRESSED_HISTOGRAMS: ['hst1', 'hst2'], ea.GRAPH: False})
def eventaccumulator(path, find, savingpath=None): ea = event_accumulator.EventAccumulator(path) ea.Reload() # loads events from file print ea.Tags() # print type(ea.Scalars(find)) # Is list # print (ea.Scalars(find)[0]).step, (ea.Scalars(find)[0]).value print('Get {0} values from {1}'.format(find, path)) values = ['From: ' + path] for item in ea.Scalars(find): step = item.step value = item.value print(step, value) values.append((step, value)) if savingpath: utils.save(values, savingpath, info='Save {0} from {1} to {2}'.format( find, path, savingpath))
def testKeyError(self): gen = _EventGenerator() acc = ea.EventAccumulator(gen) acc.Reload() with self.assertRaises(KeyError): acc.Scalars('s1') with self.assertRaises(KeyError): acc.Scalars('hst1') with self.assertRaises(KeyError): acc.Scalars('im1') with self.assertRaises(KeyError): acc.Histograms('s1') with self.assertRaises(KeyError): acc.Histograms('im1') with self.assertRaises(KeyError): acc.Images('s1') with self.assertRaises(KeyError): acc.Images('hst1')
def testNonValueEvents(self): """Tests that non-value events in the generator don't cause early exits.""" gen = _EventGenerator() acc = ea.EventAccumulator(gen) gen.AddScalar('s1', wall_time=1, step=10, value=20) gen.AddEvent(tf.Event( wall_time=2, step=20, file_version='nots2')) gen.AddScalar('s3', wall_time=3, step=100, value=1) gen.AddHistogram('hst1') gen.AddImage('im1') acc.Reload() self.assertTagsEqual(acc.Tags(), { ea.IMAGES: ['im1'], ea.SCALARS: ['s1', 's3'], ea.HISTOGRAMS: ['hst1'], ea.COMPRESSED_HISTOGRAMS: ['hst1'], ea.GRAPH: False})