如何将字符串数据保存到TFRecord?

时间:2018-08-18 03:11:58

标签: python tensorflow tfrecord

当保存到TFRecord时,我使用:

def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))


def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


def _float_feature(value):
    return tf.train.Feature(float_list=tf.train.FloatList(value=value))

one_example = tf.train.Example(
    features=tf.train.Features(
        feature={
            "image": _bytes_feature(img.tobytes()),
            "label": _bytes_feature(label.tobytes()),
            "file_name": _bytes_feature(this_city_file_name), #this line doesn't work
            "nb_rows": _int64_feature(nb_rows), 
            "nb_cols": _int64_feature(nb_cols), 
            "index_i": _int64_feature(i), 
            "index_j": _int64_feature(j),
        }
    )
)

this_city_file_name 具有字符串类型 当我运行此代码时,将导致错误:

TypeError:'xxxxxxx'具有type,但应为以下一种:((,),)

仅使用字节(this_city_file_name)也会导致错误:

TypeError:不带编码的字符串参数

从TFRecord加载时,我使用

features = tf.parse_single_example(serialized_example,
                                   features={
                                       "image": tf.FixedLenFeature([], tf.string),
                                       "label": tf.FixedLenFeature([], tf.string),
                                       "file_name": tf.FixedLenFeature([], tf.string),
                                       "nb_rows": tf.FixedLenFeature([], tf.int64),
                                       "nb_cols": tf.FixedLenFeature([], tf.int64),
                                       "index_i": tf.FixedLenFeature([], tf.int64),
                                       "index_j": tf.FixedLenFeature([], tf.int64),
                                   },
                                   )

我知道如何将int和np.array类型保存到TFRecord并从中读取 但是如何保存和加载TFRecord中的 string 数据?

1 个答案:

答案 0 :(得分:0)

我知道这很旧,但是您必须将this_city_file_name转换为字节对象。看看这个guide

以下是相关代码:

print(_bytes_feature(b'test_string'))
print(_bytes_feature(u'test_bytes'.encode('utf-8')))