将数组写成tfrecord

Posted rongye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数组写成tfrecord相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python


import tensorflow as tf
import numpy as np
# write = tf.python_io.TFRecordWriter(‘train.tfrecords‘)
# #img_raw = np.random.random_integers(0, 255, size=(7, 30)) # 创建7*30,取值在0-255之间随机数组
# list_a=[6,5,4]
# list_b=[7]
# img_raw = np.array(list_a,dtype=np.float32)
# img_label =np.array(list_b,dtype=np.int64)
# #img_label = img_label.tostring()
# img_raw = img_raw.tostring()
# example = tf.train.Example(features=tf.train.Features(
# feature={
# ‘label‘: tf.train.Feature(int64_list=tf.train.Int64List(value=[img_label])),
# ‘img_raw‘: tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
# }))
# write.write(example.SerializeToString())
# write.close()
def create_file(path):
write = tf.python_io.TFRecordWriter(‘train02.tfrecords‘)
with open(path,‘r‘) as file:
lines = file.readlines()
# print lines.__len__()
count = 0
data = []
featuresList = []
labelList = []
label = []
for line in lines:
word = line.split(" ")
features = []
for i in range(1, len(word)):
if i < (len(word) - 1):
features.append(word[i].split(":")[1])
else:
features.append(word[len(word) - 1].split(":")[1].split(" ")[0])
label.append(int(word[0]))
count = count + 1
#print(count)
featuresList.append(features)
labelList.append(label)
img_raw = np.array(featuresList[-1], dtype=np.float32)
img_label = np.array(label[-1],dtype=np.int64)
print(img_label)
print(img_raw)
img_raw = img_raw.tostring()
example = tf.train.Example(features=tf.train.Features(
feature={
‘label‘: tf.train.Feature(int64_list=tf.train.Int64List(value=[img_label])),
‘img_raw‘: tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
}))
write.write(example.SerializeToString())
write.close()
create_file("train02.txt")























































以上是关于将数组写成tfrecord的主要内容,如果未能解决你的问题,请参考以下文章

按此顺序重新排列数组

matlab中,如何在for循环中把每一个元胞数组都写成excel文件

UVA-1589 象棋(模拟)

ES6中对数组的扩展

在PHP中如何把数组写成配置文件

将数组格式的字符串转换成数组