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 (将#修改为@)