微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TFRecord-Files解析错误尺寸错误

如何解决TFRecord-Files解析错误尺寸错误

这两天我尝试以TFRecord-Type加载和解析序列化数据。 这是我的代码

 def _float_feature(value):
     #Returns a float_list from a float / double.
      return tf.train.Feature(float_list=tf.train.FloatList(value=value))
def _int64_feature(value):
  #Returns an int64_list from a bool / enum / int / uint."""
  return tf.train.Feature(int64_list=tf.train.Int64List(value=value))

#writing the Files

with tf.io.TFRecordWriter(filepath) as FileWriter:
    features={
           'spectrum_matrix'          :     _float_feature(spectrum_matrix.reshape(-1)),#Shape (222*88,)  
            'RPM_Label'                :     _float_feature(RPM_label),#Shape (222,)
            'spectrum_matrix_shape'    :     _int64_feature(spectrum_matrix.shape),#Shape(2)                 
            'BlockSize'                :     _int64_feature([BlockSize]),#Shape(1)
            'SamplingRate'             :     _int64_feature([SamplingRate]),#Shape(1)
            'CylinderNumber'           :     _int64_feature([CylinderNumber])       #Shape(1)
             }
    
        
    tf_features = tf.train.Features(feature = features)
    tf_example = tf.train.Example(features = tf_features) # create protocol buffer
    tf_serialized = tf_example.SerializetoString()     
                    
    FileWriter.write(tf_serialized) # write serialized data

#Loading and parsing of the Files
feature_map = {
    'spectrum_matrix'       : tf.io.FixedLenFeature([222*88],tf.float32,default_value=0.0),'RPM_Label'             : tf.io.FixedLenFeature([222],'spectrum_matrix_shape' : tf.io.FixedLenFeature([2],tf.int64,default_value=0),'BlockSize'             : tf.io.FixedLenFeature([1],'SamplingRate'          : tf.io.FixedLenFeature([1],'CylinderNumber'        : tf.io.FixedLenFeature([1],}


def _parse_function(serialized):
    # Parse the input tf.Example proto using the dictionary above.
    features = tf.io.parse_single_example(serialized,feature_map)
    return features

tf_file_raw = tf.data.TFRecordDataset([File_path])

for file in tf_file_raw:
    print(repr(file))
    tf_file= _parse_function(file)  #Here is the Error
    print(tf_file)

当我尝试此操作时,出现错误

InvalidArgumentError: Input to reshape is a tensor with 1 values,but the requested shape has 222 [Op:Reshape]

如果我在feature_map中遗漏了“尺寸”,则会出现以下错误

InvalidArgumentError: Key: spectrum_matrix_shape.  Can't parse serialized Example. [Op:ParseExampleV2]

我不知道该怎么做。如我所见,Tensorflow文档采用相同的方式。我的错误在哪里?

你们需要更多信息吗?

谢谢!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。