2

我正在做一个 CNN 网络模型,它获取位于目录“images”中的图像,并且每个图像都位于该目录内的一个名为图像 ID 的目录中(例如图像 1 位于:图像/1/1.png)。

class SSMDataset(Dataset):
    songs_list = []
    def __init__(self, root_dir, transform=None):
        """lista de nombre de canciones"""
        self.root_dir = root_dir
        self.songs_list = []
        self.labels_list = []
        for (dirpath, dirnames, filenames) in os.walk(self.root_dir):
            for f in filenames:
                if f.endswith('.png'):
                    o = {}
                    o['img_path'] = dirpath + '/' + f
                    self.songs_list.append(o)
                elif f.endswith('.csv'):
                    o['label_path'] = dirpath + '/' + f
                    self.labels_list.append(o)
        self.transform = transform

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

    def __getitem__(self, index):
        img_path = self.songs_list[index]['img_path']
        image = Image.open(img_path)
        fp = image.fp
        image.load()
        fp.closed
        image = np.array(image)
        image = image[np.newaxis, :, :]
        label_path = self.labels_list[index]['label_path']
        label = np.genfromtxt(label_path)
        if self.transform is not None:
            for t in self.transform:
                image, labels = t(image, label)
        return image, label

当我训练模型时,在 epoch 23 并且总是在 taht epoch 出现以下错误:

纪元:23 39% 136/350 [00:32<00:37, 5.67it/s, 准确度=0.221, 损失=1.97]

Traceback(最近一次调用最后一次):文件 dataloader.py 第 560 行,在下一个 File dataloader.py 第 560 行,在 File 第 55 行,在 getitem File Image.py 第 2543 行,打开

OSError: [Errno 24] 打开的文件太多:C:\Users\...\images\1296 /1296.png

进程以退出代码 1 结束

我已经使用 cv2、matplotlib 等不同的库进行了证明...打开图像,我使用命令“with Image.open(img_path) as im:”来确保图像在拍摄后关闭但它总是在同一时期引发相同的错误。我还包括:

import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')

在代码中,但错误继续出现在同一时期。知道如何解决吗?也许是Windows问题...?

4

0 回答 0