Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorboard/plugins/text/summary.py: 27%

26 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2017 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. 

14# ============================================================================== 

15"""Text summaries and TensorFlow operations to create them.""" 

16 

17 

18from tensorboard.plugins.text import metadata 

19from tensorboard.plugins.text import summary_v2 

20 

21 

22# Export V2 versions. 

23text = summary_v2.text 

24text_pb = summary_v2.text_pb 

25 

26 

27def op(name, data, display_name=None, description=None, collections=None): 

28 """Create a legacy text summary op. 

29 

30 Text data summarized via this plugin will be visible in the Text Dashboard 

31 in TensorBoard. The standard TensorBoard Text Dashboard will render markdown 

32 in the strings, and will automatically organize 1D and 2D tensors into tables. 

33 If a tensor with more than 2 dimensions is provided, a 2D subarray will be 

34 displayed along with a warning message. (Note that this behavior is not 

35 intrinsic to the text summary API, but rather to the default TensorBoard text 

36 plugin.) 

37 

38 Args: 

39 name: A name for the generated node. Will also serve as a series name in 

40 TensorBoard. 

41 data: A string-type Tensor to summarize. The text must be encoded in UTF-8. 

42 display_name: Optional name for this summary in TensorBoard, as a 

43 constant `str`. Defaults to `name`. 

44 description: Optional long-form description for this summary, as a 

45 constant `str`. Markdown is supported. Defaults to empty. 

46 collections: Optional list of ops.GraphKeys. The collections to which to add 

47 the summary. Defaults to [Graph Keys.SUMMARIES]. 

48 

49 Returns: 

50 A TensorSummary op that is configured so that TensorBoard will recognize 

51 that it contains textual data. The TensorSummary is a scalar `Tensor` of 

52 type `string` which contains `Summary` protobufs. 

53 

54 Raises: 

55 ValueError: If tensor has the wrong type. 

56 """ 

57 # TODO(nickfelt): remove on-demand imports once dep situation is fixed. 

58 import tensorflow.compat.v1 as tf 

59 

60 if display_name is None: 

61 display_name = name 

62 summary_metadata = metadata.create_summary_metadata( 

63 display_name=display_name, description=description 

64 ) 

65 with tf.name_scope(name): 

66 with tf.control_dependencies([tf.assert_type(data, tf.string)]): 

67 return tf.summary.tensor_summary( 

68 name="text_summary", 

69 tensor=data, 

70 collections=collections, 

71 summary_metadata=summary_metadata, 

72 ) 

73 

74 

75def pb(name, data, display_name=None, description=None): 

76 """Create a legacy text summary protobuf. 

77 

78 Arguments: 

79 name: A name for the generated node. Will also serve as a series name in 

80 TensorBoard. 

81 data: A Python bytestring (of type bytes), or Unicode string. Or a numpy 

82 data array of those types. 

83 display_name: Optional name for this summary in TensorBoard, as a 

84 `str`. Defaults to `name`. 

85 description: Optional long-form description for this summary, as a 

86 `str`. Markdown is supported. Defaults to empty. 

87 

88 Raises: 

89 ValueError: If the type of the data is unsupported. 

90 

91 Returns: 

92 A `tf.Summary` protobuf object. 

93 """ 

94 # TODO(nickfelt): remove on-demand imports once dep situation is fixed. 

95 import tensorflow.compat.v1 as tf 

96 

97 try: 

98 tensor = tf.make_tensor_proto(data, dtype=tf.string) 

99 except TypeError as e: 

100 raise ValueError(e) 

101 

102 if display_name is None: 

103 display_name = name 

104 summary_metadata = metadata.create_summary_metadata( 

105 display_name=display_name, description=description 

106 ) 

107 tf_summary_metadata = tf.SummaryMetadata.FromString( 

108 summary_metadata.SerializeToString() 

109 ) 

110 summary = tf.Summary() 

111 summary.value.add( 

112 tag="%s/text_summary" % name, 

113 metadata=tf_summary_metadata, 

114 tensor=tensor, 

115 ) 

116 return summary