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