解決pytorch trainloader遇到的多進程問題
pytorch中嘗試用多進程加載訓練數據集,源碼如下:
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=3)
結果報錯:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:if __name__ == '__main__':
freeze_support()
...The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
從報錯信息可以看到,當前進程在運行可執(zhí)行代碼時,產生了一個新進程。這可能意味著您沒有使用fork來啟動子進程或者是未在主模塊中正確使用。
后來經過查閱發(fā)現了原因,因為windows系統(tǒng)下默認用spawn方法部署多線程,如果代碼沒有受到__main__模塊的保護,新進程都認為是要再次運行的代碼,將嘗試再次執(zhí)行與父進程相同的代碼,生成另一個進程,依此類推,直到程序崩潰。
解決方法很簡單
把調用多進程的代碼放到__main__模塊下即可。
if __name__ == '__main__':
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=3)
補充:pytorch-Dataloader多進程使用出錯
使用Dataloader進行多進程數據導入訓練時,會因為多進程的問題而出錯
dataloader = DataLoader(transformed_dataset, batch_size=4,shuffle=True, num_workers=4)
其中參數num_works=表示載入數據時使用的進程數,此時如果參數的值不為0而使用多進程時會出現報錯
RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module: if __name__ == '__main__': freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.
此時在數據的調用之前加上if __name__ == '__main__':即可解決問題
if __name__ == '__main__':#這個地方可以解決多線程的問題
for i_batch, sample_batched in enumerate(dataloader):
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
利用Python中SocketServer 實現客戶端與服務器間非阻塞通信
本文主要介紹了利用Python中SocketServer 實現客戶端與服務器間非阻塞通信示例代碼,具有很好的參考價值,需要的朋友一起來看下吧2016-12-12
Python聊天室?guī)Ы缑鎸崿F的示例代碼(tkinter,Mysql,Treading,socket)
這篇文章主要介紹了Python聊天室?guī)Ы缑鎸崿F的示例代碼(tkinter,Mysql,Treading,socket),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
DRF跨域后端解決之django-cors-headers的使用
這篇文章主要介紹了DRF跨域后端解決之django-cors-headers的使用,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

