我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用google.protobuf.text_format.MessageToString()。
def build(self, network_filename='inception.prototxt'): """main method.""" network = self._build_network() print network with open(network_filename, 'w') as network_file: network_file.write(text_format.MessageToString(network)) return Net(network_filename) #if __name__ == '__main__': #__inception_builder__ = InceptionBuilder( #training_source='examples/imagenet/ilsvrc12_train_lmdb', #training_batch_size=32, #testing_source='examples/imagenet/ilsvrc12_val_lmdb', #testing_batch_size=6, #task_name='ilsvrc12', #mean_file='data/ilsvrc12/imagenet_mean.binaryproto', #labels=[('class_id', 1000)], #) #__inception_builder__.build()
def testPrintExotic(self, message_module): message = message_module.TestAllTypes() message.repeated_int64.append(-9223372036854775808) message.repeated_uint64.append(18446744073709551615) message.repeated_double.append(123.456) message.repeated_double.append(1.23e22) message.repeated_double.append(1.23e-18) message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"') message.repeated_string.append(u'\u00fc\ua71f') self.CompareToGoldenText( self.RemoveRedundantZeros(text_format.MessageToString(message)), 'repeated_int64: -9223372036854775808\n' 'repeated_uint64: 18446744073709551615\n' 'repeated_double: 123.456\n' 'repeated_double: 1.23e+22\n' 'repeated_double: 1.23e-18\n' 'repeated_string:' ' "\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n' 'repeated_string: "\\303\\274\\352\\234\\237"\n')
def testPrintExoticAsOneLine(self, message_module): message = message_module.TestAllTypes() message.repeated_int64.append(-9223372036854775808) message.repeated_uint64.append(18446744073709551615) message.repeated_double.append(123.456) message.repeated_double.append(1.23e22) message.repeated_double.append(1.23e-18) message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"') message.repeated_string.append(u'\u00fc\ua71f') self.CompareToGoldenText( self.RemoveRedundantZeros(text_format.MessageToString( message, as_one_line=True)), 'repeated_int64: -9223372036854775808' ' repeated_uint64: 18446744073709551615' ' repeated_double: 123.456' ' repeated_double: 1.23e+22' ' repeated_double: 1.23e-18' ' repeated_string: ' '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""' ' repeated_string: "\\303\\274\\352\\234\\237"')
def testPrintInIndexOrder(self): message = unittest_pb2.TestFieldOrderings() message.my_string = '115' message.my_int = 101 message.my_float = 111 message.optional_nested_message.oo = 0 message.optional_nested_message.bb = 1 self.CompareToGoldenText( self.RemoveRedundantZeros(text_format.MessageToString( message, use_index_order=True)), 'my_string: \"115\"\nmy_int: 101\nmy_float: 111\n' 'optional_nested_message {\n oo: 0\n bb: 1\n}\n') self.CompareToGoldenText( self.RemoveRedundantZeros(text_format.MessageToString(message)), 'my_int: 101\nmy_string: \"115\"\nmy_float: 111\n' 'optional_nested_message {\n bb: 1\n oo: 0\n}\n')
def testPrintMessageSet(self): message = unittest_mset_pb2.TestMessageSetContainer() ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension message.message_set.Extensions[ext1].i = 23 message.message_set.Extensions[ext2].str = 'foo' self.CompareToGoldenText( text_format.MessageToString(message), 'message_set {\n' ' [protobuf_unittest.TestMessageSetExtension1] {\n' ' i: 23\n' ' }\n' ' [protobuf_unittest.TestMessageSetExtension2] {\n' ' str: \"foo\"\n' ' }\n' '}\n') message = message_set_extensions_pb2.TestMessageSet() ext = message_set_extensions_pb2.message_set_extension3 message.Extensions[ext].text = 'bar' self.CompareToGoldenText( text_format.MessageToString(message), '[google.protobuf.internal.TestMessageSetExtension3] {\n' ' text: \"bar\"\n' '}\n')
def testPrintMessageSetAsOneLine(self): message = unittest_mset_pb2.TestMessageSetContainer() ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension message.message_set.Extensions[ext1].i = 23 message.message_set.Extensions[ext2].str = 'foo' self.CompareToGoldenText( text_format.MessageToString(message, as_one_line=True), 'message_set {' ' [protobuf_unittest.TestMessageSetExtension1] {' ' i: 23' ' }' ' [protobuf_unittest.TestMessageSetExtension2] {' ' str: \"foo\"' ' }' ' }')
def testPrintMessageExpandAnyRepeated(self): packed_message = unittest_pb2.OneString() message = any_test_pb2.TestAny() packed_message.data = 'string0' message.repeated_any_value.add().Pack(packed_message) packed_message.data = 'string1' message.repeated_any_value.add().Pack(packed_message) self.assertEqual( text_format.MessageToString(message, descriptor_pool=descriptor_pool.Default()), 'repeated_any_value {\n' ' [type.googleapis.com/protobuf_unittest.OneString] {\n' ' data: "string0"\n' ' }\n' '}\n' 'repeated_any_value {\n' ' [type.googleapis.com/protobuf_unittest.OneString] {\n' ' data: "string1"\n' ' }\n' '}\n')
def testPrintMessageSet(self): message = unittest_mset_pb2.TestMessageSetContainer() ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension message.message_set.Extensions[ext1].i = 23 message.message_set.Extensions[ext2].str = 'foo' self.CompareToGoldenText( text_format.MessageToString(message), 'message_set {\n' ' [protobuf_unittest.TestMessageSetExtension1] {\n' ' i: 23\n' ' }\n' ' [protobuf_unittest.TestMessageSetExtension2] {\n' ' str: \"foo\"\n' ' }\n' '}\n')
def testPrintExotic(self): message = unittest_pb2.TestAllTypes() message.repeated_int64.append(-9223372036854775808) message.repeated_uint64.append(18446744073709551615) message.repeated_double.append(123.456) message.repeated_double.append(1.23e22) message.repeated_double.append(1.23e-18) message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"') message.repeated_string.append(u'\u00fc\ua71f') self.CompareToGoldenText( self.RemoveRedundantZeros(text_format.MessageToString(message)), 'repeated_int64: -9223372036854775808\n' 'repeated_uint64: 18446744073709551615\n' 'repeated_double: 123.456\n' 'repeated_double: 1.23e+22\n' 'repeated_double: 1.23e-18\n' 'repeated_string:' ' "\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n' 'repeated_string: "\\303\\274\\352\\234\\237"\n')
def testPrintExoticAsOneLine(self): message = unittest_pb2.TestAllTypes() message.repeated_int64.append(-9223372036854775808) message.repeated_uint64.append(18446744073709551615) message.repeated_double.append(123.456) message.repeated_double.append(1.23e22) message.repeated_double.append(1.23e-18) message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"') message.repeated_string.append(u'\u00fc\ua71f') self.CompareToGoldenText( self.RemoveRedundantZeros( text_format.MessageToString(message, as_one_line=True)), 'repeated_int64: -9223372036854775808' ' repeated_uint64: 18446744073709551615' ' repeated_double: 123.456' ' repeated_double: 1.23e+22' ' repeated_double: 1.23e-18' ' repeated_string: ' '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""' ' repeated_string: "\\303\\274\\352\\234\\237"')
def visualize_embeddings(summary_writer, config): """Stores a config file used by the embedding projector. Args: summary_writer: The summary writer used for writting events. config: `tf.contrib.tensorboard.plugins.projector.ProjectorConfig` proto that holds the configuration for the projector such as paths to checkpoint files and metadata files for the embeddings. If `config.model_checkpoint_path` is none, it defaults to the `logdir` used by the summary_writer. Raises: ValueError: If the summary writer does not have a `logdir`. """ logdir = summary_writer.get_logdir() # Sanity checks. if logdir is None: raise ValueError('Summary writer must have a logdir') # Saving the config file in the logdir. config_pbtxt = text_format.MessageToString(config) file_io.write_string_to_file( os.path.join(logdir, PROJECTOR_FILENAME), config_pbtxt)
def testPrintExotic(self): message = unittest_pb2.TestAllTypes() message.repeated_int64.append(-9223372036854775808) message.repeated_uint64.append(18446744073709551615) message.repeated_double.append(123.456) message.repeated_double.append(1.23e22) message.repeated_double.append(1.23e-18) message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"') message.repeated_string.append(u'\u00fc\ua71f') self.CompareToGoldenText( self.RemoveRedundantZeros(text_format.MessageToString(message)), 'repeated_int64: -9223372036854775808\n' 'repeated_uint64: 18446744073709551615\n' 'repeated_double: 123.456\n' 'repeated_double: 1.23e+22\n' 'repeated_double: 1.23e-18\n' 'repeated_string: ' '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n' 'repeated_string: "\\303\\274\\352\\234\\237"\n')
def testRoundTripExoticAsOneLine(self): message = unittest_pb2.TestAllTypes() message.repeated_int64.append(-9223372036854775808) message.repeated_uint64.append(18446744073709551615) message.repeated_double.append(123.456) message.repeated_double.append(1.23e22) message.repeated_double.append(1.23e-18) message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"') message.repeated_string.append(u'\u00fc\ua71f') # Test as_utf8 = False. wire_text = text_format.MessageToString( message, as_one_line=True, as_utf8=False) parsed_message = unittest_pb2.TestAllTypes() text_format.Merge(wire_text, parsed_message) self.assertEquals(message, parsed_message) # Test as_utf8 = True. wire_text = text_format.MessageToString( message, as_one_line=True, as_utf8=True) parsed_message = unittest_pb2.TestAllTypes() text_format.Merge(wire_text, parsed_message) self.assertEquals(message, parsed_message)
def _GenerateProjectorTestData(self): config_path = os.path.join(self.log_dir, 'projector_config.pbtxt') config = projector_config_pb2.ProjectorConfig() embedding = config.embeddings.add() # Add an embedding by its canonical tensor name. embedding.tensor_name = 'var1:0' with tf.gfile.GFile(os.path.join(self.log_dir, 'bookmarks.json'), 'w') as f: f.write('{"a": "b"}') embedding.bookmarks_path = 'bookmarks.json' config_pbtxt = text_format.MessageToString(config) with tf.gfile.GFile(config_path, 'w') as f: f.write(config_pbtxt) # Write a checkpoint with some dummy variables. with tf.Graph().as_default(): sess = tf.Session() checkpoint_path = os.path.join(self.log_dir, 'model') tf.get_variable('var1', [1, 2], initializer=tf.constant_initializer(6.0)) tf.get_variable('var2', [10, 10]) tf.get_variable('var3', [100, 100]) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(write_version=tf.train.SaverDef.V1) saver.save(sess, checkpoint_path)
def visualize_embeddings(summary_writer, config): """Stores a config file used by the embedding projector. Args: summary_writer: The summary writer used for writing events. config: `tf.contrib.tensorboard.plugins.projector.ProjectorConfig` proto that holds the configuration for the projector such as paths to checkpoint files and metadata files for the embeddings. If `config.model_checkpoint_path` is none, it defaults to the `logdir` used by the summary_writer. Raises: ValueError: If the summary writer does not have a `logdir`. """ logdir = summary_writer.get_logdir() # Sanity checks. if logdir is None: raise ValueError('Summary writer must have a logdir') # Saving the config file in the logdir. config_pbtxt = _text_format.MessageToString(config) path = os.path.join(logdir, _projector_plugin.PROJECTOR_FILENAME) with tf.gfile.Open(path, 'w') as f: f.write(config_pbtxt)
def testMapOrderSemantics(self): golden_lines = self.ReadGolden('map_test_data.txt') # The C++ implementation emits defaulted-value fields, while the Python # implementation does not. Adjusting for this is awkward, but it is # valuable to test against a common golden file. line_blacklist = (' key: 0\n', ' value: 0\n', ' key: false\n', ' value: false\n') golden_lines = [line for line in golden_lines if line not in line_blacklist] message = map_unittest_pb2.TestMap() text_format.ParseLines(golden_lines, message) candidate = text_format.MessageToString(message) # The Python implementation emits "1.0" for the double value that the C++ # implementation emits as "1". candidate = candidate.replace('1.0', '1', 2) self.assertMultiLineEqual(candidate, ''.join(golden_lines)) # Tests of proto2-only features (MessageSet, extensions, etc.).
def add_local_symbol(self, field, value): if len(field.component) == 1: self.stack[-1][field.component[0].name] = value else: components = list(reversed([component.name for component in field.component])) atom = self.lookup_local_key(components.pop()) parent, base, p = self.traverse_atom(atom, components) if isinstance(p, Message): result = parse_proto(value.literal.proto.value, value.literal.proto.field.component[0].name) getattr(parent, field.component[-1].name).CopyFrom(result) self.stack[-1][field.component[0].name].literal.proto.value = text_format.MessageToString(base) else: setattr(parent, field.component[-1].name, value) self.stack[-1][field.component[0].name].literal.proto.value = text_format.MessageToString(base)
def _AddStrMethod(message_descriptor, cls): """Helper for _AddMessageMethods().""" def __str__(self): return text_format.MessageToString(self) cls.__str__ = __str__
def _AddReprMethod(message_descriptor, cls): """Helper for _AddMessageMethods().""" def __repr__(self): return text_format.MessageToString(self) cls.__repr__ = __repr__
def _AddUnicodeMethod(unused_message_descriptor, cls): """Helper for _AddMessageMethods().""" def __unicode__(self): return text_format.MessageToString(self, as_utf8=True).decode('utf-8') cls.__unicode__ = __unicode__
def compute_output_shapes(self, model): sorted_nodes = self.topologically_sorted() (tmp_handle, tmp_prototxt) = tempfile.mkstemp(suffix=".prototxt") with open(tmp_prototxt, 'w') as f: f.write(text_format.MessageToString(model)) self.prototxt = tmp_prototxt if has_pycaffe(): caffe = get_caffe_resolver().caffe net = caffe.Net(tmp_prototxt, caffe.TEST) for key, value in net.blobs.items(): try: node = self.get_node(key) dims = list(value.shape) dims = dims + [1] * (4 - len(dims)) node.output_shape = TensorShape(*dims) except: continue for node in sorted_nodes: if node.output_shape is None: node.output_shape = TensorShape(*NodeKind.compute_output_shape(node)) os.close(tmp_handle) os.remove(tmp_prototxt) else: for node in sorted_nodes: node.output_shape = TensorShape(*NodeKind.compute_output_shape(node)) # consider rewrite this function to Network.py
def build_solver(solver_filename, **kwargs): solver = caffe_pb2.SolverParameter() for k, v in kwargs.iteritems(): setattr(solver, v) with open(solver_filename, 'w') as f: f.write(text_format.MessageToString(solver))