zoukankan      html  css  js  c++  java
  • 将数组写成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")
  • 相关阅读:
    云计算初探
    MySQL、HBase、ES的特点和区别
    MongoDB、ElasticSearch、Redis、HBase这四种热门数据库的优缺点及应用场景
    主流 Kubernetes 发行版梳理
    如何在flink中传递参数
    (47)zabbix报警媒介:Ez Texting
    (46)zabbix报警媒介:Jabber
    (45)zabbix报警媒介:SMS
    (44)zabbix报警媒介:email
    (43)zabbix报警媒介介绍
  • 原文地址:https://www.cnblogs.com/rongye/p/10028549.html
Copyright © 2011-2022 走看看