参考文章:
TensorFlow 简单 pb (pbtxt) 文件读写 - 简书
Tensorflow使用TFRecords和tf.Example_Wanderer001的博客-CSDN博客
在实际应用中,如果tf.train.Example格式的数据输出到磁盘时,输出前加上了 .SerializeToString ,那么是很好加载的,只需要
def deal_file(datapath):
#定义返回的字典
ret = {}
#读取文件
fid = tf.gfile.GFile(datapath, 'rb')
example_proto = tf.train.Example.FromString(fid.read())
#只计算你想进行排查的特征
for key in fea_list:
#先拿到这个特征_index的取值
index = example_proto.features.feature.get(key + "_index")
index_2 = ",".join([str(i) for i in index.int64_list.value])
# print(index)
# print(index_2)
#因为不确定value是float_list 还是 int64_list,所以都取下,并合并(不是的那个为空,对最终的取值无影响)
value = example_proto.features.feature.get(key + "_value")
value_2 = ",".join([str(i) for i in value.int64_list.value]) + ",".join([str(i) for i in value.float_list.value])
# print(value)
# print(value_2)
ret[key] = index_2 + "_" + value_2
return ret
如果输出成了明文格式,比如下面这样
features {
feature {
key: "feature1_index"
value {
int64_list {
value: 0
}
}
}
feature {
key: "feature1_value"
value {
float_list {
value: 1
}
}
}
feature {
key: "feature2_index"
value {
int64_list {
value: 0
}
}
}
feature {
key: "feature2_value"
value {
float_list {
value: 1
}
}
}
}
那么就需要用pb的方法来进行加载(我在实际中,没有按照下面的方法解决这个问题,但是思路应该是没错的)
1:先生成tf.train.Example这种pb的对应的python编译代码
pb内容(从官网也可以找到)
syntax = "proto3";
message Example {
Features features = 1;
};
message Features {
map featrue = 1;
};
message Feature {
oneof kind{
BytesList bytes_list = 1;
FloatList float_list = 2;
Int64List int64_list = 3;
}
};
message BytesList {
repeated bytes value = 1;
}
message FloatList {
repeated float value = 1 [packed = true];
}
message Int64List {
repeated int64 value = 1 [packed = true];
}
protoc *.proto --python_out=.
这样就会生成一个python版的解析文件,可以import进到python程序中
import sys
import tensorflow as tf
import tf_train_example_pb2
from google.protobuf import text_format
datapath = "a.txt"
with tf.gfile.GFile(datapath, 'r') as fid:
pbtxt_string = fid.read()
pbtxt = tf_example_pb2.Example()
# pbtxt = tf_example_pb2
# try:
# text_format.Merge(pbtxt_string, pbtxt)
# except text_format.ParseError:
# pbtxt.ParseFromString(pbtxt_string)
text_format.Merge(pbtxt_string, pbtxt)



