解決pytorch trainloader遇到的多進(jìn)程問題
pytorch中嘗試用多進(jìn)程加載訓(xùn)練數(shù)據(jù)集,源碼如下:
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=3)
結(jié)果報錯:
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.
從報錯信息可以看到,當(dāng)前進(jìn)程在運行可執(zhí)行代碼時,產(chǎn)生了一個新進(jìn)程。這可能意味著您沒有使用fork來啟動子進(jìn)程或者是未在主模塊中正確使用。
后來經(jīng)過查閱發(fā)現(xiàn)了原因,因為windows系統(tǒng)下默認(rèn)用spawn方法部署多線程,如果代碼沒有受到__main__模塊的保護(hù),新進(jìn)程都認(rèn)為是要再次運行的代碼,將嘗試再次執(zhí)行與父進(jìn)程相同的代碼,生成另一個進(jìn)程,依此類推,直到程序崩潰。
解決方法很簡單
把調(diào)用多進(jìn)程的代碼放到__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多進(jìn)程使用出錯
使用Dataloader進(jìn)行多進(jìn)程數(shù)據(jù)導(dǎo)入訓(xùn)練時,會因為多進(jìn)程的問題而出錯
dataloader = DataLoader(transformed_dataset, batch_size=4,shuffle=True, num_workers=4)
其中參數(shù)num_works=表示載入數(shù)據(jù)時使用的進(jìn)程數(shù),此時如果參數(shù)的值不為0而使用多進(jìn)程時會出現(xiàn)報錯
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.
此時在數(shù)據(jù)的調(diào)用之前加上if __name__ == '__main__':即可解決問題
if __name__ == '__main__':#這個地方可以解決多線程的問題 for i_batch, sample_batched in enumerate(dataloader):
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用Python中SocketServer 實現(xiàn)客戶端與服務(wù)器間非阻塞通信
本文主要介紹了利用Python中SocketServer 實現(xiàn)客戶端與服務(wù)器間非阻塞通信示例代碼,具有很好的參考價值,需要的朋友一起來看下吧2016-12-12Python應(yīng)用實現(xiàn)雙指數(shù)函數(shù)及擬合代碼實例
這篇文章主要介紹了Python應(yīng)用實現(xiàn)雙指數(shù)函數(shù)及擬合代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06Python通過調(diào)用有道翻譯api實現(xiàn)翻譯功能示例
這篇文章主要介紹了Python通過調(diào)用有道翻譯api實現(xiàn)翻譯功能,結(jié)合實例形式分析了基于Python實現(xiàn)的有道翻譯api調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2018-07-07基于Python實現(xiàn)MUI側(cè)滑菜單a標(biāo)簽跳轉(zhuǎn)
這篇文章主要介紹了基于Python實現(xiàn)MUI側(cè)滑菜單a標(biāo)簽跳轉(zhuǎn),mui最接近原生APP體驗的高性能前端框架,MUI側(cè)滑常見的場景有下拉刷新,側(cè)滑抽屜,側(cè)滑刪除,側(cè)滑返回以及側(cè)滑菜單等等,下面來看看文章內(nèi)容詳細(xì)的介紹,需要的朋友可以參考一下2021-11-11python實現(xiàn)傅里葉級數(shù)展開的實現(xiàn)
這篇文章主要介紹了python實現(xiàn)傅里葉級數(shù)展開的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07Python聊天室?guī)Ы缑鎸崿F(xiàn)的示例代碼(tkinter,Mysql,Treading,socket)
這篇文章主要介紹了Python聊天室?guī)Ы缑鎸崿F(xiàn)的示例代碼(tkinter,Mysql,Treading,socket),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04DRF跨域后端解決之django-cors-headers的使用
這篇文章主要介紹了DRF跨域后端解決之django-cors-headers的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01python構(gòu)建深度神經(jīng)網(wǎng)絡(luò)(DNN)
這篇文章主要為大家詳細(xì)介紹了python構(gòu)建深度神經(jīng)網(wǎng)絡(luò)DNN,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03