Args: name(str): val(np.ndarray): 4D tensor of NHWC. assume RGB if C==3. Can be either float or uint8. Range has to be [0,255]. Returns: tf.Summary:
(name, val)
| 54 | |
| 55 | |
| 56 | def create_image_summary(name, val): |
| 57 | """ |
| 58 | Args: |
| 59 | name(str): |
| 60 | val(np.ndarray): 4D tensor of NHWC. assume RGB if C==3. |
| 61 | Can be either float or uint8. Range has to be [0,255]. |
| 62 | |
| 63 | Returns: |
| 64 | tf.Summary: |
| 65 | """ |
| 66 | assert isinstance(name, six.string_types), type(name) |
| 67 | n, h, w, c = val.shape |
| 68 | val = val.astype('uint8') |
| 69 | s = tf.Summary() |
| 70 | imparams = [cv2.IMWRITE_PNG_COMPRESSION, 9] |
| 71 | for k in range(n): |
| 72 | arr = val[k] |
| 73 | # CV2 will only write correctly in BGR chanel order |
| 74 | if c == 3: |
| 75 | arr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR) |
| 76 | elif c == 4: |
| 77 | arr = cv2.cvtColor(arr, cv2.COLOR_RGBA2BGRA) |
| 78 | tag = name if n == 1 else '{}/{}'.format(name, k) |
| 79 | retval, img_str = cv2.imencode('.png', arr, imparams) |
| 80 | if not retval: |
| 81 | # Encoding has failed. |
| 82 | continue |
| 83 | img_str = img_str.tostring() |
| 84 | |
| 85 | img = tf.Summary.Image() |
| 86 | img.height = h |
| 87 | img.width = w |
| 88 | # 1 - grayscale 3 - RGB 4 - RGBA |
| 89 | img.colorspace = c |
| 90 | img.encoded_image_string = img_str |
| 91 | s.value.add(tag=tag, image=img) |
| 92 | return s |
| 93 | |
| 94 | |
| 95 | def add_tensor_summary(x, types, name=None, collections=None, |