def add_image(self, tag, img, step): summary = Summary() bio = BytesIO() if type(img) == str: img = PIL.Image.open(img) elif type(img) == PIL.Image.Image: pass else: img = PIL.Image.fromarray(img) img.save(bio, format="png") image_summary = Summary.Image(encoded_image_string=bio.getvalue()) summary.value.add(tag=tag, image=image_summary) self.summary_writer.add_summary(summary, global_step=step)
def image_summary(self, tag, value, step): for idx, img in enumerate(value): summary = Summary() bio = BytesIO() if type(img) == str: img = PIL.Image.open(img) elif type(img) == PIL.Image.Image: pass else: img = scipy.misc.toimage(img) img.save(bio, format="png") image_summary = Summary.Image(encoded_image_string=bio.getvalue()) summary.value.add(tag=f"{tag}/{idx}", image=image_summary) self.summary_writer.add_summary(summary, global_step=step)
def add_image(self, tag, img, step): summary = Summary() bio = BytesIO() if type(img) == str: img = Image.open(img) elif type(img) == Image.Image: pass else: img = Image.fromarray(img) #scipy.misc.toimage(img) if img.mode == "F": img = img.convert('RGB') img.save(bio, format="png") image_summary = Summary.Image(encoded_image_string=bio.getvalue()) summary.value.add(tag=tag, image=image_summary) self.summary_writer.add_summary(summary, global_step=step)
def image_summary(self, tag, value, step): for idx, img in enumerate(value): summary = Summary() ##for creating image summary for model bio = BytesIO() ##for converting image to encoded form (binary) if type(img) == str: ##url to image img = PIL.Image.open(img) ##open the image elif type( img ) == PIL.Image.Image: ##Image class of PIL(directly an image) pass else: img = scipy.misc.toimage( img) ##Takes a numpy array and returns a PIL image. img.save(bio, format="png") image_summary = Summary.Image(encoded_image_string=bio.getvalue() ) ##binary encoded string for images summary.value.add(tag=f"{tag}/{idx}", image=image_summary) self.summary_writer.add_summary( summary, global_step=step) ##image summary with tag for model
def add_mpl_plot(self, tag, fig, global_step): if self.__show_mpl: import matplotlib.pyplot as plt plt.show() else: from PIL import Image import io fig.canvas.draw() pil_image = Image.frombytes('RGB', fig.canvas.get_width_height(), fig.canvas.tostring_rgb()) output = io.BytesIO() pil_image.save(output, format='PNG') pil_image_string = output.getvalue() output.close() img_summary = Summary.Image(height=pil_image.height, width=pil_image.width, colorspace=3, encoded_image_string=pil_image_string) summary = Summary( value=[Summary.Value(tag=_clean_tag(tag), image=img_summary)]) self.file_writer.add_summary(summary, global_step=global_step)