如何向tfrecords写入sparsetensor

2024-04-23 06:16:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个包含图像字节、高度、宽度、sparseTensor_标签(索引、值和形状)的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()

接下来,我读到同样的内容: 但不知道怎么读斯巴塞拉伯? 以下是我正在做的事情: ## 读卡器=tf.tf读写器()

^{pr2}$

Tags: imgrawbytesvaluetfdeftrainlabel