Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To make the tensorboard writer compatible to tf2 #283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions util/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ def __init__(self, opt):
self.name = opt.name
if self.tf_log:
import tensorflow as tf
from packaging import version
self.tf = tf
self.log_dir = os.path.join(opt.checkpoints_dir, opt.name, 'logs')
self.writer = tf.summary.FileWriter(self.log_dir)
if version.parse(tf.__version__) >= version.parse("2"):
self.writer = tf.summary.create_file_writer(self.log_dir)
else:
self.writer = tf.summary.FileWriter(self.log_dir)

if self.use_html:
self.web_dir = os.path.join(opt.checkpoints_dir, opt.name, 'web')
Expand All @@ -44,14 +48,19 @@ def display_current_results(self, visuals, epoch, step):
except:
s = BytesIO()
scipy.misc.toimage(image_numpy).save(s, format="jpeg")
# Create an Image object
img_sum = self.tf.Summary.Image(encoded_image_string=s.getvalue(), height=image_numpy.shape[0], width=image_numpy.shape[1])
# Create a Summary value
img_summaries.append(self.tf.Summary.Value(tag=label, image=img_sum))
if self.version.parse(self.tf.__version__) >= self.version.parse("2"):
with self.writer.as_default():
self.tf.summary.image(label, np.expand_dims(image_numpy, axis=0), step)
else:
# Create an Image object
img_sum = self.tf.Summary.Image(encoded_image_string=s.getvalue(), height=image_numpy.shape[0], width=image_numpy.shape[1])
# Create a Summary value
img_summaries.append(self.tf.Summary.Value(tag=label, image=img_sum))

# Create and write Summary
summary = self.tf.Summary(value=img_summaries)
self.writer.add_summary(summary, step)
if self.version.parse(self.tf.__version__) < self.version.parse("2"):
summary = self.tf.Summary(value=img_summaries)
self.writer.add_summary(summary, step)

if self.use_html: # save images to a html file
for label, image_numpy in visuals.items():
Expand Down Expand Up @@ -95,8 +104,12 @@ def display_current_results(self, visuals, epoch, step):
def plot_current_errors(self, errors, step):
if self.tf_log:
for tag, value in errors.items():
summary = self.tf.Summary(value=[self.tf.Summary.Value(tag=tag, simple_value=value)])
self.writer.add_summary(summary, step)
if self.version.parse(self.tf.__version__) >= self.version.parse("2"):
with self.writer.as_default():
self.tf.summary.scalar(tag, value, step)
else:
summary = self.tf.Summary(value=[self.tf.Summary.Value(tag=tag, simple_value=value)])
self.writer.add_summary(summary, step)

# errors: same format as |errors| of plotCurrentErrors
def print_current_errors(self, epoch, i, errors, t):
Expand Down