Commit 121a5d32 authored by Aaron Gokaslan's avatar Aaron Gokaslan Committed by Yuxin Wu

Improve PNG compression in add_image_summary (#995)

OpenCV does not fully compress PNGs by default. This additional parameter tells OpenCV to use the maximum PNG compression level for encoding images. This should reduce the size of saved Tensorboard images particularly for highly compressible domains such as segmentations.
parent ef334e31
...@@ -68,6 +68,7 @@ def create_image_summary(name, val): ...@@ -68,6 +68,7 @@ def create_image_summary(name, val):
n, h, w, c = val.shape n, h, w, c = val.shape
val = val.astype('uint8') val = val.astype('uint8')
s = tf.Summary() s = tf.Summary()
imparams = [cv2.IMWRITE_PNG_COMPRESSION, 9]
for k in range(n): for k in range(n):
arr = val[k] arr = val[k]
# CV2 will only write correctly in BGR chanel order # CV2 will only write correctly in BGR chanel order
...@@ -76,8 +77,7 @@ def create_image_summary(name, val): ...@@ -76,8 +77,7 @@ def create_image_summary(name, val):
elif c == 4: elif c == 4:
arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA) arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA)
tag = name if n == 1 else '{}/{}'.format(name, k) tag = name if n == 1 else '{}/{}'.format(name, k)
retval, img_str = cv2.imencode('.png', arr, imparams)
retval, img_str = cv2.imencode('.png', arr)
if not retval: if not retval:
# Encoding has failed. # Encoding has failed.
continue continue
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment