【问题标题】:Read txt-file with data and labels into tensorflow将带有数据和标签的 txt 文件读入 tensorflow
【发布时间】:2020-10-13 22:03:03
【问题描述】:

我对 tensorflow 比较陌生,因此我在数据准备方面遇到了困难。 我有一个包含大约 500 个 .txt 文件的文件夹。这些文件中的每一个都包含数据和数据的标签。 (数据代表 MFCC,它们是为 .wav 音频文件的每个“帧”生成的音频特征。)

每个文件如下所示:

1
1.013302233064514191e+01
-1.913611804400369110e+01
1.067932213100989847e+00
1.308777013246182364e+01
-3.591032944037165109e+00
1.294307486784356698e+01
5.628056691023937574e+00
5.311223121033092909e+00
1.069261850699697014e+01
4.398722698218969995e+00
5.045254154360372389e+00
7.757820364628694954e+00
-2.666228281486863416e+00

9.236707894117541784e+00
-1.727334954006132151e+01
5.166050472560470119e+00
6.421742650353079007e+00
2.550240091606466031e+00
9.871269941885440602e+00
7.594591526898561984e-01
-2.877228968309437196e+00
5.592507658015017924e-01
8.828475996369435919e+00
2.946838169848354561e+00
8.420693074096489150e-01
7.032494888004835687e+00

...

在每个文件的第一行,我得到了数据的标签(在本例中为 1)。 在文件的其余部分,我得到了 13 个数字,代表每帧 13 个 MFCC。每帧 MFCC 用换行符分隔。

所以我的问题是将所有这些文件的内容转换为张量以便 tensorflow 可以使用它们的简单方法是什么?

谢谢!

【问题讨论】:

    标签: python tensorflow mfcc


    【解决方案1】:

    不确定这是否是优化的方法,但可以按照以下步骤中的说明进行操作:

    1. 遍历每个Text File 并将其数据附加到List
    2. 将每个元素中的'\n' 替换为',',因为我们的目标是从中创建CSV
    3. 将列表中元素以逗号分隔的元素写入CSV File
    4. 最后,使用tf.data.experimental.make_csv_dataset 将CSV 文件转换为Tensorflow Dataset。请找到这个Tutorial,了解如何将CSV File 转换为Tensorflow Dataset

    执行上述前三个步骤的代码如下:

    import os
    import pandas as pd
    
    # The Folder where all the Text Files are present
    Path_Of_Text_Files = '/home/mothukuru/Jupyter_Notebooks/Stack_Overflow/Text_Files'
    List_of_Files = os.listdir(Path_Of_Text_Files)
    
    List_Of_Elements = []
    # Iterate through each Text File and append its data to a List
    for EachFile in List_of_Files:
        with open(os.path.join(Path_Of_Text_Files, EachFile), 'r') as FileObj:
            List_Of_Elements.append(FileObj.readlines())
    
    # Below code is to remove '\n' at the end of each Column
    for i in range(len(List_Of_Elements)):
        List_Of_Elements[i] = [sub.replace('\n', ',') for sub in List_Of_Elements[i]] 
    
    Column_Names = ['Label,', 'F1,', 'F2,', 'F3,', 'F4,', 'F5,', 'F6,', 'F7,',
                                                                 'F8,', 'F9,', 'F10,', 'F11,', 'F12,', 'F13']    
    
    # Write the Data in the List, List_Of_Elements to a CSV File
    with open(os.path.join(Path_Of_Text_Files, 'Final_Data.csv'), 'w') as FileObj:
            FileObj.writelines(Column_Names)
            
    for EachElement in List_Of_Elements:
        with open(os.path.join(Path_Of_Text_Files, 'Final_Data.csv'), 'a') as FileObj:
            FileObj.write('\n')
            FileObj.writelines(EachElement)
    
    Path_Of_Final_CSV = os.path.join(Path_Of_Text_Files, 'Final_Data.csv')
    Data = pd.read_csv(Path_Of_Final_CSV, index_col = False)
    

    为了检查我们的数据是否正常,print(Data.head()) 将输出以下数据:

    【讨论】:

      猜你喜欢
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-17
      • 1970-01-01
      相关资源
      最近更新 更多