0
我試圖使TFRecord包含圖像字節,高度,寬度,sparseTensor_labels(指數,值和形狀),以下是代碼: ##如何寫sparsetensor到tfrecords
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()
下一頁我讀的是一樣的: 但不知道如何讀取sparselabels? 以下是我在做什麼: ## 讀卡器= 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),
??
})