I am currently working on a project based on deep learning where I am using tfrecords for faster training of neural network. Actually I have encoded my data into ‘.tfrecords’, but while decoding it shows tensor of none shape. I have checked my code many times,but I didn’t find any bugs. Can anyone help me with this ? Thanks in advance. Here is my code…
def _bytes_feature(value):
#Returns a bytes_list from a string / byte
if isinstance(value, type(tf.constant(0))):
value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
return tf.train.Feature(bytes_list=tf.train.BytesList(value= [value]))
def serialize_example(image,xmins):
feature = {
'image':
_bytes_feature(image),
'xmins':
tf.train.Feature(float_list=tf.train.FloatList(value=[xmins])),
}
example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
#print(example_proto.Features.feature['xmins'])
return example_proto.SerializeToString()
with tf.io.TFRecordWriter('/content/'+'prac.tfrecords') as writer:
image = np.array(image,dtype=int)
byte_image = tf.io.serialize_tensor(image)
xmins= float(2.3)
example = serialize_example(byte_image,xmins)
writer.write(example)
For simplicity I have taken only two types of input , image and xmins. Here, I have generated ‘.tfrecords’
Decoding
def read_tf_records(example_proto):
image_feature_description = {
'image': tf.io.FixedLenFeature((), tf.string),
'xmins': tf.io.VarLenFeature(tf.float32),
}
sample = tf.io.parse_single_example(example_proto,image_feature_description)
image =tf.io.parse_tensor(sample['image'],out_type = tf.float64)
xmins = sample['xmins']
#image = tf.reshape(image,(416,416))
#print(image)
#xmins =tf.sparse.to_dense(sample['xmins'])
return image,xmins
path='/content/prac.tfrecords'
tfrecord_dataset = tf.data.TFRecordDataset(path)
decoded_data= tfrecord_dataset.map(read_tf_records)
print(decoded_data)
<MapDataset shapes: (<unknown>, (None,)), types: (tf.float64, tf.float32)>