|
a |
|
b/logger.py |
|
|
1 |
from io import BytesIO |
|
|
2 |
|
|
|
3 |
import scipy.misc |
|
|
4 |
import tensorflow as tf |
|
|
5 |
|
|
|
6 |
|
|
|
7 |
class Logger(object): |
|
|
8 |
|
|
|
9 |
def __init__(self, log_dir): |
|
|
10 |
self.writer = tf.summary.FileWriter(log_dir) |
|
|
11 |
|
|
|
12 |
def scalar_summary(self, tag, value, step): |
|
|
13 |
summary = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=value)]) |
|
|
14 |
self.writer.add_summary(summary, step) |
|
|
15 |
self.writer.flush() |
|
|
16 |
|
|
|
17 |
def image_summary(self, tag, image, step): |
|
|
18 |
s = BytesIO() |
|
|
19 |
scipy.misc.toimage(image).save(s, format="png") |
|
|
20 |
|
|
|
21 |
# Create an Image object |
|
|
22 |
img_sum = tf.Summary.Image( |
|
|
23 |
encoded_image_string=s.getvalue(), |
|
|
24 |
height=image.shape[0], |
|
|
25 |
width=image.shape[1], |
|
|
26 |
) |
|
|
27 |
|
|
|
28 |
# Create and write Summary |
|
|
29 |
summary = tf.Summary(value=[tf.Summary.Value(tag=tag, image=img_sum)]) |
|
|
30 |
self.writer.add_summary(summary, step) |
|
|
31 |
self.writer.flush() |
|
|
32 |
|
|
|
33 |
def image_list_summary(self, tag, images, step): |
|
|
34 |
if len(images) == 0: |
|
|
35 |
return |
|
|
36 |
img_summaries = [] |
|
|
37 |
for i, img in enumerate(images): |
|
|
38 |
s = BytesIO() |
|
|
39 |
scipy.misc.toimage(img).save(s, format="png") |
|
|
40 |
|
|
|
41 |
# Create an Image object |
|
|
42 |
img_sum = tf.Summary.Image( |
|
|
43 |
encoded_image_string=s.getvalue(), |
|
|
44 |
height=img.shape[0], |
|
|
45 |
width=img.shape[1], |
|
|
46 |
) |
|
|
47 |
|
|
|
48 |
# Create a Summary value |
|
|
49 |
img_summaries.append( |
|
|
50 |
tf.Summary.Value(tag="{}/{}".format(tag, i), image=img_sum) |
|
|
51 |
) |
|
|
52 |
|
|
|
53 |
# Create and write Summary |
|
|
54 |
summary = tf.Summary(value=img_summaries) |
|
|
55 |
self.writer.add_summary(summary, step) |
|
|
56 |
self.writer.flush() |