训练到一半时获取验证数据报错
报错代码
imgs = next(iter(val_dataloader))
val_dataloader = DataLoader(ImageDataset("data/%s" % opt.dataset_name, transforms_=transforms_, unaligned=True, mode="test"),batch_size=5,shuffle=True,num_workers=2,)def sample_images(batches_done):"""Saves a generated sample from the test set"""imgs = next(iter(val_dataloader))G_AB.eval()G_BA.eval()real_A = Variable(imgs["A"].type(Tensor))fake_B = G_AB(real_A)real_B = Variable(imgs["B"].type(Tensor))fake_A = G_BA(real_B)
错误
File "/content/cyclegan/cyclegan.py", line 324, in <module>sample_images(batches_done)File "/content/cyclegan/cyclegan.py", line 56, in sample_imagesimgs = next(iter(val_dataloader))File "/usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py", line 633, in __next__data = self._next_data()File "/usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py", line 1328, in _next_dataidx, data = self._get_data()File "/usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py", line 1294, in _get_datasuccess, data = self._try_get_data()File "/usr/local/lib/python3.10/dist-packages/torch/utils/data/dataloader.py", line 1132, in _try_get_datadata = self._data_queue.get(timeout=timeout)File "/usr/lib/python3.10/multiprocessing/queues.py", line 122, in getreturn _ForkingPickler.loads(res)File "/usr/local/lib/python3.10/dist-packages/torch/multiprocessing/reductions.py", line 307, in rebuild_storage_fdfd = df.detach()File "/usr/lib/python3.10/multiprocessing/resource_sharer.py", line 58, in detachreturn reduction.recv_handle(conn)
Traceback (most recent call last):File "/usr/lib/python3.10/multiprocessing/resource_sharer.py", line 145, in _servesend(conn, destination_pid)File "/usr/lib/python3.10/multiprocessing/resource_sharer.py", line 50, in sendFile "/usr/lib/python3.10/multiprocessing/reduction.py", line 189, in recv_handlereduction.send_handle(conn, new_fd, pid)return recvfds(s, 1)[0]File "/usr/lib/python3.10/multiprocessing/reduction.py", line 184, in send_handleFile "/usr/lib/python3.10/multiprocessing/reduction.py", line 159, in recvfdsraise EOFError
EOFError
解决方法
尝试了很多方法没解决,最终找到了解决方案
在dataset.py里加上以下代码
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')