【问题标题】:PyTorch Dataset: Read multiple audio files in parallel error Can't get attribute 'AudioDataset' on <module '__main__' (built-in)PyTorch Dataset: Read multiple audio files in parallel error Can't get attribute 'AudioDataset' on <module '__main__' (built-in)
【发布时间】:2021-10-24 07:08:19
【问题描述】:

我正在尝试创建一个 PyTorch Dataset 来读取两个音频文件作为功能。该代码旨在采用两条音频路径并对其进行预处理并返回频谱图、特征等,这需要花费大量时间。在创建数据集时,虽然数据集很小,但 900 个文件需要花费 15 分钟左右的大量时间,但我有很好的可用内存。有没有办法,我可以使用并行性或任何其他方法来提高它的性能?

class AudioDataset(Dataset):

    def __init__(self, paths_list, targets, preprocess=preprocess_fn):

        self.preprocess = preprocess
        self.features = []
        self.labels = []
        self.paths_list = paths_list
        
        self.targets = targets
        self._init_dataset()
        
    
    def _init_dataset(self):
        
#         paths_list, targets
        try:
            for p, target in tqdm(zip(self.paths_list, self.targets)):
                audio_1 = self.preprocess(p[0])
                audio_2 = self.preprocess(p[1])
                self.features.append([audio_1, audio_2])
                self.labels.append(target)
        
        except ValueError as e:
            print(f"Error occured at {e}")

            
#         self.transform = transform

    def __len__(self):
        return len(self.features)

    def __getitem__(self, idx):
        sample = self.features[idx]
        label = self.labels[idx]
        return sample, label

尝试使用 DataLoader 并设置工人数 = 10,但它会引发错误

[00:00<?, ?it/s]Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/cm/local/apps/python37/lib/python3.7/multiprocessing/spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "/cm/local/apps/python37/lib/python3.7/multiprocessing/spawn.py", line 115, in _main
    self = reduction.pickle.load(from_parent)
AttributeError: Can't get attribute 'AudioDataset' on <module '__main__' (built-in)>

代码在没有多个工人的情况下工作。 有没有办法让我对其进行排序? 谢谢,

【问题讨论】:

    标签: python pytorch dataset


    【解决方案1】:

    为了提高加载速度,您应该考虑将特征保存到 .pt 文件中,并设计一个不同的数据集和加载器来从 .pt 文件中读取特征。这被称为迁移学习。你可以谷歌一下。对于错误,我认为我需要有关代码和堆栈跟踪的更多信息。

    【讨论】:

      猜你喜欢
      • 2022-08-13
      • 2022-10-25
      • 1970-01-01
      • 1970-01-01
      • 2022-12-26
      • 2019-08-24
      • 2022-12-02
      • 2023-01-05
      • 2019-01-19
      相关资源
      最近更新 更多