Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorboard/util/encoder.py: 53%
38 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15"""TensorBoard encoder helper module.
17Encoder depends on TensorFlow.
18"""
21import numpy as np
23from tensorboard.util import op_evaluator
26class _TensorFlowPngEncoder(op_evaluator.PersistentOpEvaluator):
27 """Encode an image to PNG.
29 This function is thread-safe, and has high performance when run in
30 parallel. See `encode_png_benchmark.py` for details.
32 Arguments:
33 image: A numpy array of shape `[height, width, channels]`, where
34 `channels` is 1, 3, or 4, and of dtype uint8.
36 Returns:
37 A bytestring with PNG-encoded data.
38 """
40 def __init__(self):
41 super().__init__()
42 self._image_placeholder = None
43 self._encode_op = None
45 def initialize_graph(self):
46 # TODO(nickfelt): remove on-demand imports once dep situation is fixed.
47 import tensorflow.compat.v1 as tf
49 self._image_placeholder = tf.placeholder(
50 dtype=tf.uint8, name="image_to_encode"
51 )
52 self._encode_op = tf.image.encode_png(self._image_placeholder)
54 def run(self, image): # pylint: disable=arguments-differ
55 if not isinstance(image, np.ndarray):
56 raise ValueError("'image' must be a numpy array: %r" % image)
57 if image.dtype != np.uint8:
58 raise ValueError(
59 "'image' dtype must be uint8, but is %r" % image.dtype
60 )
61 return self._encode_op.eval(feed_dict={self._image_placeholder: image})
64encode_png = _TensorFlowPngEncoder()
67class _TensorFlowWavEncoder(op_evaluator.PersistentOpEvaluator):
68 """Encode an audio clip to WAV.
70 This function is thread-safe and exhibits good parallel performance.
72 Arguments:
73 audio: A numpy array of shape `[samples, channels]`.
74 samples_per_second: A positive `int`, in Hz.
76 Returns:
77 A bytestring with WAV-encoded data.
78 """
80 def __init__(self):
81 super().__init__()
82 self._audio_placeholder = None
83 self._samples_per_second_placeholder = None
84 self._encode_op = None
86 def initialize_graph(self):
87 # TODO(nickfelt): remove on-demand imports once dep situation is fixed.
88 import tensorflow.compat.v1 as tf
90 self._audio_placeholder = tf.placeholder(
91 dtype=tf.float32, name="image_to_encode"
92 )
93 self._samples_per_second_placeholder = tf.placeholder(
94 dtype=tf.int32, name="samples_per_second"
95 )
96 self._encode_op = tf.audio.encode_wav(
97 self._audio_placeholder,
98 sample_rate=self._samples_per_second_placeholder,
99 )
101 def run(
102 self, audio, samples_per_second
103 ): # pylint: disable=arguments-differ
104 if not isinstance(audio, np.ndarray):
105 raise ValueError("'audio' must be a numpy array: %r" % audio)
106 if not isinstance(samples_per_second, int):
107 raise ValueError(
108 "'samples_per_second' must be an int: %r" % samples_per_second
109 )
110 feed_dict = {
111 self._audio_placeholder: audio,
112 self._samples_per_second_placeholder: samples_per_second,
113 }
114 return self._encode_op.eval(feed_dict=feed_dict)
117encode_wav = _TensorFlowWavEncoder()