如何将sparsetensor写入tfrecords

时间:2017-07-24 11:52:07

标签: python tensorflow

我正在尝试创建一个包含图像字节,高度,宽度,sparseTensor_labels(索引,值和形状)的TFRecord,以下是代码:   ##

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

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

tfrecords_filename = 'my_dataset.tfrecords'

writer = tf.python_io.TFRecordWriter(tfrecords_filename)

for img, label in zip(image_list, label_list):
    try:
        im=np.array(Image.open(img[0]))

        im_height , im_width = im.shape
    except IOError:
        print("Image not read successfully: ", img[0])

    img_raw = im.tostring()
    indices = [i for i in range(0,len(label[0]))]
    values_ctc = [char_to_ix[i]  for i in list(label[0])]
    shape_ctc = [len(label[0])]
    example = tf.train.Example(features=tf.train.Features(feature={
        'height': _int64_feature(im_height),
        'width': _int64_feature(im_width),
        'image_raw': _bytes_feature(img_raw),
        'mask_raw': _bytes_feature(tf.compat.as_bytes(label[0])),
        'indices' : tf.train.Feature(int64_list=tf.train.Int64List( value= indices)),
        'value' :  tf.train.Feature(float_list=tf.train.FloatList( value= values_ctc)),
        'shape_ctc': tf.train.Feature(int64_list=tf.train.Int64List( value= shape_ctc))
    }))

    writer.write(example.SerializeToString())
#print(example)
writer.close()

接下来我正在阅读相同内容: 但不知道如何阅读稀疏标签? 以下是我正在做的事情:     ##     reader = tf.TFRecordReader()

_, serialized_example = reader.read(filename_queue)
serialized_example=tf.reshape(serialized_example, shape=[])
features = tf.parse_single_example(
  serialized_example,
  # Defaults are not specified since both keys are required.
  features={
    'height': parsing_ops.FixedLenFeature([], tf.int64),
    'width': parsing_ops.FixedLenFeature([], tf.int64),
    'image_raw': parsing_ops.FixedLenFeature([],dtype= tf.string),
    'mask_raw': parsing_ops.FixedLenFeature([],dtype=tf.string),
    ??
      })

1 个答案:

答案 0 :(得分:0)

我和参与此功能的一位工程师交谈过,他的答复是:

您需要使用VarLenFeature。单元测试的例子:

jQuery DataTables: Common JavaScript console errors